Professional Documents
Culture Documents
Fund. of IoT LAB - CO 252
Fund. of IoT LAB - CO 252
PRUDHVI KIRAN P
Assistant Professor, CSE - IoT Dept.
R. V. R. & J. C. College of Engineering
TASK 1
1. Introduction of Arduino IDE
2. Write an arduino program to demonstrate setup () and loop () functions
3. Write an arduino program to demonstrate serial and serial.begin() statements
4. Write an arduino program to demonstrate serial.print() statement
5. Write an arduino program to demonstrate serial.available() statement
6. Write an arduino program to demonstrate serial.read() and serial.write() statements
7. Write an arduino program to demonstrate user defined functions
1. Introduction of Arduino IDE
The Arduino Integrated Development Environment (IDE) or Arduino Software, contains a text
editor for writing code, message area, text console and toolbar with buttons for common
functions and a series of menus. This IDE connects to the Arduino board and uploads program
and communicate with it. This IDE is developed/written in Java and Arduino program we write
in this IDE is based on C++.
Arduino IDE 2.0.x is latest version as of today (September 2023) and Arduino IDE 1.8.x is most
used and discussed further. As open source, all versions source code is hosted on GitHub.
Downloading the Arduino IDE 2.0 is done through the Arduino Software page at
www.arduino.cc/en/software.
Sketch
A sketch is a program written in the Arduino IDE. Sketches can be saved on the development
computer as text files with the file extension .ino (Versions of the Arduino Software (IDE) prior to
1.0 saved sketches with the extension .pde.)
The Arduino Software (IDE) uses the concept of a sketchbook: a standard place to store your
programs (or sketches). The sketches in your sketchbook can be opened from the File >
Sketchbook at menu bar or from the Open button on the toolbar.
FILE NAME | IDE VERSION
MENU BAR
TOOL BAR SERIAL MONITOR
SKETCH FILE
SKETCH TAB MENU
TAB
Upload to Open
Board
SKETCH EDITOR
1 - FILE
2 - EDIT
//Sample Sketch
String b; Serial.print("Bytes Received: ");
int a; Serial.println(b);
void setup( ) a = Serial.available();
{ Serial.print("Bytes in Serial Receive Buffer: ");
Serial.begin(4800); Serial.println(a);
} }
void loop( ) } Observed Output
{ (In Serial Monitor)
while( Serial.available( ) ) String “hai RVRJCCE” is entered in
serial monitor and sent to board
{
a = Serial.available();
Serial.print("Bytes in Serial Receive Buffer: ");
Serial.println(a);
b = Serial.readString( );
Serial.write()
Arduino Serial Write is used to write some data on the Serial Port. Serial.write sends bytes to the
serial port while Serial.print sends ASCII characters so people can read easily.
//Sample Code
void setup()
{
Serial.begin(9600);
}
void loop()
{
char data = 0;
Observed Output
if(Serial.available()>0)
(In Serial Monitor)
{
“1” is entered in serial monitor and sent to
data = Serial.read(); board
Serial.print("Data Received is : ");
Serial.write(data);
}
}
7. Write an arduino program to demonstrate user defined functions
User Defined Functions
There are two required functions in an Arduino sketch or a program i.e. setup () and loop(). Other
functions must be created outside the brackets of these two functions. A function is declared outside
any other functions, above or below the loop function.
//Sample Sketch
int sum_func (int x, int y) void loop ()
{ {
int z = 0; }
z = x+y ;
return z; Observed Output
} (In Serial Monitor)
void setup ()
{
Serial.begin(9600);
int result = 0;
result = sum_func(5,6);
Serial.println(result);}
TASK 2
8. Write an arduino program to demonstrate data types
9. Write an arduino program to demonstrate variables
10. Write an arduino program to demonstrate constants
11. Write an arduino program to demonstrate operators
8. Write an arduino program to demonstrate data types
Arduino Data Types play an important role in Arduino Programming. Data Types are different on
the nature of element storing in them.
Data type is like a place, so when we initialize our variable then we tell our compiler that our newly
introduced integer is of which type.
Is it an integer or it has decimal as well in it or its some character like A, B, C etc.
Arduino Data Types are almost similar to C++ Data Types because it roughly follows the same
syntax.
The following table provides all the data types that you will use during Arduino programming;
9. Write an arduino program to demonstrate variables
A variable is a place to store a piece of data. It has a name, a value, and a type. Variable will have a
property called scope, which refers to Local or Global.
Variables declared inside a function (setup()/loop()/other user defined functions), are called local
variables.
Variables declared outside of all function are called global variables. A global variable can be
accessed by any function. unlike local, which are not known to function outside their own.
//Sample Sketch
int z; Global Variable
Void setup () {
}
Void loop ()
{
int x,y;
x = 0; Local Variables
y = 0; actual initialization
z = 10;
}
10. Write an arduino program to demonstrate constants
The constants in Arduino are defined as the predefined expressions. It makes the code easy to
read.
The name const represents the constant keyword. It further makes the variable as 'read-only'. The
variable will function the same as other variables, but its value cannot be changed, it remains
constant.
//Sample Sketch
const float x = 5.68; Declared as
constant/read-only, and it
float y;
can’t be changed.
void setup()
Observed Output
{
(text/error console)
Serial.begin(9600);
Constant can be used like
y = x * 2; other variables.
Serial.println(y);
x=4.5; Attempted to change the
value of constant, this will
} result in error, saying that,
void loop() it’s a read only variable.
{
}
Logical level Constants
The logical level constants are true or false. The value of true and false are defined as 1 and 0. Any
non-zero integer is determined as true in terms of Boolean language. The true and false constants
are type in lowercase rather than uppercase.
Pin level Constants
The digital pins can take two value HIGH or LOW. In Arduino, the pin is configured as INPUT or
OUTPUT using the pinMode() function. The pin is further made HIGH or LOW using the
digitalWrite() function.
LED_BUILTIN Constant
The Arduino boards have built-in LED connected in series with the resistor. The particular pin
number is defined with the constant name called LED_BUILTIN. Most Arduino boards have the
LED_BUILTIN connected to Pin number 13.
#define
Allows the programmer to give a name to a constant value before the program is compiled. The
compiler will replace references to these constants with the defined value at compile time.
Syntax: #define nameOFconstant value (Example: #define LEDpin 12)
11. Write an arduino program to demonstrate operators
The operators are widely used in Arduino programming from basics to advanced levels. It plays a
crucial role in every programming concept like C, C++, Java, etc. The operators are used to solve
logical and mathematical problems. For example, to calculate the temperature given by the
sensor based on some analog voltage.
The types of Operators classified in Arduino are:
1. Arithmetic Operators
2. Compound Operators
3. Boolean Operators
4. Comparison Operators
5. Bitwise Operators
Arithmetic Operators
Assignment ‘=‘ (Example - a = b)
Addition '+’ (Example - a = b + 2017)
Subtraction '-’ (Example - b = 2017 - a)
Multiplication '*’ (Example - d = 2.5 * e)
Division '/’ (Example - e = d / 2.5)
25
Remainder '%’ (Example - f = d % 2.5)
Campound Operators
b + + (Here, b = b + 1. It is called the increment operator.)
b + = (For example, b + = 4. It means, b = b+ 4.)
b-- (Here, b = b - 1. It is called as the decrement operator.)
b-= (For example, b - = 3. It means, b = b - 3.)
b * = (For example, b * = 6. It means, b = b * 6.)
b / = (For example, b / = 5. It means, b = b / 5.)
b % = (For example, b % = 2. It means, b = b % 2.)
Now, let's use the above operators with two variables, b and c.
b + = c ( that represents b = b + c)
b - = c ( that represents b = b - c)
b * = c ( that represents b = b * c)
b / = c ( that represents b = b / c)
b % = c ( that represents b = b % c)
Boolean Operators
1. Logical AND ( & & )
The result of the condition is true if both the operands in the condition are true. Consider the
example: if ( a = = b & & b = = c ) - this is true if both conditions are true. If any of the conditions is
false, the statement will be false.
2. Logical OR ( | | )
The result of the condition is true, if either of the variables in the condition is true. Consider the
below example.
if ( a > 0 | | b > 0 )
The above statement is true, if either of the above condition ( a> 0 or b > 0 ) is true.
3. NOT ( ! )
It is used to reverse the logical state of the operand. For example, a ! = 2. The NOT operator returns
the value 1 or TRUE when the specified operand is FALSE. It also reverses the value of the expression.
Comparision Operators
1. less than ( < )
The less than operator checks that the value of the left operand is less than the right operand. The
statement is true if the condition is satisfied.
2. greater than ( > )
The less than operator checks that the value of the left side of a statement is greater than the right
side. The statement is true if the condition is satisfied. For example, a > b. If a is greater than b, the
condition is true, else false.
3. equal to ( = = )
It checks the value of two operands. If the values are equal, the condition is satisfied. For
example, a = = b. The above statement is used to check if the value of a is equal to b or not.
4. not equal to ( ! = )
It checks the value of two specified variables. If the values are not equal, the condition will
be correct and satisfied. For example, a ! = b.
5. less than or equal to ( < = )
The less or equal than operator checks that the value of left side of a statement is less or
equal to the value on right side. The statement is true if either of the condition is satisfied.
For example, a < = b. It checks the value of a is less or equal than b.
6. greater than or equal to ( > = )
The greater or equal than operator checks that the value of the left side of a statement is
greater or equal to the value on the right side of that statement. The statement is true if the
condition is satisfied. For example, a > = b. It checks the value of a is greater or equal than b.
If either of the condition satisfies, the statement is true.
Bitwise Operators
The Bitwise operators operate at the binary level. These operators are quite easy to use. There are
various bitwise operators. Some of the popular operators are listed below:
1. bitwise NOT ( ~ )
It reverses the input bits.
Example: INPUT - 0 0 1 1 (decimal value 3)
OUTPUT - 1 1 0 0 (decimal value is 12)
TASK 3
12. Write an arduino program to demonstrate if statements
13. Write an arduino program to demonstrate switch case
14. Write an arduino program to demonstrate loops
15. Write an arduino program to demonstrate arrays
12. Write an arduino program to demonstrate if statements
Control statements are the statements that change the flow of execution of statements. Control
statements are used when the execution of the code is controlled by some conditional statements.
These control statements can be implemented by different statements like if statements, if-else
statements, and switch-case statements.
if statement
The “if statement” is the simplest form of the control statement in which the
statements are written in the body of “if statement”, only if the condition is
true then statements in the body will be executed else the compiler executes
the next statements.
//Sample Sketch
int a = 6;
int b = 4;
void setup()
{
Serial.begin(9600);
}
void loop()
{
if (a > b )
{
Serial.println( " a is greater than b ");
}
if (b > a )
{
Serial.println( " b is greater than a ");
}
}
Observed Output (In Serial Monitor)
if-else statement
The “if-else statements” is another type of control statement and the
advanced form of “if statements”, works as “either-or” like if one
scenario is false it will execute something else. Mostly, it is used in
controlling the operation by using a check on it. For example, if the
temperature of a room is below 30 degrees, turn on the green LED
which means the temperature is normal, or turn on the red LED which
means the temperature is above 30 degrees.
Sample Sketch
int a = 5;
int b= 6;
void setup ( )
{
Serial.begin ( 9600 );
}
void loop ( )
{
if ( a > b )
{
Serial.println ( " a is greater " );
}
else
{
Serial.println ( " a is smaller " );
}
}
Flag Shape
Observed Output
LED connected to PIN 3 is switched on for one second;
Switched off for one second; is repeated in loop, till the board is connected to power.
//Sample Sketch - LED Blink without delay()
unsigned long previousMillis = 0;
int ledState = LOW;
long interval = 1000;
void setup() {
Serial.begin(9600);
pinMode(3, OUTPUT);}
void loop() {
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
Serial.println(previousMillis);
Serial.println(currentMillis);
previousMillis = currentMillis;
if(ledState == LOW)
ledState = HIGH;
else
ledState = LOW;
digitalWrite(3, ledState);
}}
Observed Outputs (In Serial Monitor)
23. Write an arduino program to demonstrate the 7-segment display
COMMON
7-segment displays are made up of 8 LED segments. They are used to display
Numbers (0-9) and certain Alphabets, with 7 segments and 1 segment is
exclusive for decimal point (DP). Each of the seven LEDs is called a segment
because when illuminated the segment forms part of a numerical digit to be
displayed.
These individually LED pins (7 Pins) are labelled from A through to G
representing each individual LED. One Pin is for DP and 2 Pins (one in top
and one in bottom) are connected together and wired to form a common pin
(Common Cathode/Anode). Totally 10 pins are seen on 7-segments LED.
There are two types of LED 7-segment displays: common cathode (CC) and
common anode (CA). The difference between the two displays is the
common cathode has all the cathodes of the 7-segments connected directly
together and the common anode has all the anodes of the 7-segments
connected together.
For Common Cathode, A to G pins are connected to 7 GPIO pins and DP can
be connected to another GPIO. As this is a common cathode, one of the two
available common pin should be connected to Ground Pin. COMMON
For Common Anode, A to G pins are connected to 7 GPIO pins and DP can be connected to another
GPIO. As this is a common anode, one of the two available common pin should be connected to
voltage Pin.
For both common anode and common cathode, as these segments works like normal LED, we have
to connect 8 individual resistors (can use 220 ohms resistor) for all the pins (A to G & DP)).
COMMON
COMMON
UNDERSTANDING PROGRAMMING OF A 7 SEGMENT LED
Program structure is same as LED program, based on the number/character we want to get
displayed, we will instruct the segments to on/off. For example if we want to display value 2, then
we will send the instructions to light up (HIGH) A,B,G,E,D segments and off (LOW) remaining.
Writing Arduino sketch for Common Cathode 7 segment display is as common as a normal LED
sketch. In case of using common anode module, in digitalWrite(), all 0’s should be mentioned as 1
and all 1’s should be mentioned as o.
Circuit Diagram Arduino UNO - 7 Segment - COMMON CATHODE
//Sketch - 7 Segment - COMMON CATHODE
int a= 2; //2 //4
void loop() {
int b= 3; digitalWrite(a,1); digitalWrite(a,0);
int c= 4;
digitalWrite(a,1);
digitalWrite(b,1); digitalWrite(b,1); digitalWrite(b,1);
int d= 5;
int e= 6; digitalWrite(c,1); digitalWrite(c,0); digitalWrite(c,1);
int f= 7; digitalWrite(d,1); digitalWrite(d,1); digitalWrite(d,0);
int g= 8; digitalWrite(e,1); digitalWrite(e,1); digitalWrite(e,0);
digitalWrite(f,1); digitalWrite(f,0); digitalWrite(f,1);
int t = 500; digitalWrite(g,0); digitalWrite(g,1); digitalWrite(g,1);
delay(t); delay(t); delay(t);
void setup() //1 //3 //5
{ digitalWrite(a,1); digitalWrite(a,1);
digitalWrite(a,0);
pinMode(a, OUTPUT); digitalWrite(b,1); digitalWrite(b,0);
pinMode(b, OUTPUT);
digitalWrite(b,1);
digitalWrite(c,1); digitalWrite(c,1); digitalWrite(c,1);
pinMode(c, OUTPUT);
pinMode(d, OUTPUT); digitalWrite(d,0); digitalWrite(d,1); digitalWrite(d,1);
pinMode(e, OUTPUT); digitalWrite(e,0); digitalWrite(e,0); digitalWrite(e,0);
pinMode(f, OUTPUT); digitalWrite(f,0); digitalWrite(f,0); digitalWrite(f,1);
pinMode(g, OUTPUT); digitalWrite(g,0); digitalWrite(g,1); digitalWrite(g,1);
} delay(t); delay(t); delay(t);
//6 //8
digitalWrite(a,1); digitalWrite(a,1);
digitalWrite(b,0); digitalWrite(b,1);
digitalWrite(c,1); digitalWrite(c,1);
digitalWrite(d,1); digitalWrite(d,1);
digitalWrite(e,1); digitalWrite(e,1);
digitalWrite(f,1); digitalWrite(f,1);
digitalWrite(g,1); digitalWrite(g,1);
delay(t); delay(t);
//7 //9
digitalWrite(a,1); digitalWrite(a,1);
digitalWrite(b,1); digitalWrite(b,1);
digitalWrite(c,1); digitalWrite(c,1);
digitalWrite(d,0); digitalWrite(d,1);
digitalWrite(e,0); digitalWrite(e,0);
digitalWrite(f,0); digitalWrite(f,1);
digitalWrite(g,0); digitalWrite(g,1); Observed Output
delay(t); delay(t);
} 0-9 Numbers Displayed in 7 Segment LED
Circuit Diagram Arduino UNO - 7 Segment - COMMON ANODE
//Sketch - 7 Segment - COMMON ANODE
int a= 2;
void loop() { //2 //4
int b= 3;
digitalWrite(a,0); digitalWrite(a,0); digitalWrite(a,1);
int c= 4;
int d= 5; digitalWrite(b,0); digitalWrite(b,0); digitalWrite(b,0);
int e= 6; digitalWrite(c,0); digitalWrite(c,1); digitalWrite(c,0);
int f= 7; digitalWrite(d,0); digitalWrite(d,0); digitalWrite(d,1);
int g= 8; digitalWrite(e,0); digitalWrite(e,0); digitalWrite(e,1);
digitalWrite(f,0); digitalWrite(f,1); digitalWrite(f,0);
int t = 500; digitalWrite(g,1); digitalWrite(g,0); digitalWrite(g,0);
delay(t); delay(t); delay(t);
void setup() //1 //3 //5
{ digitalWrite(a,0);
digitalWrite(a,1); digitalWrite(a,0);
pinMode(a, OUTPUT);
digitalWrite(b,0); digitalWrite(b,0); digitalWrite(b,1);
pinMode(b, OUTPUT);
pinMode(c, OUTPUT); digitalWrite(c,0); digitalWrite(c,0); digitalWrite(c,0);
pinMode(d, OUTPUT); digitalWrite(d,1); digitalWrite(d,0); digitalWrite(d,0);
pinMode(e, OUTPUT); digitalWrite(e,1); digitalWrite(e,1); digitalWrite(e,1);
pinMode(f, OUTPUT); digitalWrite(f,1); digitalWrite(f,1); digitalWrite(f,0);
pinMode(g, OUTPUT); digitalWrite(g,1); digitalWrite(g,0); digitalWrite(g,0);
} delay(t); delay(t); delay(t);
//6 //8
digitalWrite(a,0); digitalWrite(a,0);
digitalWrite(b,1); digitalWrite(b,0);
digitalWrite(c,0); digitalWrite(c,0);
digitalWrite(d,0); digitalWrite(d,0);
digitalWrite(e,0); digitalWrite(e,0);
digitalWrite(f,0); digitalWrite(f,0);
digitalWrite(g,0); digitalWrite(g,0);
delay(t); delay(t);
//7 //9
digitalWrite(a,0); digitalWrite(a,0);
digitalWrite(b,0); digitalWrite(b,0);
digitalWrite(c,0); digitalWrite(c,0);
digitalWrite(d,1); digitalWrite(d,0);
digitalWrite(e,1); digitalWrite(e,1);
digitalWrite(f,1); digitalWrite(f,0);
digitalWrite(g,1); digitalWrite(g,0); Observed Output
delay(t); delay(t);
} 0-9 Numbers Displayed in T Segment LED
24. Write an arduino program to demonstrate button
Circuit Diagram & Sketch Arduino UNO - Button
//Sketch - Button
#define BUTTON_PIN 4
void setup()
{
Serial.begin(9600);
pinMode(BUTTON_PIN, INPUT);
}
void loop()
{
Serial.println(digitalRead(BUTTON_PIN))
;
delay(100);}
Observed Outputs (In Serial Monitor)
Uneven values are printed. Sometimes 1
and sometimes 0, without the
dependency on button press, which are
called as floating values.
A. Understanding INPUT_PULLUP
In pinMode(), along with INPUT & OUTPUT, we have third option INPUT_PULLUP. This option is the
same as INPUT (you read data from the sensor), but in addition to that, an internal pull up resistor -
between 20k and 50k Ohm - is enabled, to keep the signal HIGH by default.
When we press the button, the value is always LOW, but when we release it it’s quite random:
sometimes HIGH, sometimes LOW, and it moves a lot. We see this because the voltage for the
button is floating between 0 and 5V.
If the voltage is below a certain amount of V, the Arduino will read LOW. And if it is above a certain
amount of V, the Arduino will read HIGH. As there is no internal or external voltage reference for the
push button, the value will oscillate a lot in a random way.
What we need to do is to “force” the default state (button not pushed) to be close to HIGH or LOW,
which will make it quite stable. Then, when we press the button the state will simply go to the
opposite of the default state.
When you set the mode to INPUT_PULLUP, an internal resistor - inside the Arduino board - will be
set between the digital pin 4 and VCC (5V). This resistor - value estimated between 20k and 50k Ohm
- will make sure the state stays HIGH. When you press the button, the states becomes LOW.
Circuit Diagram Arduino UNO - Button with INPUT_PULLUP
//Sketch - Button with INPUT_PULLUP
#define BUTTON_PIN 4
void setup()
{
Serial.begin(9600);
pinMode(BUTTON_PIN,
INPUT_PULLUP);
}
void loop()
{
Serial.println(digitalRead(BUTTON_PIN))
;
Observed Outputs (In Serial Monitor)
delay(100);}
0 LED ON
1 LED OFF
25. Write an arduino program to demonstrate switch
A toggle switch is simply a push button switch with memory. The difference between a toggle
switch and a push button switch is that the toggle switch can remain either high or low, whereas a
push button input is momentarily connected when pushed. A spring pushes the button back when
you release it. Arduino sketch remains same for button and toggle switch and even INPUT_PULLUP
can also be used as same in button.
//Sketch - Toggle Switch
#define TOGGLESWITCH 4
void setup()
{
Serial.begin(9600);
pinMode(TOGGLESWITCH,
INPUT_PULLUP);
}
void loop()
{
Serial.println(digitalRead(TOGGLESWIT
CH));
Circuit Diagram Arduino UNO - Toggle Switch
0 Switch Position ON
1 Switch Position CENTER
Circuit Diagram Arduino UNO - Toggle Switch as Two Buttons
//Sketch - Toggle Switch as Two Buttons
#define TOGGLELEFT 4
#define TOGGLERIGHT 5
#define LEDRED 9
#define LEDGREEN 13
void setup()
{
pinMode(TOGGLELEFT, INPUT_PULLUP);
pinMode(LEDRED, OUTPUT);
pinMode(TOGGLERIGHT, INPUT_PULLUP);
pinMode(LEDGREEN, OUTPUT);
}
void loop()
{
//for toggle switch left direction
if(digitalRead(TOGGLELEFT)==0)
digitalWrite(LEDRED, HIGH);
else
digitalWrite(LEDRED, LOW);
//for toggle switch right direction
if(digitalRead(TOGGLERIGHT)==0)
digitalWrite(LEDGREEN, HIGH);
else
digitalWrite(LEDGREEN, LOW);
}
PINOUT OF
LM35
TEMPERATURE SENSOR
Circuit Diagram Arduino UNO - Interfacing LM 35 Sensor
//Sketch - Interfacing LM 35 Sensor
const int lm35_pin = A1; /* LM35 O/P pin */
void setup()
{
Serial.begin(9600);
}
void loop()
{
int temp_adc_val;
float temp_val;
temp_adc_val = analogRead(lm35_pin); /* Read raw value from sensor */
temp_val = (temp_adc_val * 4.88); /* Convert raw value to equivalent voltage */
temp_val = (temp_val/10); /* Convert voltage to temperature @ 10mv/°C */
Serial.print("Temperature = ");
Serial.print(temp_val);
Serial.print(" Degree Celsius\n");
delay(1000);
}
Observed Output (In Serial Monitor)
32. Write an arduino program for interfacing with PIR sensor.
The term PIR is the short form of the Passive
InfraRed. The term “passive "indicates that
the sensor does not actively take part in the
process, which means, it does not emit the
referred IR signals itself, rather passively
detects the infrared radiations coming from
the human body in the surrounding area.
The detected radiations are converted into
an electrical charge, which is proportional to
the detected level of the radiation. Then this Fresnel Lense
charge is further improved by a built in FET
(field-effect transistor (FET) is a type of
transistor commonly used for weak-signal
amplification) and the amplified signal is
further fed to the output pin of the device
which is connected to an external circuit for
further triggering of operations.
Understanding PIR Sensor Calibration
Sensitivity Adjustment
• The knob on the right controls
sensitivity. Turn it clockwise to
decrease sensitivity. Decreased
sensitivity means that the device will
trigger (i.e. report movement) only
when said movement is prolonged.
• Sensitivity can be adjusted over a
range of approximately 3 meters to 7
meters (9 to 21 feet). However the
topology of your room can affect the
actual range you get.
Time Delay Adjustment
• The knob on the left sets how long
the output will remain HIGH after
motion is detected. It can be
adjusted from 5 seconds to about 5
minutes.
Working of PIR Sensor
The passive infrared (PIR) sensor itself has two
slots (two pyroelectric sensors) in it, each slot
is made of a special material that is sensitive to
IR. The lens used here is not really doing much
and so we see that the two slots can 'see' out
past some distance (basically the sensitivity of
the sensor).
When the sensor is idle, both slots detect the
same amount of IR, the ambient amount
radiated from the room or walls or outdoors.
When a warm body like a human or animal
passes by, it first intercepts one half of the PIR
sensor, which causes a positive differential
change between the two halves. When the
warm body leaves the sensing area, the
reverse happens, whereby the sensor
generates a negative differential change. These
change pulses are what is detected.
The IR sensor itself is housed in a thermetically sealed metal can to improve
noise/temperature/humidity immunity. There is a window made of IR-transmissive material
(typically coated silicon since that is very easy to come by) that protects the sensing element. Behind
the window are the two balanced sensors.
The sensor module also consists of a specifically designed cover called Fresnel lens that helps focus
the infrared signals onto the pyroelectric sensor.
However, remember that we actually have two sensors, and more importantly we don't want two
really big sensing-area rectangles, but rather a scattering of multiple small areas. So what we do is
split up the lens into multiple section, each section of which is a Fresnel lens.
FRESNEL LENSE FRONT AND REAR VIEW
Circuit Diagram Arduino UNO - Interfacing PIR Sensor
//Sketch - Interfacing PIR Sensor
const int PIR_SENSOR_OUTPUT_PIN = 4; /* PIR sensor O/P pin */
int warm_up;
void setup() {
pinMode(PIR_SENSOR_OUTPUT_PIN, INPUT);
Serial.begin(9600);
delay(20000); /* Power On Warm Up Delay */
}
void loop() {
int sensor_output;
sensor_output = digitalRead(PIR_SENSOR_OUTPUT_PIN);
if( sensor_output == LOW )
{
if( warm_up == 1 )
{
Serial.print("Warming Up\n\n");
warm_up = 0;
delay(2000);}
Serial.print("No object in sight\n\n");
delay(1000);}
else{
Serial.print("Object detected\n\n");
warm_up = 1;
delay(1000);}}
Observed Output (In Serial Monitor)
TASK 11
33. Write an arduino program for interfacing with infrared sensor.
34. Write an arduino program for interfacing with ultrasonic sensor.
35. Write an arduino program for interfacing with accelerometer
33. Write anAarduino sketch for interfacing with Infrared Sensor
An infrared (IR) sensor is a proximity sensor, or a ‘nearness’ sensor that senses whether there is
an object near it or not. It is also used to measure distance. Note that, Infrared is the light out of
our visible spectrum, so we can not see it.
The white LED here is an IR LED which works as the transmitter and the component next to the
IR LED is a photodiode that works as the receiver in the IR sensor.
The IR transmitter continuously emits the IR light and the IR receiver keeps on checking for the
reflected light. If the light gets reflected back by hitting any object in front it, the IR receiver
receives this light. This way the object is detected in the case of the IR sensor.
The blue knob is a potentiometer. You can control the range i.e. from how far you want to
detect the object by changing the value of the potentiometer.
An IR sensor has two small LED indicators - one for Power, which is ON the entire time the
sensor is ON; the other is the Signal LED which detects the object. The signal LED has two states
or situations: ON (Active) when it detects an object and OFF (Inactive) when it doesn’t detect any
object
They are widely used in various applications such as remote control devices, security systems,
obstacle detection, and industrial automation.
Understanding Pinout & Components of IR Sensor
NOTE
There are two types of IR sensors are
available and they are, Active
Infrared Sensor & Passive Infrared
Sensor. Active infrared sensors
consist of two elements: infrared
source and infrared detector. Passive
infrared sensors are basically
Infrared detectors only.
Object Detection using IR Sensor
When the IR transmitter emits radiation, it reaches the object and some of the radiation reflects
back to the IR receiver. Based on the intensity of the reception by the IR receiver, the output of the
sensor is defined.
Circuit Diagram Arduino UNO - Interfacing Infrared Sensor (Object Detection)
//Sketch - Interfacing IR Sensor (Object Detection)
int IRSensor = 9; // connect IR sensor module to Arduino pin D9
int LED = 13; // connect LED to Arduino pin 13
void setup(){
Serial.begin(115200); // Init Serial at 115200 Baud Rate.
Serial.println("Serial Working"); // Test to check if serial is working or not
pinMode(IRSensor, INPUT); // IR Sensor pin INPUT
pinMode(LED, OUTPUT); // LED Pin Output
}
void loop(){
int sensorStatus = digitalRead(IRSensor); // Set the GPIO as Input
if (sensorStatus == 1) // Check if the pin high or not
{
// if the pin is high turn off the onboard Led
digitalWrite(LED, LOW); // LED LOW
Serial.println(“Object Detected!");
}
else {
//else turn on the onboard LED
digitalWrite(LED, HIGH); // LED High
Serial.println(“No Object");
}
}
Observed Output