IoT PPT05 Intro Arduino Programming
IoT PPT05 Intro Arduino Programming
Introduction to
Arduino Programming
“If you want peace of mind, do not find fault with others. Rather learn to see your own faults.” – Ma Sarada Devi
Introduction
• The Arduino Software (IDE) allows you to write programs (i.e. sketches)
and upload them to your board.
• 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
• 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.
• Trigonometry:
– cos()
– sin()
– tan()
• Math:
– abs()
– max()
– min()
– pow()
– sq()
– sqrt()
– random()
– randomSeed()
• 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 Dr. Manas Khatua 9
Cont…
• 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
• 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?