37 Beginner Projects
37 Beginner Projects
BEGINNERS
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:
Understanding of code:
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);
pinMode(13, OUTPUT);
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);
digitalWrite(LED_BUILTIN, LOW);
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.
Select the board and com port by going Tools >>Board as Arduino/Genuino UNO
7
Upload the sketch by clicking → Arrow circle.
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 :
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
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.
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() {
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 !”);
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
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.
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
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.
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
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
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
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
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
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
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
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)
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.
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
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
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
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,
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
62
ARDUINO CODE
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
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
65
ARDUINO CODE
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
67
68
ARDUINO CODE
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.
70
71
ARDUINO CODE
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.
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.
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
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
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
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
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
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
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
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 -
85
Upload the code to the Arduino open the serial plotter as follows,
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,
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.
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.
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
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
Schematics
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
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.
97
ARDUINO CODE
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 Ω
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();
100
ARDUINO CODE
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
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.
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 Ω
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
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 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
void loop()
{
// empty loop
}
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.
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