Internet of Things
Introduction to
Arduino Programming
Thanks to Dr.. Manas Khatu
Introduction
• The Arduino Software (IDE) allows you to write programs (i.e. sketches)
and upload them to your board.
• A sketch is consists of two mandatory functions:
Setup( ) -- it is executed once
Loop( ) -- it is executed repeatedly
• Setup( ) is used for
initialization of serial communication
defining pinMode
declaring variables
• Loop( ) is used for
writing the main code which has to execute continuously.
e.g. reading inputs from the sensors, triggering outputs to the external
device, etc.
08-08-2024 2
Cont…
• Sketches are compiled by
avr-gcc / avr-g++
– It is based on C/C++
programming language
• So, the program syntax is
almost similar to C/C++
– Supported data types
– Variables
– Constants
– Control structure
– Looping structure
– Arrays
– Strings
– Function
• One important extension is : Arduino Libraries
– Libraries are a collection of code that makes it easy for you to
connect to a sensor, display, module, etc.
08-08-2024 3
Variables
08-08-2024 4
Operators & Structures
08-08-2024 5
Few Built-in Functions
https://www.arduino.cc/reference/en/
• pinMode (pin, mode)
– It configures the specified pin to behave either as input or as output pinMode(9,OUTPUT);
– By default the digital pins in Arduino function as input.
– pin: is the number of the pin whose mode needs to be set
– mode: can be INPUT, OUTPUT, INPUT_PULLUP.
• digitalReadPin(pin) val = digitalRead(inPin);
– Reads the value from a specified digital pin, either HIGH or LOW.
• digitalWrite(pin, value)
– Used for output by using the LOW/HIGH logic level (i.e. 0V / 5V) digitalWrite(10,HIGH);
– value: LOW / HIGH
• analogRead(pin)
– Access and gets value from a particular Analog pin having 10-bit resolution (i.e. 10- val = analogRead(A3);
bit ADC)
– Returns: 0-1023 (integer)
– Arduino UNO yields a resolution between readings of: 5 volts / 1024 units. It will
map input voltages between 0 and the operating voltage(5V or 3.3V) into integer
values between 0 and 1023.
– The input range can be changed using analogReference()
• analogWrite(pin, value)
– Write the analog value (PWM wave) to a pin analogWrite(9, val / 4);
– value: it is the duty cycle value between 0 and 255 (as 6 pins).
– Note: analogRead values go from 0 to 1023, analogWrite values from 0 to 255
08-08-2024 6
Cont…
• delay(ms)
– Pause the program for the amount of time (in millisecond) delay(1000); // wait for a second
specified by ms
• Serial.begin( speed )
– It sets the speed in bps (baud rate) for serial data transmission Serial.begin(9600);
from computer to Arduino board
• Serial.available ()
– Returns: the number of bytes (characters) available to read
if (Serial.available() > 0) { }
• Serial.print( value )
– Print data to the serial port as human-readable ASCII text
Serial.print("I received: ");
– Numbers are printed using ASCII character for each digit
– Floats are printed as ASCII digits (upto 2 decimal places)
– Bytes are send as a single character
– Characters and Strings are sent as is.
• Serial.print( value, format)
Serial.print(i,DEC);
– The optional 2nd argument specifies the base (format) to use
– format: BIN / OCT / DEC / HEX // Print Decimal value of number i
• Serial.println( value) , Serial.println( value, format)
– Additionally it returns the number of bytes written
08-08-2024 7
Cont…
• Serial.read() incomingByte = Serial.read();
– Reads incoming serial data.
Serial.write(45); // send a byte with the value 45
• Serial.write(val) or .write(str) or .write(buf, len)
– Writes binary data to the serial port.
– This data is sent as a byte or series of bytes; to send the int bytesSent = Serial.write("hello"); //send the
characters representing the digits of a number use string "hello" and return the length of the string.
the print() function instead.
• Trigonometry:
– cos()
– sin()
– tan()
• Math:
– abs()
– max()
– min()
– pow()
– sq()
– sqrt()
– random()
– randomSeed()
08-08-2024 8
Example 1: Digital Read-Write
• Objective:
– Turns on and off a LED connected to digital pin 13, when pressing a
pushbutton attached to pin 2.
• The circuit:
– LED attached from pin 13 to ground through 220 ohm resistor
– One leg of the Pushbutton attached to pin 2
– That same leg of the button connects through a pull-down resistor (here
10K ohm) to ground.
– The other leg of the button connects to the 5 volt supply.
08-08-2024 9
Cont…
• When the pushbutton is
open (unpressed)
– there is no connection
between the two legs of the
pushbutton, so the pin is
connected to ground
(through the pull-down
resistor) and we read a LOW.
• When the button is closed
(pressed)
– it makes a connection
between its two legs,
connecting the pin to 5 volts,
so that we read a HIGH.
08-08-2024 10
Example 2: Binary Counter in LED
• Requirements:
– Arduino UNO
– USB connector
– Breadboard
– 4 piece LEDs
– 4 piece 1K ohm resistor
– Arduino IDE
• Connection:
– Place the LED and resistor on breadboard
– Connect the bradboard power with Arduino
– Connect the LED with Arduino
– Connect the Arduino board with PC/Laptop
• Arduino Programming
– Install IDE in PC/Laptop
– Run the IDE
– Select the Arduino board in IDE
– Select the connected COM port
– Start writing new sketch
08-08-2024 11
Sketch of Binary Counter
08-08-2024 12
Demo on Binary Counter in LED
08-08-2024 13
Read Analog Voltage
• ADC provide digital output which is proportional to analog value.
• To know what is input analog value, we need to convert the received digital value
back to analog value through program.
Aout = digital value * (Vref/2^n – 1)
• Example:
digital value = 512 and ADC is 10-bit with 5V Vref.
What analog voltage is giving the respective digital value?
Aout = 512 * (5 V / 1023) = 2.5 V
digitalValue = analogRead (pin)
pin - number of analog pin which we want to read
digitalValue: 0 – 1023
08-08-2024 14
Example: Read Analog Voltage
// select the input pin for the potentiometer
int sensorPin = A0;
// variable to store the value coming from the sensor
int digitalValue = 0;
float analogVoltage = 0.00;
void setup() {
Serial.begin(9600);
}
void loop() {
// read the value from the analog channel
digitalValue = analogRead(sensorPin); Pin 1 & 3 of Potentiometer:
Serial.print("digital value = "); connect them to Vcc and GND of
//print digital value on serial monitor Arduino
Serial.print(digitalValue);
//convert digital value to analog voltage Pin 2 of Potentiometer: Connect
analogVoltage = (digitalValue * 5.00)/1023.00; with A0 pin of Arduino
Serial.print(" analog voltage = ");
Serial.println(analogVoltage);
delay(1000);
}
08-08-2024 15
Example: Read Analog Voltage
// select the input pin for the potentiometer
int sensorPin = A0;
// variable to store the value coming from the sensor
int digitalValue = 0;
float analogVoltage = 0.00;
void setup() {
Serial.begin(9600);
}
void loop() {
// read the value from the analog channel
digitalValue = analogRead(sensorPin);
Serial.print("digital value = ");
//print digital value on serial monitor
Serial.print(digitalValue);
//convert digital value to analog voltage
analogVoltage = (digitalValue * 5.00)/1023.00;
Serial.print(" analog voltage = ");
Serial.println(analogVoltage);
delay(1000);
}
08-08-2024 16
Write Analog Value
• Digital control is used to create a
square wave, a signal switched
between ON and OFF.
• This on-off pattern can simulate
voltages in between Vcc and GND.
by changing the portion of the time
the signal spends ON versus the
time that the signal spends OFF
• The analogWrite(value) is on a scale
of 0 – 255.
Zero value means 0% duty cycle,
255 value means 100% duty cycle.
08-08-2024 17
Example: Write Analog Value
Pin 1 & 3 of Potentiometer:
connect them to Vcc and GND of
Arduino
Pin 2 of Potentiometer: Connect
with A0 pin of Arduino
OUTPUT: LED Dimming by Potentiometer
One LED connected with digital
pin 9 and grounded through 220
ohm or 1 Kohm resistor
08-08-2024 18
Lessons Learned
What is Arduino Programming
Syntax of Arduino Programming
Supporting variable, structures, operators
In-Built Arduino Function Library
Programming example - LED blink
Program and Demo on binary counter in LED
Analog Read and Write
08-08-2024 19
08-08-2024 20