You are on page 1of 26

Embark

We Teach You Conquer

Embedded System Design Using Arduino

Tenere Technologies Pvt., Ltd.


www.tenere.in

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

Tenere Technologies Pvt., Ltd.


www.tenere.in

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.

What is an Arduino UNO?


The Arduino Uno is a microcontroller board based on the ATmega328. It has 14 digital input/output pins (of which 6 can be
used as PWM outputs), 6 analog inputs, a 16 MHz ceramic resonator, a USB connection, a power jack, an ICSP header, and
a reset button. It contains everything needed to support the microcontroller; simply connect it to a computer with a USB cable
or power it with a AC-to-DC adapter or battery to get started.

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

32 KB (ATmega328) of which 0.5 KB used by boot loader


2 KB (ATmega328)
1 KB (ATmega328)

Arduino Programming IDE


The Arduino community calls a program a sketch. This is because the Arduino was originally written for artists and hobbyists.
The Arduino IDE (Integrated Development Environment) lets you write sketches (also known as programs) that provide
instructions telling the Arduino board what to do and how to do it.
Tenere Technologies Pvt., Ltd.
www.tenere.in

Embark
We Teach You Conquer

1. You can download in the IDE from


www.arduino.cc/en/Main/software install on your computer.
2. Open the Arduino IDE
3. To establish connection between Arduino and Computer using USB-B cable, plug the USB-B cable to the Arduino and
connect it with any one of the USB ports on your computer. Then go to Tools > Serial Port and choose COM
4. To ensure that you are setup to program the correct board, go to Tools > Board and select the board that you are using.
Make sure there is a check mark next to the board you are using.
5. To check programs for errors before uploading, click the top left button with a check mark
6. To upload programs to the board, click the button with an arrow on the top bar

An Arduino program is structured in four parts.


FIRST: Begin with some comments about the program
SECOND: List variables and constants that all the functions may use. Variables are names for memory locations that a
computer can use to store information that might change. Constants are numbers that won't change.
THIRD: Run the setup() function to get the system ready to run the loop() function. This is where you perform tasks that you
want done once at the beginning of your program.
FOURTH: Run the loop() function. This is where you run things in a sequence from the top of the loop to the bottom of the
loop, and then you start over again at the top, looping until the machine gets turned off.

Tenere Technologies Pvt., Ltd.


www.tenere.in

Embark
We Teach You Conquer

Notes
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________

Tenere Technologies Pvt., Ltd.


www.tenere.in

Embark
We Teach You Conquer

2 Hello World Program


Activity 1: This activity introduces you to the world of Arduino programming with the example of blinking a LED with Arduino
UNO.
Components required

Arduino UNO
LED
Resistor -220
Jumper Wires

Pin Diagram

Breadboard Layout 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);
}

Tenere Technologies Pvt., Ltd.


www.tenere.in

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);
}

Tenere Technologies Pvt., Ltd.


www.tenere.in

Embark
We Teach You Conquer

3 Serial Communication in Arduino


The goal is to start from a very basic form of Arduino Serial communication, and progressively add or improve components
so that we can ultimately transmit data from external world into computer for further processing.
What is Serial Communication?
Serial communication just means that only one bit of information is sent a time. Lots of devices communicate serially. The
best example is your USB (Universal Serial Bus) pen drive that communicates with computer for data storage and data
retrieval.
Like the USB pen drive, Arduino UNO also communicates serially with computer via USB cable. The Arduino will send and to
receive data from computer. This functionality can be used to collect data from the outside environment and allows us to use
the computer for further processing.
How to Send and Receive Serial Data with the Arduino?
The Arduino IDE has a built in serial monitor that makes it easier to send and to receive data from computer. This also helps
to debug and develop your Arduino code. An Arduino board needs to be plugged into your computer in order for the serial
monitor to function. Open the serial monitor by clicking on the button in the upper right of your Arduino window.

Arduino Serial Library


1.

Serial.begin() - used to start the serial communication with the computer.


For Example
void setup()
{
Serial.begin(9600);
}

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.

Serial.read() - Reads incoming serial data coming from computer.

5.

Serial.available() Checks whether any serial data is readily available for reading from the serial port.

Tenere Technologies Pvt., Ltd.


www.tenere.in

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.

Tenere Technologies Pvt., Ltd.


www.tenere.in

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");
}
}
}

Tenere Technologies Pvt., Ltd.


www.tenere.in

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.

Tenere Technologies Pvt., Ltd.


www.tenere.in

Embark
We Teach You Conquer

4 Sensors and Actuators interfacing with Arduino


This activity involves interfacing various sensors and actuators with Arduino UNO.
Activity 1: Interfacing Push button with Arduino UNO.
Components Required

Arduino UNO
Push button switch
Resistor -10k, 220
LED
Jumper wires

Pin diagram and Schematic Diagram

Breadboard layout diagram

Tenere Technologies Pvt., Ltd.


www.tenere.in

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.

Tenere Technologies Pvt., Ltd.


www.tenere.in

Embark
We Teach You Conquer

Activity 2: Displacement Sensor (Potentiometer) Interfacing


Components Required

Arduino UNO

Potentiometer

Resistor -10k(Brown-Black-Orange)

Jumper wires

Pin diagram and Schematic Diagram

Breadboard layout diagram

Tenere Technologies Pvt., Ltd.


www.tenere.in

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)

So if define the following syntax,(i.e. if we choose variable as int data type)


int sensorValue= analogRead(A0);
Then the variable sensorValue will be holding only the value 511 10
So if define the following syntax,,(i.e. if we choose variable as float data type)
float sensorValue= analogRead(A0);
Then the variable sensorValue will be holding only the value 511.5 10
NOTE: The Ardunio (A0-A5) do not require the pinMode() commend to initialize them as the input pins because they are
already the input pins.
Sketch
//Declare the variables used
intpotPin=A0;
void setup()
{
Serial.begin(9600);
}
void loop()
{
intpotSensorValue=analogRead(potPin);
Serial.print("ADC output in Decimal:\t");
Serial.println(potSensorValue);
floatpotSensorVoltage=potSensorValue*(5.0/1023.0);
Serial.print("ADC input Voltage:\t ");
Serial.println(potSensorVoltage);
delay(1000);
}

Tenere Technologies Pvt., Ltd.


www.tenere.in

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

Tenere Technologies Pvt., Ltd.


www.tenere.in

ADC Output

Embark
We Teach You Conquer

Activity 3: Light Sensor (Photoresistor / LDR) Interfacing


Components Required

Arduino UNO

Photoresistor / LDR (Light Dependent resistor)

Resistor -10k(Brown-Black-Orange)

LED

Jumper wires

Pin diagram and Schematic Diagram


Light-dependent resistor (LDR) is light sensor and it is the two terminal devices as shown in figure.

In darkness, the resistance of a light-dependent resistor (LDR) is quite high.


In dark = Resistance is very HIGH
When you shine some light at it, the resistance quickly drops and it becomes a reasonably good conductor of electricity. It is
thus a kind of light-activated switch.
In bright light = Resistance is LOW
The same way we used for interfacing the pushbutton, we can interface the LDR to Ardunio (usually the pull-up
configuration).

For the above configuration,


Light Intensity

Photoresistor
Resistance

Voltage at
Analog Pin 0

Darkness

Very HIGH

Zero

Bright

Very LOW

High

Normal Light

Between LOW and


HIGH

Between Zero
and High

ADC Output

Tenere Technologies Pvt., Ltd.


www.tenere.in

Embark
We Teach You Conquer

Breadboard layout diagram : Darkness Sensor

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);
}

Tenere Technologies Pvt., Ltd.


www.tenere.in

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.

Tenere Technologies Pvt., Ltd.


www.tenere.in

Embark
We Teach You Conquer

Challenge: Build an Ohmmeter Using an Arduino


An ohmmeter is a device that can measure resistance and it appears the part of Multimeter. Using the correct hardware and
code, we can turn the Arduino into a device that can measure resistance.
Schematic Diagram

Tenere Technologies Pvt., Ltd.


www.tenere.in

Embark
We Teach You Conquer

5 Temperature measurement using LM35


Activity 1: This activity involves measurement of the ambient temperature in degree Celsius through the LM35 temperature
sensor and displays the value in Serial monitor.
Components required

Arduino UNO
LM35 Temperature Sensor
LED
Resistor 220
Jumper Wires

Pin Diagram of LM35

Breadboard Layout Diagram

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

Tenere Technologies Pvt., Ltd.


www.tenere.in

Embark
We Teach You Conquer

6 Control of DC motor using Arduino and L293D Motor Driver


In this tutorial, we will see how to use the L293D DC motor controllercan control DC motors up to 36V with the Arduino.
Components required

Arduino UNO

DC Motor - 5V

L293D Motor Driver IC

Jumper Wires

How do we control the speed of DC Motor?


The property of the DC motor is that the speed of the rotor is directly proportional to the voltage applied.
For Example, if DC motor is rated as 12V, 600 rpm. This means that if we apply 12 volts the rotor will rotate at 600 rpm. So
for the motor if we apply half of the rated voltage (6V) it will rotate with half of the rated speed (300 rpm).
Why we need the motor driver IC?
If we connect the DC motor directly to the Arduino, current required by the motor cannot be supplied by an Arduino because
the maximum current the DC pin of Arduino can supply is 40 mA. So the motor may not run or the board might get damaged.
In order to rectify this problem we can have an intermediate device which can supply necessary current to the motor.

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.

Pin diagram of L293D

Tenere Technologies Pvt., Ltd.


www.tenere.in

Embark
We Teach You Conquer

Control Table

Breadboard Layout Diagram

Activity 1: Write the code to run the 5V DC motor in the counter-clockwise direction using L293D Driver.

//Declare the variables used


int motorControlPin1=2; // L293D PIN 2
int motorControlPin2=3; // L293D PIN 7
void setup()
{
pinMode(motorControlPin1,OUTPUT);
pinMode(motorControlPin2,OUTPUT);
}
void loop()
{
digitalWrite(motorControlPin1,LOW);

// Motor Turns on right hand Side for

digitalWrite(motorControlPin2,HIGH); // Pin 2-->LOW and Pin 3--> HIGH


}

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.

________________________

Tenere Technologies Pvt., Ltd.


www.tenere.in

You might also like