0% found this document useful (0 votes)
24 views121 pages

37 Beginner Projects

The document is a beginner's guide to Arduino, detailing various starter projects such as LED blinking, traffic light control, and using the Arduino serial monitor. Each project includes a list of required components, assembly instructions, and Arduino code examples. The guide serves as a comprehensive resource for newcomers to learn and experiment with Arduino programming and electronics.

Uploaded by

Alain. V
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views121 pages

37 Beginner Projects

The document is a beginner's guide to Arduino, detailing various starter projects such as LED blinking, traffic light control, and using the Arduino serial monitor. Each project includes a list of required components, assembly instructions, and Arduino code examples. The guide serves as a comprehensive resource for newcomers to learn and experiment with Arduino programming and electronics.

Uploaded by

Alain. V
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 121

ARDUINO FOR

BEGINNERS

BEST STARTER PROJECTS


FOR ANY MAKER
TABLE OF
CONTENTS
01 Led Blink 3
02 Traffic Light 9
03 LED CHASER 12
04 Arduino Serial Monitor 15
05 Analog Read Serial 19
06 Digital Read 22
07 Controlling Led with push Button 26
08 Analog Write and LED fade 28
09 Button Counter 31
10 Controlling LED brightness using PWM and potentiometer 34
11 Controlling Servo motor (Sweep) 37
12 Servo motor potentiometer 39
13 Automatic Door opening 41
14 Temperature measurement using LM35 Temperature sensor 43
15 Controlling LED with Serial Monitor. 45
16 Interfacing LCD with Arduino 48
17 LM35 temperature sensor with Arduino and LCD 52
18 Controlling DC Motor using L298 55
19 Introduction to the Joystick and switch 58
2
20 Interfacing L293D Motor driver with Arduino 61
21 Interfacing Ultrasonic sensor with Arduino 63
22 Controlling DC motor with joystick 66
23 Controlling LED with Arduino 69
24 Object counter using Arduino and IR sensor module 72
25 Blink without delay 75
26 Temperature controlled alarm 78
27 Automatic Arduino based watering system 81
28 Using Serial Plotter 84
29 Using internal EEPROM of Arduino 88
30 Vibration sensor with Arduino 91
31 Water Quantity measurement and level indicator 94
32 Tachometer using Arduino 98
33 Smartphone wireless serial monitor 101
34 Interfacing tilt sensor with Arduino 104
35 Interfacing LDR sensor with Arduino UNO to control LED brightness 106
36 Serial communication between Arduinos 108
37 I2C communication between two Arduinos 111
38 Interfacing seven segment display with Arduino 115

3
01 Led Blink
In this article we are going to introduce Arduino microcontroller first program which Includes
understanding the digital output and blinking a led with the help of programing and changing
the delay time to change the blink rate.

COMPONENTS REQUIRE

1. Arduino Uno
2. Arduino Uno Cable
3. LED (Any color)
4. Computer

ASSEMBLY

Assemble the circuit as shown in the figure. Attach led to the digital pin no-13 of Arduino UNO.
While attaching it take care of polarity of LED pins the pin with longer length is anode ( + ) and
shorter one is cathode ( - ).

4
UPLOADING SKETCH:

Download and install Arduino IDE


from Arduino website https://www.
arduino.cc/en/main/software.

Open the Arduino IDE.

Open Arduino IDE Go to the File >>


Example >> Basics >> Blink

Understanding of code:

In digital there are two states of the


voltages there is one is HIGH and
another is LOW. We can refer HIGH
as +5 V and The LOW as the – or
GND. void setup() is the loop which
execute only once during the powering up of microcontroller. In this loop we set the mode of the
pin as INPUT (ex.Push button) or as OUTPUT (ex.LED).

In this example we are working on digital OUTPUT so we set pin no 13 as OUTPUT using the
line

ARDUINO CODE

pinMode(LED_BUILTIN, OUTPUT);

In Arduino UNO Built in LED is connected to the pin no 13 so we can also


write 13 in the place of LED_BUILTIN like

pinMode(13, OUTPUT);

Which set pin 13 as OUTPUT mode.

void loop() is the loop which runs continuously without breaking. In this
loop we set LED pin 13 HIGH with this line,

digitalWrite(LED_BUILTIN, HIGH);

5
Which makes the LED glow by giving +5V to the Anode pin of the LED and turns on the LED.

Delay function is used to stop Arduino for certain time in milliseconds. The Arduino stops for
one second ( 1 second = 1000 millisecond ) by line,

delay(1000);

Then LED turns off by seting pin 13 to LOW with line,

digitalWrite(LED_BUILTIN, LOW);

Again Arduino stop for one second by line,

delay(1000);

This runs continuously to turn on and off LED which makes LED to blink.

Compiling is the process of converting code into machine language. Which also gives you
programming errors if any.

6
Compile the Code by clicking on ✔ circle.

Connect The Arduino to USB port of computer with Arduino cable.

Select the board and com port by going Tools >>Board as Arduino/Genuino UNO

And port as Port >> Select (Arduino/Genuino).

7
Upload the sketch by clicking → Arrow circle.

Look at IDE bottom it should say Done Uploading.

8
Check the hardware Led blinks by one second Interval.

And are you done with your first Arduino programming you can change delay time in line,
delay(500); to change blink rate.

ARDUINO CODE

void setup() {
// initialize digital pin LED_BUILTIN as an output.
pinMode(LED_BUILTIN, OUTPUT);
}
// the loop function runs over and over again forever
void loop() {
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the
voltage level)
delay(1000); // wait for a second
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the
voltage LOW
delay(1000); // wait for a second
}
9
02 Traffic Light
In this article we are going to make traffic light controller in which red light turns on for five
second to stop the vehicle then green light turns on for five seconds to go signal for vehicle and
then yellow to slow down the vehicle speed for to seconds and cycle keep continues.

Components Require
Arduino Uno
Arduino Uno Cable
LED’s (Red, Green, Yellow)
Breadboard
Jumper wires
Computer
Circuit Diagram

Assemble the circuit as shown in the figure. Attach Red led to the digital pin no-11 of Arduino
UNO and green and yellow to 10 and 12 respectively. While attaching it take care of polarity
of LED pins the pin with longer length is anode ( + ) and shorter one is cathode ( - ). The ground
(Cathode ) of all LED’ are common.

For code compiling and uploading into Arduino board refer the the first project of LED blinking
with Arduino.

10
11
ARDUINO CODE :

int green= 10;


int red = 11;
int yellow = 12;

void setup() {
pinMode(12,OUTPUT); // sets the 12light pin 10 to output
pinMode(red,OUTPUT); // sets the red light pin 11 to output
pinMode(yellow,OUTPUT); // sets the yellow light pin 12 to output
}

void loop() {
// turnning on red light and turn off yellow and 12light

digitalWrite (red,HIGH); // turns on the red light


digitalWrite (green,LOW); // turns off the green light
digitalWrite (yellow,LOW); // turns off the yellow light
delay(5000);

// turnning on 12light and turn off yellow and red light

digitalWrite (red,LOW); // turns off the red light


digitalWrite (green,HIGH); // turns on the green light
digitalWrite (yellow,LOW); // turns off the yellow light
delay(5000);

// turnning on yellow light and turn off red and 12light

digitalWrite (red,LOW); // turns off the red light


digitalWrite (green,LOW); // turns off the green light
digitalWrite (yellow,HIGH); // turns on the yellow light
delay(2000);

12
03 LED CHASER
In this article we are going to create 8 LED’s light chaser. This LED’s are turn on and off as
sequence defined in loop. Also we are going to learn how use for loop in Arduino IDE and how to
use it in program.

Components Required

Arduino Uno
Breadboard
Jumper Wires
LED’S 8 pieces
Arduino Cable

Circuit Diagram

13
Build the circuit as shown in fig by connecting ground terminal common to the Arduino ground
pin. Connect all the anode terminals to the pin 2,3,4,5,…..,10 respectively as shown. We are
going to turn on LED’s one after one so its look like it chasing each other.

In for loop we define the I as pin no 10 and decrease it up to pin no 3 by line

for (int i = 10 ; i > 2 ; i--)


and then set it to the output mode in loop with lines
{
pinMode(i,OUTPUT);
}
And for in the loop section we do same for turning on and off leds one by
one by 100 ms delay like,
for (int i = 10 ; i > 2 ; i--) // for loop change i value 10-3
{
digitalWrite(i,HIGH); // sets pin 10-3 as high
delay(100); // wait for 100 ms
}
and then runs continuously to turn off LED’s .

14
ARDUINO CODE

void setup() {
for (int i = 10 ; i > 2 ; i--) // i is the maximum pin no which is
connected to led
{
pinMode(i,OUTPUT); // sets the 10 to 2 pins mode as OUTPUT mode
}

void loop() {

for (int i = 10 ; i > 2 ; i--) // for loop change i value 10-3


{
digitalWrite(i,HIGH); // sets pin 10-3 as high
delay(100); // wait for 100 ms
}
for (int i = 10 ; i > 2 ; i--) // for loop change i value 10-3
{
digitalWrite(i,LOW); // sets pin 10-3 as high
delay(100); // wait for 100 ms
}
}

15
04 Arduino Serial
Monitor
This article shows you how to use serial monitor of Arduino. It helps you to print string, values
on serial monitor.

Components
Arduino
LED
Computer

SERIAL MONITOR

Arduino serial monitor is separate pop-up window that acts as a separate terminal that
communicates by receiving and sending Serial Data. This window can be accessed by clicking on
the magnifying class symbol on right side as shown in fig.

You can use it to print various data on it like string and values etc.

16
1. Printing string
Upload the following code to the Arduino and open the serial monitor it will show you an
string “Hello World !”

ARDUINO CODE

void setup() {
Serial.begin(9600); // starts serial communication
between arduino and computer
}

void loop() {
Serial.println(“Hello World !”); // prints string “Hello
World !” on new line
}

17
You can change string in code,
print - means printing data in same line
println – means printing data in new line
you can check it by changing println to print in this line,
Serial.println(“Hello World !”);

2. Printing values on serial monitor


Upload the following code to the Arduino

open the serial monitor it will show you


Counter is 1
Counter is 2

18
In this code we initialize the variable count at the top of the code with value 0. While
printing we increase the value of counter with one at each time with ++ lines also we
provided some delay for stable operation.

ARDUINO CODE

int count = 0 ; // initilize the variable count with 0


void setup() {
Serial.begin(9600); // initilize the serial communication
}

void loop() {
Serial.print(“Counter is “); // prints string Counter is
Serial.println(count++); // prints counts and increse it by one
each time
delay(1000); // wait for second
}

19
05 Analog Read Serial
In this article we are going to learn about Arduino IDE Serial monitor and reading the analog
values of potentiometer using analog read function on serial monitor.

The analog values are reference voltage measured by analog pin of the Arduino.

Components
Arduino
Bread Board
Potentiometer 10 k
Jumper Wires
Computer

Schematic

Arduino serial monitor is separate pop-up window that acts as a separate terminal that
communicates by receiving and sending Serial Data. This window can be accessed by clicking on
the magnifying class symbol on right side as shown in fig.

20
Which opens the serial monitor window as shown

21
Arduino ADC (Analog to digital
convertor) is 10 bit ADC and input
voltage for this is 5 volts. So digital
Output values are 2^10 that is 1024
descript units. In which 0 volts is 0 and 5
v is 1023 of digital value.

The value read by ADC is as shown in


image as we rotate the potentiometer
the values get changing.

If you are not able to see values change


the baud rate to 9600 as shown,

ARDUINO CODE

void setup() {
Serial.begin(115200); // starts serial communication between arduino
and computer with boud rate 9600
pinMode(A0, INPUT); // Sets A0 pin as anlog input
}
void loop() {
int val = analogRead(A0); // analog read value stored in integer
variable val
Serial.print(“Value is “); // Serial print string on monitor
Serial.println(val); // the value of val is printed on serial
monitor with new line
delay(100);
}

22
06 Digital Read
In this article we are going to learn reading the digital input that is HIGH or LOW using push
Button with serial monitor.

Components required
Arduino
Push Button
Resistor ( 1k )
Jumper Wires
Bread Board
Computer

Circuit Diagram

23
Do the connection as shown in figure. Attach the 5V pin to ground pin in series with push button
and the 1k resistor. Take the output of the by attaching wire as per the figure and connect it to
the Digital pin 3 by which we are going to read pin state that is 1 means HIGH (+5V) or 0 means
LOW.

Upload the code to the Arduino and open the serial monitor it indicates the pin state as 0 it
means pin is LOW

Press the push button and check the serial monitor it show the output as 1 means pin is HIGH

24
Try this code with different pins for practice by changing pin no.

25
ARDUINO CODE :

void setup() {
Serial.begin(9600); // Starts Serial Communication between computer and arduino
pinMode(3,INPUT); // set pin 3 as input pin
}

void loop() {
int state = digitalRead(3); // the value of pin stored in state
variable
Serial.println(state); // prints value on Serial monitor with new
line
}

26
07 Controlling Led
with push Button
In this article we are going to learn how to control LED with the help of push button and learn
if else condition. When user push the button the LED glows and it turns off when user release
push button.

Components require

Arduino
Push Button
Resistor (1k & 330 Ohm)
Jumper Wires
Bread Board
LED (Any Color)

Circuit Diagram

Assemble the circuit as shown in the figure. Connect the 5v pin to ground through the resistor
and push button in series. Connect 9 pin to the push button output. Connect the Led to the 8 pin
and ground to the ground through 330 ohm resistor.

27
When user push the button if condition satisfy that is when push button pin become HIGH
the LED pin turns HIGH else it remain LOW. You can easily understand it by looking code and
compare it with explanation.

ARDUINO CODE

void setup() {
pinMode(8,OUTPUT); //sets pin 8 as output LED
pinMode(9,INPUT); //sets pin 9 as input pin pushButton
}
void loop() {
if (digitalRead(9)== HIGH) // when 9 pin become high do 8 pin HIGH
{
digitalWrite(8,HIGH); // 8 pin goes HIGH
}
else // else 8 pin remain LOW
{
digitalWrite(8,LOW);
}
}

28
08 Analog Write
and LED fade
In this article we are going to learn about Arduino PWM pins and how to use it and controlling
LED brightness using Arduino PWM pin.

Components
Arduino UNO
LED (Any color)
Computer

Arduino PWM is
the (Pulse Width Diagram
Modulation) or PWM, is
a technique for getting
analog results with
digital means. Digital
control is used to create
a square wave, a signal
switched between on
and off. This on-off
pattern can simulate
voltages in between full
on (5 Volts) and off (0
Volts) by changing the
portion of the time the
signal spends on versus
the time that the signal
spends off. The duration
of “on time” is called
the pulse width. To get
varying analog values,
you change, or modulate, that pulse width. If you repeat this on-off pattern fast enough with an
LED for example, the result is as if the signal is a steady voltage between 0 and 5v controlling
the brightness of the LED.

29
Duty Cycle Brightness Analog Value
0 OFF 0
25 25 64
50 50 128
75 75 192
100 Full 255

Arduino Code :
This code fade the led and then glow it again with time

This type of circuits can be used to control motor speed, temperature of heating element etc.

30
ARDUINO CODE

void setup() {
pinMode(9,OUTPUT);
Serial.begin(9600);
}

void loop() {
for (int i = 0 ; i<255;i++)
{
analogWrite(9,i);
Serial.println(i);
delay(100);
}
for (int i = 255 ; i>0;i--)
{
analogWrite(9,i);
Serial.println(i);
delay(100);
}
}

31
09 Button Counter
In this article we are going to learn how to counts number in Arduino. In this we count the event
how many times button is pressed.

Components
Arduino
Push button
Resistor (1 k)
Breadboard
Computer
Jumper Wire

Diagram

32
Upload the following code to the Arduino

In this code while loop stops the execution until pin 9 goes high to low which avoid counting
with time.

Open the serial monitor. When you press the Button it will count the no of times you pressed button.

We can also use this type of mechanism to count objects and persons by using IR sensor and
leaser lights.

33
ARDUINO CODE

int count = 0; // initilize the count variable with 0


void setup() {
Serial.begin(9600); // starts serial communication between arduino and
computer
pinMode(9,INPUT); // pin 9 set as input
}

void loop() {
if(digitalRead(9)==HIGH) // when pin reads 9 as high
{
Serial.print(“You pressed button “); // print string
Serial.print(count++); // increse counter by 1 when button pressed
Serial.println(“ times”); // print times
while(digitalRead(9)==HIGH); // it stops loop untill pin reads low
}
}

34
10 Controlling LED
brightness using PWM
and potentiometer
In this article we are going to learn how to control Led brightness using PWM and
Potentiometer. Also we are going to learn about map function in Arduino which is used to
convert values range from one range to another range.

Components require
Arduino
Potentiometer (10k)
Resistor(330ohm)
Jumper wires
Breadboard
LED (Any color)

35
Assemble the circuit as shown in figure. Attach the
LED positive terminal to the pin no 9 which is pwm
pin of Arduino. Give the positive 5V supply to the
first pin of potentiometer and ground to the third
one pin. Attach middle pin of potentiometer to the
Analog pin 0 as shown.

Potentiometer has following pin out,

Upload the code to Arduino and open serial


monitor check the values shown on it shows pwm
and analog values on it.

Here the first value indicate the analog value and another one indicate pwm value. Rotate the
potentiometer and check the values as the value changes brightness of the LED changes.

36
ARDUINO CODE

void setup() {
pinMode(A0,INPUT); // sets the A0 pin as potatiometer input
pinMode(9,OUTPUT); // set pin 9 pin as pwm output pin
Serial.begin(115200); // starts serial communication between computer and
arduino
}
void loop() {
int val=analogRead(A0); // analog read value of A0 is stored in val
variable
int pwm_val=map(val,0,1023,0,255); // value of val is converted into
range of 0-1023 to 0-255.
analogWrite(9,pwm_val); // the value pwm_val is wirten as pwm output for
pin 9

Serial.print(val); // prints value of analog pin


Serial.print(“ “); // prints spaces on serial monitor
Serial.println(pwm_val); // prints pwm values to the serial monitor
}

37
11 Controlling
Servo motor (Sweep)
In this article we are going to learn what servo motor is and how to control it using Arduino
microcontroller.

Components
Servo motor (micro)
Arduino UNO
Jumper Wires

A servomotor is a rotary actuator or linear actuator that allows


for precise control of angular or linear position. Micro servo
motors are limited rotation angle 0-180 degree. We can use
servo motors as robotics arm high torque application. Pin out of
servo motor is shown in fig.

Do the connections as per the


circuit diagram shown in the
fig. Connect +5V pin to 5V pin
of Arduino, Gnd to Ground
and signal pin to the 9 pin of
Arduino which is pwm pin.

Before loading the try to


include library of servo motor
by going to sketch >> Include
Library >> Servo .

38
Upload the code to Arduino it starts motor to sweep from 0-180 and 180-0 degree by the gap of
1 degree in every 15 millisecond as given in for loop of the coding.

ARDUINO CODING

#include <Servo.h> // includes servo library


Servo myservo; // create servo object to control a servo

void setup() {
myservo.attach(9); // attaches the servo on pin 9 to the servo object
}

void loop() {
for (int pos = 0; pos <= 180; pos ++) // goes from 0 degrees to 180
degrees in steps of 1 degree
{
myservo.write(pos); // tell servo to go to position in variable ‘pos’
delay(15); // waits 15ms for the servo to reach the position
}
for (int pos = 180; pos >= 0; pos --) // goes from 180 degrees to 0 degrees
{
myservo.write(pos); // tell servo to go to position in variable ‘pos’
delay(15); // waits 15ms for the servo to reach the position
}
}

39
12 Servo motor
potentiometer
In this article we are going to control the servo motor using
potentiometer input.

Components
Arduino
Servo Motor
Bread Board
Jumper Wires
Potentiometer
Computer

A servomotor is a rotary actuator or linear actuator that allows


for precise control of angular or linear position. Micro servo
motors are limited rotation angle 0-180 degree. We can use
servo motors as robotics arm high torque application. Pin out
of servo motor is shown in fig.

Do the connections as per


the circuit diagram shown
in the fig. Connect +5V pin
to 5V pin of Arduino, GND
to Ground and signal pin to
the 9 pin of Arduino which
is pwm pin. Connect the
potentiometer pins to 5v and
GND and out pin to the A0
pin of the Arduino.

40
Upload the code and rotate the potentiometer it will turns the motor as per the position of the
potentiometer.

ARDUINO CODE

#include <Servo.h>
Servo myservo; // create servo object to control a servo
void setup() {
myservo.attach(9); // attaches the servo on pin 9 to the servo object
pinMode(A0,INPUT); //A0 pin set as INPUT pin
}

void loop() {
int val = analogRead(A0); // reads the value of the potentiometer
(value between 0 and 1023)
int pos = map(val, 0, 1023, 0, 180); // scale it to use it with the servo
(value between 0 and 180)
myservo.write(pos); // sets the servo position according to the scaled value
delay(15); // waits for the servo to get there
}

41
13 Automatic
Door opening
In this article we are going to learn how to make automatic door opening and closing with help
of PIR sensor and servo motor. When PIR sensor detect any person it opens the gate by turning
servo to 180 degree and close it after 1 second.

Components
Arduino UNO
Servo Motor
PIR Sensor module
Jumper Wires

Circuit Diagram

PIR sensor is the digital


sensor which detects
person or living things
by producing HIGH
signal which can be
read by microcontroller.
Following are pin out of
the PIR sensor

Upload the following code to the Arduino. Place the hand in front of PIR sensor it will turn the servo
motor through 180 degree and again close when we remove hand from sensor after one second.

42
ARDUINO CODE

#include <Servo.h> // include servo library


Servo door; // creating door object for servo
void setup() {
pinMode(4,INPUT); // pin 4 set as input pin of PIR sensor
door.attach(9); // door servo is attached to the pin 9
}

void loop() {
if(digitalRead(4)==HIGH) // when PIR sensor detect person
{
door.write(180); // servo opens the door
delay(1000); // wait for second
}
else
{
door.write(0); // else door remain close
}
}

43
14 Temperature
measurement using LM35
Temperature sensor
In this article we are going to learn about how to measure temperature using LM35
temperature sensor and display it on the serial monitor.

Components required
Arduino UNO
LM35 Temperature sensor
Jumper wires
Computer

LM35 is the analog temperature sensor which provide change in


10mv change per degree Celsius temperature change. Following
figure shows the diagram and pin out of the LM35 sensor.

Circuit Diagram
Do the connections as shown in the fig above connect +5v pi to
the 5V of Arduino and GND to the GND. Also connect OUT pin to
the A0 pin of Arduino.
LM35 sensor changes its
OUTPUT voltage with
change in temperature
this voltage can measured
by Arduino analog pin.
This can be converted
into temperature using
formula,

Temperature ={ (
analogRead * 500 ) / 1023
}

Where, temperature is in
degree Celsius.

44
Open the serial monitor it show you temperature as follows,

ARDUINO CODE

void setup() {
Serial.begin(9600);
}
void loop() {
int temp_adc_val = analogRead(A0); // Read analog voltage of A0
float temp_val = (temp_adc_val * 500)/1023; // Convert adc value to
equivalent voltage
Serial.print(“Temperature = “); // prints temperature string
Serial.print(temp_val); // prints temp val on seria
monitor
Serial.println(“ °C”);
delay(100); // wait for 100 milliseconds
}

45
15 Controlling LED
with Serial Monitor.
In this article we are going to learn about how to send and receive data through serial monitor
and how to use it to control LED.

Components
Arduino UNO
LED
Computer
Schematic

46
Upload the code to the Arduino. Open the serial monitor type A in command box as shown in
figure and send it. It turns on the LED and prints string LED turned on serial monitor.

To check data is available to read from serial monitor following lines are used,
If (Serial.available()>0)
To receive data ( char) from serial monitor following lines are used,
char val = Serial.read();

47
When we type character other than A that is B it turns off the LED with massage LED turned off
on Serial monitor.

ARDUINO CODE

void setup() {
Serial.begin(9600); // begin serial communication between
arduino and computer
pinMode(8,OUTPUT); // pin 8 set as output pin
}

void loop() {
if (Serial.available()>0) // when data is available from serial it
runs the function
{
char val = Serial.read(); // available data read and strores in
the val variable(char)
if(val==’A’) // if available data is A it turns on
light
{
digitalWrite(8,HIGH);
Serial.println(“LED turns on”); // print string serial monitor
}
else{ // if data is other than A it turns
off the LED
digitalWrite(8,LOW);
Serial.println(“LED turns off”); // print string serial monitor
}
}
}

48
16 Interfacing LCD
with Arduino
In this article we are going learn about how to interface LCD display with Arduino and how
to print values on it. How to do connections of LCD with Arduino and basic functions in LCD
library.

Components
Arduino Uno
16*2 LCD (16 column & 2 rows)
Jumper wires
Breadboard
10k potentiometer
330 ohm resistor
Computer

16*2 LCD display is commonly used with Arduino to display data on it. It has 16 column and 2
rows in which we can display 32 character at a time. Following are the pin out of the LCD display

49
* LCD RS pin to digital pin 12
* LCD Enable pin to digital pin 11
* LCD D4 pin to digital pin 5
* LCD D5 pin to digital pin 4
* LCD D6 pin to digital pin 3
* LCD D7 pin to digital pin 2
* LCD R/W pin to ground
* LCD VSS pin to ground
* LCD VCC pin to 5V
* 10K resistor:
* ends to +5V and ground
* wiper to LCD VO pin (pin 3)

Follow the steps for connections of LCD


display

Connect +5v pin and GND pin as shown


in fig also attach the potentiometer
between +5V and GND and middle
terminal to V0 pin. The potentiometer is
used to control contras of LCD.

Then attach the data pins as shown in the


bellow figure with D7 pin to 2, D6 pin to 3
respectively connect the D5 and D4 pin.
RS and RW pin to 11 and 12 respect.

To connect the backlight as shown in


fig with 330 ohm resistor.

50
After full assembly circuit will look like this

Open the example LCD by going to File >> Example >> Liquid Crystal >> HelloWorld

51
Upload the code to Arduino and check the Output here is LCD showing Hello World string and
counting time in seconds.

Simple code for the same output is given in following picture

ARDUINO CODE

#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
void setup() {
lcd.begin(16, 2); // set up the LCD’s number of columns and rows:
lcd.print(“hello, world!”); // Print a message to the LCD.
}

void loop() {
lcd.setCursor(0, 1); // set the cursor to column 0, line 1
(note: line 1 is the second row, since counting begins with 0):
lcd.print(millis() / 1000); // print the number of seconds since reset:
}

52
17 LM35 temperature
sensor with Arduino
and LCD
In this article we are going to learn how to interface LCD display with Arduino and how display
temperature on it.

Components
Arduino UNO

16*2 LCD Display


Jumper wires
LM35 temperature sensor
10k Potentiometer
330 ohm resistor

Schematics
For detailed connections and explanation refer the previous article of interfacing LCD with
Arduino. Check the pin out of the temperature sensor also.

Finally attach the LM35 sensor to +5V,GND and A0 pin as shown in figure.

53
Upload the code to Arduino LCD will show you welcome message and then it will print the
temperature on LCD screen as shown in fig.

Try to change temperature by holding sensor in between two figures it will rise the temperature
of sensor.

54
ARDUINO CODE

#include <LiquidCrystal.h> // includes LCD library


LiquidCrystal lcd(12, 11, 5, 4, 3, 2); // the pin no of rs = 12, en =
11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
void setup() {
lcd.begin(16, 2); // set up the LCD’s number of
columns and rows:
lcd.print(“inventr.io”); // Print a welcome message to the
LCD.
delay(500); // wait for a half second
lcd.clear(); // clears the welcome massage
}
void loop() {
int temp_adc_val = analogRead(A0); // Read analog voltage of A0
float temp_val = (temp_adc_val * 500)/1023; // Convert adc value to
equivalent voltage
lcd.print(“Temp “); // prints temperature string
lcd.setCursor(6,0); // set cursor to 6 column
and 0 row
lcd.print(temp_val); // prints temp val on seria
monitor
lcd.setCursor(10,0);
lcd.print(“°C”);
delay(500); // wait for 100 milliseconds
lcd.clear(); // clears LCD for next data
print
}

55
18 Controlling DC
Motor using L298
In this article we going to learn how to control DC motor which consume 2 Amp of current using
Arduino and push button to change running direction.

Components
Arduino Uno
Push Button * 2
1k Resistor * 2
L298 Motor driver
Jumper wire
External Power supply 12v
DC Motor 12v

Circuit Diagram

L298 is motor driver which is used to control the DC motor with 2Amp current.

56
In this example Arduino reads the input of the pushbutton by which pressing the any push
button we can run the motor in different direction.

In this example we can also change the speed of rotation by changing the digital output with
analogWrite method for the same pinas follows.

digitalWrite(countclockwise,HIGH);
analogWrite(countclockwise,155);

57
ARDUINO CODE

const int clockwise = 5; // set variable for clockwise rotation pin


const int countclockwise = 6; // set variable for anticlockwise rotation pin
const int pButton1 = 4; // set variable for push button pin
const int pButton2 = 3; // set variable for push button pin
void setup() {
pinMode(clockwise,OUTPUT); // pin 5 set as output
pinMode(countclockwise,OUTPUT); // pin 6 set as output
pinMode(pButton1,INPUT); // pin 4 set as input pin pushbutton
pinMode(pButton2,INPUT); // pin 3 set as input pin pushbutton
}

void loop() {
if(digitalRead(pButton1)==HIGH) // when push button1 read high excute
{
digitalWrite(countclockwise,LOW); // stop counterclockwise
digitalWrite(clockwise,HIGH); // start clockwise
}
if(digitalRead(pButton2)==HIGH) // when push button2 read high excute
{
digitalWrite(clockwise,LOW); // stop clockwise
digitalWrite(countclockwise,HIGH);// start counterclockwise
}
}

58
19 Introduction to the
Joystick and switch
In this article we are going learn what is joystick and how to
use it with Arduino microcontroller. Pin out of the joystick,

Joy stick is analog device which is used ease control of robot


motors and motor speeds.

COMPONENTS

Arduino UNO
Joystick
Jumper Wire
Computer

The joystick has two analog pin output and one digital pin
output which is switch.

Circuit Diagram

59
Upload the code to the Arduino it will show the x and y value on Serial monitor it also stops loop
when switch is pressed.

60
ARDUINO CODE

void setup() {
Serial.begin(115200);
pinMode(2,INPUT);
}
void loop() { // reading x and y axis values
int xAxis = analogRead(A1);
int yAxis = analogRead(A0);
// printing values of x and y axis on Serial monitor
Serial.print(“X = “);
Serial.print(xAxis);
Serial.print(“ “);
Serial.print(“Y = “);
Serial.println(yAxis);
// using swith to print switch pressed
if(digitalRead(2)==HIGH)
{
Serial.println(“Switch Pressed”);
while(digitalRead(2)==HIGH);
}
}

61
20 Interfacing L293D
Motor driver with
Arduino
In this article we are going to learn how to interface L293D motor driver IC with Arduino to
control direction of the motor.

Components
Arduino
L293D Motor driver IC
Jumper wire
Resistor 1k
Push Button
Bread Board
DC Motor

L293D IC is used to control two motor which uses 1 amp current. The pin out of the L293D as
shown in fig,

Circuit Diagram

When push button


is pressed it
rotate the motor
in clockwise
direction and other
push button turn
counterclockwise
direction and
motor runs
continuously.

62
ARDUINO CODE

const int clockwise = 5;


const int countclockwise = 6;
const int pButton1 = 2;
const int pButton2 = 3;
void setup() {
pinMode(clockwise,OUTPUT);
pinMode(countclockwise,OUTPUT);
pinMode(pButton1,INPUT);
pinMode(pButton2,INPUT);
}

void loop() {
if(digitalRead(pButton1)==HIGH)
{
digitalWrite(countclockwise,LOW);
digitalWrite(clockwise,HIGH);
}
if(digitalRead(pButton2)==HIGH)
{
digitalWrite(clockwise,LOW);
digitalWrite(countclockwise,HIGH);
}
}

63
21 Interfacing
Ultrasonic sensor
with Arduino
In this article we are going learn what ultrasonic sensor is and how to use it with Arduino.

Components
Arduino
Ultrasonic sensor
16*2 LCD
Jumper wires
10k Potentiometer
330ohm Resistor

Ultrasonic waves are sound waves


which can be used to measuring
distance between obstacles in path.
It creates sound waves by trigger
pin and it strikes on object and
come back which is detected by
the receiver the time is calculated
between transmission and receiving
sound waves by which we can
measure distance. Pinout ultrasonic
sensor is given in following diagram.

Connect the +5v pin to the 5V pin of


Arduino and Gnd to the ground. Trigger pin to pin 8 and Echo pin to the pin 7 of Arduino.

In order to generate sound wave you need to set the Trig pin High State for 10 µs. That will send
out an 8 cycles sonic burst which will travel at the speed of sound and it will be received in the
Echo pin. The Echo pin will output the time in microseconds the sound wave traveled which is
measured reading pulses on pin 7.

64
The distance can be calculated by using relation speed of sound suppose distance is 10 cm

Speed of sound = 340 m/s = 0.034 cm/µs


Time = distance/ speed = 10 / 0.034 = 294 µs
Distance = t * 0.034 /2 it is cm
We can convert it into inches by
Distance = duration*0.0133/2

65
ARDUINO CODE

#include <LiquidCrystal.h> // includes LCD library


LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
long duration; // variable for storing time duration
int dist_cm, dist_in; //variable for distance in cm and inch
void setup() {
lcd.begin(16, 2); // set up the LCD’s number of columns and rows:
pinMode(8, OUTPUT); // set 8 pin trigger pin as output pin
pinMode(7, INPUT); // set echo pin as input pin
}
void loop() {
digitalWrite(8, HIGH); // sets the trigger pin high for 10 microseconds
delayMicroseconds(10);
digitalWrite(8, LOW); // sets the trigger pin low
duration = pulseIn(7, HIGH);
dist_cm= duration*0.034/2;
dist_in = duration*0.0133/2;

lcd.setCursor(0,0); // Sets the location at which subsequent text


written to the LCD will be displayed
lcd.print(“Distance: “); // Prints string “Distance” on the LCD
lcd.print(dist_cm); // Prints the distance value from the sensor
lcd.print(“ cm”);
lcd.setCursor(0,1);
lcd.print(“Distance: “);
lcd.print(dist_in);
lcd.print(“ inch”);
delay(10);
}

66
22 Controlling
DC motor with
joystick
In no of applications like Arduino based robots, crane controls and speed controls its necessary
to control speed and direction of the DC motor in this article we are going to learn how to
control speed and direction of DC motor using joystick.

Components Connections
Arduino UNO Components Pin’s Arduino Pin’s Extra
Jumper wires L298 IN1 6 -
DC Motor L298 IN2 5 -
L298 motor driver Joystick VCC +5V -
Joystick
Joystick GND GND -
12V power supply
Joystick Vax A0 -
L298 M1 A&B - Motor

Schematics

Do all connections as per the


schematics. Upload the code to
the Arduino and connect the 12v
adapter to the circuit. Move the
joystick in x direction it will run
the motor and increase the speed
with throttle also move it to the
backward direction to change
motor direction.

67
68
ARDUINO CODE

const int clockwise = 5; // set variable for clockwise rotation pin


const int countclockwise = 6; // set variable for counterclockwise
rotation pin
void setup() {
Serial.begin(9600); // starts serialcommunication
pinMode(clockwise,OUTPUT); // pin 5 set as output
pinMode(countclockwise,OUTPUT); // pin 6 set as output
}

void loop() {
int xPosition = analogRead(A0); // reads the x axis position
if(xPosition > 480 && xPosition < 540) // reads the miidle position ofjoystick
{
digitalWrite(countclockwise,LOW); // stop counterclockwise
digitalWrite(clockwise,LOW); // stop clockwise
}
else if(xPosition > 0 && xPosition < 480) // if value is positive 0-480
{
digitalWrite(countclockwise,LOW); // stop counterclockwise
int pwmVal1 = map(xPosition,0,480,255,0); // map the reverse value
analogWrite(clockwise,pwmVal1); // analog write pwm clockwise
}
else if(xPosition > 540) // if value is negative >540
{
digitalWrite(clockwise,LOW); // stop clockwise
int pwmVal2 = map(xPosition,540,1023,0,255); // map the vlue
analogWrite(countclockwise,pwmVal2); // analog write pwm counterclockwise
}
Serial.println(xPosition); // prints xPosition on serial monitor
}

69
23 Controlling LED
with Arduino
In this article we are going to learn about RGB led and how to control it using Arduino.

Components required
Arduino Uno
RGB LED
330ohm resistor * 3
Jumper wires

RGB LED is the three in one LED which contains the RED, GREEN
and BLUE color LED in single. This type of LED’s are used to
produce various colors and as indicator.

This is the common cathode LED in which anode is used to


control colors. By providing voltage to the different anode we can
produce different color. Following fig shows the pinout for the RGB LED where Red, Blue and
Green are anode pins.

For interfacing the LED to the


Arduino connect the LED anode
terminal to the Arduino digital pins as
shown in figure trough the 330 ohm
resistor.

Upload the code to the Arduino and


it will produce Red, Blue and Green
color with dimming effect.

70
71
ARDUINO CODE

#define red 10 // difine 10 pin to the red color


#define blue 9 // difine 9 pin to the red color
#define green 8 // difine 8 pin to the red color
void setup() {
pinMode(red,OUTPUT); // set the red pin to output
pinMode(blue,OUTPUT); // set the blue pin to output
pinMode(green,OUTPUT); // set the green pin to output
}
void loop() { // generates the dimming effect for red color
for (int i =0 ; i < 255 ; i++)
{
analogWrite(red,i);
delay(10);
}
digitalWrite(red,LOW); // generates the dimming effect for green color
for (int i =0 ; i < 255 ; i++)
{
analogWrite(green,i);
delay(10);
}
digitalWrite(green,LOW); // generates the dimming effect for red color
for (int i =0 ; i < 255 ; i++)
{
delay(10);
analogWrite(blue,i);
}
digitalWrite(blue,LOW);

72
24 Object counter
using Arduino and
IR sensor module
In this article we are going to learn about IR (Infra-Red) sensor module and also use it for the
counting object.

Components required
Arduino
IR module
16*2 LCD
Jumper wire
10k Potentiometer
Resistor 330 ohm

IR sensor produce digital High output when any object detected by it which is measured by
using digital pin.

The pinout of the IR module as follows

Schematics
Do the connections
as per the schematics.
Connect the IR sensor
module VCC pin to 5v
and GND pin to ground
and Dout pin to pin 7.

Upload the code to


the Arduino check the
output on LCD display it
will show something like
this.

73
This counter will get increase when we moved any object from the IR sensor. We can use this as
people counter in malls with placing the sensor both side entry and exit.

74
ARDUINO CODE

#include <LiquidCrystal.h> // includes LCD library


LiquidCrystal lcd(12, 11, 5, 4, 3, 2); // the pin no of rs = 12, en = 11,
d4 = 5, d5 = 4, d6 = 3, d7 = 2;
int object = 0 ;
void setup() {
lcd.begin(16, 2); // set up the LCD’s number of columns and rows:
pinMode(7,INPUT); // sets pin 7 as IR module input pin
lcd.print(“Object Counnter”); // print object counter on lcd
lcd.setCursor(7,1); // set cursor to second row ans 7 column to print
lcd.print(object); // print intial value of object
}

void loop() {
if(digitalRead(7)==HIGH) // when ir sensor detect object
{
object++; // object increses
lcd.setCursor(7,1);
lcd.print(object); // print value of object
while(digitalRead(7)==HIGH); // wait till the pin become low
}
}

75
25 Blink without
delay
In case of blinking two or more LED’s with different rate it not possible to use delay function
that’s why we are going to learn about millis() function in Arduino which counts the time after
the powering on the Arduino in milliseconds. We can use this time to do operations. This is very
important in case of automation process in which time is mandatory and loop is continues.

Connections’
Components
Arduino UNO Components Pin’s Arduino Pin’s Extra

330ohm Resistor LED1&2 (Cathode) GND -

LED’s * 2 LED1 (Anode) 13 R 330 Ω

Computer LED2 (Anode) 10 R 330 Ω

In this example we are using two LED’s which blinks with different rate at same time. First led
blinks with time interval of 1 second and another one with half second.

Schematics

In this case we can’t blink both led


with different rate with delay time
function by which if we want to
blink rate for one LED is 1 sec and
for another 0.5 sec then first LED
will turn on off for 1.5 sec like this
second one also will turn on for
same time.

76
Hence we use millis() function as follows,

In this case starts the measuring time with unsigned long currentMillis = millis();

Lines. If current millisecond goes above time interval that is 1 sec it will turn the state of LED pin
ON(HIGH) to of off (LOW) or off(LOW) to on (HIGH). Previous time interval and now time are
make same by comparing it with each other.

We do same for the second LED by using same timer like doing different task at different time
with same watch.

77
ARDUINO CODE

int ledState1 = LOW; // ledState1 used to set the LED


int ledState12 = LOW;
unsigned long previousMillis1 = 0; // will store last time LED was updated
unsigned long previousMillis12 = 0;
const long interval1 = 1000; // interval1 at which to blink (milliseconds)
const long interval12 = 500;
void setup() {
pinMode(13, OUTPUT); // set the digital pin as output
pinMode(10, OUTPUT);
}

void loop() {
unsigned long currentMillis = millis();
// count the milliseconds from powering on arduino
if (currentMillis - previousMillis1 >= interval1) {
// when one second pass
previousMillis1=currentMillis; // change prevseconds to currentseconds
if (ledState1==HIGH) // compare the LED prev state
{ledState1=LOW;}
else
{ledState1=HIGH;}
digitalWrite(13,ledState1);
} // FOr second LED with 500 millisecond delay
if (currentMillis - previousMillis12 >= interval12) {
previousMillis12=currentMillis;
if (ledState12==HIGH)
{ledState12=LOW; }
else
{ledState12=HIGH; }
digitalWrite(10,ledState12);
}
}

78
26 Temperature
controlled alarm
In some cases there is need to monitor temperature based on predetermined condition as the
temperature goes above certain preset value the alarm goes turn on to reduce it. In following
example we are going to do same as the temperature goes above 10 degree it will turn on the
buzzer to notify operator to turn off it.

Components Connections
Arduino UNO
Components Pin’s Arduino Pin’s Extra
Piezo buzzer
LM35 Temperature sensor
LM35 +5V 5V -
OUT A0 -
GND GND -
Buzzer Anode 5 -
Buzzer Cathode GND -

Schematic

Pin out of LM35

79
Following example we defined the value of threshold at beginning which is 10 degree. We
measure the temperature by using LM35 with analog pin A0. Then compared it with threshold
to check whether the temperature is high or low than threshold if it goes above we turn on the
buzzer in tune with for loop.

You can practice the code by changing the values of threshold also open the serial monitor to
read the actual temperature read.

80
ARDUINO CODE

const int buzz_pin = 5;


int threshold = 10;
void setup() {
Serial.begin(9600);
pinMode(buzz_pin,OUTPUT);
}
void loop() {
int temp_adc_val = analogRead(A0); // Read analog voltage of A0
float temp_val = (temp_adc_val * 500)/1023;
// Convert adc value to equivalent voltage
if(temp_val > threshold)
{
for (int i =50 ; i > 255 ; i++)
{
analogWrite(buzz_pin,i);
delay(30);
analogWrite(buzz_pin,0);
Serial.println(temp_val);
}
}
else
{
analogWrite(buzz_pin,0);
}
Serial.println(temp_val);

81
27 Automatic Arduino
based watering
system
While taking care of gardens it is necessary to watering it regularly. Some time we forgot to
water the plants. So in this article we are going to make soil moisture sensor based watering
system which sense the moisture of soil and turn on the water pump to water the plants and
turn it back off when moisture become previse range.

Components
Pin out of IRF540 Pin out of moisture sensor
Arduino Uno
Soil moisture sensor
12V Pump
12V Power adapter
IRF540 Mosfet
5 kΩ resistor
Jumper wires

Connections

Components Pin’s Arduino Pin’s Extra


G 5 -
D Motor GND -
S GND Don’t forgot to attach 5 kΩ resistor
between G & S
Ao A0 -
GND GND -

VCC +5V -

82
Schematics

Do the

Do the connections as shown in schematics. Upload the code to the Arduino and insert the
moisture sensor into the soil. Connect the circuit to 12v power supply to the circuit. Sensor
reads the moisture of soil if moisture drops below the threshold its start the pump and turn it on
until moisture become again it level.

83
ARDUINO CODE

void setup() {
pinMode(5,OUTPUT); // sets pin 5 as output for water pump
}

void loop() {
int moisture = analogRead(A0); // reads analog value of moisture
if (moisture > 1000) // if moisture goes above 1000 turn pump on
{
digitalWrite(5,HIGH);
while(moisture < 200); // wait untill moisture value rich 200
digitalWrite(5,LOW); // turn off the pump off
}
}

84
28 Using Serial
Plotter
Many of times we need to plot data on graph with time change or any other conditions like time
vs temperature or Temperature vs Humidity on x and y axis of Cartesian coordinate system.
This graphs can be plotted with serial plotter.

Components
Arduino UNO Connections
Potentiometer 10 kΩ
Components Pin’s Arduino Pin’s Extra
Jumper wires
Potentiometer pin 1 GND -
Computer
Middle A0 -
Potentiometer pin 3 5V -

Schematics In following code we


measure the analog values
and we plot it on the graph
with respect to time in
seconds on X axis and
values on Y axis. Also we
measure the time with
millis function as in blink
without delay tutorial.
We converted it into pure
integer and seconds by
dividing 1000.

In case of serial plotting


we use Space (“ “) to divide
data on x and y axis and
use new line println(); for
end data.

85
Upload the code to the Arduino open the serial plotter as follows,

Tools >> Serial plotter

It will open the graph rotate the potentiometer and see the changes in y axis data with respect
to time on x axis. But seconds red plotter will rise slowly here blue line shows analog values and
red line shows the sec data as follows,

86
We can also plot only the analog value by uploading following code. Just comment
down the last two values and add ln to print of analog values as follows in loop.

Open the serial plotter it will show result something like this as follows,

Here blue line indicate change in analog value with time.

87
ARDUINO CODE

void setup() {
Serial.begin(9600);
}
void loop() {
unsigned long currentMillis = millis(); // counts the time
int sec = currentMillis/1000; // convert millis to sec
Serial.print(analogRead(A0)); // prints analog value on Y axis
Serial.print(“ “); // use space to to seperate data
Serial.println(sec); // prints time on Y axis with data end with new line
}

88
29 Using internal
EEPROM of Arduino
In some cases it’s necessary to save the data or variable even after the power loss of Arduino to
use it again when power comes back. Like last state of light is on or off when power went off can
be stored and read again using EEPROM. EEPROM is Electrically Erasable Programmable Read-
Only Memory which can used to store data permanently and read it again when necessary.

Following are the EEPROM size Arduino has

1. Arduino UNO 1kB Connections


2. Arduino Nano 1kB
Components Pin’s Arduino Pin’s Extra
3. Arduino Mega 4kB
Potentiometer pin 1 GND -
4. LilyPad 1kB
Middle A0 -
Potentiometer pin 3 5V -
Components
Arduino UNO
Potentiometer 10 kΩ
Jumper wires
Computer

Schematics In following two example we


are going to write data in first
example and read the same
data again by using EEPROM.

Writing data to EEPROM

For using internal EEPROM


we have to use EEPROM
library we initialize and
defined the address variable
to define address it is locating
in EEPROM we can access the
location by using this address.
The maximum address of this
location is 1Kb that is 1023
locations.

89
We created the while loop to store
the data in each memory location
that is less than 1023 address. We
measure the analog values and
converted into 1 bit by dividing
analog value by 4 because the each
memory location can only store the
value up to 255. Finally we write data
to EEPROM using EEPROM.write();
function.

Upload the code and open serial


monitor it will show EEROM address
and which value stored at which
location. Rotate the potentiometer to
change values.

Remove the power from Arduino


and connect again then upload the
EEPROM read code

Reading data from EEPROM

In this code we created the while


loop to read data from EEPROM
the hole process are same but we
use the function EEPROM.read(); to
read data. Upload the code and open
serial monitor it will show you the last
values stored at each location.

90
ARDUINO CODES
Write Data
#include <EEPROM.h> // added EEPROM library
int address = 0; // initialise the EEPROM address
int EEPROMSize = 1023; // maximum EEPROM size for arduino 1Kb=1023bites
void setup() {
Serial.begin(9600); // begin the serial communication
EEPROM.begin(); // bigin the EEPROM comunication

// loop runs once


while (address < EEPROMSize) // define the while loop for less than EEPROM address
{
int val = analogRead(A0); // reads and stores the value in val variable
int data = val/4; // convert it into 1 bit data that is less than 256
address++; // increase the address by one to change it
EEPROM.write(address,data); // write data at address using function EEPROM.
write();
Serial.print(address); // prints data on Serial monitor
Serial.print(“ “);
Serial.print(data);
Serial.println(“data write succssesfully”);
delay(500); // give time to Store data in EEPROM
}
// while loop END
}

void loop() { // empty loop becouse code runs only once


}
Read Data
#include <EEPROM.h> // added EEPROM
int address = 0; // initialise the EEPROM address
int EEPROMSize = 1023; // maximum EEPROM size for arduino 1Kb=1023bites
void setup() {
Serial.begin(9600); // begin the serial communication
EEPROM.begin(); // bigin the EEPROM

// loop runs once


while (address < EEPROMSize) // define the while loop for less than EEPROM address
{
address++; // increase the address by one to change it
int data = EEPROM.read(address); // reaed data at address using function EEPROM.
read();
Serial.print(address); // prints data on Serial monitor
Serial.print(“ “);
Serial.print(data);
Serial.println(“data read succssesfully”);
delay(500); // give time to Store data in EEPROM
} // while loop END
}

void loop() { // empty loop becouse code runs only once


}

91
30 Vibration sensor
with Arduino
The vibration sensor SW40 is used to measure vibration. We can measure the frequency of
vibrations using this sensor. We are going to build earthquake detector using vibration sensor
and plot graph using this.

Components
Arduino Pin out of SW40 vibration sen-
sor module
Vibration sensor
Buzzer
Jumper wires

Connections

Components Pin’s Arduino Pin’s Extra


SW40 DOUT 3 -
SW40 GND GND -
SW40 +5V 5V -
Buzzer Anode 13 -
Buzzer Cathode GND -

Schematics

In this example pulseIn();


function measures the time
to how much time pin become
high and then measured it. If
this time changes some range
then it will detect vibrations and
activate the buzzer for creating
alarm.

92
Open the serial monitor as well as serial plotter and shake the sensor with hand it will show you
values and graph of vibrations.

93
ARDUINO CODE

void setup() {
Serial.begin(9600); // initilise the serial communication
pinMode(3,INPUT); // pin 3 as input
pinMode(13,INPUT); // set pin as output
}
void loop() {
long measurement=pulseIn (3, HIGH); // measures pulses time
Serial.println(measurement); // prints time pulses
if (measurement > 1000)
{
digitalWrite(13,HIGH); // write buzzer pin to HIGH
delay(10);
}
else{
digitalWrite(13,HIGH); // else write buzzer pin to LOW
}
}

94
31 Water Quantity
measurement and
level indicator
Measurement of fluid quantity is important in industrial and commercial level. This article we
are going to make water quantity measurement and level indicator using Arduino. Ultrasonic
sensor measures the height of water from the level and convert it into volume. It measures the
volume in milliliters. Also shows water level in type of bar.

Components
Arduino UNO
16*2 LCD
Jumper wires
Ultrasonic sensor
Container to store liquid
Potentiometer 10kΩ Connections
330 Ω resistor
Components Pin’s Arduino Pin’s Extra
Ultrasonic sensor +5V +5v -
Schematics
Ultrasonic sensor GND GND -
Ultrasonic sensor Trig 8 -
Ultrasonic sensor Echo 7 -
LCD RS 12 -
LCD Enable 11 -
LCD D4 5 -
LCD D5 4 -
LCD D6 3 -
LCD D7 2 -
LCD R/W GND -
LCD VSS GND -
LCD VCC 5V -
10K Pot 1 pin 5V -
10K Pot 2 pin V0 of LCD -
10K Pot 3 pin GND -
LED + & - +5v & GND 330 Ω

95
Do the LCD screen connection same as the LCD hello world tutorial. Connect the ultrasonic
sensor to the Arduino as per the schematics.

Here we calculate the water level in the tank by subtracting height measured by sensor from
total height of container in centimeters. We calculate the area of container and multiply it with
water level to get actual water quantity in cm^3 or milliliter.

Suppose we have container with total height of 300 cm and distance measured by sensor is 200
cm then

Water level = 300 – 200 = 100 cm

Volume = water level * Area of container

You can change area and total height of container with yours at the top of the code.

96
Upload the code and
attach the sensor
to the top of the
container pour the
water and check the
water level increase
with water pouring.

LCD will show


something like this

First row indicate


how much water in
tank and second row
indicates the actual
level of water.

97
ARDUINO CODE

#include <LiquidCrystal.h> // includes LCD library


LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
long duration; // variable for storing time duration
int total_hight=300; // total hight of drum
int area = 78; // suppose drum with diameter 10 cm area will 3.14/4*10^2 = 78
void setup() {
lcd.begin(16, 2); // set up the LCD’s number of columns and rows:
pinMode(8, OUTPUT); // set 8 pin trigger pin as output pin
pinMode(7, INPUT); // set echo pin as input pin
}

void loop() {
digitalWrite(8, HIGH); // sets the trigger pin high for 10 microseconds
delayMicroseconds(10);
digitalWrite(8, LOW); // sets the trigger pin low
duration = pulseIn(7, HIGH);
float hight = duration*0.034/2;
float level = total_hight- hight;
int row = map(level,0,300,0,6);
float Vol = level*area;
lcd.clear();
lcd.setCursor(0,0);
lcd.print(“Water Qun”);
lcd.setCursor(10,0);
lcd.print(Vol);
lcd.setCursor(14,0);
lcd.print(“ml”);
for (int i=0 ; i<=row ; i++)
{
lcd.setCursor(i,1);
lcd.print(“#”);
}
delay(200);
}

98
32 Tachometer
using Arduino
RPM measurement is the necessary in lots of cases like speed measurement of vehicle, REV
meters in vehicles and speed fans and motors. For measuring rpm we measures the how many
time any object create an event in one minute. We use this event to calculate RPM. We can
easily get speed with multiplying RPM with periphery of the vehicle tire.

Components Connections
Arduino UNO
Components Pin’s Arduino Pin’s Extra
16*2 LCD
IR VCC +5v -
Jumper wires
IR GND GND -
10 kΩ potentiometer
DOUT 2 -
330 Ω resistor
LCD RS 12 -
IR module
LCD Enable 11 -
Any rotating Object
LCD D4 5 -
LCD D5 4 -
Connections
LCD D6 3 -
LCD D7 2 -
LCD R/W GND -
LCD VSS GND -
LCD VCC 5V -
10K Pot 1 pin 5V -
10K Pot 2 pin V0 of LCD -
10K Pot 3 pin GND -
LED + & - +5v & GND 330 Ω

Select any rotating object to measure


the speed of that like fan or any motor.
Attach the wheel to the motor shaft tape
the small white tape strip on that. Like
follows,

99
When white strip come in front of IR sensor it will create
HIGH signal which is detected by Arduino by using
Arduino external interrupt. We attach the sensor pin to
interrupt pin. When pin goes from low to HIGH it calls the
function isr();

Which increase the rev by 1. This revolutions are then


converted into RPM and its set to 0 when one loop passes.

Upload the code rotate the motor or


hold the sensor in front of rotating
disk it will shows you RPM measured
as follows,

100
ARDUINO CODE

#include <LiquidCrystal.h> // includes LCD library


LiquidCrystal lcd(12, 11, 10, 9, 8, 7);
// the pin no of rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
float rev=0;
int rpm;
int oldtime=0;
int time;
void setup()
{
lcd.begin(16,2);
attachInterrupt(0,isr,RISING); //attaching the interrupt
}
void loop()
{
delay(100);
detachInterrupt(0); //detaches the interrupt
time=millis()-oldtime; //finds the time
rpm=(rev/time)*60000; //calculates rpm
oldtime=millis(); //saves the current time
rev=0;
lcd.clear(); // clears the lcd
lcd.setCursor(0,3);
lcd.print(“Tachometer”);
lcd.setCursor(0,1);
lcd.print(“RPM”);
lcd.setCursor(10,1);
lcd.print(rpm);
attachInterrupt(0,isr,RISING); //attaches the interrupt
}
void isr() //interrupt service routine
{
rev++;
}

101
33 Smartphone
wireless serial
monitor
In some cases it’s necessary to monitoring data wirelessly. In this article we are going to learn
how to use wireless serial monitor to monitor data and control the Arduino digital pins with
example of led.

Components Connections
Arduino UNO
Components Pin’s Arduino Pin’s Extra
HC-05 Bluetooth module
HC-05 VCC 5V -
LED
HC-05 GND GND -
Jumper wires
HC-05 TXD RX or D0 -
HC-05 RXD TX or D1 -
LED Anode 13 -
LED Cathode GND -
Pin out of HC-05 Bluetooth module

While connecting the Bluetooth module to the


Arduino take care of following notes,

Schematics
1. Connect RX pin to TX
2. Connect TX pin to RX
3. Remove RX and TX pin before uploading
the code that is connect it after the code is
uploaded to board.

102
Upload the code to Arduino it is same as we learned in past tutorial of controlling LED with
serial monitor.

After uploading the code connect the RX and TX pins as said earlier. The power LED on the
Bluetooth module start blinking continuously. Download the Serial Bluetooth app from play
store. Go to setting Bluetooth turn on it and search for new device HC05 enter the password
1234 and connect it. Open the app and side bar >> device >> HC05 close and tap on the connect
button in app bar. Then monitor will show connected massage.

Then serial monitor


will print Connected to
Arduino. Then send the
letter A to turn on the
LED and B for the Turn
off it.

103
ARDUINO CODE

void setup() {
Serial.begin(9600); //begin serial communication between arduino and computer
Serial.println(“Connected to arduino”); // prints string on serial monitor
pinMode(13,OUTPUT); // pin 8 set as output pin
}

void loop() {
if (Serial.available()>0) // when data is available from serial it runs the function

{
char val = Serial.read(); // available data read and strores in the val variable(char)
if(val==’A’) // if available data is A it turns on light
{
digitalWrite(13,HIGH);
Serial.println(“LED turns on”); // print string serial monitor
}
else if(val==’B’)
{ // if data is B it turns off the LED
digitalWrite(13,LOW);
Serial.println(“LED turns off”); // print string serial monitor
}
}
}

104
34 Interfacing tilt
sensor with
Arduino
Tilt detection is used in many cases like accident detection
in two wheeler and game controllers. It has ball type sphere
inside shows continuity when its straight and when it’s
tilted it does not show continuity.

Components
Tilt sensor
Arduino UNO
LED
Buzzer
Jumper Wires Connections
330 Ω Resistor
Components Pin’s Arduino Pin’s Extra
Tilt sensor 1 +5v -
Tilt sensor 2 GND & 10 330 Ω
Buzzer Anode 3 -
Buzzer Cathode GND -
LED Anode 4 -
LED Cathode GND 330 Ω

In following example when the tilt


sensor detect the tilt it will turn on
the LED and Buzzer. When tilt sensor
detect tilt it will destroy the continuity
and produce LOW signal by which turn
on LED and buzzer output.

105
You can use this type of circuits for controlling motors and robots which has tilt mechanisms.

ARDUINO CODE
const int tiltSensor = 10; // difined the tilt sensor input to pin 10
const int LED = 4; // difined the LED output to pin 4
const int Buzzer = 3; // difined the Buzzer output to pin 3
void setup() {
pinMode(tiltSensor,INPUT); // sets tilt sensor pin as input
pinMode(LED,OUTPUT); // sets tilt LED pin as OUTPUT
pinMode(Buzzer,OUTPUT); // sets tilt Buzzer pin as OUTPUT
}

void loop() {
bool tiltDetect = digitalRead(tiltSensor); // boolian for detecting tilt
if (tiltDetect == LOW) // when tilt detected
{
digitalWrite(LED,HIGH); // turns on LED and Buzzer
digitalWrite(Buzzer,HIGH);
}
else
{
digitalWrite(LED,HIGH); // else LED and Buzzer remain off
digitalWrite(Buzzer,HIGH);
}
}

106
35 Interfacing LDR
sensor with Arduino
UNO to control LED
brightness
LDR is light dependent resister which internal resistance changes with light. When photon of
light falls on the LDR its resistance changes with intensity of photon. As the intensity of light
increases the resistance of LDR reduces and increases with decrees in light.

Components
Arduino UNO
LED
1 kΩ resistor LDR (Light dependent resistor)
330 Ω resistor
Jumper wires

Connections

Components Pin’s Arduino Pin’s Extra


LDR 1 & 2 +5v & GND 1 kΩ
LED Anode 11 -
LED Cathode GND 330 Ω

107
Do the all connections as per the schematics and upload the code to the Arduino. Open the
serial monitor.

Serial monitor will say its day when light is enough bright if there is no light it will say its dark
and try to adjust brightness with turning on LED.

ARDUINO CODE
void setup() {
pinMode(10,OUTPUT); // pin 10 as output for LED
Serial.begin(9600); // begins serial communication
}
void loop() {
int brightness = analogRead(A0); // reads analog value by LDR
int pwm_val = map(brightness,0,1023,255,0); // converts LDR value to in
range of 0-255
analogWrite(10,pwm_val); // write pwm value to LED pin to adjust
brightness
if(brightness > 900) // if anlog val is greater than 900 then
serial monitor say its day
{
Serial.println(“Its a shiny day light is turned off”);
}
else if(brightness < 100) // if anlog val is less than 100 then
serial monitor say its night
{
Serial.println(“Its a dark night light is turned on”);
}
}

108
36 Serial communication
between Arduinos
The serial communication is the mostly used in Arduino microcontroller to communicate to
devices such as you want send data from one Arduino to another Arduino. Also you can control
devices of connected to another microcontroller. In this article we are going to control LED
brightness of second Arduino by potentiometer connected to first Arduino.

Components
Arduino UNO * 2 Connection
Jumper wires
Components Pin’s Arduino 1 Pin’s Arduino 2 Pin’s
LED
Pot 1 +5v -
330 Ω resistor
Pot 2 A0 -
Potentiometer
Pot 3 GND -
Notes
- RX TX
1. Serial communication baud rate of
- TX RX
both devices should be same.
LED Cathode GND 330 Ω
2. RX connect to the TX
LED Anode - 11
3. TX connect to the RX
- GND GND
4. Both devices should have common
ground.

Schematics

Before
connecting the
Arduino RX TX
pins to each
other upload
the codes to
the respective
Arduino’s
and do all
connections.
The codes of
each Arduino’s
are follows.

109
Sender
The first one Arduino reads the potentiometer analog values and convert it into pwm values (0-
255) and we send it to serial port with Serial.write(); function. This data is transferred trough TX
of first Arduino.

Receiver side
Second Arduino receive the data from first Arduino and write it to the LED pwm pin which
controls the brightness of the LED.

After uploading the codes and successful connection rotate the potentiometer and see the
dimming effect of LED connected to receiver side.

110
ARDUINO CODE

Sender
void setup() {
Serial.begin(9600); // enables serial communication
}

void loop() {
int val = analogRead(A0); // reads analog voltages from A0 pin
int pwm_val = map(val,0,1023,0,255); // conver 0-1023 to 0-255
Serial.write(pwm_val); // write data to serial port
}
Receiver
void setup() {
Serial.begin(9600); // enable serial communication
pinMode(11,OUTPUT); // sets 11 pin as output of LED
}
void loop() {
if(Serial.available()>0) // when data is available to read
{
int val = Serial.read(); // read the data
analogWrite(11,val); // write led pin to recived value in val
}
}

111
37 I2C communication
between two Arduinos
I2C is a serial protocol used on a low speed 2 wire interface. It was developed by Philips to
communicate internal IC’s to each other. It has limitation of distance it cannot used for long
distance communication. Speed of transmission decreases with increase in speed.

Components
Arduino * 2
Potentiometer
Jumper wires
LED
330 Ω resistor

In I2C communication one device acts Connection as


master and another devices acts as
slave. In one circuit there is only one Components Pin’s Arduino 1 Pin’s Arduino 2
(master) Pin’s (slave)
master and all other devices acts as
slave. Slave can be up to 200 or more. Pot 1 +5v -
We can send and receive data between Pot 2 A0 -
master and slave in following article Pot 3 GND -
we are going to send data from master - SCL or A5 SCL or A5
to control LED on slave device also - SDA or A4 SDA or A4
we print it on serial monitor. In case of LED Cathode - GND
I2C communication we use 5k resistor LED Anode - 11 as
pullup resistor. In Arduino there are
- GND GND
inbuilt pullup resistors.

In Arduino UNO A5 pin is SCL (Serial clock line) and A4 is SDA (Serial data line).

112
Schematics

Upload the code to the Arduino and open the serial monitor of second Arduino that is slave it
will show pwm values of the LED. Rotate the potentiometer on master and check the brightness
of the LED.

113
Master code

Slave code

114
ARDUINO CODES
Master
//i2c Master Code(UNO)
#include <Wire.h> // wire library for I2C communication
void setup()
{
Wire.begin(); // begin two wire I2C Communication
}

void loop()
{
int val = analogRead(A0); // reads analog voltages
int pwm_val = map(val,0,1023,0,255); // convert analog values in range 0-255
Wire.beginTransmission(5); // start I2C communication with 5 address
Wire.write(pwm_val); // write pwm_val to address
Wire.endTransmission(); // ends transmission
}
Slave
//i2c Slave Code
#include <Wire.h> // wire library for I2C communication

void setup()
{
Serial.begin(9600); // begin serial communication
Wire.begin(5); // begin two wire I2C Communication
Wire.onReceive(receiveEvent); // function to call when wire recieve data

pinMode(11,OUTPUT); // set pin 11 output


}

void loop()
{
// empty loop
}

void receiveEvent(int howMany)


{
while(Wire.available()) // when data is available to recieve
{
int pwm_val = Wire.read(); // store recieved value in pwm_val
analogWrite(11,pwm_val); // write value to 11 pin
Serial.println(pwm_val); // prints val to serial monitor
}
}

115
38 Interfacing seven
segment display
with Arduino
Seven segment displays are mostly used to prints number like temperature data, RPM
indicator, Counters and Numerical displays etc. The seven segment displays are two
types based on common terminal that is common anode and common cathode.
Components
Arduino UNO
Jumper wires Connections
Seven Segment display {Common Anode}
Components Pin’s Arduino Pin’s Extra
Bread board
a 9 330 Ω
330 Ω resistor * 8
b 12 330 Ω
c 4 330 Ω
Pin out of the seven segment display
d 3 330 Ω
as follows. To display the number we
e 2 330 Ω
turn on the respective internal LED by
f 11 330 Ω
connecting it to Arduino pins.
g 10 330 Ω
dp 5 330 Ω
On the basis of type of seven segment
com +5V -
display the common terminal is
connected to the positive terminal or ground. As explained below the
internal structure of seven segment displays are follows.

Common Anode Common Cathode

116
The following one is common anode seven segment display.

Schematic

To make one number we turn on the b and c LED. We defined the pins and turn it on by making b
and c pins low in array because we using the common anode display.

char pinNo[8]={a,b,c,d,e,f,g,dp};

int one[8]={1,0,0,1,1,1,1,0};

117
Following code explains how one is created in loop upload the code and try to make one number

Do the connections as shown in schematics. Upload the code to the Arduino and it will start to
print no on display from 0 to 9 like this,

118
ARDUINO CODE
#define a 9
#define b 12
#define c 4
#define d 3
#define e 2
#define f 11
#define g 10
#define dp 5
char pinNo[8]={a,b,c,d,e,f,g,dp};
int one[8]={1,0,0,1,1,1,1,0};
int two[8]={0,0,1,0,0,1,0,0};
int three[8]={0,0,0,0,1,1,0,0};
int four[8]={1,0,0,1,1,0,0,0};
int five[8]={0,1,0,0,1,0,0,0};
int six[8]={0,1,0,0,0,0,0,0};
int seven[8]={0,0,0,1,1,1,1,0};
int eight[8]={0,0,0,0,0,0,0,0};
int nine[8]={0,0,0,0,1,0,0,0};
int zero[8]={0,0,0,0,0,0,1,0};

void setup()
{
for(int i =0; i<9 ;i++)
{
pinMode(pinNo[i],OUTPUT);
}
}

void loop()
{
one_number();
delay(1000);
two_number();
delay(1000);
three_number();
delay(1000);
four_number();
delay(1000);
five_number();
delay(1000);
six_number();
delay(1000);
seven_number();
delay(1000);
eight_number();
delay(1000);
nine_number();
delay(1000);
zero_number();
delay(1000);
}

void one_number(){
for (int i=0;i<9;i++)
{
digitalWrite(pinNo[i],one[i]);
}
}

119
void two_number(){
for (int i=0;i<9;i++)
{
digitalWrite(pinNo[i],two[i]);
}
}
void three_number(){
for (int i=0;i<9;i++)
{
digitalWrite(pinNo[i],three[i]);
}
}
void four_number()
{
for (int i=0;i<9;i++)
{
digitalWrite(pinNo[i],four[i]);
}
}
void five_number()
{
for (int i=0;i<9;i++)
{
digitalWrite(pinNo[i],five[i]);
}
}
void six_number()
{
for (int i=0;i<9;i++)
{
digitalWrite(pinNo[i],six[i]);
}
}
void seven_number()
{
for (int i=0;i<9;i++)
{
digitalWrite(pinNo[i],seven[i]);
}
}
void eight_number()
{
for (int i=0;i<9;i++)
{
digitalWrite(pinNo[i],eight[i]);
}
}
void nine_number()
{
for (int i=0;i<9;i++)
{
digitalWrite(pinNo[i],nine[i]);
}
}
void zero_number()
{
for (int i=0;i<9;i++)
{
digitalWrite(pinNo[i],zero[i]);
}
}

120
CONTACT US
Business: [email protected]
Order Inquiry: [email protected]
Other: [email protected]
www.inventr.io

You might also like