Exercise No: IT18512 – Internet of Things Laboratory Date:
INTERFACING LED, SWITCH AND BUZZER WITH
RASPBERRY PI
Aim:
To interface LED, switch and buzzer with Raspberry pi.
Description :
Raspberry Pi is the name of a series of single-board computers made by the Raspberry Pi
Foundation, a UK charity that aims to educate people in computing and create easier access to
computing education. It is useful in interfacing various devices for interoperability and for
developing integrated 10T systems. The Led and buzzers are integrated with the GPIO (General
Purpose Input Output) pins of Raspberry pi circuit. The circuit is booted over monitor with
Raspian OS system. The code for controlling LED lights and buzzers over a switch is
written in Python.
Procedure :
Terminal Launch :
1. Open terminal.
2. Launch IDLE by typing.
3. After the IDLE launches, open a new window by FILE>OPEN or Ctrl + N
4. Type the code.
5. Save the code by FILE > SAVE or Ctrl + S.
6. To run your code RUN>RUN or Ctrl + F5.
Connection with Raspberry Pi :
1. Connect GPIO 11 of Raspberry Pi to LED through connecting jumperwires.
2. Connect GPIO 13 to Raspberry Pi to LED through connecting jumperwires.
3. Connect GPIO 15 of Raspberry Pi to buzzer pin through connectingjumper wires.
4. Connect GPIO 23 of Raspberry Pi to switch pin through connectingjumper wires.
5. Using jumper wires connect the ground pin of Raspberry Pi and LEDinterfacing
board.
6. Now power up the Raspberry Pi and boot.
Register No: 2127210801104 Page No:
Exercise No: IT18512 – Internet of Things Laboratory Date:
Pin Diagram :
Code :
Button Press :
import time
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BOARD)
GPIO.setup(11,GPIO.OUT)
GPIO.setup(13,GPIO.OUT)
GPIO.setup(23,GPIO.IN,pull_up_down=GPIO.PUD_UP)
try:
while True:
button_state = GPIO.input(23)
if button_state == False:
GPIO.output(11,True)
GPIO.output(13,True)
print('Button Pressed...')
time.sleep(0.2)
else:
GPIO.output(13,False)
GPIO.output(11,False)
Register No: 2127210801104 Page No:
Exercise No: IT18512 – Internet of Things Laboratory Date:
print('Button released')
except:
GPIO.cleanup()
Output :
Test Case – 1:
Even count :
import time
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BOARD)
GPIO.setup(11,GPIO.OUT)
GPIO.setup(13,GPIO.OUT)
GPIO.setup(23,GPIO.IN,pull_up_down=GPIO.PUD_UP)
count=0
try:
while True:
button_state = GPIO.input(23)
if button_state == False:
count = count + 1
if(count%2==0):
GPIO.output(11,True)
GPIO.output(13,True)
print(count)
time.sleep(1)
else:
GPIO.output(13,False)
GPIO.output(11,False)
time.sleep(1)
except:
GPIO.cleanup()
Output :
Register No: 2127210801104 Page No:
Exercise No: IT18512 – Internet of Things Laboratory Date:
Test Case – 2 :
LED blink multiple times for single click :
import time
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BOARD)
GPIO.setup(11,GPIO.OUT)
GPIO.setup(13,GPIO.OUT)
GPIO.setup(23,GPIO.IN,pull_up_down=GPIO.PUD_UP)
try:
while True:
button_state = GPIO.input(23)
if button_state == False:
for i in range(0,8):
GPIO.output(11,True)
GPIO.output(13,True)
time.sleep(0.2)
GPIO.output(13,False)
GPIO.output(11,False)
time.sleep(0.2)
except:
GPIO.cleanup()
Output :
Register No: 2127210801104 Page No:
Exercise No: IT18512 – Internet of Things Laboratory Date:
Test Case – 3 :
Alternate LED glow :
import time
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BOARD)
GPIO.setup(11,GPIO.OUT)
GPIO.setup(13,GPIO.OUT)
GPIO.setup(15,GPIO.OUT)
GPIO.setup(23,GPIO.IN,pull_up_down=GPIO.PUD_UP)
try:
while True:
button_state = GPIO.input(23)
if button_state == False:
for i in range(0,5):
GPIO.output(11,True)
GPIO.output(13,True)
time.sleep(1)
GPIO.output(13,False)
GPIO.output(11,False)
time.sleep(1)
GPIO.output(15,True)
GPIO.output(13,True)
time.sleep(1)
GPIO.output(15,False)
GPIO.output(13,False)
time.sleep(1)
except:
GPIO.cleanup()
Output :
Result :
Thus the implementation of the LED, switch and buzzer with Raspberry Pi
and interface board using python has been successfully completed.
Register No: 2127210801104 Page No:
IT18512 – INTERNET OF THINGS LABORATORY
Exercise No: Date:
MOVEMENT DETECTION WITH PIR
AIM: To design an IoT application to illustrate the working of PIR in movement detection.
COMPONENTS NEEDED:
• Raspberry Pi/Arduino.
• HC-SR04 Module
• Jumper wires.
PROCEDURE:
1. Open Terminal
2. Launch IDLE IDE by typing
3. After the IDLE launches, open a new window by FILE>OPEN or Ctrl+N
4. Type the code
5. Save the code by FILE>SAVE or Ctrl+S
6. To run your code RUN or Ctrl+F5.
7. Connect VCC to Pin 2 (VCC)
8. Connect GND to Pin 6 (GND)
9. TRIG to Pin 12 (GPIO18)
10. Connect ECHO to GPIO 24.
CODE:
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
TRIG = 23
ECHO = 24
print "Distance Measurement In Progress"
GPIO.setup(TRIG,GPIO.OUT)
GPIO.setup(ECHO,GPIO.IN)
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)
REGISTER NO: 2127210801104 PAGE NO:
IT18512 – INTERNET OF THINGS LABORATORY
while GPIO.input(ECHO)==0:
pulse_start = time.time()
while GPIO.input(ECHO)==1:
pulse_end = time.time()
pulse_duration = pulse_end - pulse_start
distance = pulse_duration x 17150
distance = round(distance, 2)
print "Distance:",distance,"cm"
GPIO.cleanup()
OUTPUT:
RESULT:
Thus designed an application using PIR sensor and measured the distance.
REGISTER NO: 2127210801104 PAGE NO:
IT18512 – INTERNET OF THINGS LABORATORY
Exercise No: Date:
Simulation of Traffic Signal
AIM: To design an IoT application to simulate the working of traffic signal.
COMPONENTS NEEDED:
• Raspberry Pi/Ardino.
• Traffic light sensor
• Jumper wires.
• Power supply.
PROCEDURE:
1. Open Terminal
2. Launch IDLE IDE by typing
3. After the IDLE launches, open a new window by FILE>OPEN or Ctrl+N
4. Type the code
5. Save the code by FILE>SAVE or Ctrl+S
6. To run your code RUN>RUN or Ctrl+F5.
7. Connect three pins to GPIO 12 ,GPIO 16 and GPIO 23
8. Connect button to GPIO 19.
9. Connect ground to PIN 3
CODE:
import RPi.GPIO as GPIO
import time
try:
def lightTraffic(led1, led2, led3, delay ):
GPIO.output(led1, 1)
Print(“GREEN”)
time.sleep(delay)
GPIO.output(led1, 0)
GPIO.output(led2, 1)
Print(“YELLOW”)
time.sleep(delay)
GPIO.output(led2, 0)
GPIO.output(led3, 1)
REGISTER NO: 2127210801104 PAGE NO:
IT18512 – INTERNET OF THINGS LABORATORY
Print(“RED”)
time.sleep(delay)
GPIO.output(led3, 0)
GPIO.setmode(GPIO.BCM)
ledGreen = 16
ledYellow = 12
ledRed = 23
GPIO.setup(ledGreen, GPIO.OUT)
GPIO.setup(ledYellow, GPIO.OUT)
GPIO.setup(ledRed, GPIO.OUT)
while True:
lightTraffic(ledGreen, ledYellow, ledRed, 1)
except KeyboardInterrupt:
print "You've exited the program"
finally:
GPIO.cleanup()
OUTPUT:
RESULT:
Thus design an IoT application to simulate the working of traffic signal..
REGISTER NO: 2127210801104 PAGE NO:
IT18512 – INTERNET OF THINGS LABORATORY
Exercise No: Date:
CONTROLLING LED INTENSITY USING PWM SIGNAL
AIM:
To vary the brightness of an LED using PWM signal.
COMPONENTS NEEDED:
● Raspberry Pi/Ardino.
● Interface board.
● Jumper wires.
● Power supply.
PROCEDURE:
1. Open Terminal
2. Launch IDLE IDE by typing
3. After the IDLE launches, open a new window by FILE>OPEN or Ctrl+N
4. Type the code
5. Save the code by FILE>SAVE or Ctrl+S
6. To run your code RUN>RUN or Ctrl+F5.
7. Connect LED to pin GPIO 21 .
8. Connect ground to PIN 3
CODE:
import RPi.GPIO as GPIO
from time import sleep
led_pin = 21
GPIO.setmode(GPIO.BCM)
GPIO.setup(led_pin, GPIO.OUT)
pwm = GPIO.PWM(led_pin, 100)
pwm.start(0)
try:
while 1:
Print(“INCREASING”)
for x in range(100):
Print(x)
pwm.ChangeDutyCycle(x)
sleep(0.01)
REGISTER NO: 2127210801104 PAGE NO:
IT18512 – INTERNET OF THINGS LABORATORY
Print(“DECREASING”)
for x in range(100,0,-1):
Print(x)
pwm.ChangeDutyCycle(x)
sleep(0.01)
# If keyboard Interrupt
except KeyboardInterrupt:
pass
pwm.stop
GPIO.cleanup()
OUTPUT:
RESULT:
Using PWM signal the intensity of the LED was varied.
REGISTER NO: 2127210801104 PAGE NO:
IT18512 – INTERNET OF THINGS LABORATORY
Exercise No: Date:
WORKING WITH SERVO MOTOR
AIM:
To control the rotation of a servo motor.
COMPONENTS NEEDED:
● Raspberry Pi/Ardino.
● Interface board.
● SJ 90
● Power supply.
PROCEDURE:
1. Open Terminal
2. Launch IDLE IDE by typing
3. After the IDLE launches, open a new window by FILE>OPEN or Ctrl+N
4. Type the code
5. Save the code by FILE>SAVE or Ctrl+S
6. To run your code RUN>RUN or Ctrl+F5.
7. Connect orange wire to pin GPIO 21.
8. Connect red wire to GCC
9. Connect brown wire to ground
CODE:
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BOARD)
GPIO.setup(12, GPIO.OUT)
p = GPIO.PWM(12, 50)
p.start(7.5)
try:
while True:
p.ChangeDutyCycle(7.5) # turn towards 90 degree
print(“90 degree”)
time.sleep(1) # sleep 1 second
p.ChangeDutyCycle(2.5) # turn towards 0 degree
print(“0 degree”)
time.sleep(1) # sleep 1 second
p.ChangeDutyCycle(12.5) # turn towards 180 degree
REGISTER NO: 2127210801104 PAGE NO:
IT18512 – INTERNET OF THINGS LABORATORY
print(“180 degree”)
time.sleep(1) # sleep 1 second
except KeyboardInterrupt:
p.stop()
GPIO.cleanup()
OUTPUT:
RESULT:
The servo motor working was connected and rotated.
REGISTER NO: 2127210801104 PAGE NO:
IT18512 – INTERNET OF THINGS LABORATORY
Exercise No: Date:
UNDERSTAND THE DATA COLLECTED FROM LASER EMITTER SENSOR.
AIM:
To understand the data collected from laser emitter sensor.
COMPONENTS NEEDED:
● Raspberry Pi/Ardino.
● Interface board.
● Jumper wires.
● Power supply.
PROCEDURE:
1. Open Terminal
2. Launch IDLE IDE by typing
3. After the IDLE launches, open a new window by FILE>OPEN or Ctrl+N
4. Type the code
5. Save the code by FILE>SAVE or Ctrl+S
6. To run your code RUN>RUN or Ctrl+F5.
7. Connect LED1 to pin GPIO 37.
8. Connect LED2 to pin GPIO 38.
CODE:
import RPi.GPIO as GPIO
import time
LedPin = 11 # pin11
def setup():
GPIO.setmode(GPIO.BOARD) # Numbers GPIOs by physical location
GPIO.setup(LedPin, GPIO.OUT) # Set LedPin's mode is output
GPIO.output(LedPin, GPIO.HIGH) # Set LedPin high(+3.3V) to off led
def loop():
while True:
print('...led on')
GPIO.output(LedPin, GPIO.HIGH) # led on
time.sleep(0.5)
print('led off...')
GPIO.output(LedPin, GPIO.LOW) # led off
time.sleep(0.5)
def destroy():
REGISTER NO: 2127210801104 PAGE NO:
IT18512 – INTERNET OF THINGS LABORATORY
GPIO.output(LedPin, GPIO.LOW) # led off
GPIO.cleanup() # Release resource
if name == ' main ': # Program start from here
setup()
try:
loop()
except KeyboardInterrupt
destroy()
OUTPUT:
…led on
led off…
…led on
led off…
…led on
led off…
…led on
led off…
RESULT:
Thus understood the data collected from laser emitter sensor.
REGISTER NO: 2127210801104 PAGE NO:
IT18512 – INTERNET OF THINGS LABORATORY
Exercise No: Date:
UNDERSTAND THE DATA COLLECTED FROM TOUCH SENSOR.
AIM:
To understand the data collected from touch sensor.
COMPONENTS NEEDED:
● Raspberry Pi/Ardino.
● Interface board.
● Jumper wires.
● Power supply.
PROCEDURE:
9. Open Terminal
10. Launch IDLE IDE by typing
11. After the IDLE launches, open a new window by FILE>OPEN or Ctrl+N
12. Type the code
13. Save the code by FILE>SAVE or Ctrl+S
14. To run your code RUN>RUN or Ctrl+F5.
15. Connect LED1 to pin GPIO 37.
16. Connect LED2 to pin GPIO 38.
CODE:
import RPi.GPIO as GPIO
TouchPin = 11
LedPin = 12
def setup():
GPIO.setmode(GPIO.BOARD) # Numbers GPIOs by physical location
GPIO.setup(LedPin, GPIO.OUT) # Set LedPin's mode is output
GPIO.setup(TouchPin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.output(LedPin, GPIO.HIGH) # Set LedPin high(+3.3V) to off led
def loop():
while True:
if GPIO.input(TouchPin) == GPIO.LOW:
print ('Touch off')
GPIO.output(LedPin, GPIO.LOW) # led off
REGISTER NO: 2127210801104 PAGE NO:
IT18512 – INTERNET OF THINGS LABORATORY
else:
print ('Touch on')
GPIO.output(LedPin, GPIO.HIGH) # led on
def destroy():
GPIO.output(LedPin, GPIO.HIGH) # led off
GPIO.cleanup() # Release resource
if name == ' main ': # Program start from here
setup()
try:
loop()
except KeyboardInterrupt
destroy()
OUTPUT:
Touch off
Touch on
Touch off
Touch on
Touch off
Touch on
Touch off
Touch on
RESULT:
Thus understood the data collected from touch sensor.
REGISTER NO: 2127210801104 PAGE NO:
IT18512 – INTERNET OF THINGS LABORATORY
REGISTER NO: 2127210801070 PAGE NO:
IT18512 – INTERNET OF THINGS LABORATORY
Exercise No: Date:
INTERFACING ESP32 WITH DHT SENSOR TO DETECT THE ROOM
TEMPEATURE AND HUMIDITY
AIM : To write a C program for the implementation of interfacing the \ DHT sensor with ESP32
to detect the room temperature and humidity.
DESCRIPTION :
The ESP32 kit, when integrated with the Arduino IDE, offers a powerful and accessible platform
for microcontroller development. At its core, the ESP32 boasts a dual-core processor, onboard
Wi-Fi, and Bluetooth capabilities, making it a versatile choice for IoT projects. This integration
simplifies programming and shields users from complex low-level tasks. The Arduino IDE offers
a rich ecosystem of libraries and examples that expedite project development. Moreover, it
provides essential features like the Serial Monitor for debugging, a straightforward code editor,
and hassle-free code uploads to the ESP32 board. Whether you’re a novice or an expert, this
combination empowers you to swiftly build innovative applications, connect to the internet, and
utilize a wide array of sensors and peripherals.
PROCEDURE:
Connection with ESP32 kit
• Connect the GPIO 4 of the ESP32 kit with the IO pin in the DHT sensor using the jumper
wires.
• Connect the Ground pin of the ESP32 kit with the GND Pin in the DHT sensor using the
jumper wires.
• Connect the +3.3v pin in the ESP32 kit with the +3.3v pin in the DHT sensor using the
jumper wires.
REGISTER NO: 2127210801074 PAGE NO:
IT18512 – INTERNET OF THINGS LABORATORY
CIRCUIT DIAGRAM:
CODE :
#include “DHT.h”
#define DHTPIN 4 // Digital pin connected to the DHT sensor #define DHTTYPE DHT11
// DHT 11
DHT dht(DHTPIN, DHTTYPE);
Void setup()
{
Serial.begin(9600);
Serial.println(“DHT11 test!”);
pinMode(5,OUTPUT);
dht.begin();
}
Void loop()
{
Float h = dht.readHumidity();
// Read temperature as Celsius (the default)
float t = dht.readTemperature();
// Check if any reads failed and exit early (to try again).
REGISTER NO: 2127210801074 PAGE NO:
IT18512 – INTERNET OF THINGS LABORATORY
If (isnan(h) || isnan(t))
{
Serial.println(“Failed to read from DHT sensor!”);
return;
}
Serial.println(“**************”);
Serial.print(“Humidity: “);
Serial.println(h);
Serial.print(“Temperature: “);
Serial.println(t);
SAMPLE INPUT AND OUTPUT :
RESULT :
Thus the implementation of interfacing the ESP32 kit with the DHT sensor to detect the current
room temperature and humidity has been successfully completed.
REGISTER NO: 2127210801074 PAGE NO:
IT18512 – INTERNET OF THINGS LABORATORY
Exercise No: Date:
BUZZER CONTROL USING BLUETOOTH
AIM: To implement buzzer control using Bluetooth.
PIN CONFIGURATION:
ESP32
GPIO 2 BUZZER
CODE:
#include "BluetoothSerial.h"
#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED) #error
Bluetooth is not enabled! Please run `make menuconfig` to and enable it
#endif
#define BUZZER 2 BluetoothSerial SerialBT;
// Handle received and sent messages String message = "";
char incomingChar; void setup() {
pinMode(BUZZER, OUTPUT);
Serial.begin(115200);
SerialBT.begin("ESP32"); //Bluetooth device name
Serial.println("The device started, now you can pair it with bluetooth!");
}
void loop() {
if (SerialBT.available()){
char incomingChar = SerialBT.read(); if (incomingChar != '\n'){
message += String(incomingChar);
}
else{ message = "";
}
REGISTER NO: 2127210801104 PAGE NO:
IT18512 – INTERNET OF THINGS LABORATORY
Serial.write(incomingChar);
}
// Check received message and control output accordingly if (message =="on"){
digitalWrite(BUZZER, HIGH);
}
else if (message =="off"){
digitalWrite(BUZZER, LOW);
}
delay(20);
}
OUTPUT:
RESULT: Thus buzzer control using Bluetooth has been implemented successfully.
REGISTER NO: 2127210801104 PAGE NO:
IT18512 – INTERNET OF THINGS LABORATORY
Exercise No: Date:
COMMUNICATION WITH ESP32 THROUGH BLUETOOTH
AIM: To understand how to communicate with ESP32 through Bluetooth.
NO PIN CONFIGURATION NEEDED!!
CODE:
#include "BluetoothSerial.h"
#if !defined(CONFIG_BT_ENABLED) ||
!defined(CONFIG_BLUEDROID_ENABLED)
#errorBluetooth is not enabled! Please run `make menuconfig` to and enable it#endif
BluetoothSerial SerialBT;void setup() { Serial.begin(115200);
SerialBT.begin("ESP32test"); //Bluetooth device name Serial.println("The device started, now
you can pairit with bluetooth!");
void loop() {
if (Serial.available()) { SerialBT.write(Serial.read());
if (SerialBT.available()) { Serial.write(SerialBT.read());
delay(20);
REGISTER NO: 2127210801104 PAGE NO:
IT18512 – INTERNET OF THINGS LABORATORY
OUTPUT:
RESULT: Thus communication with ESP32 through Bluetooth has been implemented
successfully
REGISTER NO: 2127210801104 PAGE NO:
IT18512 – INTERNET OF THINGS LABORATORY
Exercise No: Date:
TFT DISPLAY
AIM: To understand how to display TFT.
PIN CONFIGURATION:
ESP32 TFT
5V( RS232 TO TTL) LED
GND GND
GPIO 12 CS
GPIO 14 RES
GPIO 13 A0
GPIO 21 SDA
3.3V VCC
CODE:
#include <Adafruit_GFX.h> #include <Adafruit_ST7735.h>#include <SPI.h>
//define pins of TFT screen#define TFT_CS 12
#define TFT_RST 14
#define TFT_DC 13
#define TFT_SCLK 22
#define TFT_MOSI 21
REGISTER NO: 2127210801104 PAGE NO:
IT18512 – INTERNET OF THINGS LABORATORY
Adafruit_ST7735 tft = Adafruit_ST7735(TFT_CS, TFT_DC, TFT_MOSI, TFT_SCLK,
TFT_RST);
void setup(void) {
Serial.begin(115200); //initialise serial communication at 115200 bps
Serial.print("ST7735 TFT grafics test");
tft.initR(INITR_BLACKTAB); //initialize a ST7735S chip, black tab
Serial.println("Initializing...");
tft.setTextWrap(true);
void loop()
{ tft.fillScreen(ST7735_BLACK);
tft.setCursor(0, 0);
tft.setTextColor(ST7735_RED);
tft.setTextSize(2);
tft.println("Hello!");
delay(2000);
tft.setTextColor(ST7735_YELLOW);
tft.setTextSize(2);
tft.println("IOT Dev Kit!");
delay(1000);
tft.setTextColor(ST7735_GREEN);
tft.setTextSize(3);
tft.println("BYE!");
delay(2000);}
REGISTER NO: 2127210801104 PAGE NO:
IT18512 – INTERNET OF THINGS LABORATORY
OUTPUT:
RESULT: Thus TFT display has been executed successfully.
REGISTER NO: 2127210801104 PAGE NO: