0% found this document useful (0 votes)
57 views16 pages

IoT Basics and Smart Sensors - EXP 12-14

This document provides information about interfacing ultrasonic and servo sensors with a Raspberry Pi 4 board. It includes the theory of operation for an ultrasonic sensor and servo motor. The procedures describe connecting the sensors to the Raspberry Pi and coding in Python to read the ultrasonic distance measurements and control the servo position. The observations show the distance values updating and servo rotating as programmed. It is concluded that ultrasonic sensors and servo motors can be used for applications like obstacle avoidance and robotics when interfaced with a Raspberry Pi.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
57 views16 pages

IoT Basics and Smart Sensors - EXP 12-14

This document provides information about interfacing ultrasonic and servo sensors with a Raspberry Pi 4 board. It includes the theory of operation for an ultrasonic sensor and servo motor. The procedures describe connecting the sensors to the Raspberry Pi and coding in Python to read the ultrasonic distance measurements and control the servo position. The observations show the distance values updating and servo rotating as programmed. It is concluded that ultrasonic sensors and servo motors can be used for applications like obstacle avoidance and robotics when interfaced with a Raspberry Pi.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

IoT basics and Smart Sensors

Department of Electronics and Telecommunication Engineering


Third Year
Semester V
A Y. 2022-23

IoT basics and Smart Sensors


Experiment No. 12

Name:

Division:

Roll No.:

Date of Performance:

Date of Submission:

Grade:

Sign:

Department of Electronics and Telecommunication Engineering Third Year Semester V 83


IoT basics and Smart Sensors

Experiment No. 12

Aim: Interface an ultrasonic sensor with Raspberry Pi 4 Model B board and analyze its
working.

Hardware: Raspberry Pi 4 Model B board, Ultrasonic Sensor, breadboard, jumper


wires.Software: Thonny – Python IDE

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

It is a high-performance 64-bit quad-core processor, dual-display support at resolutions up to 4K


via a pair of micro-HDMI ports, hardware video decodes at up to 4Kp60, up to 8GB of RAM, dual-
band 2.4/5.0 GHz wireless LAN, Bluetooth 5.0, Gigabit Ethernet, USB 3.0 and PoE capability.
Specifications:
Processor: - Broadcom BCM2711, quad-core Cortex-A72 (ARM v8) 64-bit SoC @ 1.5GHz
Memory: - 1GB, 2GB, 4GB or 8GB LPDDR4 (depending on model) with on-die ECC
Connectivity: - 2.4 GHz and 5.0 GHz IEEE 802.11b/g/n/ac wireless LAN, Bluetooth 5.0, BLE Gigabit
Ethernet, 2 × USB 3.0 ports, 2 × USB 2.0 ports.

Department of Electronics and Telecommunication Engineering Third Year Semester V 84


IoT basics and Smart Sensors

GPIO: - Standard 40-pin GPIO header


Thonny – Python IDE
Thonny is a Python Integrated Development Environment (IDE). It has a built-in debugger and it
offers the ability to do step through expression evaluation as well.

Circuit Diagram:

Procedure:

1. Connect Raspberry Pi 4 Model B board to keyboard, mouse and screen.


2. Power on the Pi using Pi power adapter and open Thonny – Python IDE.
3. Create a new file and save it with an appropriate name.
4. Type the code.
5. Debug the code and execute it to remove any bugs or errors.
6. After successful compilation this file can be saved.
7. The Raspberry Pi can be turned off completely now.
8. The ultrasonic sensor should be interfaced with Pi according to the circuit diagram.
9. After confirmation that all the connections are made correctly, the Raspberry Pi can be
powered on.
10. Execute the Thonny Python file and check the output.

Department of Electronics and Telecommunication Engineering Third Year Semester V 85


IoT basics and Smart Sensors

Code:
#Libraries
import RPi.GPIO as GPIO
import time

#GPIO Mode (BOARD / BCM)


GPIO.setmode(GPIO.BCM)

#set GPIO Pins


GPIO_TRIGGER = 18
GPIO_ECHO = 24
#set GPIO direction (IN / OUT)
GPIO.setup(GPIO_TRIGGER, GPIO.OUT)
GPIO.setup(GPIO_ECHO, GPIO.IN)

def distance():
# set Trigger to HIGH
GPIO.output(GPIO_TRIGGER, True)

# set Trigger after 0.01ms to LOW


time.sleep(0.00001)
GPIO.output(GPIO_TRIGGER, False)

StartTime = time.time()
StopTime = time.time()

# save StartTime
while GPIO.input(GPIO_ECHO) == 0:
StartTime = time.time()

# save time of arrival


while GPIO.input(GPIO_ECHO) == 1:
StopTime = time.time()

# time difference between start and arrival


TimeElapsed = StopTime - StartTime
# multiply with the sonic speed (34300 cm/s)
# and divide by 2, because there and back
distance = (TimeElapsed * 34300) / 2

return distance

if __name__ == '__main__':
try:
while True:
dist = distance()
print ("Measured Distance = %.1f cm" % dist)
time.sleep(1)

# Reset by pressing CTRL + C


except KeyboardInterrupt:
print("Measurement stopped by User")
GPIO.cleanup()

Department of Electronics and Telecommunication Engineering Third Year Semester V 86


IoT basics and Smart Sensors

Screenshot of the experiment during execution:

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.

Department of Electronics and Telecommunication Engineering Third Year Semester V 87


IoT basics and Smart Sensors

Department of Electronics and Telecommunication Engineering


Third Year
Semester V
A Y. 2022-23

IoT basics and Smart Sensors


Experiment No. 13

Name:

Division:

Roll No.:

Date of Performance:

Date of Submission:

Grade:

Sign:

Department of Electronics and Telecommunication Engineering Third Year Semester V 88


IoT basics and Smart Sensors

Experiment No. 13

Aim: Interface a servo motor with Raspberry Pi 4 Model B board and analyze its working

Hardware: Raspberry Pi 4 Model B board, servo motor, breadboard, jumper wires.


Software: Thonny – Python IDE

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

Department of Electronics and Telecommunication Engineering Third Year Semester V 89


IoT basics and Smart Sensors

It is a high-performance 64-bit quad-core processor, dual-display support at resolutions up to 4K


via a pair of micro-HDMI ports, hardware video decodes at up to 4Kp60, up to 8GB of RAM, dual-
band 2.4/5.0 GHz wireless LAN, Bluetooth 5.0, Gigabit Ethernet, USB 3.0 and PoE capability.
Specifications:
Processor: - Broadcom BCM2711, quad-core Cortex-A72 (ARM v8) 64-bit SoC @ 1.5GHz
Memory: - 1GB, 2GB, 4GB or 8GB LPDDR4 (depending on model) with on-die ECC
Connectivity: - 2.4 GHz and 5.0 GHz IEEE 802.11b/g/n/ac wireless LAN, Bluetooth 5.0, BLE Gigabit
Ethernet, 2 × USB 3.0 ports, 2 × USB 2.0 ports.
GPIO: - Standard 40-pin GPIO header

Thonny – Python IDE


Thonny is a Python Integrated Development Environment (IDE). It has a built-in debugger and it
offers the ability to do step through expression evaluation as well.

Circuit Diagram:

Procedure:

1. Connect Raspberry Pi 4 Model B board to keyboard, mouse and screen.


2. Power on the Pi using Pi power adapter and open Thonny – Python IDE.
3. Create a new file and save it with an appropriate name.
4. Type the code.
5. Debug the code and execute it to remove any bugs or errors.
6. After successful compilation this file can be saved.

Department of Electronics and Telecommunication Engineering Third Year Semester V 90


IoT basics and Smart Sensors

7. The Raspberry Pi can be turned off completely now.


8. The ultrasonic sensor should be interfaced with Pi according to the circuit diagram.
9. After confirmation that all the connections are made correctly, the Raspberry Pi can be
powered on.
10. Execute the Thonny Python file and check the output.

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")

Department of Electronics and Telecommunication Engineering Third Year Semester V 91


IoT basics and Smart Sensors

Screenshot of the experiment during execution:

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.

Department of Electronics and Telecommunication Engineering Third Year Semester V 92


IoT basics and Smart Sensors

Department of Electronics and Telecommunication Engineering


Third Year
Semester V
A Y. 2022-23

IoT basics and Smart Sensors


Experiment No. 14

Name:

Division:

Roll No.:

Date of Performance:

Date of Submission:

Grade:

Sign:

Department of Electronics and Telecommunication Engineering Third Year Semester V 93


IoT basics and Smart Sensors

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.

The IR sensor module consists mainly of:


1. IR Transmitter,
2. Photodiode Receiver,
3. LM393 Comparator IC,
4. Variable Resistor (Trim pot),
5. Power LED,
6. output LED.
When we connect the IR sensor module to the 5v power supply. The Infrared LED (IR-TX) starts
emitting infrared rays. Then we should set the threshold voltage at the Non-Inverting input (3) of the
IC by rotating the potentiometer knob for setting the sensor sensitivity.
If infrared rays reach the object's surface and some of the radiation is reflected back to the IR
receiver (IR-RX). The Photodiode or IR receiver (IR-RX) detects the infrared light.

Department of Electronics and Telecommunication Engineering Third Year Semester V 94


IoT basics and Smart Sensors

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

It is a high-performance 64-bit quad-core processor, dual-display support at resolutions up to 4K


via a pair of micro-HDMI ports, hardware video decodes at up to 4Kp60, up to 8GB of RAM, dual-
band 2.4/5.0 GHz wireless LAN, Bluetooth 5.0, Gigabit Ethernet, USB 3.0 and PoE capability.
Specifications:
Processor: - Broadcom BCM2711, quad-core Cortex-A72 (ARM v8) 64-bit SoC @ 1.5GHz
Memory: - 1GB, 2GB, 4GB or 8GB LPDDR4 (depending on model) with on-die ECC
Connectivity: - 2.4 GHz and 5.0 GHz IEEE 802.11b/g/n/ac wireless LAN, Bluetooth 5.0, BLE Gigabit
Ethernet, 2 × USB 3.0 ports, 2 × USB 2.0 ports.
Department of Electronics and Telecommunication Engineering Third Year Semester V 95
IoT basics and Smart Sensors

GPIO: - Standard 40-pin GPIO header

Thonny – Python IDE


Thonny is a Python Integrated Development Environment (IDE). It has a built-in debugger and it
offers the ability to do step through expression evaluation as well.

Circuit Diagram:

Procedure:

1. Connect Raspberry Pi 4 Model B board to keyboard, mouse and screen.


2. Power on the Pi using Pi power adapter and open Thonny – Python IDE.
3. Create a new file and save it with an appropriate name.
4. Type the code.
5. Debug the code and execute it to remove any bugs or errors.
6. After successful compilation this file can be saved.
7. The Raspberry Pi can be turned off completely now.
8. The ultrasonic sensor should be interfaced with Pi according to the circuit diagram.
9. After confirmation that all the connections are made correctly, the Raspberry Pi can be
powered on.
10. Execute the Thonny Python file and check the output.

Department of Electronics and Telecommunication Engineering Third Year Semester V 96


IoT basics and Smart Sensors

Code:

import RPi.GPIO as GPIO


import time

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()

Department of Electronics and Telecommunication Engineering Third Year Semester V 97


IoT basics and Smart Sensors

Screenshot of the experiment during execution:

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.

Department of Electronics and Telecommunication Engineering Third Year Semester V 98

You might also like