IoT Basics and Smart Sensors - EXP 12-14
IoT Basics and Smart Sensors - EXP 12-14
Name:
Division:
Roll No.:
Date of Performance:
Date of Submission:
Grade:
Sign:
Experiment No. 12
Aim: Interface an ultrasonic sensor with Raspberry Pi 4 Model B board and analyze its
working.
Theory:
Ultrasonic Sensor
The HC-SR04 is an ultrasonic distance sensor. This economical sensor provides 2cm to 400cm of
non-contact measurement functionality with a ranging accuracy that can reach up to 3mm. Each
HC-SR04 module includes an ultrasonic transmitter, a receiver and a control circuit. There are only
four pins that you need to worry about on the HC-SR04: VCC (Power), Trig (Trigger), Echo
(Receive), and GND (Ground). This sensor has additional control circuitry that can prevent
inconsistent "bouncy" data depending on the application.
Raspberry Pi 4 Model B
Circuit Diagram:
Procedure:
Code:
#Libraries
import RPi.GPIO as GPIO
import time
def distance():
# set Trigger to HIGH
GPIO.output(GPIO_TRIGGER, True)
StartTime = time.time()
StopTime = time.time()
# save StartTime
while GPIO.input(GPIO_ECHO) == 0:
StartTime = time.time()
return distance
if __name__ == '__main__':
try:
while True:
dist = distance()
print ("Measured Distance = %.1f cm" % dist)
time.sleep(1)
Observations:
It is observed that the value of the distance between the ultrasonic sensor and the obstacle is
displayed on screen. This distance is updated every second and updates itself for any change in
the position of the obstacle and the ultrasonic sensor.
Conclusions:
On the basis of the observations, it can be concluded that the ultrasonic sensor can be interfaced
with a Raspberry Pi 4 board for any kind of distance measurement or obstacle avoidance
applications.
Name:
Division:
Roll No.:
Date of Performance:
Date of Submission:
Grade:
Sign:
Experiment No. 13
Aim: Interface a servo motor with Raspberry Pi 4 Model B board and analyze its working
Theory:
Servo Motor
The SG90 servo is a lightweight and high-quality servo motor. The servo is designed to work with
almost all radio control systems. It equips Carbon Fiber Gears which make the servo motor much
lighter than the same metal gear motor. For small load applications using the metal gear, servo
motors add unnecessary weight, so using these lightweight plastic gear servo motors is suggested.
It is a 180° rotation servo. It is a Digital Servo Motor that receives and processes PWM signals.
● Speed (sec/60deg): 0.10/4.8V
● Torque (Kg-cm): 1.80/4.8V
● Pulse Width: 500-2400 µs
● Rotation Angle: 180 degree
Raspberry Pi 4 Model B
Circuit Diagram:
Procedure:
Code:
#Libraries
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BOARD)
GPIO.setup(11,GPIO.OUT)
servo = GPIO.PWM(11,50)
servo.start(0)
print ("Waiting for 1 second")
time.sleep(1)
print ("Rotating at intervals of 12 degrees")
duty = 2
while duty <= 17:
servo.ChangeDutyCycle(duty)
time.sleep(1)
duty = duty + 1
print ("Turning back to 0 degrees")
servo.ChangeDutyCycle(2)
time.sleep(1)
servo.ChangeDutyCycle(0)
servo.stop()
GPIO.cleanup()
print ("Everything's cleaned up")
Observations:
It is observed that as the control pin of the servo is connected to GPIO 11 of the Raspberry Pi board,
the servo rotates to the angular position that is written in the form of degrees.
Conclusions:
On the basis of the observations, it can be concluded that the servo motor can be used for building
any kind of movement in an electronic system, especially robotic applications.
Name:
Division:
Roll No.:
Date of Performance:
Date of Submission:
Grade:
Sign:
Experiment No. 14
Aim: Interface an IR (Infrared) sensor with Raspberry Pi 4 Model B board and give an alarm
with the help of a buzzer when an obstacle is detected.
Apparatus
Hardware: Raspberry Pi 4 Model B board, Infrared Sensor, buzzer, breadboard, jumper wires.
Software: Thonny – Python IDE
Theory:
IR Sensor
The IR Sensor Module or infrared (IR) sensor is a basic and most popular sensor in electronics. It
is used in wireless technology like remote controlling functions and detection of surrounding objects/
obstacles. IR sensors mainly consist of an Infrared (IR) LED and a Photodiode, this pair is generally
called IR pair. An IR LED is a special purpose LED, it can emit infrared rays ranging from 700 nm
to 1 mm wavelength. These types of rays are invisible to our eyes. In contrast, a photodiode or IR
Receiver LED detects the infrared rays.
When reflected infrared light Falls on the Photodiode, the resistance of the photodiode falls from a
huge value and the voltage across the photodiode drops. So, a high amount of voltage from the
photodiode is given to the Inverting input (2) of the IC. Then the LM393/LM358 IC compares this
voltage with the threshold voltage. In this condition, the Inverting input voltage is greater than the
non-Inverting input voltage so the IC output is Low (0). So, the sensor output is Low (0).
When the Photodiode or IR receiver (IR-RX) does not detect the infrared light, then the resistance
of the photodiode will be very high. So, a Low amount of voltage from the photodiode is given to the
Inverting input (2) of the IC. Then the LM393/LM358 IC compares this voltage with the threshold
voltage. In this condition, the Inverting input voltage is less than the non-Inverting input voltage so
the IC output is High (1). So, the sensor output is High (1).
Raspberry Pi 4 Model B
Circuit Diagram:
Procedure:
Code:
sensor = 16
buzzer = 18
GPIO.setmode(GPIO.BOARD)
GPIO.setup(sensor,GPIO.IN)
GPIO.setup(buzzer,GPIO.OUT)
GPIO.output(buzzer,False)
print ("IR Sensor Ready.....")
print (" ")
try:
while True:
if GPIO.input(sensor):
GPIO.output(buzzer,False)
print ("Object detected")
while GPIO.input(sensor):
time.sleep(0.2)
else:
GPIO.output(buzzer,True)
except KeyboardInterrupt:
GPIO.cleanup()
Observations:
It is observed that when an obstacle is detected by the IR sensor, the buzzer rings. Also, along with
the buzzer; the output LED of the sensor also turns on.
Conclusions:
Based on the observations, it can be concluded that the IR sensor can be interfaced with a
Raspberry Pi 4 Model B board for any kind of distance measurement, obstacle avoidance and line
following applications.