Iot Practical
Iot Practical
1|Page
CERTIFICATE
2|Page
INDEX
3|Page
PRACTICAL NO: - 1
Requirements:
- Raspberry Pi 3/4
- MicroSD card (at least 16 GB)
- MicroSD card reader
- Power supply
- HDMI cable
- Monitor or TV
- USB keyboard and mouse
Steps:
1. Insert the microSD card into the card reader and connect it to your computer.
2. Download the Raspberry Pi Imager from the official Raspberry Pi website.
3. Use the Imager to write the operating system (e.g., Raspberry Pi OS) to the microSD card.
4. Insert the microSD card into the Raspberry Pi, connect peripherals (keyboard, mouse,
monitor), and power on the device.
4|Page
PRACTICAL NO: - 2
Requirements:
- A prepared microSD card with Raspberry Pi OS.
Steps:
1. After inserting the microSD card into the Raspberry Pi, power it on.
2. On the first boot, you'll be prompted to complete the setup wizard.
3. Configure language, time zone, and Wi-Fi settings (if applicable).
4. Update the system by opening the terminal and running:
bash
sudo apt update
sudo apt upgrade
5|Page
PRACTICAL NO: - 3
Exercises:
1. Hello World Program:
print("Hello, World!")
6|Page
PRACTICAL NO: - 4
Requirements:
- Breadboard
- LED
- 220Ω resistor
- Jumper wires
- GPIO pins on Raspberry Pi
Steps:
1. Connect the positive leg of the LED to GPIO pin 17 and the negative leg to a resistor
connected to the ground (GND) pin.
7|Page
2. Open Thonny and run the following code to turn the LED on and off :
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
GPIO.setup(17, GPIO.OUT)
while True:
GPIO.output(17, GPIO.HIGH) LED on
time.sleep(1)
GPIO.output(17, GPIO.LOW) LED off
time.sleep(1)
OUTPUT:-
8|Page
PRACTICAL NO: - 5
Objective: To create an LED chaser effect using multiple LEDs connected to the Raspberry
Pi.
Requirements:
- 5 LEDs
- 5 resistors (220Ω)
- Jumper wires
- Breadboard
Steps:
1. Connect 5 LEDs to GPIO pins (e.g., 17, 18, 27, 22, 23).
2. Write a Python script to turn each LED on and off in sequence:
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
for pin in leds:
GPIO.setup(pin, GPIO.OUT)
while True:
for pin in leds:
GPIO.output(pin, GPIO.HIGH)
time.sleep(0.1)
GPIO.output(pin, GPIO.LOW)
9|Page
OUTPUT:-
10 | P a g e
PRACTICAL NO: - 6
Steps:
1. Open the Raspberry Pi terminal.
2. Enter the command to open the Raspberry Pi configuration tool:
sudo raspi-config
11 | P a g e
PRACTICAL NO: - 7
AIM: Reading and Displaying Distance Using Ultrasonic Sensor
Objective
The objective of this practical is to interface the ultrasonic sensor with Raspberry Pi and write
a Python program using Thonny to calculate and display the distance of an object based on
the time taken by sound waves to reflect back to the sensor.
Requirements
- Raspberry Pi (any model with GPIO support)
- HC-SR04 Ultrasonic Sensor
- Breadboard
- Jumper Wires
- Resistors: 330Ω, 470Ω
- Thonny IDE (pre-installed on Raspberry Pi OS)
Theory
The HC-SR04 ultrasonic sensor uses sonar to determine the distance to an object. It sends out
an ultrasonic wave, which reflects back when it hits an object. The sensor then measures the
time taken for the wave to return and calculates the distance using the formula:
Where:
- Time is the time taken by the ultrasonic wave to travel to the object and back.
- 34300 is the speed of sound in cm/s.
The distance is divided by 2 because the wave travels to the object and back.
12 | P a g e
Circuit Diagram
- Connect the VCC of the ultrasonic sensor to the 5V pin of the Raspberry Pi.
- Connect the GND of the sensor to the GND pin of the Raspberry Pi.
- Connect the Trig pin of the sensor to GPIO 23.
- Connect the Echo pin of the sensor to GPIO 24 (via a voltage divider using 330Ω and 470Ω
resistors to drop the 5V signal to 3.3V for the Pi).
Connections:
13 | P a g e
Procedure
1. Connect the Hardware
- Use jumper wires to connect the VCC, GND, TRIG, and ECHO pins of the ultrasonic
sensor to the appropriate Raspberry Pi GPIO pins as mentioned above.
- Use resistors (330Ω and 470Ω) in series between the ECHO pin and GPIO pin 24 to lower
the voltage to 3.3V, which is safe for the Raspberry Pi.
GPIO.setmode(GPIO.BCM)
TRIG = 23
ECHO = 24
GPIO.setup(TRIG, GPIO.OUT)
GPIO.setup(ECHO, GPIO.IN)
try:
while True:
GPIO.output(TRIG, False)
print("Waiting For Sensor To Settle")
time.sleep(2)
GPIO.output(TRIG, True)
time.sleep(0.00001)
GPIO.output(TRIG, False)
while GPIO.input(ECHO) == 0:
pulse_start = time.time()
while GPIO.input(ECHO) == 1:
pulse_end = time.time()
14 | P a g e
print("Distance:", distance, "cm")
time.sleep(1) # Adding a delay between readings
except KeyboardInterrupt:
print("Measurement stopped by user")
GPIO.cleanup()
Observation
When an object is placed in front of the sensor, the distance is calculated and displayed as
expected. The ultrasonic sensor accurately detects the distance of objects placed within its
range.
Result
The practical was successfully completed. We interfaced the HC-SR04 ultrasonic sensor with
the Raspberry Pi, were able to measure, and display the distance of an object in real-time
using a Python script.
15 | P a g e