Module 3
Arduino Programming
Electrical Components
Solderless Breadboard
Solderless
Breadboard
Prototyping without soldering
LED
● Light-emitting Diode
● Pasess current one way
● Emits Lights
● spec of current threshold : 20mA
Anode + Cathode -
Cathode - Anode +
Resistor
Color code represent the resistance value
Resistor Color Code
Push Button
Press to Turn On
Release to Turn Off
Generate Digital Input
CS G523, Dr. Vinay Chamola, EEE Dept. , BITS-Pilani
Potentiometer
Variable Resistor
Ohm's Law
Basic Concept: Ohm's Law
Applying Ohm's Law to LED
Voltage across LED is 2V
Voltage across R1 resistor is 5V
Maximum current for LED is 0.02A
5V
R= 5V/0.02A= 250Ω
Arduino Program Structure
Define variables here
Define INPUT/OUTPUT Pins here
Sketch = Program =
Source Code
Create your main program here
Pin Setup
PinMode Syntax
pinMode(pin, INPUT/OUTPUT)
Digital Output
digitalWrite
Syntax:
digitalWrite(pin, HIGH/LOW)
Eg
digitalWrite(9,HIGH)
delay
Pauses the program for the amount of
time (in miliseconds) specified as
parameter
Syntax
delay(ms)
Connection
Exercise: pinMode, digitalWrite
1.Write a sketch to turn on one LED
Use digital pin 13 and use digitalWrite
Challenge:
2. Write a sketch to blink one LED
3.Write a sketch to alternate blinking 2 LEDs
Time for Exercise: 10 mins
Turn on LED
// the setup function runs once when you
press reset or power the board
void setup() {
// initialize digital pin LED_BUILTIN as an
output.
pinMode(13, OUTPUT);
}
// the loop function runs over and over again
forever
void loop() {
digitalWrite(13, HIGH); // turn the LED on
(HIGH is the voltage level)
}
Blink LED (Challenge)
// the setup function runs once when you press
reset or power the board
void setup() {
// initialize digital pin LED_BUILTIN as an
output.
pinMode(12, OUTPUT);
}
// the loop function runs over and over again
forever
void loop() {
digitalWrite(12, HIGH); // turn the LED on
(HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(12, LOW); // turn the LED off by
making the voltage LOW
delay(1000); // wait for a second
}
Two LED: Hint on Connection
Analog Output
Analog Output Applications
Pulse Width Modulation (PWM)
To create an analog signal, the microcontroller
use a technique called PWM. By varying the
pulse width or duty cycle, we can create an
analog voltage.
Analog Output
A few PINs on the Arduino Digital PINs allow to
modify the output to mimic analog signal
Pin 3, 5, 6, 9, 10, 11
They are indicated by a "~" besides the pin no.
Analog Write Pin
The analog write
pins are those pins
with ~ sign
analogWrite Syntax
Syntax
analogWrite(PIN, value)
Eg
analogWrite(9, 125)
Connection
LED analog value
void setup() {
// initialize digital pin LED_BUILTIN as an
output.
pinMode(9, OUTPUT);
}
// the loop function runs over and over again
forever
void loop() {
analogWrite(9, 50) ;
}
Challenge: analogWrite
1.Write a simple sketch to change the
brightness the LED light from low to high
repeatedly
2.Write a sketch to fade the LED brightness
from low to high, and high to low repeatedly.
Time: 10 mins
Gradually increasing intensity (Challenge solution)
void setup() {
// initialize digital pin LED_BUILTIN as an output.
pinMode(9, OUTPUT);
}
// the loop function runs over and over again
forever
void loop() {
analogWrite(9, 0) ;
delay(1500); // wait for a second
analogWrite(9, 50) ;
delay(1500);
analogWrite(9, 150) ;
delay(1500);
analogWrite(9, 255);
delay(1500); // wait for a second
}
Serial Communication
A serial bus consists of just two wires - the
transmitter TX wire for sending data and receiver RX
wire for receiving data.
Serial Communication on Arduino
Serial.begin(baudrate)
COM Port
Serial monitor
Print to Serial Monitor
Serial.print(value)
Serial.println(value)
Ex: Print to Serial Monitor
Print some text to the Serial Monitor. Eg
void setup() {
Serial.begin(9600); // set up Serial library at 9600 bps
Serial.println("Hello World");
}
void loop()
{
}
Print what’s sent in serial monitor
void setup() {
Serial.begin(9600); //set up serial library baud rate to 9600
}
void loop() {
if(Serial.available()) //if number of bytes (characters) available for
reading from serial port
{
Serial.print(" I received: "); //print I received
Serial.write(Serial.read()); //send what you read
}
}
Read Data from Serial Monitor
Serial.available()
Serial.read();
HINT : Serial Communication
Use the correct ascii input
99
Serial Read Example: print ascii value
void setup() {
Serial.begin(9600);
}
void loop() {
while (Serial.available() == 0);
int val = Serial.read() -'0';
Serial.println(val);
}
Challenge!
Challenge: Serial Communication
1. Use Serial Read to control the LED ON/OFF
If Serial Read value is 1, turn on LED
If Serial Read value is 0, turn off LED
Time: 10 mins
Connection
46
Turn on/off based on inputs (Challenge)
void setup() {
Serial.begin(9600);
pinMode(13, OUTPUT);
}
void loop() {
while (Serial.available() == 0);
int val = Serial.read() -'0';
if(val==0)
{digitalWrite(13, LOW);}
else
{digitalWrite(13, HIGH); }
Serial.println(val);
}
47
Digital Input
digitalRead Syntax
Syntax:
digitalRead(PIN)
Eg
digitalRead(2)
Connection
digitalRead(2)
10K Ω
Exercise: digitalRead
1. Write a simple sketch to read the digital
output of pin 2 and to display on serial
monitor.
Setup : just a loose wire hanging from pin 2 and
based on whether you connect to 5V or GND
you get 1 or 0.
int ledPin = 2;
int val=0; // variable to store the read value
void setup()
{
pinMode(ledPin, INPUT); // sets the digital pin 13 as
output
}
void loop()
{
val = digitalRead(ledPin); // read the input pin
Serial.println(val);
}
Analog Input
AnalogRead Syntax
Syntax
analogRead(PIN)
Eg
analogRead(A0)
• 10-bit analog to digital converter
• map input voltages between 0 and 5 volts into
integer values between 0 and 1023.
Exercise: analogRead
Write a simple sketch to read the analog output
of a potentiometer/an analog pin and output to
the serial monitor
A0
Map
Syntax:
map(value, fromLow, fromHigh, toLow, toHigh)
Eg
int val = analogRead(0);
val = map(val, 0, 1023, 0, 255);
analogWrite(9, val);
Challenge
Exercise: Output the brightness
1. Print out the brightness value of the varying
brightness LED done in previously done exercise
Challenge 2: Analog Read/Write
Parts Needed:
● Arduino Uno
● Solderless breadboard
● 1 LED
● 1 220 resistor
● 1 Potentiometer
● Wires
● Connect the circuit shown
on the left
● Code a sketch to vary the
brightness of the LED
using potentiometer.
Tone
60
Tone
Generates a square wave of the specified frequency
(and 50% duty cycle) on a pin.
Syntax:
tone(pin, frequency)
tone(pin, frequency, duration)
noTone(pin)
Ex: Tone
Connect like below and copy the code from
https://www.arduino.cc/en/Tutorial/ToneMelody?from=Tutori
al.Tone and run the program
Challenge
Add a Piezo and photoresistor sensor as
below. Move your hands over the sensor,
this will change the light that falls on the
photoresistor’s face. This will change the
voltage on the analog pin that will
determine the frequency note to play
Circuit Diagram
Hint
Module 4
Arduino Shields
Motor Shield
Pin Used:
3,8,911,12,13,A0,A
1
Able to drive two DC
motors with Arduino UNO,
controlling the speed and
direction of each one
independently.
Connect to
external power
Ref:
supply https://www.arduino.cc/en/Main/ArduinoM
otorShieldR3
Ethernet Shield
Pin Used: 4,10,11,12,13
The Arduino Ethernet
Shield connects your
Arduino to the internet
Eg monitor twitter hashtag
or uploading sensor data.
The shield can save data to
microSD card.
Ref: https://www.arduino.cc/en/Main/ArduinoEthernetShield
WiFi Shield
Pin Used: 4,7,10,11,12,13
The Arduino WiFi Shield
connects your Arduino to
the internet wirelessly
The shield can save data to
microSD card.
Ref:
https://www.arduino.cc/en/Main/Arduino
WiFiShield
Module 5
Arduino Standard
Libraries
EEPROM Library
• Arduino has built-in EEPROM which act as
permanent storage like hard disk. The data will stay
in the EEPROM when the board is turned off and
may be retrieved later by another sketch.
#include <EEPROM.h>
Ref: https://www.arduino.cc/en/Reference/EEPROM
Ethernet Library
• This library allows an Arduino board to connect to
the internet
• Allow Arduino to act as server or client.
• Arduino communicates with the shield using the SPI
bus.
#include <SPI.h>
#include <Ethernet.h>
Ref: https://www.arduino.cc/en/Reference/Ethernet
Firmata Library
This library implements firmata protocol that allow you
to control your Arduino from software on a computer.
#include <Firmata.h>
Ref: https://www.arduino.cc/en/Reference/Firmata
Liquid Crystal Library
This library allows an Arduino board to control
LiquidCrystal displays (LCDs) based on the Hitachi
HD44780 (or a compatible) chipset, which is found on
most text-based LCDs.
#include <LiquidCrystal.h>
Ref: https://www.arduino.cc/en/Reference/LiquidCrystal
SD Library
The SD library allows for reading from and writing to
SD or microSD cards, e.g. on the Arduino Ethernet
Shield.
Use SPI for data transfer.
#include <SPI.h>
#include <SD.h>
https://www.arduino.cc/en/Reference/SD
Servo Library
The Servo library allows you to control up to 12 servo
motors on UNO and 48 on Mega.
Most servo can be positioned from 0 to 180 degree.
#include <Servo.h>
https://www.arduino.cc/en/Reference/Servo
SPI Library
The Serial Peripheral Interface (SPI) is a synchronous
serial data protocol used by microcontrollers for
communicating with one or more peripheral devices
quickly over short distances.
It can also be used for communication between two
microcontrollers.
#include <SPI.h>
https://www.arduino.cc/en/Reference/SPI
SPI Communication
With an SPI connection there is always one master device
controlling the peripheral devices. Typically there are three lines
common to all the devices:
• MISO (Master In Slave Out) - The Slave line for sending data
to the master,
• MOSI (Master Out Slave In) - The Master line for sending
data to the peripherals,
• SCK (Serial Clock) - The clock pulses which synchronize data
transmission generated by the master
and one line specific for every device:
• SS (Slave Select) - the pin on each device that the master can
use to enable and disable specific devices.
SPI Communication
Arduino SPI Pins
Arduino UNO/Mega SPI:
MOSI - pin 11/50
MISO - pin 12/51
CLK - pin 13/54
CS - pin 4/53
Pin 4 - SS for Ethernet
Controller for UNO/Mega
SoftwareSerial Library
The Arduino hardware has built-in support for serial
communication on pins 0 and 1
The softwareSerial Library allows you to use any digital
pins to send and receive serial message instead of pin
0 and 1.
#include <SoftwareSerial.h>
https://www.arduino.cc/en/Reference/softwareSerial
Stepper Library
The Stepper library allows you to control unipolar or
bipolar stepper motors.
#include <Stepper.h>
https://www.arduino.cc/en/Reference/Stepper
WiFi Library
The WiFi library is based on the Ethernet library. It
allows you to wirelessly connect to internet.
#include <WiFi.h>
https://www.arduino.cc/en/Reference/WiFi
Wire Library
The Wire library allow your Arduino to
communicate with I2C/TWI interface.
#include <Wire.h>
https://www.arduino.cc/en/Reference/Wire
I2C Communication
• Used only two bidirectional lines - Serial Data Line
(SDA) and Serial Clock Line (SCL), pulled up with
resistors.
• Typical voltages used are +5 V or +3.3 V
• Allowed up to 127 uniquely addressed devices
• Up to 400kHz data rates
• Data rate depends on the length of the wire, the
type of cable, and the pull up resistor value
I2C Connection
● Bidirectional Bus
● Device pull down, resistor pull up
● Resistor value typically from 1.5k to 4K.
● More flexible than SPI, but slower
● The Slave address start from 01 to 7F.
Wire Library
Board I2C / TWI pins
Uno, Ethernet A4 (SDA), A5 (SCL)
Mega2560 20 (SDA), 21 (SCL)
Leonardo 2 (SDA), 3 (SCL)
Due 20 (SDA), 21 (SCL), SDA1, SCL1
Installing Additional Library
• Libraries are usually distributed in zip files
• To install additional libraries, goto Sketch Menu ->
Include Library -> Add Zip Library
https://www.arduino.cc/en/Reference/Libraries
88
Ex: Install Library
Download CapSense library from
http://playground.arduino.cc/Main/CapacitiveSensor
?from=Main.CapSense and install on your Arduino IDE