Arduino 37-in-1 Sensor Kit Guide
Arduino 37-in-1 Sensor Kit Guide
Orange
37 in 1 Sensor Kit
For Beginners
01. Index
1. Arduino Introduction
2. Arduino IDE?
5. PS2 Joystick
Chapter 00 01
02. Index
7. Common Cathode Two Color LED
8. RGB LED
9. Shock Switch
14. Buzzer
1. Arduino Introduction
In this section of this blog, we will talk about the Arduino Development
Board.
Arduino is an open source project and was started with the intention of en-
couraging embedded systems students to learn about micro-controllers.
Before we begin, let me clear one very important doubt which every begin-
ner faces in the initial stage of their learning phase and that is the difference
between micro-controller and microprocessor.
But don't worry, I have explained that thing in the below section in such a way
that it will clear all your doubts and after reading this you will not need to look
back on this topic.
So, both micro-controller and microprocessor are things that are designed to
control applications, perform logical and mathematical operations.
If both are designed to perform the same operation, what is the difference
between these two things? This question may be on your mind.
Chapter 01 01
The other difference is that if you want to use a microprocessor then you
have to interface the memory unit, EEPROM and everything that is essen-
tial to perform the tasks. Micro-controllers, on the other hand, don't need
all those things because those things are built-in with them.
So, this was about the difference between micro-controller and micropro-
cess.
What is Arduino?
As discussed earlier, Arduino is a mi-
cro-controller that can be used to
build small-scale applications. Nowa-
days, we can see the use of Arduino
extensively in the automation indus-
try, there are also a lot of jobs available The Arduino project
in the Indian market for Arduino Mas- was started in 2005.
ters. Aiming to provide a
low-cost and easy
The reason for the massive popularity way for novices and
of Arduino is that Arduino is an professionals.
open-source project and is designed
by the community that constantly
works on it.
Chapter 01 02
How to Install Arduino IDE On Windows?
Arduino software from there or you can download it from their official website.
2. After the download is complete, extract that zip file. You can use WinRAR
software to extract the file.
3. After extracting that zip file, you will see a folder. In that folder you will find
an .exe file.
5. When you will click on that option, the system will start installing Arduino
software.
6. Follow the instructions asked by the installer and do not make any changes
to the settings.
7. Those options are for the user who installed Arduino on their system and they
are updating the software or reinstalling the software on their system.
8. In your case, you are installing the software afresh, so you don't need to make
those changes in your Arduino installation process.
9. While installing the software, the software installer will ask you to install the
driver on your system. You have to allow the installer to install that driver.
10. Those drivers are of Arduino board and if you do not install them on your
system then your system will not recognize Arduino board.
11. After this process is over, you will see the Arduino IDE icon on your system's
applications list.
Chapter 01 03
There is one more driver that you need to install on your system. The name of
that driver is CH340G driver and you will find the link to download that soft-
ware on the product page
Please note - there is a misconception that boards that contain the ch340G
driver are of poor quality but this is not true.
CH340G is just a driver used to transfer information from your computer to the
controller board.
In the next part of this blog, we will learn about the UI of Arduino IDE and un-
derstand the usage of buttons available in the application.
Chapter 01 04
2.0. Arduino IDE - User Interface
You have installed Arduino IDE on your system, now open it.
The Arduino IDE looks like this. In the above UI, you get all the functions
that are useful for uploading code to your Arduino.
The IDE is divided into three sections. Text editor, output section and
menu section.
You write your code in a text editor and you can upload your code to your
Arduino with the help of the options available in the menu section.
Chapter 02 05
2.1. Menu Option
Talking about the menu option, in that menu you get the following op-
tions.
The menu option looks like this. I have explained the working of those
functions int the below sections please have a look.
Chapter 02 06
2.1. Menu Option
Talking about the menu option, in that menu you get the following op-
tions.
1. File
- In this option you will get the option to save the file on the system and
open the recent files if needed.
2. Edit
- Using this option, you can adjust the settings of your editor.
3. Sketch
- In this section you get the option to add libraries. To check what the
library is all about, we request you to check the following section.
4. Tools
- This option is made for the selection of information related to the board.
With the help of this option you can either add boards or install new
boards in your IDE.
5.
- As the name suggests, you can use this section if you need any help
regarding the software or program you have written.
5. Search
- This option is of serial monitor and with the help of this option you can
open serial monitor.
If your code is using the serial print function, you will see the code's output
Chapter 02 07
2.1. Menu Option
In this option you will see the update of the uploading process of the code.
If there is any problem with your code then IDE will generate some error
and those errors we can see in this section.
In this option you will see the update of the uploading process of the code.
If there is any problem with your code then IDE will generate some error
and those errors we can see in this section.
This section is designed for Arduino Code. In this section, we can write our
Arduino.
Chapter 02 08
2.2. How To Upload The Arduino
Code To The Arduino Board?
In the above section we have seen different sections of Arduino IDE. In this
section we will use those sections of the Arduino IDE to upload our first
Arduino code to the Arduino board.
We will try to upload the blink code to the board. We will get the code
from file section.
Please see the following image to understand the process.
When you will click on the blink option shown in the image, a new Arduino tab
will open and in that tab, we will find the Arduino code, which we can use to
toggle the 13-number pin.
So, now that we have the code, we are uploading this code to the Arduino and
for uploading you can press the ctrl+U button on the keyboard to start the pro-
cess of uploading the code.
So this is how we can upload the code to Arduino. In the next part of this blog,
we will learn how to add libraries to that Arduino IDE?
Chapter 02 09
3. What is an Arduino Library?
The Arduino library is a combination of code that has been written by an Ardui-
no contributor.
We use Arduino libraries, that means we use objects and methods of classes.
So, even though we don't need to write much code, we can learn a lot by modi-
fying existing code.
So, that was about introduction to Arduino library, if you have any doubt you
can contact me at info@[Link].
There are two ways of adding library, either you can go to sketch section, then
you can add the required library by clicking on the add library option or if the
required library is not available on Arduino then we get ‘Add Zip Library’ option
in that section.
Chapter 03 10
3.1. What is an Arduino Library?
I have faced this issue many times, I never got my required library in the
Arduino IDE. I always used to download the zip files of the library from the
Internet and then used to add that to the Arduino with the help of the
‘Add zip Library’ Option.
I hope the above section has cleared all your doubts about what is Arduino
library and how to add a new Arduino library to the Arduino?
Chapter 00
Chapter 03 11
01
3.2. Void Loop And Void Setup
Voidloop()
This is one more inbuilt function that is used in the Arduino IDE. This loop keeps
continuously running until someone don’t turn Off the Arduino.
We can write our code-cases in this loop and those cases will keep on running.
Analog read and analog write these are the two functions that deals with the
analog values.
AnalogWrite function is used to write the analog values whereas analogRead func-
tion is used to read the analog values that we pushing on the GPIO pin of the Ardui-
no.
The analogwrite function take two parameters, pin number and pwm value.
Whereas the analogread function only take one parameter and that is pin number.
Store = analogRead(A1);
In the above example the analogRead function is reading the the data which is
coming on the pin number A1 and then storing the information in the store variable.
So, this was about the analogRead function. This function is used when we are
required to work with the variable voltages.
In next section of this blog we will learn about the digitalRead and digitalWrite
function.
Chapter 03 12
Chapter 00 01
3.3. What is an Arduino Library?
I have faced this issue many times, I never got my required library in the
Arduino IDE. I always used to download the zip files of the library from the
internet and then used to add that to the Arduino with the help of the ‘Add
zip Library’ Option.
I hope the above section has cleared all your doubts about what is Arduino
library and how to add a new Arduino library to the Arduino?
Chapter
Chapter 0300 1301
3.4. Void Loop And Void Setup
Voidloop()
This is one more inbuilt function that is used in the Arduino IDE. This loop keeps
continuously running until someone don’t turn Off the Arduino.
We can write our code-cases in this loop and those cases will keep on running.
Analog read and analog write these are the two functions that deals with the
analog values.
AnalogWrite function is used to write the analog values whereas analogRead func-
tion is used to read the analog values that we pushing on the GPIO pin of the Ardui-
no.
The analogwrite function take two parameters, pin number and pwm value.
Whereas the analogread function only take one parameter and that is pin number.
Store = analogRead(A1);
In the above example the analogRead function is reading the the data which is
coming on the pin number A1 and then storing the information in the store variable.
So, this was about the analogRead function. This function is used when we are
required to work with the variable voltages.
In next section of this blog we will learn about the digitalRead and digitalWrite
function.
Chapter 00 01
Chapter 03 14
3.4. Void Loop And Void Setup
Digital Read And Digital Write
DigitalRead and digitalWrite functions are used for reading and writing digital
values.
Talking about the digitalWrite function, this function takes two parameters,
pin number and HIGH/LOW String.
When we give HIGH parameter to the function, the function will put HIGH level
signal on the GPIO pin of the Arduino and when we put LOW then the function
will produce LOW level signal on the GPIO pin.
For Example,
digitalWrite (12, HIGH);
digitalWrite (12, LOW);
In the above line of code, the first line of code producing HIGH level signal on
the 12 number pin and in the second line code the function is producing LOW
level signal on the 12 number pin.
Talking about the digitalRead function, this function takes one parameter and
that is pin number.
For example,
Store = digitalRead(12);
In the above function, the code is running the data of the pin number 12 and
storing the received input in the ‘store’ variable.
Chapter
Chapter00
03 01
15
4. I2C Communication
4.1 Introduction
So, in this section of this blog we will learn about the I2C communication Proto-
col.
We have already discussed the basic things of the I2C communication. In this
section, we will learn explore this topic a little but more.
Till now we understood what is I2C communication protocol and the impor-
tance of it but do we know How it works?
You may have used the I2C communication before this, if not, don’t worry, I am
explaining everything from the scratch. And this section will cover all your
doubts.
Before we talk about the technical things of the I2C communication, let me ex-
plain you this thing is laymen term first.
We understood the basics of the I2C communication, we know with the help of
the I2C communication, we can connect multiple devices to the same line and
can communicate with all those devices without any interference of the sig-
nals.
But do you know how this is possible? I2C communication uses address meth-
odology. The slaves that are connected to the master has unique address.
The master uses that unique address to communicate with the slave devices
that are connected to it.
Let’s say we have connected 3 slave devices to the master and now master
wants to transfer the data to the third slave device.
Chapter 04
16
4.1 Introduction
In that case, the master will store the I2C address of the device and will connect
to the device which I2C address matches with the I2C address the master has.
After connecting with the I2C device the master and slave will start the transfer
of the data.
So, we know that the master device is a main component of the I2C communi-
cation. In I2C communication protocol, the master is the first component
which initiates the communication.
The slave devices that are connected to the master device, waits for the
request from the master device and when they receive the request from the
master, they either sends the data to the master or receives the data from the
master.
Starting the communication and collecting the data from the slave devices is
the job of the I2C master.
Chapter
Chapter00
04 01
17
4.3. I2C Waveform – Explained
Ok, I believe you have checked the above the image, now I want you to check
the following image also.
Please do not skip taking look at these images, if skip these images then you
will not understand whatever I will tell you in the coming sections of this blog.
Chapter
Chapter00
04 01
18
4.3. I2C Waveform – Explained
The above image shows the message bit. The data that will be transferred from
master to the slave devices will be broken up in to the messages.
The information is shown in the above image is responsible for the successful
transfer of the data from slaves to the masters or vice versa.
1) Start Condition –
2) Address Frame –
3) Read Write Bit –
4) ACK or NACK Bit –
5) Data Frame –
6) Stop Condition –
1) Start Condition –
This is first step of starting the communication between master and slave. At
this stage, the master will make the SDA line low before the SCL line.
This signifies the start of the I2C communication.
2) Address Frame –
We know that every slave has its unique I2C address, at this this stage, the
master will put the I2C address of the device in the message to which it wants
to connect with.
3) Read/Write Bit –
This is a second important bit that will be sent by master. Based on this bit, the
master reads or writes the data to the slave devices.
4) Data Frame –
After receiving the read / Write bit from the master the data transfer between
the slave device and master starts.
6) End Condition –
This is final stage of the data transfer. In this stage, the master will make the
SDA line low before the SCL line goes LOW.
Chapter
Chapter00
04 01
19
Orange 37 in 1 Sensor Kit
Hello Geek, In this blog, we will talk about Orange 37 in 1 sensor kits. This kit is
compatible with all micro-controllers and microprocessors.
The only concern is the power supply. The sensor you get with this kit has an
operating voltage of 5v.
So, if your controller is not capable of handling the 5V input voltage, I suggest
you use either a voltage level converter or a resistor divider circuit.
The sensors you get with this kit are compatible with all types of Arduino
boards. As I told you earlier you just need to take care of the operating voltage
of the sensor.
If you have not bought any Arduino board yet then we request you to buy an
Arduino board from here.
The following image shows the list of components we get with this kit. Please
take a look
Chapter
Chapter00
04 01
20
Orange 37 in 1 Sensor Kit
So, that was the introduction part of the orange sensor kit. In the next part of
this blog, we will understand the working and interfacing of these sensors
with the Arduino board.
With this Orange 37 in 1 sensor kit, you are getting three types of hall effect
sensors. The working principle of those three sensors is the same but the
output of those sensors is slightly different.
The hall effect sensors we are getting with this kit are as follows:
These are the three types of sensors that you are getting with this orange 37
in 1 sensor kit.
If we talk about the output type of these sensors, then the output of linear
hall effect sensor is analog and the output of switch hall effect sensor is digi-
tal.
Talking about third hall effect sensor, we get two types of output analog
output and digital output in that sensor.
This was about the hall effect sensor that we are getting with this kit.
In the next part of this blog, we will talk about interfacing Hall Effect sensor
with Arduino.
You will need the following components to understand the working of the
Hall Effect Sensor.
Components List
• Connecting Cable
• Arduino Board
• 5v Power Supply
• Breadboard
Chapter
Chapter00
04 01
21
Interfacing The Hall Effect Sensor
As I told you earlier that the working of these three hall sensors is same, the
only difference is that the output type of these sensors is different.
So, we have to make the connection according to the output type of the
sensor.
I have shared the connection diagram below, please have a look and if you
have any doubts please let us know.
Programming for the Orange Kit Hall Effect sensor is very easy. Since the
output of these sensors can be either digital or analog, we just need to con-
nect the signal pin of the sensor to the correct GPIO pin of the Arduino.
If you are using a linear Hall sensor or a number third sensor, you will need to
connect the signal pin of that sensor to the analog pin of the Arduino.
Chapter
Chapter00
04 01
22
4.1. Arduino Code For Orange Kit Hall Effect Sensor
Sensor number three has four pins, two of those four pins are signal pins. You
will find the sensor output on those pins.
You don't need to connect all those two pins to Arduino. You just need to con-
nect the analog or digital pins to the Arduino. The selection of this PIN will
depend on the requirement of your application.
If your application requires analog reading from the sensor then you can con-
nect the hall effect sensor's analog output pin to Arduino and if your applica-
tion requires the sensor's digital output as input then you can connect the
sensor's digital pin Can connect to Arduino.
Talking about the switch type of Hall Effect sensor, the output type of this
sensor is digital. So we have to connect this pin to any digital GPIO pin of Ar-
duino.
Chapter
Chapter00
04 01
26
4.2. Code Explanation
Since the output type of the sensor will be either analog or digital, we can
use either the analog read or digital read function to read the output of the
sensor.
I have shared two code snippets below; One code snippet is for detecting
analog output and the other is for digital output.
You can use either of these two snippets as per your application requirement.
void setup()
{
pinMode(13, OUTPUT); // Pin 13 has an LED connected on most Arduino
boards:
pinMode(hall_Sensor,INPUT); //Pin 2 is connected to the output of proximi-
ty sensor
[Link](9600);
}
void loop()
{
if(digitalRead(hall_Sensor)==HIGH) //Check the sensor output
{
digitalWrite(13, HIGH); // set the LED on
}
else
{
digitalWrite(13, LOW); // set the LED off
}
inputVal = digitalRead(hall_Sensor);
[Link](inputVal);
delay(1000); // wait for a second
}
Chapter 04 01
27
4.2 Digital Hall Effect Sensor
Chapter 04 28
5. 2 Axis Joystick
We all used joystick in our childhood. The kind of Super Mario games we used
to play when we were kids were controlled by a joystick controller. Plus, the
electronic toy cars you used to play with as a child were controlled by joysticks.
So, as you have already used the joystick, it will be so much easier for you to un-
derstand the working of the joystick.
The joystick has two potentiometers. When we move the joystick, the position
of the potentiometer changes.
Change in the position of the potentiometer means that the resistance of the
potentiometer changes. Therefore, as the resistance is changing, the output
voltage of the potentiometer also changes.
When the microcontroller receives input from those potentiometers, the mi-
crocontroller takes further necessary action based on the logic that you have
added to your code.
Coming back to our discussion, that joystick module has four output pins. We
will discuss more about the interfacing diagram in the below section.
Chapter 05 29
5.1. Interfacing the Joystick Module With the Arduino
5.2. Arduino Code For Joystick Module
I have explained the function of those pins in the below section, please take a
look.
1. X - This pin generates the analog output voltage. (0-255) and is used to moni-
tor the movement of the joystick on the X-plane.
2. Y - This pin also generates the analog output voltage and is used to monitor
the movement of the joystick on the Y-plane.
3. Switch - This pin is the output of the switch placed under the joystick. When
we press the joystick, this pin generates high voltage. Sometimes this switch is
also used to locate the Z-axis of the joystick.
Now that you know the function of each pin, we can now connect the pins of
the joystick to the Arduino.
Analog pin of module pin 'X' and pin 'Y' You can connect analog pin of Arduino
and digital pin of module to digital pin of sensor.
Chapter 05
30
5.2. Arduino Code For Joystick Module
The joystick's output is either analog or digital. Therefore, we can use the
analog read and digital read functions to read the output.
Since the joystick's output is in simple format, we don't need to install any
library to work with this board, normal analog read function and digital read
function will work for the application.
In the following code, we have not used any library. We have used a variable to
store the value of the sensor and that value we are printing on the serial moni-
tor using serial. print method.
It was about joystick code cases. If you have any doubts regarding the code
then you can mention your doubts in the comment section.
#define joyX A0
#define joyY A1
int button=2;
int buttonState = 0;
int buttonState1 = 0;
void setup() {
pinMode(7,OUTPUT);
pinMode(button,INPUT);
digitalWrite(button, HIGH);
[Link](9600);
}
void loop() {
int xValue = analogRead( joyX);
int yValue = analogRead( joyY);
int button_output = digitalRead(button);
[Link](xValue);
[Link]("\t");
[Link](yValue);
[Link]("\t");
[Link](button_output);
Chapter
Chapter00
05 01
31-code
6. LDR (Light Dependent Switch)
Light Dependent Resistor are also known as photoresistor they are widely used
in electronic sectors.
They are easily available is low prices so it becomes most preferable choice to
the product selection engineer.
Another application of the LDR is, they are used in the IR video recording cam-
eras. The IR video recording cameras works in two mode.
In first mode, they work similar to the normal camera but in second mode they
turn on the IR lights to record the videos and to switch between these two
modes the IR video recording cameras make use of LDR.
Chapter 06 32
So, these are the applications of the LDR, in the next section of this blog
we will learn about the interfacing of the LDR with the Arduino.
vv
6.2. Interfacing The LDR With The Arduino
Chapter 06 33
6. LDR (Light Dependent Resistor)
Chapter 06 34
7. Common Cathode Two Color LED
We are getting command cathode two color LED module with the orange 37
in 1 sensor kit.
That module has two led on it and the anode termianal of those led is inter-
connected and the cathode terminal of those two led is left open.
The function of these two leds is same but the only difference is the packag-
ing of the product.
You can see in the above image you are getting one SMD package LED and
one DIP package LED.
Now you know about which LED you are getting with this kit but do you
know the how LED will produce different color?
The common cathode two color LED has two LEDs in it. The cathode terminal
of both LEDs is connected together and taken out of the led package where-
as the anode terminals of those LEDs are not connected together they are
separately taken out.
The LEDs that are inside of this LED can be turned on one after another or at
the same.
We will have to connect the anode terminals of this LED to the positive volt-
age the particular LED will turn on.
And if we want to control the brightness of that particular LED then we will
have to vary the input voltage to that LED.
And to vary the input voltage, we can use the PWM function.
The same technique we have used to control the brightness of the LED.
As I told earlier, we can turn on both LEDs at the same time or we can turn on
each LED on after another.
You can try out turning on each LEDs on one after another and also you can
try turning on them at the same using the below section discussed code.
But before that we will understand the interfacing of this LED with the Ardui-
no.
Chapter 07 35
7. Interfacing of Common Cathode LED with the Arduino
I have explained the function of those pins below please take a look.
Cathode Pin - The cathode pins of both LED are connected to this pin.
Anode Pin – This pin has three two anode terminals. One is of red LED and the
other one is of green LED.
You can check the following image to understand the interfacing diagram
properly.
In the following you can see we have connected the anode pin of the red LED
to 6 pin number of the Arduino and the anode terminal of the green LED to
the 7 number pin of the Arduino.
The reason behind connecting LEDs to those pins is those pins are PWM pins
and as our application need PWM functionality we have used those.
You can use any GPIO pin of the Arduino to turn on the LEDs but if you want
to control the brightness of those LEDs then you will have to use pwm pins
only.
So, this was about the interfacing of the LEDs with the Arduino. In next sec-
tion you will get the Arduino code for controlling the brightness of the LED.
Chapter 07 36
7.2. Arduino Code For Common Cathode LED
We have already talked about the technique that we are going to use to con-
trol the Common cathode LED.
Now we will have to implement our logic. I have used my logic to implement
the discussed things but if you better logic then you can use it.
Chapter 07 37
8. RGB Colour LED
You may have used LEDs many times in your projects. In this kit, you are get-
ting a LED but this LED is little bit different than normal LED. This LED pro-
duces three different types of color.
I know you may be interested in knowing about the working of the LED, in
this section we will understand the working of the RGB LED.
Before we talk about the working of the RGB led I would like to tell you the
basic things of the RGB. So any color that you see in your surroundings is
made up of three primary colors. Those Three primary colors are as follows.
1. Red
2. Green
3. Blue
And the following methods are used for creating different types of colors.
1. Additive Mixing
2. Subtracting Mixing.
Chapter 08 38
8.1 RGB
7.2. Colour
Arduino LEDFor Common Cathode LED
Code
These are the two different LEDs we are getting with the kit. The function of
these two leds is same but the only difference is the packaging of the product
is different.
Till this point we were talking about the color mixing process of the RGB led
but do you know how the RGB LED emit different colors?
The RGB led works on the PWM value, when the variable voltage is applied to
the input terminal of the RGB LED, the LED start emitting different colors.
So, we can say the color of the LED depends on the PWM value we are apply-
ing to the input pin of the LED.
We can generate PWM voltage by using the analogWrite function. There are
other ways of generating PWM voltage also we have talked about those
methods in the booklet of the kit.
In the next section of this blog we will talk about the interfacing of the sensor
with the Arduino.
The RGB LED that you getting with this orange 37 in 1 sensor kit has only
three pins. Vcc, gnd and signal. To turn on the LED, You will have to apply
PWM signal to the signal pin of the RGB led.
In my case, I have connected the signal pin of my RGB to the 9-number pin of
the Arduino. And the other power pins to the power pins of the Arduino.
You can check the following image to understand the interfacing diagram
properly.
Chapter 08 39
8.2. Interfacing
7.2. Arduino Codeof RGB LED withCathode
For Common the Arduino
LED
You can use the following code and if you have any doubt please let us know
in the comment section.
Chapter 08 40
8.3. Arduino
7.2. Arduino Code
Code For
For RGB LED Cathode LED
Common
You may have used LEDs many times in your projects. In this kit, you are
gettAs discussed in the earlier sections, we have used analogRead function
for writing the PWM values.
You can use the following code and if you have any doubt please let us know
in the comment section.
Chapter 08 39
9. Shock Switch
This is shock sensor that we are getting with this kit. Now, where do we use
this sensor?
This sensor can be used in many applications such shock sensor-based wake
up system, toys and many more places.
But how does this sensor detect the shock? What part it has in that cylindri-
cal part? We are going to discuss about all those things in the below section.
So, the shock switch has two main parts. One is thin metal spring and other
one is conductive material.
The thin spring is wrapped around the conductive material when the sensors
moves the spring also moves and while moving the spring touches to the
conductive material and this is how the connection happens.
The output of this sensor will be digital so we can use digital read function to
read the output of the sensor.
The shock pin has three pins. Signal pin, Vcc and GND pin. The signal pin of
the sensor we can connect to the any GPIO pin of the Arduino and the power
pin of the sensor to the power pins of the Arduino.
Here while interfacing the shock sensor with the Arduino we will have to use
either pull-up resister or pull-up resister.
I have shared the images of both methods. Please take a look and if you face
any issues you can mention your issue in the comment section.
Chapter 09 40
9.1 Interfacing The Shock Switch With The Arduino
InThe
theshock
belowpin has three
section, pins.
I have Signal
shared pin,
the Vccfor
code andtheGND pin. The
Arduino. signal
In this pin
code of
I am
the sensor
using we can connect
a digitalRead function to
to the
readany
theGPIO pinof
output ofthe
thesensor.
Arduino and the power
pin of the sensor to the power pins of the Arduino.
You can use the following code and can start working with the shock sensor.
InHere
the while interfacing
following code wetheareshock sensor
reading with the
the output ofArduino we and
the sensor will have to use
printing it
either pull-up resister
on the serial monitor. or pull-up resister.
I have
So, shared
to check the
the images
output of both
of the methods.
sensor you willPlease take
have to a look
open and
to the if you
serial face
moni-
any issues you can mention your issue in the comment section.
tor.
Chapter 09 41
9.2 Arduino Code For shock Sensor
Chapter 09 42
10. Knock Sensor:
The working principle of the knock sensor is the same as that of the shock
sensor.
When we rotate this sensor, this sensor generates a high-level voltage signal.
It is used in doorbell applications and automatic switching boards.
The interfacing of these sensors is simple. In the next part of this blog, we will
understand the interfacing of sensors with Arduino.
The interfacing of knock sensor is little bit different than the shock sensor. IN
the interfacing of the shock sensor with the Arduino we were using the
pull-up or pull-down resisters but in case of the knock sensor we don’t need
to add any external circuit.
In the following image you can see I have connected the output of the sensor
with the Arduino and the power pins of the sensor with the power pins of the
Arduino.
Please see the following image of the sensor to understand the interfacing of
the sensor.
Chapter 10 43
10.1 Interfacing The Shock Switch With The Arduino
10.2 Arduino
The shock Code
pin has threeFor
[Link] Sensor
Signal pin, Vcc and GND [Link] signal pin of
the sensor we can connect to the any GPIO pin of the Arduino and the power
pin of the sensor to the power pins of the Arduino.
Here while interfacing the shock sensor with the Arduino we will have to use
Ineither
the below section,
pull-up I have
resister shared
or pull-up the code for the Arduino. In this code I am
resister.
using a digitalRead function to read the output of the sensor.
I have shared the images of both methods. Please take a look and if you face
You
anycan useyou
issues the can
following code
mention and
your can in
issue start
theworking
commentwith the shock sensor.
section.
In the following code we are reading the output of the sensor and printing it
on the serial monitor.
So, to check the output of the sensor you will have to open to the serial moni-
tor.
Chapter 10 44
10.2 Arduino Code For Shock Sensor
void setup() {
[Link](9600);
pinMode (ledPin, OUTPUT) ;
pinMode (knockPin, INPUT) ;
}
void loop() {
knockDetect = digitalRead(knockPin) ;
if (knockDetect == LOW)
{
lastKnockTime = millis();
if (!impactAlarm)
{
[Link]("Status: Collision/Fall Detected");
digitalWrite(ledPin,HIGH);
impactAlarm = true;
delay(100);
}
}
else
{
if( (millis()-lastKnockTime) > 500 && impactAlarm)
{
The shock pin has three pins. Signal pin, Vcc and GND pin. The signal pin of
digitalWrite(ledPin,LOW);
the sensor we can connect
[Link]("Status: to the any GPIO pin of the Arduino and the power
No Collision/Fall");
impactAlarm = false;
pin of
}
the sensor to the power pins of the Arduino.
}
Here while interfacing the shock sensor with the Arduino we will have to use
either pull-up resister or pull-up resister.
I have shared the images of both methods. Please take a look and if you face
Inany
theissues
belowyou can mention
section, your issue
I have shared in the
the code comment
for section.
the Arduino. In this code I am
using a digitalRead function to read the output of the sensor.
You can use the following code and can start working with the shock sensor.
In the following code we are reading the output of the sensor and printing it
on the serial monitor.
So, to check the output of the sensor you will have to open to the serial moni-
tor.
Chapter 10 45
11. Infrared Sensor Generator
The infrared sensor are used in many applications. You can see the infrared
sensor in wireless remotes they are also can be used in data transmission
systems.
In this section you will understand the working of the IR sensor and interfac-
ing of the sensor with the Arduino.
Infrared sensor generates infrared rays. The switching speed of the infrared
sensor is very fast that is why they are used in data transmission application
also.
The wireless remote controller that we use in our daily life has an infrared
sensor, control unit and power unit in it.
The power unit power the module and the control-unit is designed for gener-
ating the IR receiver acceptable code.
The code is nothing but a blinking pattern on the Infrared sensor. Our Ardui-
no program will convert the control signals to encoded signals and this en-
coded signal will be then decoded at the IR receiver.
Talking about the IR receiver are the sensors that are used to receive the sig-
nals that are transmitted by the IR transmitter.
IR receiver receives the signal and pass those received signals to the control
unit and then the control unit take the further required actions.
This Is how IR Receiver and IR Transmitter works. In the next section of this
blog we will understand the interfacing of the IR transmitter.
Chapter 11 46
11.1 Interfacing of the IR Sensor with the Arduino
We have connected the IR receiver to the Arduino. In this section, we will un-
derstand the interfacing of the Arduino with the IR receiver.
In the following code, we are using IR sensor library. You can download that
library using the following link.
[Link]
After installing the library, you can use the following code.
Chapter 11 47
11.2 Arduino Code For Shock Sensor
#include <IRremote.h>
#include <IRremoteInt.h>
int RECV_PIN = 13; // The digital pin that the signal pin of the
sensor is connected to
IRrecv receiver(RECV_PIN); // Create a new receiver object that
would decode signals to key codes
decode_results results; // A varuable that would be used by receiv-
er to put the key code into
void setup() {
[Link](9600); // Setup serial port to send key codes to com-
puter
[Link](); // Enable receiver so that it would start pro-
cessing infrared signals
}
void loop() {
if([Link](&results)) { // Decode the button code and
put it in "results" variable
[Link]([Link], HEX); // Print the code as a hexadec-
imal value
[Link](); // Continue listening for new signals
}
}
Chapter 11 48
12. IR Obstacle Sensor
IR sensor is an electronic device, that emits the light in order to sense some
object of the surroundings.
An IR can measure the heat of an object as well as detects the motion. Usual-
ly, in infrared spectrum, all the objects radiate some form of thermal radia-
tion.
These types of radiations are invisible to our eyes, but infrared sensor can
detect these radiations.
1. IR Receiver
2. IR Transmitter
Infrared Transmitter is a light emitting diode (LED) which emits infrared radia-
tions called as IR LED’s.
Even though an IR LED looks like a normal LED, the radiation emitted by it is
invisible to the human eye.
Chapter 12 49
12.3 Interfacing of the IR Sensor with the Arduino
The interfacing of the IR obstacle sensor with the Arduino is very. This module
has only three pins out of those three pin two are power pins and one is signal
pin.
Those power pins you can connect to the power pins of the Arduino and you
can connect the signal pin to the analog pin of the Arduino.
Chapter 12 50
12.4. Arduino Code IR Obstacle Sensor
In this section we will understand about the Arduino code for the IR Sensor.
In the code below you can see we have used analogRead function to read the
output of the IR sensor.
After that you can see there we are storing the output of the sensor in a vari-
able. And then we have used [Link] function to print the output of the
sensor.
Please check the code below and if you have any doubt please mention in the
comment section.
Chapter 12 51
13. IR Flame Sensor
Different flame sensors are available in the market. What you are getting with
this kit is the IR flame sensor.
IR flame sensors are used in fire systems. They detect the fire and send a
signal to the controller unit, then the controller unit takes the necessary ac-
tions to avoid damage from the fire. But do you know how the IR flame
sensor detects fire.
The answer to the question is, IR sensors are designed to capture gases that
appear in the infrared spectral band.
When an explosion occurs, the light produced by the flames appears in the
infrared spectral band and produces a flame sensor signal based on the
received frequency of light.
It was about the introduction of flame sensors; In the next part of the blog we
will learn about interfacing of flame sensors with Arduino.
You just need to connect the signal pin of the flame sensor to the analog pin of
the Arduino and the other power pin of the sensor to the power pin of the Ardui-
no.
Chapter 13 52
13.2 Arduino Code For Flame Sensor
So, as you connected the IR Flame Sensor to the Arduino, we will now move
to the Arduino Code section of the Flame Sensor.
The code is very simple and requires no explanation. In this code, we have
used a variable and the function of that variable is to collect the output of the
IR flame sensor.
void setup() {
// initialize serial communication @ 9600 baud:
[Link](9600);
}
void loop() {
// read the sensor on analog 2:
int sensorReading = analogRead(2);
// map the sensor range (four options):
// ex: 'long int map(long int, long int, long int, long int, long int)'
int range = map(sensorReading, sensorMin, sensorMax, 0, 3);
// range value:
switch (range) {
case 0: // A fire closer than 1.5 feet away.
[Link]("** Close Fire **");
break;
case 1: // A fire between 1-3 feet away.
[Link]("** Distant Fire **");
break;
case 2: // No fire detected.
[Link]("No Fire");
break;
}
Chapter 13 53
14. Buzzer
Buzzers are used in many devices. It is used as an indicator in many systems
such as home appliances, alarm systems, electronic bells.
Talking about the working principle of the buzzer, the buzzer converts electri-
cal energy into sound energy.
When the voltage is applied to the buzzer, the piezo crystal expands and
shrinks inside the plastic casing. This causes the plate vibrates near the crys-
tal and the sound you hear is of the same vibration.
So, it was about the buzzer. There are two main types of buzzer. Active buzzer
and passive buzzer. In the next part of this blog, we will understand the differ-
ence between these two types.
As I mentioned earlier, there are many types of buzzers. Active buzzer and passive
buzzer.
Talking about active buzzer, it has inbuilt oscillating source. As soon as you power
it up, the active buzzer starts to sound but in the case of a passive buzzer they do
not have an inbuilt oscillating source.
If you want a passive buzzer to generate a sound signal, then you have to give the
buzzer a different frequency.
Chapter 14 54
14.2. Buzzer Interfacing with Arduino
pin directly to the GPIO pin of the Arduino and the GND pin of the buzzer to
the GND pin of the Arduino.
In my case, I have used an active buzzer. But you can make use of passive
buzzer, but remember, if you want to make a different sound from the pas-
sive buzzer here, then you have to give different frequency to the VCC pin of
the buzzer.
As you have wired the buzzer. Now, we will move to the Arduino code for
buzzer.
Chapter 13 55
14.2 Arduino Code For Buzzer
The code for the buzzer is very simple. You just need to use the digitalWrite
function and apply high or low voltage to turn on or off the buzzer.
void setup() {
// initialize serial communication @ 9600 baud:
[Link](9600);
}
void loop() {
// read the sensor on analog 2:
int sensorReading = analogRead(2);
// map the sensor range (four options):
// ex: 'long int map(long int, long int, long int, long int, long int)'
int range = map(sensorReading, sensorMin, sensorMax, 0, 3);
// range value:
switch (range) {
case 0: // A fire closer than 1.5 feet away.
[Link]("** Close Fire **");
break;
case 1: // A fire between 1-3 feet away.
[Link]("** Distant Fire **");
break;
case 2: // No fire detected.
[Link]("No Fire");
break;
}
Chapter 13 56
15. Vibration Sensor
Vibration sensors are used in many devices such as mechanical machines. It
is used to measure the frequency of vibrations generated by machines and
those measured frequencies are then converted into voltage signals.
The Vibration sensor we are getting with this kit has an onboard gain amplifi-
er. That amplifier is used to adjust the gain of the module. If the signals gen-
erated by the vibration sensor are not accurate then you can adjust the trim-
mer next to the gain amplifier.
There is a lot of information about vibration sensors, now we will talk about
the interfacing of vibration sensors with Arduino.
Vibration sensor interfacing with the Arduino is super easy. It has three pins,
two for powering the module and one for output.
We have to connect the power pin of the vibration sensor to the Arduino and
the signal pin to any GPIO pin of the Arduino.
You can refer to the following image to understand the wiring diagram.
Chapter 15 57
15.2 Arduino Code For Vibration Sensor
The code for the buzzer is very simple. You just need to use the digitalWrite
function and apply high or low voltage to turn on or off the buzzer.
void setup() {
// initialize serial communication @ 9600 baud:
[Link](9600);
}
void loop() {
// read the sensor on analog 2:
int sensorReading = analogRead(2);
// map the sensor range (four options):
// ex: 'long int map(long int, long int, long int, long int, long int)'
int range = map(sensorReading, sensorMin, sensorMax, 0, 3);
// range value:
switch (range) {
case 0: // A fire closer than 1.5 feet away.
[Link]("** Close Fire **");
break;
case 1: // A fire between 1-3 feet away.
[Link]("** Distant Fire **");
break;
case 2: // No fire detected.
[Link]("No Fire");
break;
}
Chapter 15 58
16. 5V Single Channel
A relay is an electrically operated switch that can be turned on or off, letting the
current go Relay Module can be found in many electronic devices. It is an elec-
tromagnetic switch that is used for switching high voltage requirement appli-
ances.
In this section we are going to understand the interfacing of the relay with the
Arduino and will understand a few basic things of the relay module.
Relay module has five pins, out of those five pins three are of input and two are
of output. I have explained the function of those pins below please have a look.
No Normally Open –
This is output pin of the relay module. This pin remain open until the coil of the
relay module is not energized.
NC Normally Close –
This is also an Output pin. This pin stays connected to the common terminal
until the relay module is not energized.
This is an input pin. When the relay module is powered on the Normally open
pin will be connected to the common terminal pin and when the power to the
relay module is turned off then the common terminal will be connected to the
normally close terminal.
These two pins are the power pins and are connected to the internal coil of the
relay.
The operating voltage of the coil of the relay module that we are getting with
this kit, is 5V.
So, we can directly connect the 5V supply to this pin.
16
Chapter 00 01 59
16. 5V Single Channel Relay
Signal Pin –
This pin is connected to the base terminal of the SMD transistor which is pres-
ent on the relay module. When we give 5V to this pin, this pin will turn on the
power to the relay coil.
So, this was about the introduction of the relay module in the next section of
this blog we will understand the interfacing of the relay module.
In this section, we will be learning about the interfacing of the relay module
with the Arduino.
As discussed earlier, the relay module has six pins and out of those six pins, five
pins three are input pin.
Chapter
Chapter00
16 01
60
16.1. Interfacing The Relay Module With The Arduino –
You can see GND, VCC and signal pin over there. The GND and VCC pin of the relay
module you can connect to the VCC and GND pin of the Arduino and the Signal pin
of the relay module you can connect to any GPIO pin of the Arduino.
This was about the one end of the relay module now we will understand the con-
nections of the other side.
In the above image you can see COM, NC and NO pins. These are connected to the
load.
Let’s take an example, let’s say you want to turn on an LED lamp with the help of
the relay module.
In order to turn on the led we will have to connect the positive pin of the Lamp to
the mains supply and the negative pin of supply to the common pin of the relay
module and to the NO (Normally Open) pin we can connect the negative pin of
the LED Lamp.
Chapter
Chapter00
16 01
61
16.2. Interfacing The Relay Module With The Arduino –
In this section we will understand the Arduino code that we will use to the relay
module.
So, the relay is connected to the Arduino and Arduino is powering the relay
module so we will have to use digitalWrite function.
I have added 200 sec below between the execution of two functions. The reason
behind adding delay between two functions is, if there is no delay between the
relay turning on event and turning off event then we will not understand the dif-
ference between these two events.
So, to understand the difference between these two functions, I have used the
delay function.
If you don’t have any doubts about the code then you can use the following Ar-
duino. If you have any doubts then mention your issues in the comment section
below.
Chapter
Chapter00
16 01
62
16.1. Arduino Code For Controlling the Relay Module –
int relay = 8;
volatile byte relayState = LOW;
// Timer Variables
long lastDebounceTime = 0;
long debounceDelay = 10000;
void setup() {
// Pin for relay module set as output
pinMode(relay, OUTPUT);
digitalWrite(relay, HIGH);
// PIR motion sensor set as an input
pinMode(PIRInterrupt, INPUT);
// Triggers detectMotion function on rising mode to turn the relay on, if the condition is met
attachInterrupt(digitalPinToInterrupt(PIRInterrupt), detectMotion, RISING);
// Serial communication for debugging purposes
[Link](9600);
}
void loop() {
// If 10 seconds have passed, the relay is turned off
if((millis() - lastDebounceTime) > debounceDelay && relayState == HIGH){
digitalWrite(relay, HIGH);
relayState = LOW;
[Link]("OFF");
}
delay(50);
}
void detectMotion() {
[Link]("Motion");
if(relayState == LOW){
digitalWrite(relay, LOW);
}
relayState = HIGH;
[Link]("ON");
lastDebounceTime = millis();
}
Chapter 01
Chapter00
16 63
17. Reed Switch Module
Reed Switch are one of the contactless switches. These switches are used in
many applications such as cars, washing machines but the burglar system is
one of the most popular application.
In this section of this blog we will understand working of the reed switch.
We are getting two types of the Reed Switches with the kits I have shown the
images of those two modules below please take a look.
So, this two Reed switches we are getting with this kit. In the next section of
this blog we will understand the basics of the functions.
The reed switch has two ferromagnetic contacts inside. These contacts are
placed two microns apart from each other.
When the magnet is placed near this switch the ferromagnet blades get
attracted towards one another.
In this way the connection happens and the switch acts as a closed switch.
The reed switch has two pins. You can connect one end of the reed switch to the
5v pin of the Arduino and the other end of the reed switch to any GPIO pin of the
Arduino.
The following image shows the interfacing diagram of the reed switch with the
Arduino.
Chapter
Chapter00
16 01
64
16.3. Arduino Code For Reed Switch
In the following Arduino code, we have used digitalRead function to read the
output of the sensor.
You can upload the code to your Arduino and can see the output of the code
on the serial monitor.
Chapter
Chapter00
16 01
65
Orange 37 in 1 Sensor Kit
Hello Geek, In this blog, we will talk about Orange 37 in 1 sensor kits. This kit is
compatible with all micro-controllers and microprocessors.
The only concern is the power supply. The sensor you get with this kit has an
operating voltage of 5v.
So, if your controller is not capable of handling the 5V input voltage, I suggest
you use either a voltage level converter or a resistor divider circuit.
The sensors you get with this kit are compatible with all types of Arduino
boards. As I told you earlier you just need to take care of the operating voltage
of the sensor.
If you have not bought any Arduino board yet then we request you to buy an
Arduino board from here.
The following image shows the list of components we get with this kit. Please
take a look
Chapter
Chapter00
04 01
20
17. Analog and Digital Temperature
Sensor
We are getting two temperature sensors with this kit. We can use these two
sensors in our IOT projects.
The analog temperature sensor has a thermocouple sensor in it, the output of
the thermocouple sensor changes as per the change in the temperature.
Talking about the digital temperature sensor, this sensor generates either high
voltage or Low Voltage depending on the threshold temperature we set.
This was the basic introduction of the temperature sensor; in the next section
of this blog we will discuss about the interfacing of the analog and digital
sensor with the Arduino.
The analog and digital temperature sensor both has three pins. Out of those
three pins two pins are power pins and on is signal pin.
The signal pin of those sensor we can connect to any GPIO pin of the arduiuno.
Chapter
Chapter00
16 01
64
17.2. Interfacing the analog Temperature sensor with the
Arduino
Chapter
Chapter00
16 01
65
17.2. Interfacing the Temperature sensor with the Ardui-
no
We are getting two temperature sensors with this kit. We can use these two
sensors in our IOT projects.
The analog temperature sensor has a thermocouple sensor in it, the output of
the thermocouple sensor changes as per the change in the temperature.
Talking about the digital temperature sensor, this sensor generates either high
voltage or Low Voltage depending on the threshold temperature we set.
This was the basic introduction of the temperature sensor; in the next section
of this blog we will discuss about the interfacing of the analog and digital
sensor with the Arduino.
The analog and digital temperature sensor both has three pins. Out of those
three pins two pins are power pins and on is signal pin.
The signal pin of those sensor we can connect to any GPIO pin of the arduiuno.
Chapter 16 66
17.3. Arduino Code For Analog temperature sensor –
We have learned about the interfacing of the sensor with the Arduino. Now, as
we know how the analog and digital sensor work, we can design our logic to
read the output of the sensor.
So far, we understood that the analogRead function is used to read the analog
values and digitalRead function is used to read the digital output of the sensor.
In the temperature sensor code, we are using both of these functions to get
the output of the sensor.
I have shared two code snippets below. One code snippet is for analog tem-
perature sensor and other one is of digital temperature sensor.
Please upload the code to your Arduino and see the output of the sensor on
the serial monitor.
void setup() {
[Link](9600);
}
void loop() {
Vo = analogRead(ThermistorPin);
R2 = R1 * (1023.0 / (float)Vo - 1.0); //calculate resistance on
thermistor
logR2 = log(R2);
T = (1.0 / (c1 + c2*logR2 + c3*logR2*logR2*logR2)); // temperature
in Kelvin
T = T - 273.15; //convert Kelvin to Celcius
// T = (T * 9.0)/ 5.0 + 32.0; //convert Celcius to Farenheit
[Link]("Temperature: ");
[Link](T);
[Link](" C");
Chapter 16 67
17.3. Arduino Code For Digital temperature sensor –
#include <OneWire.h>
void setup(void) {
// initialize inputs/outputs
// start serial port
[Link](9600);
}
void loop(void) {
byte i;
byte present = 0;
byte data[12];
byte addr[8];
ds.reset_search();
if ( ) {
[Link]("No more addresses.\n");
ds.reset_search();
return;
}
[Link]("R=");
for( i = 0; i < 8; i++) {
[Link](addr[i], HEX);
[Link](" ");
}
Chapter 17 68
17.3. Arduino Code For digitaltemperature sensor –
if ( addr[0] == 0x10) {
[Link]("Device is a DS18S20 family device.\n");
}
else if ( addr[0] == 0x28) {
[Link]("Device is a DS18B20 family device.\n");
}
else {
[Link]("Device family is not recognized: 0x");
[Link](addr[0],HEX);
return;
}
[Link]();
[Link](addr);
[Link](0x44,1); // start conversion, with parasite power on at
the end
present = [Link]();
[Link](addr);
[Link](0xBE); // Read Scratchpad
[Link]("P=");
[Link](present,HEX);
[Link](" ");
for ( i = 0; i < 9; i++) { // we need 9 bytes
data[i] = [Link]();
[Link](data[i], HEX);
[Link](" ");
}
[Link](" CRC=");
[Link]( OneWire::crc8( data, 8), HEX);
[Link]();
Chapter 17 69
18. DHT11 Temperature and Humidity Sensor Module
If you are working on the IOT project then this sensor will help you in gathering
the surrounding atmosphere. This sensor has inbuilt capacitive temperature
sensor and thermometer in it.
The DHT11 sensor is designed in such way that it will hold the moisture. When
the water vapor gets collected on the subtract of the sensor. The ions will be
released by the subtract. Increase in the ions will reduce the resistance be-
tween two electrodes.
Now as the resistance is decreasing between two electrodes, we will get the
variations in the voltage readings. Based on the variations we received from
the sensor we can calculate the humidity.
Talking about the temperature sensor, DHT11 Sensor has NTC type thermistor
in it. This thermistor is nothing but a temperature sensor.
The output value of the thermistor changes as per the change in the surround-
ing temperature. So by calculating the change in the output voltage of the
sensor we can find out the reading of the temperature.
In the following section I have shown the interfacing diagram of the DHT11
sensor with the Arduino.
The DHT11 Sensor has three pins. You can connect those three pins to the Ardu-
ino as shown in the below figure.
Chapter 18 70
18.3. Interfacing DHT11 Sensor With Arduino
In the following section I have shown the interfacing diagram of the DHT11
sensor with the Arduino.
The DHT11 Sensor has three pins. You can connect those three pins to the Ardu-
ino as shown in the below figure.
18
Chapter 00 01 71
18.4. Arduino Code DHT11 Interfacing With Arduino
#include "DHT.h"
void setup() {
[Link](9600);
[Link](F("DHTxx test!"));
[Link]();
}
void loop() {
// Wait a few seconds between measurements.
delay(2000);
// Check if any reads failed and exit early (to try again).
if (isnan(h) || isnan(t) || isnan(f)) {
[Link](F("Failed to read from DHT sensor!"));
return;
}
Chapter 00
18 01 72
19. Switch Module
We are getting the switch module with this kit. If your working on a project
that needs switch then you can use this module.
This switch module has three pins. Out of those three pins two pins are power
pins and one pin is a signal pin.
In the following image I have shown the interfacing diagram of the switch
please take a look and if there is any doubt please let us know in the comment
section.
When we press the switch the switch module will generate HIGH level signal.
So, if we want to read the output of the sensor then we will have to use the digi-
talRead function.
In the following code you can see we have used the digitalRead function and
then printing it on the serial monitor.
19
Chapter 00 01 73
19.1. Arduino Code For Switch Module
Lorem ipsum
void setup() {
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
Lorem ipsum
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
}
void loop() {
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
Chapter 00
19 01 74
We are getting a Mercury Switch Module with this product. The switch can be
used for measuring the tilt.
Talking about the construction of this switch, it has two electric contacts en-
closed in a cylindrical shape made up of glass material. When the switch is
tilted the mercury that is inside the glass envelop material gets connected to
the terminals of the switch and the switch act as a closed switch.
Usually in normal switches, when we press them or turn them on a little arcing
can be seen that arcing produces EMI (Electromagnetic interference) in the
electronic circuits.
But in the case of mercury switch, the arcing is absorbed by the mercury. So,
the use of mercury switch modules reduces the effect of the (Electromagnetic
interference) EMI and that is why it is most favorable switch module in the ap-
plications where EMI is the most concern.
• VCC
• GND
• Signal
VCC and the GND pins are the power pins of the sensor and the signal pin we
can connect to any GPIO of the Arduino.
Please see the following Image to understand the working of the Mercu-
ry-Switch Module.
20
Chapter 00 01 75
20.1. Interfacing Mercury-switch Module With The Ardui-
no
As discussed earlier, when we will tilt the sensor, the sensor will produce high
level signal and if we are using an Arduino then to read the output of the
sensor, we will have to use digitalRead function.
In the following code you can see we are using digitalRead function and stor-
ing the output of the sensor to the variable.
As we are printing the output of the sensor on the serial monitor, we will have
to open the serial monitor to see the output.
20
Chapter 00 01 76
20.3 Arduino Code for Mercury Switch Module
void setup()
{
pinMode(led_pin, OUTPUT);
pinMode(switch_pin, INPUT);
}
void loop()
{
val = digitalRead(switch_pin); // check mercury switch state
if(val == HIGH)
{
digitalWrite(led_pin, HIGH);
}
else
{
digitalWrite(led_pin, LOW);
}
}
20
Chapter 00 01 77
21. Magic Cup Module
The Working principle of the magic cup module is similar to the Mercury
switch module. The only difference is, you get Two LEDs on this module.
Those LEDs are used for indication purpose. When you will tilt the sensor, the
brightness of one LED will start increasing and the brightness of other led start
decreasing.
So, this was the basic introduction of the Magic cup module with the Arduino.
In the next section of this blog we will understand the interfacing of the magic
cup module.
As discussed earlier, we will get the sensor output on the signal pin. In the fol-
lowing code, we have used a variable to store the output of the sensor.
In the code below, we are continuously checking the output of the sensor and
once the change in the state of the variable is occurring, we are tuning on the
LED.
21
Chapter 00 01 78
21.2. Arduino Code For Magic Cup Module
As we have connected the magic cup module to the Arduino, now we can write
our code for getting the output from the module.
As discussed earlier, we will get the sensor output on the signal pin. In the fol-
lowing code, we have used a variable to store the output of the sensor.
In the code below, we are continuously checking the output of the sensor and
once the change in the state of the variable is occurring, we are tuning on the
LED.
Please take a look at the following code and if you have any doubts please let
us know.
int LedPinA = 5;
int LedPinB = 6;
int ButtonPinA = 7;
int ButtonPinB = 4;
int buttonStateA = 0;
int buttonStateB = 0;
int brightness = 0;
void setup ()
{
pinMode (LedPinA, OUTPUT);
pinMode (LedPinB, OUTPUT);
pinMode (ButtonPinA, INPUT);
pinMode (ButtonPinB, INPUT);
}
void loop ()
{
buttonStateA = digitalRead (ButtonPinA);
if (buttonStateA == HIGH&&brightness!= 255)
{
brightness ++ ;
}
buttonStateB = digitalRead (ButtonPinB);
if (buttonStateB == HIGH&&brightness!= 0)
{
brightness -- ;
}
analogWrite (LedPinA, brightness); // A few Guan Yuan (ii) ?
analogWrite (LedPinB, 255 - brightness);// B Yuan (ii) a few Bang ?
delay (5); // number can be changed. larger number = longer light swap.
}
21
Chapter 00 01 79
22. Microphone Sound Sensor
We are getting two types of sound sensors with this kit.
The working of these sound sensors is same. In the following section we will
understand the working of the sound sensor.
Sound sensor are designed for converting analog signals to digital signals.
They convert any sound signals into digital signals.
We can see these sensors in everywhere. These sensors act as an ear of the
electronic circuits. But do you know how they work?
When the sound frequency hit on this sensor, the vibrations will be produced
on the surface of the diaphragm and then based on the intensity of the sound
signals that diaphragm produces the voltage.
This how the sound sensor converts the sound frequency into electrical sig-
nals.
The sound sensor has three pins and we get the output of the sensor on the
signal pin of the module.
We have connected the signal pin of the module to the seven number GPIO
pin of the Arduino.
Chapter 00
22 01 80
22.1. Interfacing of the sound sensor with the Arduino
void setup ()
{
pinMode (Led, OUTPUT) ;// define LED as output interface
pinMode (buttonpin, INPUT) ;// output interface D0 is de-
fined sensor
}
void loop ()
{
val = digitalRead(buttonpin);// digital interface will be as-
signed a value of pin 3 to read val
if (val == HIGH) // When the sound detection module de-
tects a signal, LED flashes
{
digitalWrite (Led, HIGH);
}
else
{
digitalWrite (Led, LOW);
}
}
Chapter 00
22 01 81
22.2. Arduino Code For Analog Microphone Sensor
int sensorPin = A0; // select the input pin for the poten-
tiometer
int ledPin = 13; // select the pin for the LED
int sensorValue = 0; // variable to store the value
coming f rom the sensor
void setup ()
{
pinMode (ledPin, OUTPUT);
[Link] (9600);
}
void loop ()
{
sensorValue = analogRead (sensorPin);
digitalWrite (ledPin, HIGH);
delay (sensorValue);
digitalWrite (ledPin, LOW);
delay (sensorValue);
[Link] (sensorValue, DEC);
}
Chapter 00
22 01 82
23. Broken LED Module/ Photocoupler
We are getting this very useful module with this kit. This type of switches is
mostly used in 3D printing machines and the applications where electronic
interference issues are biggest concern.
Talking about the optical emitter, it is nothing but LED which continuously
emits the light and the optical receiver which is placed just in front of the opti-
cal emitter, the keep receiving the light that is being transmitted by the optical
emitter.
The optical receiver is nothing but a photodiode. When the light rays fall on the
photodiode the barrier gap between PN junction of the photodiode starts de-
creasing and at one point that barrier gap becomes zero and the photodiode
acts as a switch.
In this way, until the light is falling on the photodiode the photodiode will be
operating as a closed switch and when the light source is gone then it acts as
an open switch.
Chapter 00
23 01 83
23.1. Interfacing of the Broken LED Module With the LED
In the following image I have shown the interfacing of the LED with the Ardui-
no. The Broken LED module has three pins. Out of those three pins S is the
signal pin. I have connected that pin to the seven number pin of the Arduino.
In this section we are discussing about the code for the broken light LED. As we
have discussed earlier the broken light LED Module either produces HIGH volt-
age or LOW voltage.
So, to read the output of the sensor we will have to use the digitalRead func-
tion. In the code below we have used digitalRead function and we are printing
the output of the sensor on the serial monitor.
You can see the output of the sensor by opening serial monitor.
Initially when there will be no obstacle between the optical emitter and optical
receiver, we will see ‘Switch is on signal’ on the screen but when we put obsta-
cle between the optical emitter and optical receiver then we will see ‘switch is
off’ on the screen.
Chapter 00
23 01 84
23.2. Arduino Code For Broken LED Module
void setup()
{
pinMode(Led, OUTPUT); // LED pin as output
pinMode(buttonpin, INPUT); //photo interrupter pin
as input
}
void loop()
{
val=digitalRead(buttonpin); //read the value of the
sensor
if(val == HIGH) // turn on LED when sensor is blocked
{
digitalWrite(Led,HIGH);
}
else
{
digitalWrite(Led,LOW);
}
}
Chapter 00
23 01 85
24. Rotary Encode/ Potentiometer
We are getting this very useful module with this kit. This type of switches is
mostly used in 3D printing machines and the applications where electronic
interference issues are biggest concern.
Talking about the optical emitter, it is nothing but LED which continuously
emits the light and the optical receiver which is placed just in front of the opti-
cal emitter, the keep receiving the light that is being transmitted by the optical
emitter.
The optical receiver is nothing but a photodiode. When the light rays fall on the
photodiode the barrier gap between PN junction of the photodiode starts de-
creasing and at one point that barrier gap becomes zero and the photodiode
acts as a switch.
In this way, until the light is falling on the photodiode the photodiode will be
operating as a closed switch and when the light source is gone then it acts as
an open switch.
24
Chapter 00 01 86
24.1. Interfacing The rotary Encoder with the Arduino
In this section, we are understanding the interfacing of the rotary encoder with
the Arduino.
The rotary encoder has three pins. Output, vcc and GND.
The output pin of the rotary encoder we can a to the any analog pin of the Ar-
duino.
The output of the sensor is connected to the analog pin and output type of the
sensor is analog so we will have to use analogRead function to read the output
of the sensor.
Please refer to the following code to work with the rotary encoder.
Chapter 00
24 01 87
Thank You...!
A beginner at electronics systems comes across this common problem “How should I
step into learning embedded systems? Which components should I buy? This is why
Robu has designed the kits which will assist you to understand the embedded system
from zero to hero level. This will solve the dilemma while selecting the proper products
for your need.
Moreover, free e booklets and Video tutorials that are being shared with these kits have
everything in it that's being taught within the paid lectures. That means you'll master
those technical skills without paying an enormous amount to training institutes. So pre-
pare to be the master of your dreams and start gaining the knowledge which is effective
to you!