Programming arduino.

Chiri
5 min readMar 5, 2023

--

Arduino programming

  • by levis chiri

Introduction

  • Arduino programming language is mainly c++ language though some new Key words and functions are included e.g(digitalWwrite ,pinMode)(C.programminng)
  • In default arduino program is made of two parts(functions).
  • This functions are.
void setup(); & void loop();

void setup();

  • Used for configuration of pin functionality
  • The setup() runs once, when the Arduino is first powered on.
  • This is where you configure the digital pins to be either inputs or outputs using a function named pinMode() Examples ..pins connected to LEDs will be OUTPUTs and the switch pin will be an INPUT.

void loop();

  • The loop() runs continuously after the setup() has completed.
  • The loop() is where you will check for voltage on the inputs, and turn outputs on and off.
  • Arguments are information that you pass to functions, telling them how they should do their job
  • Here Key words such as digitalWrite,digitalRead,analogWrite and analogRead are used.
  • The loop function contains the main body of the program( this where instructions on what the arduino should do are given).
  • digitalRead() needs one argument: what pin to check. In
  • Example
int SwitchState=0; 
void setup()
{
pinMode(3,OUTPUT);
pinMode(2,OUTPUT);
pinMode(5,INPUT):
}
void loop()
{
switchState=digitalRead(5);
}
  • in this .. program, digitalRead() is going to check the state of pin 2 and store the value in the switchState variable. If there’s voltage on the pin when digitalRead() is called, the switchState variable will get the value HIGH (or 1). If there is no voltage on the pin, switchState will get the value LOW (or 0).

Arduino key words

digital i/o

pinMode

  • The pinMode() function is used to configure a specific pin to behave either as an input or an output.
  • Syntax
void setup(){ pinMode(pin,MODE); }
  • In this syntax the
  • pin ‒ the number of pin which mode you want to set.
  • Mode‒ OUTPUT,INPUT or INPUTP_PULLUP.

digitalWrite

  • this is a key word in arduino used to enable (HIGH) or disable(LOW) the internal pull up on the input pin.
  • syntax for digitalWrite(function name(parameters(pin number& state of the pin.))
digitalWrite(pin,state);

digitalRead

  • A key word in arduino programming used to read the state of a pin i.e (LOW or HIGH)
  • Syntax
  • Name of the function(parameters (pin number)
digitalRead(pin);
  • Example of program with both digitalWrite and digitalRead
int button=7 
void setup()
{
pinMode(LED_BUILTIN,OUTPUT);
pinMode(button,INPUT);
} void loop()
{
if(digitalRead((button)==HIGH))
{ digitalWrite(LED_BUILTIN,HIGH);
}
else{ digitalWrite(LED_BUITIN,LOW);
}
}

analog i/o

analogRead

  • Reads the value from a specified analog pin.
  • The input voltage between 0 to 5V is mapped into integers from 0 to 1023 since arduion board contain a multichannel 10-bit analog to digital converter.
  • syntax for anlogRead
analogRead(pin);
  • Arduino boards contain a multichannel, 10-bit analog to digital converter. This means that it will map input voltages between 0 and the operating voltage(5V or 3.3V) into integer values between 0 and 1023.
  • So to get the value in a range of 0–5v we can divide the number by 1024 and then multiply it by 5. and for 0–3.3v we divide with 1024 and multiply with 3.3.

analogWrite

  • Writes an analog value (PWM wave) to a pin.
  • can be used to light an LED at varying brightness and drive a motor at various speeds.
  • After a call to analogWrite() the pin will generate a steady rectangular wave of specified duty cycle until the next call on the same pin.
  • NOTE (for arduino uno and Nano use pins 3,5,6,9,10,11) pin 5,6(980HZ) the others 490HZ.
  • syntax
analogWrite(pin, PWMvalue)
  • Parameters
  • Pin ‒the arduino pin to write to. the type is int
  • value ‒ the duty cycle: between 0(always off) and 255(always on).
  • Example
int ledPin =9;//Led connected to digital pin 9; 
int analogPin=3;//potentiometer connected to analog pin3
int val =0;
void setup()
{
pinMode(ledPin,OUTPUT);//set the pin as output
}
void loop()
{
val = analogRead(analogpin);//read the input pin
analogWrite(ladPin, val/4);
//analogvalue go from 0 to 1023, analogWrite values from 0 to 255.
}

analogReference(Function)

  • Configures the reference voltage used for analog input.
  • The options ranges are
  • DEFAULT − The default analog reference of 5 volts (on 5V Arduino boards) or 3.3 volts (on 3.3V Arduino boards)
  • INTERNAL − An built-in reference, equal to 1.1 volts on the ATmega168 or ATmega328 and 2.56 volts on the ATmega8 (not available on the Arduino Mega)
  • INTERNAL1V1 − A built-in 1.1V reference (Arduino Mega only)
  • INTERNAL2V56 − A built-in 2.56V reference (Arduino Mega only)
  • EXTERNAL − The voltage applied to the AREF pin (0 to 5V only) is used as the reference
  • Function syntax
analogReference(type);
  • Tis types are (DEFAULT, INTERNAL, INTERNAL1V1, INTERNAL2V56, EXTERNAL)

Using arduino serial monitor

  • To use the serial monitor you’re going to use a new command, Serial.begin()in the setup function. This opens up a connection between the Arduino and the computer, so you can see the values from the analog input on your computer screen.
  • The argument number 9600 is commonly used. Mainly, we need to set a baud rate, which is done by writing Serial.begin(9600);.
  • The argument 9600 is the speed at which the Arduino will communicate, 9600 bits per second.
  • .
  • The syntax of using Serial.begin
Serial.begin(9600); //Note that the S is capital
  • You will use the Arduino IDE’s serial monitor to view the information you choose to send from your microcontroller. When you open the IDE’s serial monitor verify that the baud rate is 9600.
  • The Serial.print() sends information from the Arduino to a connected computer. You can see this information in your serial monitor. If you give Serial.print() an argument in quotation marks, it will print out the text you typed. If you give it a variable as an argument, it will print out the value of that variable.
  • You can use Serial.print(“”); with quotation marks inside brackets to print a data directly to the serial monitor.
  • Also there is Serial.println() prints the information from arduino in a new line.
  • Example of code
viod setup()
{ Serial.begin(9600);
} void loop()
{
Serial.print("hello World");
Serial.println("Hello world");
} //The OUTPUT //hello World //Hello world

--

--

Chiri

Telecommunication Engineering Students, and an Embedded Engineer