You are on page 1of 38

Basic Electronics and Electrical

Engineering: ECE 249


UNIT IV: Introduction of
Arduino and Sensors
Lecture No.: 28,29
Topic: Blinking of LED,
Ultrasonic sensor basics

Delivered By: Dr. Irfan Ahmad Pindoo


Head, Patent and Design Cell
Division of Research and Development
Intellectual Property Rights Cell
Blinking an LED
 Arduino is composed of two major parts: the Arduino board, which is the
piece of hardware you work on when you build your objects; and the Arduino
Integrated Development Environment, or IDE, the piece of software you run
on your computer.
 You use the IDE to create a sketch (a little computer program) that you
upload to the Arduino board. The sketch tells the board what to do.

Prepared By: Irfan Ahmad Pindoo 2


Blinking an LED: Program

Prepared By: Irfan Ahmad Pindoo 3


Blinking an LED: Program
 Function digitalWrite() is used to instruct the controller/Arduino to set the
state of a particular digital pin as LOW or HIGH. In the digitalWrite() function
we also pass two arguments that are one is pin number and second is state
digitalWrite(pinNumber, state). We are setting pin number 13 as high.

 And the second function is delay() which is used to provide a delay to the
upper instruction so that the next instruction will wait till the delay time gets
completed. In delay, we pass one parameter that is time to be delayed in
milliseconds. Here we want to delay the pin number high state for 1000
milliseconds or 1 second.

Prepared By: Irfan Ahmad Pindoo 4


Blinking an LED: Simulation

Prepared By: Irfan Ahmad Pindoo 5


Ultrasonic Sensor
 An ultrasonic Sensor is a device used to measure the distance between the
sensor and an object without physical contact. This device works based on
time-to-distance conversion.

Prepared By: Irfan Ahmad Pindoo 6


Working Principle of Ultrasonic Sensor
 Ultrasonic sensors measure distance by sending and receiving the ultrasonic
wave. The ultrasonic sensor has a sender to emit the ultrasonic waves and a
receiver to receive the ultrasonic waves. The transmitted ultrasonic wave
travels through the air and is reflected by hitting the Object. Arduino
calculates the time taken by the ultrasonic pulse wave to reach the receiver
from the sender.

 We know that the speed of sound in air is nearly 344 m/s,

 So, the known parameters are time and speed (constant). Using these
parameters, we can calculate the distance traveled by the sound wave.

Prepared By: Irfan Ahmad Pindoo 7


HC-SR04 Hardware Overview
 The HC-SR04 is an affordable and easy to use distance measuring sensor
which has a range from 2cm to 400cm (about an inch to 13 feet).

 The sensor is composed of two ultrasonic transducers. One is transmitter


which outputs ultrasonic sound pulses and the other is receiver which
listens for reflected waves. It’s basically a SONAR which is used in
submarines for detecting underwater objects.

Prepared By: Irfan Ahmad Pindoo 8


HC-SR04 Ultrasonic Sensor Pinout
 The HC-SR04 is an affordable and easy to use distance measuring sensor
which has a range from 2cm to 400cm (about an inch to 13 feet).

 The sensor is composed of two ultrasonic transducers. One is transmitter


which outputs ultrasonic sound pulses and the other is receiver which
listens for reflected waves. It’s basically a SONAR which is used in
submarines for detecting underwater objects.

Prepared By: Irfan Ahmad Pindoo 9


HC-SR04 Ultrasonic Sensor Pinout
The sensor has 4 pins. VCC and GND go
to 5V and GND pins on the Arduino, and
the Trig and Echo go to any digital Arduino pin.
Using the Trig pin we send the ultrasound wave
from the transmitter, and with the Echo pin we
listen for the reflected signal.

Prepared By: Irfan Ahmad Pindoo 10


How to Connect HC-SR04 Ultrasonic Sensor to Arduino

Prepared By: Irfan Ahmad Pindoo 11


HC-SR04 Ultrasonic Sensor Arduino Code

Prepared By: Irfan Ahmad Pindoo 12


How to Connect HC-SR04 Ultrasonic Sensor to Arduino

Prepared By: Irfan Ahmad Pindoo 13


How to Connect HC-SR04 Ultrasonic Sensor to Arduino
• First we have to define the Trig and Echo pins. In this case they are the pins number 9 and
10 on the Arduino Board and they are named trigPin and echoPin. Then we need a Long
variable, named “duration” for the travel time that we will get from the sensor and an
integer variable for the distance.

// defines pins numbers


const int trigPin = 9;
const int echoPin = 10;

// defines variables
long duration;
int distance;

Prepared By: Irfan Ahmad Pindoo 14


How to Connect HC-SR04 Ultrasonic Sensor to Arduino

• In the setup we have to define the trigPin as an output and the echoPin as an Input and
also start the serial communication for showing the results on the serial monitor.

void setup() {
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
Serial.begin(9600); // Starts the serial communication
}
• In the loop first we have to make sure that the trigPin is clear so you have to set that pin
on a LOW State for just 2 µs. Now for generating the Ultra sound wave we have to set the
trigPin on HIGH State for 10 µs.

Prepared By: Irfan Ahmad Pindoo 15


How to Connect HC-SR04 Ultrasonic Sensor to Arduino
// Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);

// Sets the trigPin on HIGH state for 10 micro seconds


digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);

Using the pulseIn() function we read the travel time and put that value into the variable “duration”. This
function has 2 parameters, the first one is the name of the Echo pin and for the second is the state of the pulse
we are reading, either High or Low.

// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);

Prepared By: Irfan Ahmad Pindoo 16


How to Connect HC-SR04 Ultrasonic Sensor to Arduino
In this case, we need this set to it HIGH, as the HC-SR04 sensors sets the Echo pin to High after sending the 8
cycle ultrasonic burst from the transmitter. This actually starts the timing and once we receive the reflected
sound wave the Echo pin will go to Low which stops the timing. At the end the function will return the length
of the pulse in microseconds.

For getting the distance we will multiply the duration by 0.034 and divide it by 2

// Calculating the distance


distance= duration*0.034/2;

// Prints the distance on the Serial Monitor


Serial.print("Distance: ");
Serial.println(distance);
At the end we will print the value of the distance on the Serial Monitor.

Prepared By: Irfan Ahmad Pindoo 17


Basic Electronics and Electrical
Engineering: ECE 249
UNIT IV: Introduction of
Arduino and Sensors
Lecture No.: 30
Topic: Temperature Sensor and
IR Sensor Basics and Simulation

Delivered By: Dr. Irfan Ahmad Pindoo


Head, Patent and Design Cell
Division of Research and Development
Intellectual Property Rights Cell
Temperature Sensor
 A temperature sensor is a device, typically, a thermocouple or resistance
temperature detector, that provides temperature measurement in a readable
form through an electrical signal.

 A thermometer is the most basic form of a temperature meter that is used to


measure the degree of hotness and coolness.

 A thermocouple (T/C) is made from two dissimilar metals that generate an


electrical voltage in direct proportion to the change in temperature. An RTD
(Resistance Temperature Detector) is a variable resistor that changes its
electrical resistance in direct proportion to the change in the temperature in
a precise, repeatable, and nearly linear manner.

Prepared By: Irfan Ahmad Pindoo 19


Working of Temperature Sensor
 Temperature sensors measure temperature readings via electrical signals.
They contain two metals that generate an electrical voltage or resistance
when a temperature change occurs.

 The sensor plays a vital role in maintaining a specific temperature for a


variety of industries, including medical applications, HVAC systems, and
electrical appliances in our homes. Temperature sensors are critical for
accuracy and temperature control in industries like these.

Prepared By: Irfan Ahmad Pindoo 20


Types of Temperature Sensor
1. Contact Temperature Sensor
This type of temperature sensor is in direct contact
with the material under observation. It works on the
principle of Heat Conduction.
• Useful in distinguishing solids, liquids, and gases
over a broad scope of temperature.

Prepared By: Irfan Ahmad Pindoo 21


Types of Temperature Sensor
2. Non-contact Temperature Sensor
This type of sensor helps in determining the
temperature of liquids and gases that radiates energy
in response to rise in heat.

It works on the principle of Convection and Radiation.

Prepared By: Irfan Ahmad Pindoo 22


Working of Temperature Sensor
 The temperature sensor in Arduino converts the surrounding
temperature to voltage. It further converts the voltage to
Celcius, Celcius to Fahrenheit, and prints the Fahrenheit
temperature on the LCD screen.

 We will use a temperature sensor (TMP 36) of low voltage. Such


sensors are also stable while dealing with large capacitive loads.
It is also suitable for automotive applications.

 The temperature sensors TMP 35, TMP 36, and TMP 37 are the
sensors with the same features.

 The operating voltage of the TMP 36 sensor ranges from 2.7V to


5.5V.

Prepared By: Irfan Ahmad Pindoo 23


Working of Temperature Sensor
 The TMP36 temperature sensor is an easy way to measure temperature
using an Arduino! The sensor can measure a fairly wide range of
temperature (-50°C to 125°C), is fairly precise (0.1°C resolution), and is
very low cost, making it a popular choice.

 Unlike a thermistor, the TMP36 does not have a temperature sensitive


resistor. Instead this sensor uses the property of diodes; as a diode
changes temperature the voltage changes with it at a known rate. The
sensor measures the small change and outputs an analog voltage between
0 and 1.75V DC based on it.

Prepared By: Irfan Ahmad Pindoo 24


Working of Temperature Sensor

Prepared By: Irfan Ahmad Pindoo 25


Working of Temperature Sensor

Since this sensor is very simple and does


not require any supporting components
we will be directly connecting the sensor
to the Arduino.

Prepared By: Irfan Ahmad Pindoo 26


InterfacingTemperature Sensor with Arduino: Code

Prepared By: Irfan Ahmad Pindoo 27


InterfacingTemperature Sensor with Arduino: Code

Prepared By: Irfan Ahmad Pindoo 28


Code Explanation
 Calculating the output voltage from the Analog temperature data:
 We used the inbuilt analogRead() function to read the data from the A0
pin. After reading we then calculate Vout using the formula given below:

 Vout = (reading from A0 pin) * (3.3 / 1024)

 For Example: If the reading from the A0 pin is 248V then Vout will be
~800mV or ~0.8V.

Prepared By: Irfan Ahmad Pindoo 29


Code Explanation
 How to measure temperature:
 Simply connect the left pin to power (5V) and the right pin to the ground
to use the TMP36. The analog voltage on the middle pin will thereafter be
directly proportional (linear) to the temperature in °C.

 Simply apply the following formula to convert voltage to temperature:

 Temperature (°C) = (Vout – 0.5) * 100

 For Example: If the output voltage is 0.8V then the temperature will be
approx. to 30°C.

Prepared By: Irfan Ahmad Pindoo 30


Code Explanation: SImulation

Prepared By: Irfan Ahmad Pindoo 31


IR Sensor
 The PIR sensor is a special type of sensor which is usually used for security
purposes. It detects the objects by reading the Infrared radiations emitted
by the objects. Any object whose temperature is above absolute zero
emits radiation. This radiation is not visible to human eyes. The PIR sensor
is designed to detect this Infrared radiation.

 In this article, We will learn how can we make a Motion Detection System
using Arduino. When the PIR Sensor will detect any motion, it will show
that on the Serial Monitor and the buzzer will start.

Prepared By: Irfan Ahmad Pindoo 32


Components Required
 Arduino UNO -> A microcontroller board based on the ATmega328P
 PIR Sensor -> Which detects the motion
 Buzzer -> A device that produces sound or alarm
 Jumper Wires -> For connecting the elements of the circuit

Prepared By: Irfan Ahmad Pindoo 33


Circuit Diagram

In this circuit, the PIR sensor


detects the motion and
sends the digital value to the
Arduino and Arduino sends
the signal to the Serial
Monitor and the buzzer will
be started. otherwise, it will
be off.

Prepared By: Irfan Ahmad Pindoo 34


Circuit Diagram

• Arduino Digital pin 9 is connected with the (+ve) pin


of Buzzer
• Arduino GND pin is connected with (-ve) pin of
Buzzer
• Arduino Digital pin 2 is connected with the Signal
pin of the PIR Sensor
• Arduino 5V pin is connected with the Power pin of
the PIR Sensor
• Arduino GND pin is connected with the GND pin of
the PIR Sensor

Prepared By: Irfan Ahmad Pindoo 35


Code

Prepared By: Irfan Ahmad Pindoo 36


Code

Prepared By: Irfan Ahmad Pindoo 37


Output Simulation

Prepared By: Irfan Ahmad Pindoo 38

You might also like