Arduino Book Erf
Arduino Book Erf
Embark
We Teach You Conquer
Contents
1. Getting Started 1
2. Hello World Program . 5
3. Serial Communication in Arduino 9
4. Sensors and Actuators interfacing with Arduino ..15
5. Temperature Measurement using LM35 .. 27
6. Control of DC motor using Arduino and L293D Motor Driver 31
Embark
We Teach You Conquer
1 Getting Started
What is an Arduino?
The name is an Italian masculine first name, meaning "strong friend. It is the Open Source Hardware board created in Ivrea,
Italy in 2005 by Massimo Banzi & David Cuartielles. Arduino provides a great toolset for designers, tinkers, and anyone who
sometimes just want to play with an idea that uses electronics. The genius of Arduino is that it provides just enough access
to get specific tasks done without programming and other complexities.
Arduino Specifications
Microcontroller
Operating Voltage
Input Voltage (recommended)
Digital I/O Pins
Analog Input Pins
DC Current per I/O Pin
Clock Speed
ATmega328
5V
7-12V
14 (of which 6 provide PWM output)
6
40 mA
16 MHz
Flash Memory
SRAM
EEPROM
Embark
We Teach You Conquer
Embark
We Teach You Conquer
Notes
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
Embark
We Teach You Conquer
Arduino UNO
LED
Resistor -220
Jumper Wires
Pin Diagram
Sketch
//initialize the variables used
int ledPin=13;
// setup the pin 13 as output
void setup()
{
pinMode(ledPin,OUTPUT);
}
//write the loop function
void loop()
{
digitalWrite(ledPin,HIGH);
}
Tenere Technologies Pvt., Ltd.
www.tenere.in
Embark
We Teach You Conquer
Activity 2: Write a code to light up the LED for 3 seconds and switch OFF
the LED for 2 seconds continuously.
//initialize the variables used
int ledPin=13;
// setup the pin 13 as output
void setup()
{
pinMode(ledPin,OUTPUT);
}
//write the loop function
void loop()
{
digitalWrite(ledPin,HIGH);
delay(3000);
digitalWrite(ledPin,LOW);
delay(2000);
}
Embark
We Teach You Conquer
Activity 3: Write a code to blink the LED for only five times.
//initialize the variables used
int ledPin=13;
// setup the pin 13 as output
void setup()
{
pinMode(ledPin,OUTPUT);
}
//write the loop function
void loop()
{
inti;
for(i=0;i<5;i++)
{
digitalWrite(ledPin,HIGH);
delay(1000);
digitalWrite(ledPin,LOW);
delay(1000);
}
while(1);
}
Embark
We Teach You Conquer
The above code sets up the serial commutation with computer at the baud rate of 9600.
2.
Serial.print() - Prints data to the serial port as human-readable form in the serial monitor.
For Example
Serial.print(78) gives "78"
Serial.print(1.23456) gives "1.23"
Serial.print('N') gives "N"
Serial.print("Hello world.") gives "Hello world."
3.
Serial.println() - Prints data to the serial port as human-readable form in the serial monitor followed by a
carriage return. This command takes the same forms as Serial.print().
4.
5.
Serial.available() Checks whether any serial data is readily available for reading from the serial port.
Embark
We Teach You Conquer
Activity: 1 Write a simple program to demonstrate serial communication and send serial data from the Arduino Board.
void setup()
{
Serial.begin(9600);
}
void loop()
{
Serial.print(78);
Serial.print(1.23456);
Serial.print('N')
Serial.print("Hello,there ..!!!");
}
Note: Change the Serial.print() in the above code with Serial.println() and check the output.
Embark
We Teach You Conquer
Activity 2: Control a LED from computer through serial communication so that when you press the number 1 the LED
should be ON and when you press the number 0 the LED should be OFF.
Sketch
//Declare Variables
int ledPin=13;
void setup()
{
pinMode(ledPin,OUTPUT);
Serial.begin(9600); // Start serial Communication with computer
}
void loop ()
{
// Check for serial communication
while(Serial.available()>0)
{
int keyboardValue = Serial.read(); // If Yes,Read from Keyboard
if (keyboardValue == '1' )
{
digitalWrite(ledPin,HIGH);
Serial.println("LED is ON");
}
if (keyboardValue == '0')
{
digitalWrite(ledPin, LOW);
Serial.println("LED is OFF");
}
}
}
Embark
We Teach You Conquer
Activity 3: Modify the code such that when you press the character a the LED should be ON and when you press the
character b the LED should be OFF.
Challenge: Modify the code such that when you send the string on the LED should be ON and when enter the string off
the LED should be OFF.
Challenge: Modify the code such that when you send the string on the LED should be ON and when enter the string off
the LED should be OFF.
Embark
We Teach You Conquer
Arduino UNO
Push button switch
Resistor -10k, 220
LED
Jumper wires
Embark
We Teach You Conquer
Sketch
//Declare the variables used
intpushButtonPin=8;
intledPin=9;
void setup()
{
//Intialize them as either Input or Output
pinMode(pushButtonPin,INPUT);
pinMode(ledPin,OUTPUT);
}
void loop()
{
//Read the pushbutton status
intpushButtonStatus=digitalRead(pushButtonPin);
//If it is HIGH,light up the LED
if(pushButtonStatus == HIGH)
{
digitalWrite(ledPin,HIGH);
delay(3000);
}
else
{
digitalWrite(ledPin,LOW);
}
}
Challenge: Modify the above code to implement the following One press to light up the LED and another press to switchoff the LED like a ON-OFF switch in the TV or on your mobile phone etc.
Embark
We Teach You Conquer
Arduino UNO
Potentiometer
Resistor -10k(Brown-Black-Orange)
Jumper wires
Embark
We Teach You Conquer
Theory
The analogRead() is normally used read the data (the voltage) from various sensors connectedto analog pins A0-A5 (6
pins)
in the Ardunio. Since the value the analogRead() is reading is the analog signal , the in-built 10 bit ADC of the
Ardunio converts that analog voltage value into equivalent value based on the following formula
The reference voltage (Vref) is usually the setup of the user. But usually this value 5V or 12V. Ardunio has 10 bit ADC. So it
can represent the analog values in 10 bit digital values ranging from 00000000002 (decimal value 0) to 11111111112
(Decimal Value 1023).
The syntax of analogRead () command is
DataType VariableName = analogRead(Pin_number);
The Pin_number is the analog input pins of Ardunio (A0-A5 (6 pins) )
The data type depends upon the accuracy you desire. If you put DataType as int then it will be whole number and DataType
as float then it will be the fractional number.
For example, if analog input voltage from the sensor is 2.5V then the ADC will be returning (the analogRead () command
will be returning)
Embark
We Teach You Conquer
Execute the code and Fill up the following table by changing the POT wiper,
Sl.No
When measuring
Vin = 1V
Vin = 1.5V
Vin = 2V
Vin = 2.5V
Vin = 3V
ADC Output
Embark
We Teach You Conquer
Arduino UNO
Resistor -10k(Brown-Black-Orange)
LED
Jumper wires
Photoresistor
Resistance
Voltage at
Analog Pin 0
Darkness
Very HIGH
Zero
Bright
Very LOW
High
Normal Light
Between Zero
and High
ADC Output
Embark
We Teach You Conquer
Sketch
intLDRpin=A0;
intledPin=9;
int threshold=30;
void setup()
{
pinMode(ledPin,OUTPUT);
Serial.begin(9600);
}
void loop()
{
intLDRsensorValue=analogRead(LDRpin);
Serial.println(LDRsensorValue);
if(LDRsensorValue < threshold )
{
digitalWrite(ledPin,HIGH);
}
else
{
digitalWrite(ledPin,LOW);
}
// delay(1000);
}
Embark
We Teach You Conquer
Exercise: Modify the above code to vary the brightness of LED based on the intensity of light (Smart Dimmer Circuit).
Hint: Use map () and analogWrite() function of Arduino.
Embark
We Teach You Conquer
Embark
We Teach You Conquer
Arduino UNO
LM35 Temperature Sensor
LED
Resistor 220
Jumper Wires
Sketch
void setup()
{
Serial.begin(9600);
}
void loop()
{
intsensorTemp=analogRead(A0);
floatsensorTempVoltage= sensorTemp*(5.0/1023.0);
floattempinC=sensorTempVoltage*100.0;
Serial.print("Temperature in C:\t");
Serial.println(tempinC);
delay(1000);
}
Tenere Technologies Pvt., Ltd.
www.tenere.in
Embark
We Teach You Conquer
Activity 2: Measure of the ambient temperature in Fahrenheit using LM35 temperature sensor and displays the value in
Serial monitor.
Hint: Temperature in Fahrenheit
Activity 3: Switch ON the actuator like LED, Fan, and Air Conditioner etc. when the temperature exceeds particular
temperature.
Breadboard Layout Diagram
Embark
We Teach You Conquer
Arduino UNO
DC Motor - 5V
Jumper Wires
This device is known as Driver. But driver cannot control the motor. It can supply only necessary current to the motor. So the
control part is taken care by Arduino.
Embark
We Teach You Conquer
Control Table
Activity 1: Write the code to run the 5V DC motor in the counter-clockwise direction using L293D Driver.
Exercise: Modify the code to run the 5V DC motor in the clockwise direction using L293D Driver.
Tenere Technologies Pvt., Ltd.
www.tenere.in
Embark
We Teach You Conquer
Activity 2: Write the code to control the speed of the 5V DC motor using POT in any direction (either clockwise or counterclockwise) using L293D Driver.
Sketch
//Declare the variables used
int motorControlPin1=2; // L293D PIN 2
int motorControlPin2=3; // L293D PIN 7
intenabledPin=9;
void setup()
{
pinMode(enabledPin,OUTPUT);
pinMode(motorControlPin1,OUTPUT);
pinMode(motorControlPin2,OUTPUT);
}
void loop()
{
intpotSensorValue=analogRead(A0);
intspeedControl=map(potSensorValue,0,1023,0,255);
analogWrite(enabledPin,speedControl);
digitalWrite(motorControlPin1,LOW); // Motor Turns on right hand Side
for Pin 2-->LOW and Pin 3--> HIGH
digitalWrite(motorControlPin2,HIGH);
}
Challenge: Design control circuit to control 5V DC motor using L293D with two push button switches, one to switch ON the
motor and other to reverse the direction of the motor while running.
________________________