Advance Electronics
Advance Electronics
1
INDEX
2
Different Arduino Extensions 41
Project: Distance Detection Using Ultrasonic Sensor 43
Hardware required 44
Software required 44
Circuit Diagram 44
Steps to Add Extensions in Blockzie Software 44
Chapter 5: Block of Pins in Blockzie software 52
Pins Block Pattle 53
Chapter 5: Serial Blocks in Blockzie software 57
Serial Monitor of Blockzie 59
What You’ll Need to Use the Serial Monitor 59
Serial Monitor 60
Functions of the Serial Monitor 64
Chapter 6: LED Blinking 67
Introduction 68
Hardware Required 68
Software Required 68
Circuit Diagram 69
Code 69
Output 69
Chapter 7: Blinking Two LEDs 70
Introduction 71
Hardware Required 71
Software Required 71
Circuit Diagram 72
Code 73
Output 73
Chapter 8: Pushbutton with LED 74
Introduction 75
Hardware Required 75
Software Required 75
Circuit Diagram 76
Code 77
Output 77
Chapter 9: Push Button with Multiple LEDs 78
Introduction 79
Hardware Required 79
Software Required 79
Circuit Diagram 80
Code 81
Output 81
Chapter 10: RGB LED 82
3
Introduction 83
What is an RGB LED? 83
Types of RGB LED 84
Working of the RGB LED 85
How To Create Different Colors? 85
PULSE-WIDTH MODULATION(PWM) 86
How to Generate Colors using RGB LED? 87
Specifications 88
Applications 88
Hardware Required 89
Software Required 89
Circuit Diagram 90
Code 91
Output 92
Chapter 11: 7 Segment Display with Arduino Uno 93
Introduction 94
Pin Diagram of 7 Segment Display 95
Types of Seven segments 95
How to know the type of the seven segments? 96
How to know the pin? 96
Working of 7 Segment Display 97
Applications 97
Hardware Required 98
Software Required 98
Circuit Diagram 99
Code 100
Output 103
Chapter 12: Laser Diode Module with Arduino Uno 104
Introduction 105
What is a laser diode module? 105
Construction of the laser diode 106
Working of the laser diode 107
The KY-008 laser diode module 108
Pin Diagram of the Laser Module 108
Applications 109
Project 1: Interfacing the laser diode module with an Arduino Uno 110
Hardware Required 110
Software Required 110
Circuit Diagram 111
Code 111
Output 112
Project 2: Laser security alarm with the LDR and Arduino Uno 112
4
Hardware Required 112
Software Required 112
Circuit Diagram 113
Code 114
Output 114
Chapter 13: Smoke detection alarm using MQ-2 gas sensor and Arduino Uno 116
Introduction 117
How Does a Gas Sensor Work? 117
What is an MQ2 Gas Sensor? 118
Pin diagram of MQ2 gas sensor 120
Construction of the Gas Sensor 120
Working of MQ2 gas sensor 121
How to Adjust the Sensitivity of Gas Sensor 122
Technical Specifications 122
Applications 122
Project: MQ2 gas sensor with Arduino Uno 123
Hardware Required 123
Software Required 123
Circuit Diagram 124
Code 125
Output 125
Chapter 14: Breath Analyzer using Arduino Uno 126
Introduction 127
What is an MQ3 Alcohol Sensor? 128
Pin diagram of MQ3 Alcohol sensor 129
Working of MQ3 Alcohol sensor 130
Technical Specifications 130
Applications 130
Project: Breath Analyzer using Arduino Uno 131
Hardware Required 131
Software Required 131
Circuit Diagram for the Analog Output 132
Code 133
Circuit Diagram for the Digital Output 134
Code 135
Output 135
Chapter 15: MQ-4 gas sensor with Arduino Uno 136
What is the MQ4 methane gas sensor? 137
MQ4 gas sensor pinout 137
Working principle of the MQ4 gas sensor 139
Applications of the MQ4 gas sensor 139
Project: MQ4 gas sensor interfacing with Arduino Uno 140
5
Hardware required 140
Software required 140
Circuit diagram of the MQ4 gas sensor with LEDs and Arduino Uno 141
Code 142
Output 142
Chapter 16: LPG Gas Detection System 143
What is the MQ5 Gas sensor? 144
Schematic Diagram Of The MQ5 Sensor 145
Parts Of The MQ-5 Sensor 145
Working Principle Of MQ5 Sensor 145
Technical Specifications Of MQ – 5 Sensor 145
Hardware required 146
Software required 146
Circuit Diagram 147
Code 148
Output 148
6
Chapter 1: Arduino Uno
7
In this tutorial, we get the information of Arduino Uno
For example, an Arduino that is connected to four motors and a motor driver (a small circuit to control
the motors) can be used to drive these motors at given intervals. You must instruct the Arduino to
control the motors using a programming language, giving the Arduino steps to complete in code form.
However, you can use other languages to program Arduino, usually using a third-party programming
tool like Snap4Arduino, ArduBlock and others. One such program is Blockzie, which allows one to
use a visual programming language with Arduino.
INTRODUCTION TO ARDUINO
Arduino is an open-source platform used to create electronics projects. The Arduino consists of a
physical programmable circuit board (often referred to as a microcontroller) and a piece of software, or
IDE (integrated development environment), that runs on your computer, which is used to write and
upload computer code to a physical board.
The Arduino platform has just become quite popular among people with electronics and for good
reasons. Unlike most previous programmable circuit boards, the Arduino doesn't require a separate
piece of hardware (called a programmer) to load new code onto the board — you can simply use a
USB cable. Additionally, the Arduino IDE uses a simplified version of C++, making it easier to learn
the program. Finally, Arduino offers a standard form factor that divides the functions of the
micro-controller into more accessible packages.
8
TYPES OF ARDUINO
The characteristics of different types of Arduino boards are listed in tabular form.
9
Different Types of microcontroller Boards
This board includes digital I/O pin-14, power jack, analog i/ps-6, ceramic resonator-A16 MHz, USB
connection, RST button and ICSP header. All these can support microcontrollers for further operation
by connecting this board to the computer. The power supply of this board can be done with the help of
AC to DC adapter, USB cable, or battery.
The Arduino platform has recently become very popular among electronics enthusiasts, and for good
reason. Unlike most previous programmable circuit boards, Arduino doesn't require a separate piece of
hardware (called a programmer) to load new code onto the board — you can just use a USB cable.
Additionally, the Arduino IDE uses a simplified version of C++, which makes it easier to learn to
program. Finally, Arduino provides a standard form factor that breaks down the functions of a
micro-controller into more accessible packages.
10
TECHNICAL SPECIFICATIONS OF ARDUINO UNO
⮚ It also has an Arduino integrated WiFi module. The Arduino UNO board is based on the
integrated WiFi ESP8266 module and the ATmega328P microcontroller.
⮚ Arduino UNO automatically pulls power from the external power supply. It can also draw
power from USB.
11
ARDUINO UNO BOARD DESCRIPTION
12
ATmega328 microcontroller:-
Each Arduino board has its microcontroller. You can assume it is the brain of your board. The
Arduino’s main IC (integrated circuit) is slightly different from board to board. The microcontrollers
are usually of the ATMEL Company. You must know your board’s IC before loading up a new
program from the Arduino IDE. This information is available on the top of the IC.
USB:-
An Arduino board can be powered by using the USB cable from your computer. All you need to do is
connect the USB cable to the USB connection.
Power Button:-
Arduino boards can be powered directly from the AC mains power supply by connecting it to the
Barrel Jack.
Voltage Regulator:-
The function of the voltage regulator is to control the voltage given to the Arduino board and stabilize
the DC voltages used by the processor and other elements.
Crystal Oscillator:-
The crystal oscillator helps Arduino in dealing with time issues. How does Arduino calculate time?
The answer is, by using the crystal oscillator. The number printed on top of the Arduino crystal is
16.000H9H. It tells us that the frequency is 16,000,000 Hertz or 16 MHz.
Arduino Reset:-
You can reset your Arduino board, i.e., start your program from the beginning. You can reset the UNO
board in two ways. First, by using the reset button on the board. Second, you can connect an external
reset button to the Arduino pin labeled RESET.
Analog pins:-
The Arduino UNO board has six analog input pins A0 through A5. These pins can read the signal from
an analog sensor like the humidity sensor or temperature sensor and convert it into a digital value that
can be read by the microprocessor.
13
ICSP pin:-
Mostly, ICSP is an AVR, a tiny programming header for the Arduino consisting of MOSI, MISO,
SCK, RESET, VCC, and GND. It is often referred to as an SPI (Serial Peripheral Interface), which
could be considered an "expansion" of the output. You are slaving the output device to the master of
the SPI bus.
TX and RX LEDs:-
On your board, you will find two labels: TX (transmit) and RX (receive). They appear in two places on
the Arduino UNO board. First, at the digital pins 0 and 1, indicate the pins responsible for serial
communication. Second, the TX and RX led. The TX led flashes with different speeds while sending
the serial data. The speed of flashing depends on the baud rate used by the board. RX flashes during
the receiving process.
Digital I/O:-
The Arduino UNO board has 14 digital I/O pins (of which 6 provide PWM (Pulse Width Modulation)
output. These pins can be configured to work as input digital pins to read logic values (0 or 1) or as
digital output pins to drive different modules like LEDs, relays, etc. The pins labeled “~” can be used
to generate PWM.
AREF:-
AREF stands for Analog Reference. It is sometimes used to set an external reference voltage (between
0 and 5 Volts) as the upper limit for the analog input pins.
14
ARDUINO UNO PINOUT
15
Chapter 2: Blockzie Software
16
In this tutorial, we will see how to interface the Arduino Uno hardware with Blockzie Software. Before
moving to this part we will see what Blockzie software is, advantages and application of it.
As an example, an Arduino that is hooked up to, say, four motors and a motor driver (a small circuit
for controlling motors) can be used to run these motors at given intervals. You must instruct the
Arduino to control the motors using a programming language, giving the Arduino steps to complete in
the form of code.
Arduino is designed to be programmed in its own Arduino language, though this language is composed
of functions pulled from C/C++.
You can, however, use other languages to program an Arduino, usually by utilizing a third-party
programming tool such as Snap4Arduino, ArduBlock, and others. One such program is Blockzie,
which allows one to use the visual programming language with an Arduino.
In this tutorial, we will go over the basics of how to use Blockzie software to program the Arduino
Uno.
● Blockzie is a Block programming software that makes it easy to create interactive stories,
animations, games, music, and art by the help of Block coding .
● Simple mouse movements allow kids to create images, insert sounds, and deliver interesting
projects. with simple commands, users can create and animate characters such that they move,
dance, and interact.
1. Easy to learn : Drag and Drop approach helps any non technical student easily learn block
coding
2. Easy to teach
3. Encourages experimentation
4. Reduces syntax errors
5. Easy debugging
APPLICATIONS OF BLOCKZIE
● Application development
● Drone development
● Space Technology
● Game development
● Interactive Education
● Creative animation
● Science experiment
● Robotics Development
● Basic electronics
17
INTERFACING ARDUINO UNO TO BLOCKZIE SOFTWARE
To run the scripts written in Blockzie, you must first upload it to Uno. Follow the procedure given
below to interface Uno with Blockzie.
HARDWARE REQUIRED
SOFTWARE REQUIRED
18
STEPS TO CONNECT ARDUINO UNO TO BLOCKZIE SOFTWARE
Get all your equipment ready. Now connect the narrow end of the USB B cable to Arduino and
the other end of cable to the USB port of your PC. This USB B cable will help us to upload our
program from PC to Arduino board:
2. Once you plug in your Arduino board a LED will start blinking which
indicates your Arduino board is connected to PC.
19
3. Open Blockzie Software
When you click on the “No device selected” then the “Choose an Device” window will open.
Different types of boards like Arduino Uno, Arduino Nano, Arduino Mini, ESP32, etc. have
appeared. Select your device which you use.
Here, we are working on “Arduino Uno” so find and select “Arduino Uno”.
In doing so, the Arduino Uno blocks appear in the Block palette.
20
6. Click on “Connect”
After selection of Arduino Uno, Blockzie searches for nearby Arduino Uno.
21
7. Select the Port to which Arduino is connected e.g. COMXX or ttyXX. Once
you select the port, the icon beside the Connect tab will become connected.
22
Chapter 3: Arduino Programming in Upload
23
In this tutorial, you’ll see what Blockzie's Upload mode is and how to use it to control our hardware
wirelessly, i.e. without being connected to the computer.
Upload mode is one of the two modes you can write your programs in Blockzie. This mode allows you
to write scripts and upload it to the board so that you can use it even when it is not connected to your
computer, for example, you need to upload a script for making moving robots.
To run the scripts written in Blockzie, you must first upload it to Uno. Follow the procedure given
below to interface Uno with Blockzie.
Get all your equipment ready. Now connect the narrow end of the USB B cable to Arduino and
the other end of cable to the USB port of your PC. This USB B cable will help us to upload our
program from PC to Arduino board:
24
2. Once you plug in your Arduino board a LED will start blinking which indicates your
Arduino board is connected to PC.
25
5. Select “Arduino Uno”
When you click on the “No device selected” then the “Choose an Device” window will open.
Different types of boards like Arduino Uno, Arduino Nano, Arduino Mini, ESP32, etc. have
appeared. Select your device which you use.
Here, we are working on “Arduino Uno” so find and select “Arduino Uno”.
In doing so, the Arduino Uno blocks appear in the Block palette.
6. Click on “Connect”
After selection of Arduino Uno, Blockzie searches for nearby Arduino Uno.
26
7. Select the Port to which Arduino is connected e.g. COMXX or ttyXX. Once you select the
port, the icon beside the Connect tab will become connected.
27
10. On the top right corner, there is a toggle button named Mode. This button is used to switch
between the two working modes of Blockzie, namely the Realtime mode and the Upload mode.
11. Switch to Upload mode by toggling this button. You’ll observe some changes in the UI.
12. For starters, the stage is replaced by the editor window. The editor window shows the
equivalent C++ code of the script you create.
13. The other difference that you’ll notice is in the block palette. The number of palettes is also
reduced. Those palettes whose blocks are used to interact with the stage and sprite such as
motion, sound, looks disappear.
28
ACTIVITY 1: LED BLINKING
In this tutorial, we are learning about how to blink on board LED in the upload mode.
HARDWARE REQUIRED
SOFTWARE REQUIRED
CODE
1. To control the LED connected to pin 13, we’re going to select mode of pin by using set pin ()
mode output block.
3. We need two such blocks to make the LED blink. So, duplicate it by right-clicking on it.
29
5. Now, to control the speed of blinking, we’re going to use the wait block. Go to
the Control palette and drag and drop one below each set digital pin () output block.
7. Observe that this script will run only once; in other words, the LED will turn ON, wait for a
second, then turn OFF.
8. To make the process continuous, we’ll use the forever block from the Control palette. It will run
the script till the time you stop the script.
30
9. To start any script, a Event block is necessary. Therefore, drag and drop When Arduino Uno
begin block above the script.
Now, we need to upload it to the board. For that, switch to Upload mode by toggling this button. The
Stage is replaced by an editor window with the equivalent C++ program for the script. To upload the
code, click on the Upload button.
31
ACTIVITY 2: CONTROL THE LED BRIGHTNESS USING THE POTENTIOMETER
In this activity, we will see how to control the brightness of the LED using the potentiometer.
The potentiometer is a device that is used to measure the voltage or electric potential. It provides a
variable resistance when the shaft of the device is turned.
Here, we will measure the amount of resistance as an analog value produced by the potentiometer. We
will connect the potentiometer to the Arduino UNO board
and will measure the state of the potentiometer. The required code will be uploaded from our computer
to the Arduino board.
The variable resistance measured by the potentiometer can be easily read as an analog value into the
Arduino board.
WHAT IS A POTENTIOMETER?
The potentiometer is a three-terminal device. It has a rotating contact that acts as an adjustable voltage
divider.
The potentiometer structure consists of a sliding contact (called wiper), a resistive element, electrical
terminals, and a housing.
The sliding contact moves along the resistive element, while the housing consists of the wiper and the
element.
Working: The fixed input voltage is applied across the two ends of a potentiometer, which further
produces the adjustable output voltage at the wiper or slider.
As the slider moves from one end to another, the divider can vary the output voltage from maximum to
Ground.
The middle terminal of the potentiometer is connected to the analog pin to read the analog data.
HARDWARE REQUIRED
32
SOFTWARE REQUIRED
33
CIRCUIT DIAGRAM
CODE
4. In this project, we are controlling LED output by Potentiometer, so connect LED at PWM pin
11 Drag and Drop Set digital pin block and set its mode as output.
5. We taking Input from Potentiometer so Drag and Drop set pin A2 to Input to read Analog
values
6. Select Set PWM pin block and insert read Analog pin to sense Analog data.
34
7. To make the process continuous, we’ll use the forever block from the Control palette. It will
run the script till the time you stop the script.
Now, we need to upload it to the board. For that, switch to Upload mode by
toggling this button. The Stage is replaced by an editor window with the equivalent C++
program for the script. To upload the code, click on the Upload Code button.
Once the upload is complete, we can see Brightness control of LED by using potentiometer.
35
ACTIVITY 3: INTERFACING HUMIDITY AND TEMPERATURE SENSOR WITH ARDUINO UNO
In this activity we will see how to interface the DHT11 sensor with the Arduino Uno.
Humidity is the measure of water vapour present in the air. The level of humidity in air affects various
physical, chemical and biological processes. In industrial applications, humidity can affect the
business cost of the products, health and safety of the employees. So, in semiconductor industries and
control system industries measurement of humidity is very important. Humidity measurement
determines the amount of moisture present in the gas that can be a mixture of water vapour, nitrogen,
argon or pure gas etc… Humidity sensors are of two types based on their measurement units. They
are a relative humidity sensor and Absolute humidity sensor. DHT11 is a digital temperature and
humidity sensor.
The DHT11 detects water vapour by measuring the electrical resistance between two electrodes. The
humidity sensing component is a moisture-holding substrate with electrodes applied to the surface.
When water vapour is absorbed by the substrate, ions are released from the substrate which increases
the conductivity between the electrodes. The change in resistance between the two electrodes is
proportional to the relative humidity. Higher relative humidity decreases the resistance between the
electrodes, while lower relative humidity increases the resistance between the electrodes.
HARDWARE REQUIRED
36
SOFTWARE REQUIRED
CIRCUIT DIAGRAM
CODE
37
4. To start any script, a Event block is necessary. Therefore, drag and drop When Arduino begin
block above the script.
5. Take Initialisation DHT block Block and Select pin 2 to connect to Arduino
6. To observe output of the sensor on the serial monitor , Drag and Drop Serial begin to initialize
serial transmission.
7. In this code, we have to serial print temperature and humidity data continuously on the serial
monitor with a speed of 1 sec.
38
39
Chapter 4: Arduino Extensions in Blockzie software
40
In this tutorial, we are learning about what extensions are in the Blockzie software, and how to add
extensions in the Blockzie software. We are also seeing how to add extensions in Blockzie software.
WHAT IS EXTENSION?
● An extension is a category of blocks that can be added to the Block Palette and extends the
scope of the Blockzie software.
● Extensions make it possible to connect Blockzie projects sources of information on the web
(such as Google Translate and Amazon Text to Speech), or blocks allowing for more advanced
functionality.
● They add a collection of command and reporter blocks that can be used to interact with a
particular device or data source.
● You can add extensions of Arduino in only and only Upload mode.
● When an extension is enabled, its blocks appear in a location with the same name as the
extension.
● For Arduino programming, 28 different types of extensions are given.
In Blockzie software for programming the Arduino with sensors and devices 28 different extensions
are given. Those are following:
41
42
PROJECT: DISTANCE DETECTION USING ULTRASONIC SENSOR
In this project we will show how to find distance using ultrasonic sensor.
An ultrasonic sensor is a device that uses sound waves to detect objects and measure distances. It
works by emitting high-frequency sound waves that are inaudible to humans. These sound waves
bounce off objects in their path and then bounce back to the sensor. By measuring the time it takes for
the sound waves to travel back, the sensor can calculate the distance to the object. This technology is
often used in a variety of applications, such as parking sensors, object detection, and even in some
robots to help them navigate their surroundings.
43
HARDWARE REQUIRED
SOFTWARE REQUIRED
CIRCUIT DIAGRAM
Get all your equipment ready. Now connect the narrow end of the USB B cable to Arduino and
the other end of cable to the USB port of your PC. This USB B cable will help us to upload our
program from PC to Arduino board:
44
2. Once you plug in your Arduino board a LED will start blinking which
indicates your Arduino board is connected to PC.
45
4. Click on “ No device selected” from the toolbar.
When you click on the “No device selected” then the “Choose an Device” window will open.
Different types of boards like Arduino Uno, Arduino Nano, Arduino Mini, ESP32, etc. have
appeared. Select your device which you use.
Here, we are working on “Arduino Uno” so find and select “Arduino Uno”.
In doing so, the Arduino Uno blocks appear in the Block palette.
46
6. Click on “Connect”
After selection of Arduino Uno, Blockzie searches for nearby Arduino Uno.
7. Select the Port to which Arduino is connected e.g. COMXX or ttyXX. Once
you select the port, the icon beside the Connect tab will become connected.
47
8. Click on “Go to Editor”.
48
10. Click on Realtime to Upload
For programming of Arduino Uno, Convert Realtime to Upload. Even the extensions are only
on
displayed on Upload mode.
49
13. Now the blocks of the Ultrasonic Sensor are displayed on Blocks Pattle.
50
14. Code
15. Upload
16. Output
When you place an object in front of the ultrasonic sensor, the distance between the object and
the ultrasonic sensor is displayed on the serial monitor.
51
Chapter 5: Block of Pins in Blockzie software
52
In this tutorial, we will see different blocks of pins available for programming of Arduino via Blockzie
software. We also see examples of these blocks.
pin: Digital 0 to 13
Analog A0 to A5
mode: input, output,
input_pullup
digitalWrite(pin, value)
This block is used to set the
value of a pin as HIGH or
LOW.
pin: 0 to 13
value: high or low
analogWrite(pin, value)
It writes a PWM value or
analog value to a pin. We can
light an LED with varying
brightness with the help of Control LED brightness
this block.. It is also used to
drive a motor at varying
speeds.
53
digitalRead(pin)
This function reads a digital
value from a pin. pin is the
number of the digital I/O pin
you want to read. This LED with Push Button
function returns one of two
values: HIGH or LOW.
analogRead(pin)
This block reads the value
from the specified analog pin
present on the particular
LED brightness control with
Arduino board.
potentiometer
The ADC (Analog to Digital
Converter) on the Arduino
board is a multichannel
converter.
54
This block is used to rotate the
servo motor from 0 to 180
degrees.
pin: 3, 5, 6, 9, 10, 11
Types of interrupt:
55
FALLING: Interrupt will be
triggered when the signal goes
from HIGH to LOW
pin: 2, 3
The "detachInterrupt()"
function is used in Arduino
programming to disable
interrupts on a specific pin.
By detaching an interrupt, you
prevent the Arduino from
responding to any external
events or signals on that pin.
56
Chapter 5: Serial Blocks in Blockzie software
57
In this tutorial, we are learning about what a serial monitor is and how to use the serial monitor in
Arduino programming.
While programming the Arduino microcontroller when there is a need for debugging the code to
remove the errors in the program, this is when the serial monitor comes in handy. Similarly, the serial
monitor is also used to interact with Arduino boards using the computer keyboard. So, this way, the
serial monitor of Blockzie is of great importance for programmers as it helps them debug and run the
Arduino script. In this guide, we have explained how we can effectively use the serial monitor.
The serial monitor of Blockzie software is an extremely useful tool for troubleshooting and
fault-finding in your code. If your code is giving you erratic or unexpected results then a good place to
start is by displaying the key variables used in your code through the serial monitor tool to see where
the problem arises.
Arduino serial monitor for beginners in electronics. Send and receive data between the serial monitor
window on a computer and an Arduino. The serial monitor is a utility that is part of the Arduino IDE.
Send text from an Arduino board to the serial monitor window on a computer. In addition, send text
from the serial monitor window to an Arduino board. Communication between the serial monitor and
Arduino board takes place over the USB connection between the computer and Arduino.
Arduino can communicate with other Arduino, computers, or other devices like Bluetooth, GPS, GSM,
and many more. These devices communicate with Arduino using a special communication protocol
called serial communication. In this post, you will learn how to start serial communication between
Arduino and the computer and print Arduino messages on the computer screen. In serial
communication, we use two transmission lines. One for transmitting data (Tx) and another for
receiving data (Rx). Every Arduino board has at least one serial communication port.
58
SERIAL MONITOR OF BLOCKZIE
From the name, we can say that this is some kind of a display monitor that displays the data of inputs
and outputs of the Arduino code using serial communication with the Arduino board.
The serial monitor is usually just used to display data from the Arduino on a computer monitor. But it
can also be used as an input device that takes input from a user and sends it to the Arduino. This is
useful for creating serial monitor-based menus, calculators, and password logins, where the user is
asked to input information and the Arduino performs an action based on that input.
Arduino Serial Monitor is a terminal window to print the data using print commands.
You can open the Arduino Serial Monitor easily in the following way.
You can open the monitor only when an Arduino is connected to the PC via a USB cable.
Arduino can communicate with a computer using its USB cable. The serial monitor is available in the
Blockzie. After opening the serial monitor you have to just choose the baud rate at which Arduino is
programmed to send or receive data. You can program the baud rate of serial communication on
To use other serial ports available on some boards, we will need a USB-to-serial adapter. Connect the
TX pin of the adapter to the RX pin of the serial port and the RX pin of the adapter to the TX pin of
the serial port. Connect them with the same ground.
59
SERIAL MONITOR
1. To stop the auto scroll of the incoming data on the serial monitor you have to un-click the icon
that has two downward arrows that we have highlighted in the above image. By turning off the
auto scroll option, you can scroll the data by yourself.
2. Coming to the next option is about when you have to clear the serial monitor you can simply
click on this icon, and it will clear the serial monitor for you.
3. To send data to Arduino from the serial monitor you have to type that information in the gray
bar that is highlighted in red. Once you have written the command or information, you can
simultaneously send it by pressing the Ctrl + Enter key.
4. To select Baud rate, end of line, Hex form and Auto scroll.
60
i) Baudrate:
To set the baud rate of the serial monitor you can select it from the option highlighted in red
next to the data organizing option.
To arrange or organize the data sent to the Arduino, we can click on the option next to the gray
bar, which has four other options.
61
Next to display the desired data you have to use the function Serial.print() by giving it that variable
name as its Argument. Below we have given the code to illustrate how to use the Serial.begin()
function for communicating with Arduino and Serial.print() function for displaying the data on the
serial monitor.
62
63
FUNCTIONS OF THE SERIAL MONITOR
1)
baud_rate: The baud rate that will be used for serial communication. Can be 4800, 9600,
14400, 19200, etc.
This function is used to define the baud rate that will be used for serial communication. For
communicating with specific devices, the device baud rate needs to be used.
Example:
2)
Prints data to the serial port as human-readable ASCII text. This command can take many
forms. Numbers are printed using an ASCII character for each digit. Floats are similarly printed
as ASCII digits, defaulting to two decimal places. Bytes are sent as a single character.
Characters and strings are sent as is. For example-
64
3)
This function is used to get the number of bytes available for reading from the serial port. It
gives the number of bytes of data that have arrived and are stored in the serial receive buffer.
Example:
We will see an example code for a better understanding. We will first initialize the serial
communication at a 9600 baud rate and then check for serial availability. If there is any data
present in the serial buffer then the LED on pin 13 will glow.
Code:
65
4)
This function takes the data which is input to the Arduino board. Data is stored in int data type.
It returns the first data byte of the data which is received as serial data. It also returns the value
-1 when no data is available.
Example:
66
Chapter 6: LED Blinking
67
INTRODUCTION
An LED, or light-emitting diode, is a component that converts electrical energy into light energy. LEDs
are polarized components, which means they only allow electricity to flow through them in one
direction. The longer leg on the LED is called an anode, it will connect to power. The shorter leg is a
cathode and will connect to the ground. When voltage is applied to the anode of the LED, and the
cathode is connected to the ground, the LED emits light.
As a result, we show that the LED is continuously ON and OFF for one second.
HARDWARE REQUIRED
SOFTWARE REQUIRED
68
CIRCUIT DIAGRAM
Here, the negative terminal of the LED is connected to the ground of the board and the Positive
terminal of the LED is connected with a 220-ohm resistor and pin 13 of the board.
CODE
OUTPUT
By uploading a code, you will observe that the LED will be turned on for one second and it will be
turned off for one second continuously.
If you want to blink the LED for more time, you have to change the value of delay in a code.
69
Chapter 7: Blinking Two LEDs
70
INTRODUCTION
In this project, we blink two LEDs simultaneously. As a result, we show that when the 1st LED is
turned on at that time the 2nd LED is turned OFF and when the 2nd LED is turned ON at the time 1st
LED is turned OFF.
HARDWARE REQUIRED
SOFTWARE REQUIRED
71
CIRCUIT DIAGRAM
Here, the positive terminal of the Red LED is connected with 13 no. pin of board and resistor, the
negative terminal is connected with GND. The positive terminal of the Green LED is connected with 7
no. pin of the board, the negative terminal is connected with GND.
Here, the red wire is connected to pin13, and the blue wire is connected to the GND.
Similarly, the green wire is connected to pin 7, and the orange wire is connected to the GND.
72
CODE
OUTPUT
When you press the space key of the keyboard , you will observe that when the red LED will be turned
on; the green LED will be turned off and when the green LED will be turned on at that time the red
LED will be turned off. This process is repeated continuously.
73
Chapter 8: Pushbutton with LED
74
INTRODUCTION
Pushbutton is also known as Switch. A switch interrupts the flow of electricity, breaking the circuit
when open. When a switch is closed, it will complete a circuit. There are many types of switches. The
ones in your kit are called momentary switches, or push buttons because they are only closed when
pressure is applied.
In this project, Pushbutton is worked as input and LED is worked as output. When we pressed the
pushbutton, the LED turned ON. We also show the status of a button on the serial monitor.
HARDWARE REQUIRED
SOFTWARE REQUIRED
75
CIRCUIT DIAGRAM
Here, the LED is connected with pin no. 13 and the Pushbutton is connected with pin no. 2 of the
Arduino Uno board. The negative terminal of both is connected with the GND of the Arduino board.
76
CODE
OUTPUT
By uploading a code, you will observe that when you press the pushbutton at that time LED will be
turned on.
Here, the pushbutton worked as an input device, meaning when you press the pushbutton at that time
High signal is generated and the Arduino board receives this signal, that time the Arduino board sends
high signal to LED then LED will be turned on. Here, the LED worked as an output device and the
pushbutton worked as an input device.
77
Chapter 9: Push Button with Multiple LEDs
78
INTRODUCTION
In this tutorial, we control multiple LEDs with one pushbutton. When we press the pushbutton, the
LEDs will be turned ON. When we release the pushbutton LEDs will be turned OFF.
HARDWARE REQUIRED
SOFTWARE REQUIRED
79
CIRCUIT DIAGRAM
Here, Red, Green and Blue LEDs are connected with pin no. 3,5 and 8 respectively of Arduino Uno
board. The pushbutton is connected with pin no. 2 of the Arduino Uno board.
80
CODE
OUTPUT
By uploading a code, you will get that when you press the pushbutton at that time LEDs will be turned
on.
Here, the LEDs worked as an output device and the pushbutton worked as an input device.
81
Chapter 10: RGB LED
82
INTRODUCTION
In this tutorial, you will learn about Arduino RGB-led interfacing. The RGB led consists of three
different led’s, from the name you can guess that these led’s are red, green and blue. We can obtain
many other colors by mixing up these colors. The Arduino has an analog write function which will
help us in obtaining different colors for Arduino RGB led.
The human eye has only three cones (most people). These cones are detectors for red, green, and blue
color. However, LEDs and LCDs are only capable of displaying those colors individually. These LEDs
can be mixed to show different intensities of these three colors. Because of the small size of each LED
your eyes are tricked into seeing different colors that aren’t actually there!
With the Arduino and a technique called PWM, we can pulse each color of the LED separately to
create many different colors. The PWM of the Arduino is programmable from 0-255. 0 would be off
and 255 would be 100% brightness. That’s 256 possible variations. This gives us a total number of
16,777,216 possible colors to play with.
The RGB LED is a type of LED that can produce three main colors that are RED, GREEN, and BLUE.
In other words, it’s a single LED that contains three LEDs inside it. One for the RED color, one for the
GREEN color, and one for the BLUE color. The RGB LED that we are going to discuss in this article
is non-programmable and it is of two types. One is a common anode RGB LED and the other is a
common cathode RGB LED.
A common anode RGB LED consists of four terminals out of which one is for the common anode, one
is for the RED LED cathode terminal, one is for the GREEN LED cathode terminal, and the last one is
for the BLUE LED cathode terminal. We call it a common anode RGB LED because in this type of
RGB LED, the anode terminal of all three LEDs is shorted internally and connected to one terminal
and that terminal is known as a common anode terminal.
On the other hand, a common cathode RGB LED also consists of four terminals but in this type of
RGB LED one terminal is for the common cathode, one is for the RED LED anode terminal, one is for
the GREEN LED anode terminal and the last one is for the BLUE LED anode terminal. We call it a
common cathode RGB LED because in this type of LED, the anode terminal of all three LEDs is
shorted internally and connected to one terminal and that terminal is known as a common anode
terminal.
83
TYPES OF RGB LED
There are two type of RGB LEDs you will encounter and they are wired very differently
● Common Anode (+) – This type of LED has four pins. A cathode (-) for each color and a
shared anode (+). The LED shares a common 3.3V to 5V input voltage. Grounding each anode
will light the different colors.
● Common Cathode (-) – This type of LED has four pins. An anode (+) for each color and
shared cathode (-). The LED shares a common ground, applying 3.3V to 5V to each Anode will
light the different colors. This is the most common RGB LED.
84
WORKING OF THE RGB LED
To light up a common anode RGB LED, you have to connect its common terminal to the positive
terminal of the power source. Then to light up the RED color, connect the RED color terminal to the
negative terminal of the power source. Do the same for the other colors. You can also light up two or
all three colors simultaneously, then you will get different color combinations.
To light up a common cathode RGB LED, you have to connect its common terminal to the negative
terminal of the power source. Then to light up the RED color, connect the RED color terminal to the
positive terminal of the power source. It will also work in the same manner as that of the common
anode RGB LED.
By directly applying a voltage to both the types of LED you can get the RED color, BLUE color,
GREEN color, a combination of RED and BLUE color, a combination of RED and GREEN color, a
combination of BLUE and GREEN color, and a combination of all the three colors. So, you will get a
total of 7 colors.
The reason that you can mix any color you like by varying the quantities of red, green and blue light is
that your eye has three types of light receptors in it (red, green and blue). Your eye and brain process
the amounts of red, green and blue and convert it into a color of the spectrum.
In a way, by using the three LEDs we are playing a trick on the eye. This same idea is used in TVs,
where the LCD has red, green and blue color dots next to each other making up each pixel.
To have an idea of how to combine the colors, take a look at the following chart. This is the simplest
color mixing chart but gives you an idea of how it works and how to produce different colors.
You can create one of those three colors – red, green or blue – by activating just one LED.
For example, if you want to produce blue, you activate the blue LED and turn off the other two.
85
If we set the brightness of all three LEDs to be the same, then the overall color of the light will be
white.
If we turn off the blue LED, so that just the red and green LEDs are the same brightness, then the light
will appear yellow.
We can control the brightness of each of the red, green and blue parts of the LED separately, making it
possible to mix any color we like.
Black is not so much a color as an absence of light. So the closest we can come to black with our LED
is to turn off all three colors.
PULSE-WIDTH MODULATION(PWM)
The brightness of an LED is proportional to the current going through it, but it would be rather difficult
to use a microcontroller to accurately control the current flowing through an LED. Fortunately, human
vision has a nice phenomenon called persistence of vision. Persistence of vision is the phenomenon
where an image that is seen for only a fraction of a second will continue to be “seen” by your brain
even after the original image has vanished or moved. This this the same principle behind film and
television, where a rapidly changing image tricks your brain into seeing continuous motion. By turning
our LED on and off rapidly, we can trick the brain into seeing an “average” value of brightness based
on the duty cycle of the driving PWM signal.
Pulse-width modulation (PWM) is the practice of modulating the duty cycle of a signal, used in this
application to control the average power sent to each LED. In the following figure, we show three
different duty cycles, first with 50% duty cycle, then 10% and 90% duty cycle. During the 10% duty
cycle, the signal is at the logic high level for only a brief time each cycle, but with the 90% duty cycle,
most of the signal’s period is spent at logic high level. If the frequency of the signal is fast enough,
then there will be no visible flicker, and the LED’s brightness will be proportional to the signal’s duty
cycle.
86
HOW TO GENERATE COLORS USING RGB LED?
To generate a wide range of colors using an RGB LED just by varying the brightness of the individual
R, G and B LEDs.
You might already have performed an experiment with dimming an LED using Arduino, where the
brightness of LED is controlled either directly using the program or by interfacing a potentiometer.
The technique used to control the brightness of an LED is called Pulse Width Modulation or simply
PWM.
PWM is a technique where the amount of power delivered to a device can be controlled accurately and
efficiently. PWM technique can be used to control the brightness of an LED, the speed of a motor or
the direction of a servo motor.
There are two important factors to consider when dealing with PWM Technique: Duty Cycle and
Frequency.
Duty cycle indicates the duration for which the pulse is HIGH over its period. It is measured in
percentage and it indicates the voltage between OFF and ON levels (usually 0V and 5V).
Arduino UNO has 6 Pins that can be used to generate PWM Signals: 3, 5, 6, 9, 10 and 11. In order to
generate a PWM signal of a specific duty cycle, you need to use the analogWrite(pin, value); function.
Here, the value varies from 0 – 255 and corresponds to 0 – 100% Duty cycle.
87
Frequency of the pulse or the PWM Signal plays an important role as well and it must be sufficiently
high than what the device feels its effect.
SPECIFICATIONS
● Forward voltage: RED color – 1.8 to 2.2 V, GREEN color – 3.0 to 3.4 V, BLUE color – 3.0 to
3.4 V
● Forward current: RED color – 20mA, GREEN color – 20mA, BLUE color – 20mA
● Reverse current(at 5V): RED color – 10uA, GREEN color – 10uA, BLUE color – 10uA
● Luminous Intensity: RED color – 800 mcd, GREEN color – 4000 mcd, BLUE color – 900 mcd
● Operating temperature: -25 to 85 degree Celsius
APPLICATIONS
● Decoration Lights
● Hydroponics and Aeroponics
● Backlight
● Room Lights
● Traffic Lights
88
HARDWARE REQUIRED
SOFTWARE REQUIRED
89
CIRCUIT DIAGRAM
Connect the cathode of the RGB led which is the longer pin of RGB led to the GND of Arduino and
the other three pins to the pins 11, 10 and 9 of Arduino through the 220-ohm resistors. The resistors
will prevent the excess amount of current to flow through the RGB led.
If you are using the common anode RGB led, then connect the long lead to the 5V of Arduino.
90
CODE
91
OUTPUT
By uploading a code, you will get that the RGB LED produces different colors of light like red, green,
blue, raspberry, cyan, yellow, and white.
92
Chapter 11: 7 Segment Display with Arduino Uno
93
INTRODUCTION
Almost everywhere we can find a seven segment display; it is so common that we often do not notice
it at all. From pocket calculators, digital clocks and electronic test equipment to petrol pumps
everywhere information is delivered through a seven segment display.
An LED or Light Emitting Diode is a solid state optical PN-junction diode that emits light energy in
the form of photons.
Seven segment displays are the output display device that provides a way to display information in the
form of images or text or decimal numbers which is an alternative to the more complex dot matrix
displays. It is widely used in digital clocks, basic calculators, electronic metres, and other electronic
devices that display numerical information. It consists of seven segments of light-emitting diodes
(LEDs) which are assembled like numerical 8.
The 7-segment displays are just seven LEDs lined up in a particular pattern. In this case, the number
‘8’ shape we’re all familiar with. Each of the seven LEDs is called a segment because when
illuminated the segment forms part of a numerical digit (both Decimal and Hex) to be displayed. An
additional 8th LED is sometimes used for an indication of a decimal point.
Each one of the seven LEDs in the display is given a positional segment with one of its connection
pins being brought straight out of the rectangular plastic package. These individual LED pins are
labelled from a through to g representing each individual LED. The other LED pins are connected and
wired to form a common pin.
To turn on and off a particular part of the display, you set the appropriate pin HIGH or LOW just like
you would with a regular LED. So that some segments will be light and others will be dark allowing
the desired character pattern of the number to be generated on the display. This then allows us to
display each of the ten decimal digits 0 through to 9 on the same 7-segment display.
94
PIN DIAGRAM OF 7 SEGMENT DISPLAY
Seven segment displays are of two types: Common Cathode (CC) and Common Anode (CA). The
Internal structure of both types is nearly the same. The difference is the polarity of the LEDs and
common terminals. As their name suggests, the common cathode has all the cathodes of the LEDs in a
7-segment connected together and the common anode has all the anodes of the LEDs in a 7-segment
connected together.
In the common cathode display, all the cathode connections of the LED segments are connected
together to ‘logic 0’ / GND. The individual segments are then illuminated by applying a HIGH / ’logic
1’ signal to the individual Anode terminals (a-g).
95
In the common anode display, all the anode connections of the LED segments are joined together
to logic “1”. The individual segments are illuminated by applying a ground, logic “0” or “LOW”
signal to the Cathode of the particular segment (a-g).
You can know the type by testing it, when you connect one of the center pins from the seven segment
to GND and connect one of the other pins to VCC and it turned on, that means the seven segments is a
common cathode, otherwise, the seven segments are common anode.
You can test them by connecting the center to (GND to cathode / VCC to anode) and any other pin to
(VCC to cathode / GND to anode).
96
WORKING OF 7 SEGMENT DISPLAY
The number 8 is displayed when the power is given to all the segments and if you disconnect the
power for ‘g’, then it displays the number 0. In a seven-segment display, power (or voltage) at different
pins can be applied at the same time, so we can form combinations of display numerical from 0 to 9.
Since seven-segment displays can not form alphabets like X and Z, they can not be used for the
alphabet and they can be used only for displaying decimal numerical magnitudes. However,
seven-segment displays can form alphabets A, B, C, D, E, and F, so they can also be used for
representing each display unit is usually has a dot point (DP). The display point could be located
either towards the left or towards the right of the display pattern. This type of pattern can be used to
display numerals from 0 to 9 and letters from F hexadecimal digits.
APPLICATIONS
● The applications of 7 segments are mostly in digital calculators, electronic meters, digital
clocks, digital clocks, clock radios, etc.
● Today most of the applications are using LCDs, because of low current consumption.
● Used in applications where font size is required to be bigger.
● Microcontroller Independent, hence used in small circuit projects.
● Used in combination with four segments to display measurement/sensor value with four
characters.
● Has bright illumination, hence used where the display is required to work in low light or dark
conditions.
97
HARDWARE REQUIRED
SOFTWARE REQUIRED
98
CIRCUIT DIAGRAM
99
CODE
100
101
102
OUTPUT
By uploading a code, you will get that the one to nine numbers are continuously displayed on a seven
segment display with a delay of one second.
103
Chapter 12: Laser Diode Module with Arduino Uno
104
INTRODUCTION
In this tutorial, we will learn about what is the laser, what is the laser diode module, the working
principle and the applications of the laser diode module. We will also learn about how to interface the
laser diode with an Arduino Uno. We will also make a laser security alarm by using a laser diode
module and LDR.
The word LASER is an acronym for Light Amplification by Stimulated Emission of Radiation. A laser
is a device that amplifies or increases the intensity of light and produces highly directional light.
Lasers amplify or increase the intensity of light and generate the light. The laser emits light through a
process called stimulated emission of radiation which amplifies or increases the intensity of light.
Some lasers generate visible light but others generate ultraviolet or infrared rays which are invisible.
Natural white light is made up of all the VIBGYOR colors of the visible light spectrum, which is a
broad wide band of many different frequencies. The ordinary LEDs give a light output often consisting
of one color, but even that light contains electromagnetic waves, which cover a quite wide band of
frequencies. The lens system focusing the light has a fixed focal length, but the focal length required to
focus various wavelengths (colors) of light is different.
Therefore, each color will focus at different points, causing ‘chromatic aberration’. The laser diode
light contains only a single frequency. Therefore, it can be focused by even a simple lens system to an
extremely small point. There is no chromatic aberration since only one wavelength exists, also all of
the energy from the light source is concentrated into a very small spot of light.
Depending on the application, there are various ways to interface a laser with Arduino. A laser diode
finds applications in CD/DVD reading and recording, optical-fiber communications, laser printing and
scanning, laser pointers, directional lighting, barcode readers and so on.
The laser diode is quite a tiny electronic component that physically emits radiation. When it comes to
semiconductor laser diodes, the radiation (light) they emit is coherent, meaning that emitted photons
stick together and can form a laser beam.
Laser diodes can produce a narrow beam of laser light in which all the light waves have similar
wavelengths. Because of this property, laser beams are very bright and can be focused over a very tiny
spot.
Laser diodes are widely used in various devices like barcode readers, laser printers, security systems,
fiber optic communications, etc.
105
CONSTRUCTION OF THE LASER DIODE
The above figure shows a simplified construction of a laser diode, which is similar to a light-emitting
diode (LED). It uses gallium arsenide doped with elements such as selenium, aluminum, or silicon to
produce P-type and N-type semiconductor materials. While a laser diode has an additional active layer
of undoped (intrinsic) gallium arsenide, it has a thickness of only a few nanometers, sandwiched
between the P and N layers, effectively creating a PIN diode (P type-Intrinsic-N type). It is in this layer
that the laser light is produced.
106
WORKING OF THE LASER DIODE
The laser diode works on the principle that every atom in its excited state can emit photons if electrons
at a higher energy level are provided with an external source of energy.
There are basically three phenomena by which an atom can emit light energy and that are Absorption,
Spontaneous Emission & Stimulated emission.
Absorption
In absorption, the electrons at lower energy levels jump to higher energy levels i.e., from the valence
band to the conduction band when the electrons are provided with an external source of energy. Now,
there are holes at a lower energy level i.e., valence band and electrons at a higher energy level i.e.,
conduction band.
Spontaneous Emission
Now, if the electrons in higher energy levels are unstable then they will tend to move to the lower
energy level in order to achieve stability. But if they move from higher energy levels to lower energy
levels, they will definitely release the energy which will be the energy difference between these two
levels. The energy released will be in the form of light and thus photons will be emitted. This process
is called spontaneous emission.
Stimulated Emission
In stimulated emission, the photons strike electrons at a higher energy level and these photons are
supplied from an external light energy source. When these photons strike the electrons, electrons gain
energy and they recombine with holes and release an extra photon. Thus, one incident photon
stimulates another photon to release. Thus, this process is called stimulated emission.
107
THE KY-008 LASER DIODE MODULE
A red laser beam in a dot shape is produced by the Laser Transmitter Module KY-008 for Arduino. A
resistor and a red laser diode head with a wavelength of 650 nm, 5V make up the KY-008 Laser
transmitter module. Use caution when handling the laser and avoid staring straight at its head.
108
APPLICATIONS
Consumer electronics: This includes laser printers, CDs and DVD players, and fiber optic
communication.
Industrial applications: When it comes to industrial applications, the laser diode is preferred as it is a
source of a high-intensity laser beam and used for cutting, drilling, welding, etc.
Medical applications: Laser diodes are used for the elimination of unwanted tissues and tumors and
also in dental medication.
Scientific instrumentation: Instruments like spectrometers, range finders, and contactless
measurements can be done with the help of laser diodes.
The laser diode in telecom: Laser diodes with 1.3 μm and 1.55 μm bands are used as the main source
of light in telecom and as the band changes laser diodes find application in optical amplification.
Now, moving to the project. We will make two projects by using the laser diode module:
109
PROJECT 1: INTERFACING THE LASER DIODE MODULE WITH AN ARDUINO UNO
In this project we see how to interface the laser module with the Arduino Uno.
HARDWARE REQUIRED
SOFTWARE REQUIRED
110
CIRCUIT DIAGRAM
Here, the signal(S) pin of the laser diode module with an Arduino Uno. The ground (-) pin of the laser
diode module is connected to the GND pin of the Arduino Uno.
CODE
111
OUTPUT
By uploading a code, you will get the laser head continually turn the laser on and off in one-second
intervals.
You can also change the delay in the code if you want to blink the laser light at different time intervals.
PROJECT 2: LASER SECURITY ALARM WITH THE LDR AND ARDUINO UNO
In this project, we used the Laser Diode Module KY-008 to construct a Laser Light Security System
using Arduino with an Alarm. The objective of the project is to build a security system. The buzzer
alarm will begin to ring whenever any item blocks the LASER ray.
This project can be executed anywhere; in addition to buildings or other structures, it can also be used
to secure other valuable commodities like jewels, diamonds, and priceless antiques in museums, etc.
With the LASER beam security system, many people secure their homes, offices, stores, warehouses,
etc.
HARDWARE REQUIRED
SOFTWARE REQUIRED
112
CIRCUIT DIAGRAM
Here, the LDR is connected to the A0 pin of the Arduino Uno. The laser module is connected to pin 9
of the Arduino. The LED and buzzer are connected to the 13 and 12 pins of the Arduino board
respectively.
113
CODE
OUTPUT
114
The project essentially operates on the interruption principle. If the LASER light is cut off in any way,
the alarm will sound and the LED will be turned on and off at 0.1-sec intervals. The laser is a focused
light source that emits a single-color, straight beam of light.
Since the LDR is light-sensitive, when a laser beam strikes it, it produces a voltage. The voltage output
of the LDR varies as the laser beam is blocked from reaching it, and eventually, the alarm will sound.
115
Chapter 13: Smoke detection alarm using MQ-2 gas sensor and
Arduino Uno
116
INTRODUCTION
Nowadays, sensors play a major role in safety systems. In order to detect smoke and fire, smoke
sensors are used. They provide appropriate action on time for safety purposes. There are numerous
types of sensors available to measure environmental characteristics like light, noise, smoke, etc.
One such sensor used in safety systems to detect harmful gasses is the MQ2 Gas sensor. If you want to
make an indoor air quality monitoring system, breath checker, or early fire detection system, the MQ2
sensor is a suitable choice.
In this tutorial, we are going to discuss how to interface the MQ2 smoke detector sensor with Arduino
and its code. The MQ2 sensor is a powerful sensor in order to measure different types of gasses like
Butane, Methane, LPG, Smoke, Alcohol, Propane, Hydrogen concentrations in the air.
Smoke sensors are found pretty much at every place. This sensor is essentially a fire protection device.
Therefore, it has vast applications. Whether it’s the industrial sector or a commercial sector, this sensor
is utilized everywhere. In the homes and restaurants, they are used in the kitchens to give the signal if a
flame gets burst. Industries bought this sensor so that it sends the signal if the explosion happens.
The Grove Gas Sensor (MQ2) module is useful for gas leakage detection (home and industry). It is
suitable for detecting H2, LPG, CH4 and CO.
The sensor has a sensing element in order to sense the smoke or any gaseous material. In this sensor,
the sensing material is a stainless steel mesh. This mesh contains aluminum-oxide-based ceramic,
coated with tin dioxide.
117
There are mainly 6 leads in the sensing element. Two are used for heating of sensing elements, the
remaining four are used for the output signal.
Let's consider that we want to identify the Oxygen gas, the oxygen atoms get absorbed on the sensing
element when it will be heated at a higher temperature.
Tin oxide has donor electrons present in it, which are attracted towards oxygen. So by identifying the
donor electrons, we can detect the amount of oxygen that gets absorbed as well as we can oppose the
flow of current to give the signal.
When reducing gasses are present on the sensor, oxygen atoms try to interact with reducing gasses and
reduce the absorbed surface density of the oxygen atom. Hence current can flow through the sensor to
generate an analog output signal.
This output signal generates the voltage values by which we will get an idea about the concentration of
gasses. When the concentration of gas is high, voltage will be high.
The MQ2 module of the gas sensor is useful for gas leakage detection in the home as well as in the
industries. It observes the level of gasses present in the atmosphere. The gas sensor operates on 5V and
800mW.
Depending on the level of concentration, the sensor observes the potential difference and this changes
the resistance of the material inside the sensor. The output voltage value gives us the type of gas.
Normally these sensors are available as a comparator. These comparators are able to set up a threshold
value for gas concentration. The digital pin will go high only when the concentration of the gas is
above the threshold value. The sensor will work in analog mode to detect the concentration of the gas.
118
To use the MQ2 gas sensor module, you need to power the device with 5V supply and the Power LED
will start to glow. You should give it some preheating time before starting reading the output.
While measuring the gas present, the Output LED will glow in a specific concentration of the gas. You
can change it by using the potentiometer. Else you can also use the Analog Output to see how your
program reacts to different concentrations of gasses present.
The MQ-2 smoke sensor is sensitive to smoke and to the following flammable gasses:
● LPG
● Butane
● Propane
● Methane
● Alcohol
● Hydrogen
119
PIN DIAGRAM OF MQ2 GAS SENSOR
● VCC provides power to the sensor comparator board and needs to be connected to the 5V of
the Arduino
● GND is the ground pin and needs to be connected to the GND pin of the Arduino.
● DO is the digital output pin, which shows the digital representation of the detected gas.
● AO is the analog output pin from which we can detect the gas type by analyzing analog values.
Generally, all types of gas sensors including MQ2 gas sensors are constructed by metal oxide
semiconductors. These gas sensors have one sensing element which consists of the following elements.
● Tubular ceramic
● Electrodes
● Heater Coil
120
● Gas sensing layer
● Electrode line
This sensing element is responsible to process all the parameters and detect the type of gas.
The smoke sensor has a built-in potentiometer that allows you to adjust the sensor sensitivity
according to how accurately you want to detect gas.
The voltage that the sensor outputs changes accordingly to the smoke/gas level that exists in the
atmosphere. The sensor outputs a voltage that is proportional to the concentration of smoke/gas.
In other words, the relationship between voltage and gas concentration is the following:
● The greater the gas concentration, the greater the output voltage
● The lower the gas concentration, the lower the output voltage
Working Mechanism
The output can be an analogue signal (A0) that can be read with an analogue input of the Arduino or a
digital output (D0) that can be read with a digital input of the Arduino.
121
HOW TO ADJUST THE SENSITIVITY OF GAS SENSOR
In order to adjust the sensitivity of this sensor for the particular type of gas, you need to keep this
sensor in the particular gas type you want to detect. Now we have to rotate the potentiometer on the
sensor.
Rotate it till the Red LED on the sensor gets ON. If we have to increase the sensitivity, rotate it
clockwise, and if we have to decrease the sensitivity we have to rotate it anticlockwise.
When we are rotating this potentiometer basically we are adjusting the threshold value of the sensor.
The comparator on this board is constantly checking the threshold value.
Once this threshold gets crossed the digital pin goes high and the LED will turn on. In this way
calibrate your sensor, it may take some time but once you calibrate it, it will give you accurate
readings.
TECHNICAL SPECIFICATIONS
APPLICATIONS
122
PROJECT: MQ2 GAS SENSOR WITH ARDUINO UNO
In this project, we will see how to interface the MQ2 gas sensor with Arduino Uno.
HARDWARE REQUIRED
SOFTWARE REQUIRED
123
CIRCUIT DIAGRAM
Here, the GND of the MQ2 gas sensor and negative terminals of both the LEDs and buzzer are
connected to the GND of the Arduino Uno.
The positive terminals of both the LEDs, buzzer and AO pin of the gas sensor are connected to the 11,
12 10 and A5 pins of the Arduino Uno respectively.
The VCC pin of the gas sensor is connected to the 5V pin of the Arduino Uno.
124
CODE
OUTPUT
By uploading a code, you will get that when the IR sensor detects the value of smoke as more than 400
then the green light and buzzer both will be turned on otherwise only the red light will be turned on.
Here, the MQ-2 smoke detection sensor worked as an input device and the buzzer and the LED worked
as an output device.
125
Chapter 14: Breath Analyzer using Arduino Uno
126
INTRODUCTION
Have you ever heard about breathalyzer devices? They are the devices that estimate the alcohol taken
by a person. Mainly, helps to warn a drunk driver to avoid accidents. Thus, these breathalyzer devices
use an Alcohol sensor. Certainly, the MQ3 sensor.
The MQ-3 is an Alcohol Sensitive Sensor that is often used in a breathalyzer. It's a low-cost
semiconductor sensor that can detect alcohol. MQ-3 is suitable for detecting Alcohol, Benzine, CH4,
Hexane, LPG and CO.
MQ3 Sensor belongs to the MQ series that are used to detect different gasses present in the air. This
alcohol sensor detects the concentration of alcohol 25 to 500 ppm in the air. Along with high
sensitivity to detect alcohol, it can also detect benzene with a lower sensitivity. It is a metal oxide
semiconductor that detects the presence of alcohol vapors in the surroundings by changing resistance.
As the concentration of alcohol rises in the air, the conductivity of the sensor becomes higher. This
change in conductivity is converted to an output value that indicates the concentration of alcohol. This
sensor is very popular and widely used because of its high sensitivity and can be changed via a
potentiometer attached to the module and fast response time.
It works on 5V and takes around 800mW to operate. When this sensor comes in contact with alcohol
vapors, its resistance is changed as per the concentration of alcohol. This means that when alcohol
concentration is high then its resistance would be low and when alcohol concentration is low then its
resistance would be high.
Alcohol sensors are commonly available these days and a simple breath analyzer can be constructed
using an Arduino board. This alcohol sensor MQ-3 has digital as well as analog out pin. For a quick
implementation digital output can be used. Only a bunch of things are required for the interface..
The alcohol sensor we will use is the MQ-3 sensor. This is a sensor that is not only sensitive to alcohol,
particularly ethanol, which is the type of alcohol which is found in wine, beer, and liquor.
This type of sensor circuit can be used as a breathalyzer to check a person's blood alcohol level. Just as
we exhale carbon dioxide when we breathe out, we also will breathe out some alcohol if we have
alcohol in our blood. Any alcometer device can measure this alcohol content.
The more ethanol in your blood, the more there is in the air on exhalation. This alcohol content gives a
good indication for if a person is drunk and how drunk they are.
The amount of alcohol exhaled into the air is proportional to the amount of alcohol which will be
found in a person's blood. Alcometers use a built-in formula to estimate blood alcohol content from
exhaled air alcohol content.
127
For different countries, the level of alcohol in the blood that defines a person as over the limit for
driving varies. The range ranges from 0.01 to 0.10. Most countries have a limit of about 0.05. For
example, Greece, Greenalnd, and Iceland all have limits of 0.05. Canada has a higher limit set at 0.08.
In the United States, it is also 0.08. This means that if the alcometer reading measures above this, the
person can receive a DUI.
For our circuit, it can function as an alcometer so that we get an estimate of a person's blood alcohol
level.
It consists of a sensitive material SnO2 for the detection of alcohol. SnO2 is less conductive in clean
air as compared to alcohol gas.
When the sensor is targeted to alcohol gas the conductivity of SnO2 increases. The increase in
conductivity of the sensor is directly proportional to the concentration of alcohol. So the concentration
of alcohol can be easily measured using any microcontroller.
The MQ3 sensor has a very fast and high sensitivity. You can use this to make an Alcohol detector
using Arduino.
MQ3 alcohol sensor module features both analog outputs fetched from its AO pin and digital output
fetched from its DO pin. The analog output voltage lies between 0-5V where the output voltage
increases relatively with the concentration of alcohol vapors coming in contact with the sensor.
Whereas the digital output voltage (0/1) is obtained after passing the analog output from the LM393
comparator situated at the backside of the sensor module.
128
The in-built potentiometer is manually calibrated to change the sensitivity of the digital output and set
a threshold value. This is done with the help of the DOUT LED.
When the concentration of alcohol vapors will be greater than the threshold value set, the digital output
will be LOW. This will be easily monitored when the DOUT LED lights up. Additionally, rotating the
potentiometer clockwise results in a higher sensitivity.
The MQ3 alcohol sensor module’s front side consists of the sensor enclosed in an anti-exposing
network and a plastic case. This is to protect the sensor as it gets heated up over time and will
eventually come in contact with flammable alcohol vapors.
The backside of the module consists of two LEDs: DOUT LED (lights when DO is LOW) and the
power LED (lights when the module is powered on), an LM393 comparator, and a potentiometer.
● The MQ3 sensor module consists of four pins namely VCC, GND, AOUT, and DOUT. The
table below gives a brief description of them.
Pin Description
VCC Positive power supply pin that powers up the sensor module.
Analog output pin. It generates a signal proportional to the concentration of alcohol vapors
AOUT
coming in contact with the sensor.
Digital Output pin. It also produces a digital signal whose limit can be set using the in-built
DOUT
potentiometer.
129
WORKING OF MQ3 ALCOHOL SENSOR
The tabular sensing part of the sensor is made up of aluminum oxide which has a coating of tin
dioxide(SnO2). When this layer of SnO2 is heated at a high temperature, oxygen gets adsorbed at the
surface. So, when there is no alcohol, the electrons from the conduction band of SnO2 get attracted
towards the oxygen. Thus, this forms the depletion layer below the surface of the SnO2. Hence,
worked as a potential barrier that allows no current to flow.
When the layer senses the alcohol, oxygen is reacted with the alcohol, and the density of adsorbed
oxygen gets decreased. This reduces the potential barrier and electrons are allowed to release into
SnO2. As a result, the current starts to flow in a sensor.
TECHNICAL SPECIFICATIONS
APPLICATIONS
130
PROJECT: BREATH ANALYZER USING ARDUINO UNO
In this tutorial, we will learn about how to detect the presence of alcohol with Arduino. This system is
also used to detect whether the person is drunk or not.
HARDWARE REQUIRED
SOFTWARE REQUIRED
131
CIRCUIT DIAGRAM FOR THE ANALOG OUTPUT
132
CODE
133
CIRCUIT DIAGRAM FOR THE DIGITAL OUTPUT
134
CODE
OUTPUT
By uploading a code, you will get an alert of whether the person is drunk or not. If the sensor is
connected to analog output, if the sensor detects the value of the alcohol according to code then the
status is displayed on the serial monitor.
If the sensor is connected to a digital pin and the alcohol is detected then the status of this is displayed
on the serial monitor.
135
Chapter 15: MQ-4 gas sensor with Arduino Uno
136
In this tutorial, we will learn about what is the MQ-4 gas sensor, its working principle, the pin
diagram, and its applications. We will also make a simple project by using the MQ-4 gas sensor.
Gas leaks are hazardous; not only can a gas leak cause air pollution, but it may also lead to fire
outbreaks and even death. Hence Gas sensors work by notifying us whenever there is a gas leak by
releasing an output signal similar to the amount of gas leak.
Methane is a part of every household. It is a need of humans. For survival, it is necessary as we cook
our daily food items using this natural gas. But, the leakage of this gas is harmful. Many accidents
happen because of this. Therefore, it is important to use a device that detects the leakage when it
happens. And, the MQ4 sensor is especially for this detection.
Gas sensors are quite useful devices for controlling pollutants in the air. Similar is the MQ4 Methane
Gas sensor. The MQ4 sensor is a compact gas sensor which is highly sensitive to methane gas and less
prone to alcohol and other gases. This methane gas sensor provides an output signal according to the
sensed concentration of CH4 in the surrounding environment. This sensor is proven to be effective in
alarm systems, and gas detection circuits whether at a commercial level or in coal mines, etc.
It is suitable for detecting Methane, Propane, Butane, and other combustible gas. This gas sensor is
highly responsive in very little time, so based on the sensitivity requirements; it can be adjusted
through a potentiometer. This is an analog output sensor, used like a CNG (compressed natural gas)
sensor within the series of MQ sensors.
MQ4 methane gas sensor is a MOS (metal oxide semiconductor) type sensor, used to detect the
methane gas concentration within the air at either home or industries & generates output like analog
voltage by reading it. Here, the range concentration for sensing ranges from 300 pm – 10,000 ppm
which is appropriate for the detection of a leak.
This gas sensor mainly includes a detecting element like ceramic based on aluminum-oxide (Al₂O₃),
coated with Tin dioxide (SnO2) and arranged within a stainless-steel mesh.
When methane gas and detecting elements get in contact with each other then the resistivity of the
detecting element will be changed. After that, the change is measured to get the methane gas
concentration. The ignition of Methane gas is extremely exothermic which means it generates a huge
amount of heat once ignited.
137
MQ4 GAS SENSOR PINOUT
This methane gas sensor module contains an onboard voltage regulator, pull-up resistors to define a
default state, a potentiometer for sensitivity, and a capacitor for noise filtering. It has two inbuilt LEDs
i.e., a power LED and a digital output indication LED. The pinout of the MQ-4 Methane Gas Sensor
module is as shown:
Pin1 (H Pins): These pins are two where one of them is used to connect the supply and the remaining
pin is connected to ground
Pin2 (A Pins): Both pins A & B are interchangeable and will be connected to supply voltage.
Pin3 (B Pins): A & B pins are exchangeable where one pin acts like output and another pin will be
pulled to the GND terminal.
The pin configuration of the MQ-4 methane gas sensor module includes four pins which are discussed
below.
VCC Pin: This pin provides voltage to the module and the typical operating voltage is +5V
GND Pin: This pin is used to connect the sensor module to the GND terminal of the system
DO (Digital Out) Pin: This pin provides digital output by setting a threshold value with the help of
the potentiometer
AO (Analog Out): This pin provides output analog voltage which ranges from 0 to 5V depending on
the intensity of the gas.
138
WORKING PRINCIPLE OF THE MQ4 GAS SENSOR
When the module is powered, the power LED lights up. The MQ-4 works by sensing the methane
concentration in the air. The analog pin of the sensor then generates an analog signal proportional to
the amount of CH4 in the air. We can measure the analog output of a sensor with an ADC
microcontroller.
Nowadays most microcontrollers come with a built-in ADC peripheral which can be used to read the
analog output of sensors such as MQ4. The microcontroller reads the analog output signal of the
methane gas sensor and performs signal conditioning to convert the measured analog voltage into
Methane concentration in the air. Based on this measurement, we can take proper actions such as alarm
triggering, etc. whatever you have coded for it to do.
Similarly, the same goes for the digital output pin of the MQ4 methane sensor. When methane gas is
detected, the digital pin goes HIGH and the Digital output inbuilt LED glows up. This digital pin can
also be used for different activities being commanded by a microcontroller.
The applications of the MQ4 methane gas sensor include the following.
● This sensor is used in gas leakage-detecting devices for homes, industries, offices, etc
● These sensors detect methane & natural gasses
● Used in portable gas detectors
● Industrial Combustible gas detector
● Gas leak alarm
● Environmental pollution control
● Coal mines
● Gas sensing devices
139
PROJECT: MQ4 GAS SENSOR INTERFACING WITH ARDUINO UNO
In this project, two-color LEDs are used for indicating the gas leakage like Green & Red.
When the concentration of gas is below the critical value, then the Greenlight will be turned ON.
Similarly, if the gas concentration exceeds the critical value, then the red LED will be turned ON.
HARDWARE REQUIRED
SOFTWARE REQUIRED
140
CIRCUIT DIAGRAM OF THE MQ4 GAS SENSOR WITH LEDS AND ARDUINO UNO
Here, the AO pin of the sensor is connected to the A5 pin of the Arduino Uno. The VCC and GND
pins of the sensor are connected to the 5V and GND pins of the board respectively. The positive
terminal of the red and green LEDs is connected to pins 11 and 8 of the Arduino Uno board. The
negative terminal of both LEDs is connected to the GND pin of the Arduino Uno board.
141
CODE
OUTPUT
By uploading a code, you will get that when the value of the gas sensor exceeds the limit then the red
LED will be turned on; otherwise only the green LED remains turned on. The value of the gas sensor
is displayed on the serial monitor.
142
Chapter 16: LPG Gas Detection System
143
MQ series sensors use a small heater inside the electrochemical sensor to measure a variety of gas
compounds. This MQ-5 gas detector is used to detect leakages of LPG (liquefied petroleum gas),
natural gas and city gas. Its sensitivity to alcohol and smoking is low.
Nowadays, natural gasses are everywhere. they domestic and industrial utility both In open Tasks
Case-ending showing possession or relation for you transport and energy granting to do easy yes. If
caution from No Handled Area has at that time this one important fire of danger forest Are yes. at this
very place given to MQ-5 inflammable gas sensor work coming yes. MQ-5 inflammable gas sensor
LPG, H2, LPG, CH4 and CO of address hook Can yes.
MQ-5 Methane LPG Liquid Propane Gas Sensor Modules are widely used in gas leakage detecting
equipment in family and industry and are suitable for detecting LPG, natural gas, town gas, to avoid
the noise of alcohol and cooking fumes and cigarette smoke .
The MQ5 ( MQ-5 ) is used in gas leakage detecting equipment in consumer and industry applications,
this sensor is suitable for detecting LPG, natural gas, coal gas. Avoid the noise of alcohol, cooking
fumes, and cigarette smoke.
The sensitivity can be adjusted by the potentiometer. The sensitive material of the MQ-5 gas sensor is
SnO2, which has lower conductivity in clean air. When the target combustible gas exists, The sensor’s
conductivity is higher along with the gas concentration rising.
144
SCHEMATIC DIAGRAM OF THE MQ-5 SENSOR
There are four pins on the MQ-5 sensor module, two of which are for VCC and GND. Like other
basic sensor modules, the other two can simultaneously produce analog and digital data. Since the
operating voltage range of the module is 5V with 0.1 percent accuracy, we are using Arduino's 5V pin
to power the circuit. As seen in the example, the module has two built-in LEDs. The power LED
turns on when the board is turned on, and the doubt LED turns on when the trigger value of the
potentiometer is reached. The analog signal input from the gas sensor is converted into a digital signal
by the OP-AMP comparator on this board.
145
APPLICATIONS OF THE MQ5 SENSOR
In this tutorial, we'll learn how to make a gas sensor device that will help inform you when your gas
supply leaks.
HARDWARE REQUIRED
SOFTWARE REQUIRED
146
CIRCUIT DIAGRAM
The AO pin of the gas sensor connected with the A0 pin of the Arduino Uno. The VCC pin of the gas
sensor is connected to the 5V of the Arduino Uno. The +VE terminals of Buzzer and LED are
connected with pins 8 and 5 of the Arduino Uno. The -VE terminals of both and GND pins are
connected to GND pins of Arduino Uno.
147
CODE
OUTPUT
By uploading a code, we will get the alert of the leakage of LPG gas by the MQ-5 LPG gas sensor. If
the value of the LPG gas is detected greater than 250 by the sensor then the buzzer will be turned on
and otherwise LED will be turned on. The status of the gas is displayed on the serial monitor.
148