IoT_PPT05_Intro_Arduino_Programming
IoT_PPT05_Intro_Arduino_Programming
Introduction to
Arduino Programming
08-08-2024 2
Cont…
• Sketches are compiled by
avr-gcc / avr-g++
– It is based on C/C++
programming language
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/
• 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.
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…
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.
• Example:
digital value = 512 and ADC is 10-bit with 5V Vref.
What analog voltage is giving the respective digital value?
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.
08-08-2024 17
Example: Write Analog Value
08-08-2024 19
08-08-2024 20