You are on page 1of 62

Internet of Things (IoT Lab) Aditya College of Engineering

DEPARTMENT OF CSE –INTERNET OF THINGS (IoT)

Course Objectives:
To introduce the raspberry PI platform, that is widely used in IoT applications
To introduce the implementation of distance sensor on IoT devices
Course Outcomes:
Ability to introduce the concept of M2M (machine to machine) with necessary protocols and get
awareness in implementation of distance sensor
Get the skill to program using Python Scripting Language which is used in many IoT devices
Experiments:
1. Getting started with Raspberry Pi, Install Raspian on your SD card
2. Python-based IDE (integrated development environments) for the Raspberry Pi and how to trace and
debug Python code on the device
3. Using Raspberry pi
a. Calculate the distance using distance sensor.
b. Basic LED functionality.
4. Raspberry Pi interact with online services through the use of public APIs and SDKs
5. Using Arduino
a. Calculate the distance using distance sensor.
b. Basic LED functionality.
c. Calculate temperature using temperature sensor
6. Using Node MCU
a. Calculate the distance using distance sensor.
b. Basic LED functionality.
ADDITIONAL EXPERIMENTS
7) DHT11 Temperature and Humidity sensor Interfacing with Arduino Uno and display output on LCD
8) StreetLight Automation using LDR sensor
9) OLED Inerfacing with Arduino Uno and display graphical Information

Department of CSE-IoT III B-Tech I Sem 1|P ag e


Internet of Things (IoT Lab) Aditya College of Engineering

1. Getting started with Raspberry Pi, Install Raspian on your SD card

To get started with your Raspberry Pi, you’ll need the following:

 a power supply

 boot media (e.g. a microSD card with ample storage and speed)

You can set up your Raspberry Pi as an interactive computer with a desktop, or as a headless computer
accessible only over the network. To set your Raspberry Pi up headless, you don’t need any additional
peripherals; you can preconfigure a hostname, user account, network connection, and SSH when
you install an operating system. If you want to use your Raspberry Pi directly, you’ll need the following
additional accessories:

 a display

 a cable to connect your Raspberry Pi to your display

 a keyboard

 a mouse

Power Supply

The following table shows the USB-PD power mode required to power various Raspberry Pi models.
You can use any high quality power supply that provides the correct power mode.

Model Recommended Power Supply Raspberry Pi Power


(Voltage/Current) Supply

Raspberry Pi 5 5V/5A, 5V/3A limits peripherals to 600mA 27W USB-C Power Supply

Raspberry Pi 4 Model B 5V/3A 15W USB-C Power Supply

Raspberry Pi 3 (all 5V/2.5A 12.5W Micro USB Power


models) Supply

Raspberry Pi 2 (all 5V/2.5A 12.5W Micro USB Power


models) Supply

Raspberry Pi 1 (all 5V/2.5A 12.5W Micro USB Power


models) Supply

Department of CSE-IoT III B-Tech I Sem 2|P ag e


Internet of Things (IoT Lab) Aditya College of Engineering
Model Recommended Power Supply Raspberry Pi Power
(Voltage/Current) Supply

Raspberry Pi Zero (all 5V/2.5A 12.5W Micro USB Power


models) Supply

Plug your power supply into the port marked "POWER IN", "PWR IN", or "PWR". Some Raspberry Pi
models, such as the Zero series, have output USB ports with the same form factor as the power port. Be
sure to use the correct port on your Raspberry Pi!

Boot Media

Raspberry Pi models lack onboard storage, so you have to supply it. You can boot your Raspberry Pi
from an operating system image installed on any supported media: commonly microSD cards, but also
USB storage, network storage, and storage connected via a PCIe HAT. However, only recent Raspberry
Pi models support all of these media types.

All Raspberry Pi consumer models since the Raspberry Pi 1 Model A+ feature a microSD slot. Your
Raspberry Pi automatically boots from the microSD slot when the slot contains a card.

Department of CSE-IoT III B-Tech I Sem 3|P ag e


Internet of Things (IoT Lab) Aditya College of Engineering

Recommended SD cards

We recommend using an SD card with at least 16GB of storage with Raspberry Pi OS. If you use
Raspberry Pi OS Lite, we recommend at least 4GB.

Because of a hardware limitation, the following devices will only boot from a boot partition of 256GB
or less:

 Raspberry Pi Zero

 Raspberry Pi 1

 early Raspberry Pi 2 models with the BCM2836 SoC

Other operating systems have different requirements. Check the documentation for your operating
system for capacity requirements.

Keyboard

You can use any of the USB ports on your Raspberry Pi to connect a wired keyboard or USB Bluetooth
receiver.

Department of CSE-IoT III B-Tech I Sem 4|P ag e


Internet of Things (IoT Lab) Aditya College of Engineering

Mouse

You can use any of the USB ports on your Raspberry Pi to connect a wired mouse or USB Bluetooth
receiver.

Department of CSE-IoT III B-Tech I Sem 5|P ag e


Internet of Things (IoT Lab) Aditya College of Engineering

Display

If your Raspberry Pi has more than one HDMI ports, plug your primary monitor into the port
marked HDMI0.

Raspberry Pi models have the following display connectivity:

Model Display outputs

Raspberry Pi 5 2x micro HDMI

Raspberry Pi 4 (all models) 2x micro HDMI, audio and composite out via 3.5mm TRRS jack

Raspberry Pi 3 (all models) HDMI, audio and composite out via 3.5mm TRRS jack

Raspberry Pi 2 (all models) HDMI, audio and composite out via 3.5mm TRRS jack

Raspberry Pi 1 Model B+ HDMI, audio and composite out via 3.5mm TRRS jack

Raspberry Pi 1 Model A+ HDMI, RCA connector

Raspberry Pi Zero (all models) mini HDMI

Department of CSE-IoT III B-Tech I Sem 6|P ag e


Internet of Things (IoT Lab) Aditya College of Engineering
Most displays don’t have micro or mini HDMI ports. However, you can use a micro HDMI to HDMI
cable or mini HDMI to HDMI cable to connect those ports on your Raspberry Pi to any HDMI display.
For displays that don’t support HDMI, consider an adapter that translates display output from HDMI to
a port supported by your display.

Audio

All Raspberry Pi models with HDMI, micro HDMI, or mini HDMI support audio output over HDMI.
All Raspberry Pi models support audio over USB. All Raspberry Pi models equipped with Bluetooth
support Bluetooth audio. All variants of the Raspberry Pi 1, 2, 3, and 4 include a 3.5mm
auxiliary TRRS jack which may require amplification for sufficient output volume.

Networking

The following Raspberry Pi models come with WiFi and Bluetooth connectivity:

 Raspberry Pi 5

 Raspberry Pi 4

 Raspberry Pi 3B+

 Raspberry Pi 3

Department of CSE-IoT III B-Tech I Sem 7|P ag e


Internet of Things (IoT Lab) Aditya College of Engineering
 Raspberry Pi Zero W

 Rsapberry Pi Zero 2 W

The "Model B" suffix indicates variants with an Ethernet port; "Model A" indicates no Ethernet port. If
your model doesn’t have an Ethernet port, you can still connect to a wired internet connection using a
USB-to-Ethernet adapter.

Install an operating system

To use your Raspberry Pi, you’ll need an operating system. By default, Raspberry Pis check for an
operating system on any SD card inserted in the SD card slot.

Depending on your Raspberry Pi model, you can also boot an operating system from other storage
devices, including USB drives, storage connected via a HAT, and network storage.

To install an operating system on a storage device for your Raspberry Pi, you’ll need:

 a computer you can use to image the storage device into a boot device

 a way to plug your storage device into that computer

Most Raspberry Pi users choose microSD cards as their boot device.

We recommend installing an operating system using Raspberry Pi Imager.

Department of CSE-IoT III B-Tech I Sem 8|P ag e


Internet of Things (IoT Lab) Aditya College of Engineering
Raspberry Pi Imager is a tool that helps you download and write images on macOS, Windows, and
Linux. Imager includes many popular operating system images for Raspberry Pi. Imager also supports
loading images downloaded directly from Raspberry Pi or third-party vendors such as Ubuntu. You can
use Imager to preconfigure credentials and remote access settings for your Raspberry Pi.

Imager supports images packaged in the .img format as well as container formats like .zip.

If you have no other computer to write an image to a boot device, you may be able to install an
operating system directly on your Raspberry Pi from the internet.

Install using Imager

You can install Imager in the following ways:

 Download the latest version from raspberrypi.com/software and run the installer.

 Install it from a terminal using your package manager, e.g. sudo apt install rpi-imager.

Once you’ve installed Imager, launch the application by clicking the Raspberry Pi Imager icon or
running rpi-imager.

Click Choose device and select your Raspberry Pi model from the list.

Department of CSE-IoT III B-Tech I Sem 9|P ag e


Internet of Things (IoT Lab) Aditya College of Engineering

Next, click Choose OS and select an operating system to install. Imager always shows the
recommended version of Raspberry Pi OS for your model at the top of the list.

Department of CSE-IoT III B-Tech I Sem 10 | P a g e


Internet of Things (IoT Lab) Aditya College of Engineering

Connect your preferred storage device to your computer. For example, plug a microSD card in using an
external or built-in SD card reader. Then, click Choose storage and select your storage device.

Department of CSE-IoT III B-Tech I Sem 11 | P a g e


Internet of Things (IoT Lab) Aditya College of Engineering
Next, click Write.

In a popup, Imager will ask you to apply OS customisation. We strongly recommend configuring your
Raspberry Pi via the OS customisation settings. Click the Edit Settings button to open OS
customisation.

If you don’t configure your Raspberry Pi via OS customisation settings, Raspberry Pi OS will ask you
for the same information at first boot during the configuration wizard. You can click the No button to
skip OS customisation.

OS customisation

The OS customisation menu lets you set up your Raspberry Pi before first boot. You can preconfigure:

 a username and password

 WiFi credentials

 the device hostname

 the time zone

 your keyboard layout

 remote connectivity
Department of CSE-IoT III B-Tech I Sem 12 | P a g e
Internet of Things (IoT Lab) Aditya College of Engineering
When you first open the OS customisation menu, you might see a prompt asking for permission to load
WiFi credentials from your host computer. If you respond "yes", Imager will prefill WiFi credentials
from the network you’re currently connected to. If you respond "no", you can enter WiFi credentials
manually.

The hostname option defines the hostname your Raspberry Pi broadcasts to the network using mDNS.
When you connect your Raspberry Pi to your network, other devices on the network can communicate
with your computer using <hostname>.local or <hostname>.lan.

The username and password option defines the username and password of the admin user account on
your Raspberry Pi.

The wireless LAN option allows you to enter an SSID (name) and password for your wireless network.
If your network does not broadcast an SSID publicly, you should enable the "Hidden SSID" setting. By
default, Imager uses the country you’re currently in as the "Wireless LAN country". This setting
controls the WiFi broadcast frequencies used by your Raspberry Pi. Enter credentials for the wireless
LAN option if you plan to run a headless Raspberry Pi.

The locale settings option allows you to define the time zone and default keyboard layout for your Pi.

Department of CSE-IoT III B-Tech I Sem 13 | P a g e


Internet of Things (IoT Lab) Aditya College of Engineering

The Services tab includes settings to help you connect to your Raspberry Pi remotely.

If you plan to use your Raspberry Pi remotely over your network, check the box next to Enable SSH.
You should enable this option if you plan to run a headless Raspberry Pi.

 Choose the password authentication option to SSH into your Raspberry Pi over the network
using the username and password you provided in the general tab of OS customisation.

 Choose Allow public-key authentication only to preconfigure your Raspberry Pi for


passwordless public-key SSH authentication using a private key from the computer you’re
currently using. If already have an RSA key in your SSH configuration, Imager uses that public
key. If you don’t, you can click Run SSH-keygen to generate a public/private key pair. Imager
will use the newly-generated public key.

Department of CSE-IoT III B-Tech I Sem 14 | P a g e


Internet of Things (IoT Lab) Aditya College of Engineering

OS customisation also includes an Options menu that allows you to configure the behaviour of Imager
during a write. These options allow you to play a noise when Imager finishes verifying an image, to
automatically unmount storage media after verification, and to disable telemetry.

Department of CSE-IoT III B-Tech I Sem 15 | P a g e


Internet of Things (IoT Lab) Aditya College of Engineering

Write

When you’ve finished entering OS customisation settings, click Save to save your customisation.

Then, click Yes to apply OS customisation settings when you write the image to the storage device.

Finally, respond Yes to the "Are you sure you want to continue?" popup to begin writing data to the
storage device.

Department of CSE-IoT III B-Tech I Sem 16 | P a g e


Internet of Things (IoT Lab) Aditya College of Engineering

If you see an admin prompt asking for permissions to read and write to your storage medium, it’s safe
to proceed.

Department of CSE-IoT III B-Tech I Sem 17 | P a g e


Internet of Things (IoT Lab) Aditya College of Engineering

Grab a cup of coffee or go for a walk. This could take a few minutes.

Department of CSE-IoT III B-Tech I Sem 18 | P a g e


Internet of Things (IoT Lab) Aditya College of Engineering

If you want to live especially dangerously, you can click cancel verify to skip the verification process.

When you see the "Write Successful" popup, your image has been completely written and verified.
You’re now ready to boot a Raspberry Pi from the storage device!

Department of CSE-IoT III B-Tech I Sem 19 | P a g e


Internet of Things (IoT Lab) Aditya College of Engineering

Next, proceed to the first boot configuration instructions to get your Raspberry Pi up and running.

Configuration on first boot

If you used OS customisation in Imager to preconfigure your Raspberry Pi, congratulations! Your
device is ready to use. Proceed to next steps to learn how you can put your Raspberry Pi to good use.

If your Raspberry Pi does not boot within 5 minutes, check the status LED. If it’s flashing, see the LED
warning flash codes for more information. If your Pi refuses to boot, try the following mitigation steps:

 if you used a boot device other than an SD card, try booting from an SD card

 re-image your SD card; be sure to complete the entire verify step in Imager

 update the bootloader on your Raspberry Pi, then re-image your SD card

If you chose to skip OS customisation in Imager, your Raspberry Pi will run a configuration wizard on
first boot. You need a monitor and keyboard to navigate through the wizard; a mouse is optional.

Department of CSE-IoT III B-Tech I Sem 20 | P a g e


Internet of Things (IoT Lab) Aditya College of Engineering

Bluetooth

If you’re using a Bluetooth keyboard or mouse, this step will walk you through device pairing. Your
Raspberry Pi will scan for pairable devices and connect to the first device it finds for each item.

This process works with built-in or external USB Bluetooth adapters. If you use a USB adapter, plug it
in before booting your Raspberry Pi.

Locale

This page helps you configure your country, language, and time zone, and keyboard layout.

Department of CSE-IoT III B-Tech I Sem 21 | P a g e


Internet of Things (IoT Lab) Aditya College of Engineering

User

This page helps you configure the username and password for the default user account.

By default, older versions of Raspberry Pi OS set the username to "pi". If you use the username "pi",
avoid the old default password of "raspberry" to keep your Raspberry Pi secure.

Wi-Fi
Department of CSE-IoT III B-Tech I Sem 22 | P a g e
Internet of Things (IoT Lab) Aditya College of Engineering
This page helps you connect to a WiFi network. Choose your preferred network from the list.

If your network requires a password, you can enter it here.

Department of CSE-IoT III B-Tech I Sem 23 | P a g e


Internet of Things (IoT Lab) Aditya College of Engineering

Browser

This page lets you select Firefox or Chromium as your default internet browser. You can optionally
uninstall the browser you don’t set as default.

Department of CSE-IoT III B-Tech I Sem 24 | P a g e


Internet of Things (IoT Lab) Aditya College of Engineering

Software updates

Once your Raspberry Pi has internet access, this page helps you update your operating system and
software to the latest versions. During the software update process, the wizard will remove the non-
default browser if you opted to uninstall it in the browser selection step. Downloading updates may take
several minutes.

Department of CSE-IoT III B-Tech I Sem 25 | P a g e


Internet of Things (IoT Lab) Aditya College of Engineering

When you see a popup indicating that your system is up to date, click OK to proceed to the next step.

Finish

At the end of the configuration wizard, click Restart to reboot your Raspberry Pi. Your Raspberry Pi
will apply your configuration and boot to the desktop.

Department of CSE-IoT III B-Tech I Sem 26 | P a g e


Internet of Things (IoT Lab) Aditya College of Engineering

RESULT: Hence we have studied on getting started with raspberry Pi and installation of Rasbian OS
on SD card.

Department of CSE-IoT III B-Tech I Sem 27 | P a g e


Internet of Things (IoT Lab) Aditya College of Engineering
2) Python-based IDE (integrated development environments) for the Raspberry Pi
and how to trace and debug Python code on the device

IDLE (Integrated Development and Learning Environment) is a simple, lightweight Python IDE that
comes bundled with the standard Python distribution. It's designed to be easy to use, making it a good
choice for beginners and those who prefer a straightforward development environment. While IDLE is
not as feature-rich as some other IDEs, it provides essential tools for writing, running, and debugging
Python code on the Raspberry Pi.

IDLE Features:
Editor:

The IDLE editor allows you to write and edit Python code.
Syntax highlighting helps you identify code elements visually.
Automatic indentation improves code readability.
Shell:

IDLE includes an interactive Python shell, allowing you to execute Python commands and test code
snippets interactively.
Debugger:

IDLE comes with a basic debugger that allows you to set breakpoints, step through code, and inspect
variables.
Debugging in IDLE can be initiated by selecting "Debug" from the "Run" menu.
Integrated Documentation:

IDLE integrates documentation directly into the IDE, making it easy to access information about
Python functions and modules.
Tracing and Debugging in IDLE:
Here's a basic guide on how to trace and debug Python code using IDLE on the Raspberry Pi:

Open IDLE:

Department of CSE-IoT III B-Tech I Sem 28 | P a g e


Internet of Things (IoT Lab) Aditya College of Engineering

Launch IDLE by opening a terminal and typing idle or by navigating to the Programming menu in the
Raspberry Pi desktop environment.
Open or Create a Python Script:

Use the IDLE editor to open an existing Python script or create a new one.
Set Breakpoints:

Place breakpoints in your code by clicking on the left margin of the editor at the line where you want to
pause execution.
Run in Debug Mode:

Select "Debug" from the "Run" menu to start debugging your script.
Alternatively, you can use the F5 key as a shortcut for starting the debugger.
Debugging Controls:

IDLE provides basic debugging controls in the debugger window.


Use buttons like "Go," "Step," and "Over" to control the execution flow.
The "Variables" window displays the values of variables during debugging.
Inspect Variables:

While debugging, you can inspect the values of variables by hovering over them or by checking the
"Variables" window.
Exit Debug Mode:

When you're done debugging, you can exit debug mode by selecting "Exit" from the "Run" menu.
Limitations of IDLE:
While IDLE is a simple and accessible IDE, it has some limitations:

Basic Features:
IDLE lacks some of the advanced features found in other IDEs, such as Visual Studio Code or
PyCharm.

Department of CSE-IoT III B-Tech I Sem 29 | P a g e


Internet of Things (IoT Lab) Aditya College of Engineering
User Interface:
The user interface might not be as polished or customizable as in other IDEs.

Advanced Debugging Features:


The debugging features in IDLE are relatively basic compared to other IDEs, which may limit its
usefulness for complex projects.

For more advanced debugging capabilities or features, users might consider exploring other IDEs like
Thonny or Visual Studio Code on the Raspberry Pi. These IDEs offer more sophisticated tools and a
broader range of features for Python development.

RESULT: Hence we have studied Python-based IDE (integrated development environments) for the
Raspberry Pi and how to trace and debug Python code on the Raspberry Pi.

Department of CSE-IoT III B-Tech I Sem 30 | P a g e


Internet of Things (IoT Lab) Aditya College of Engineering

3. Using Raspberry pi
a. Calculate the distance using distance sensor.
b. Basic LED functionality.

A) Calculate the distance using distance sensor.


AIM : To calculate distance using distance sensor by interfacing with Ultrasonic sensor
APPARATUS REQUIRED:

 Raspberry Pi with pre-installed OS


 HC-SR04 Ultrasonic Sensor
 Power supply (5v)
 1KΩ resistor (3 pieces)
 1000uF capacitor
 16*2 character LCD

WORKING PRINCIPLE:

HC-SR04 Ultrasonic Sensor:


The Ultrasonic Sensor is used to measure the distance with high accuracy and stable readings. It can
measure distance from 2cm to 400cm or from 1 inch to 13 feet. It emits an ultrasound wave at the
frequency of 40KHz in the air and if the object will come in its way then it will bounce back to the
sensor. By using that time which it takes to strike the object and comes back, you can calculate the
distance.

Department of CSE-IoT III B-Tech I Sem 31 | P a g e


Internet of Things (IoT Lab) Aditya College of Engineering

The ultrasonic sensor uses a technique called “ECHO”. “ECHO” is simply a reflected sound wave. You
will have an ECHO when sound reflects back after reaching a dead end.

HCSR04 module generates a sound vibration in ultrasonic range when we make the ‘Trigger’ pin high
for about 10us which will send a 8 cycle sonic burst at the speed of sound and after striking the object,
it will be received by the Echo pin. Depending on time taken by sound vibration to get back, it provides
appropriate pulse output. If the object is far away then it takes more time for ECHO to be heard and the
output pulse width will be big. And if the obstacle is near, then the ECHO will be heard faster and
output pulse width will be smaller.

Department of CSE-IoT III B-Tech I Sem 32 | P a g e


Internet of Things (IoT Lab) Aditya College of Engineering
We can calculate the distance of the object based on the time taken by ultrasonic wave to return back to
the sensor. Since the time and speed of sound is known we can calculate the distance by the following
formulae.

 Distance= (Time x Speed of Sound in Air (343 m/s))/2.

The value is divided by two since the wave travels forward and backward covering the same distance.
Thus the time to reach obstacle is just half the total time taken

So Distance in centimeter = 17150*T

Procedure:
Complete working of Raspberry Pi Distance Measure goes as,

1. Triggering the sensor by pulling up the trigger pin for 10uS.

2. Sound wave is sent by the sensor. After receiving the ECHO, sensor module provides an output
proportional to distance.

3. We will record the time when the output pulse goes from LOW to HIGH and when again when its
goes form HIGH to LOW.

4. We will have start and stop time. We will use distance equation to calculate the distance.

5. The distance is displayed in 16x2 LCD display.

Accordingly we have written the Python Program for Raspberry Pi to do the following functions:

1. To send trigger to sensor

2. Record start and stop time of pulse output from sensor.

3. To Calculate the distance by using START and STOP time.

4. To Display the result obtained on the 16*2 LCD.

Department of CSE-IoT III B-Tech I Sem 33 | P a g e


Internet of Things (IoT Lab) Aditya College of Engineering

CODE
import time
import RPi.GPIO as IO #calling for header file which helps in using GPIOs of PI
string_of_characters = 0
IO.setwarnings(False) #do not show any warnings
IO.setmode (IO.BCM) #programming the GPIO by BCM pin numbers. (like PIN29 as GPIO5)
IO.setup(17,IO.OUT) #initialize GPIO17,27,24,23,18,26,5,6,13,19,21 as an output
IO.setup(27,IO.OUT)
IO.setup(24,IO.OUT)
IO.setup(23,IO.OUT)
IO.setup(18,IO.OUT)
IO.setup(26,IO.OUT)
IO.setup(5,IO.OUT)
IO.setup(6,IO.OUT)
IO.setup(13,IO.OUT)
IO.setup(19,IO.OUT)
IO.setup(21,IO.OUT)
IO.setup(16,IO.IN) #initialize GPIO16 as an input
def send_a_command (command): #steps for sending a command to 16x2 LCD
pin=command
PORT(pin);
IO.output(17,0)
#PORTD&= ~(1<<RS);
IO.output(27,1)
#PORTD|= (1<<E);
time.sleep(0.001)
#_delay_ms(50);
IO.output(27,0)
#PORTD&= ~(1<<E);
pin=0
PORT(pin);
def send_a_character (character): #steps for sending a character to 16x2 LCD
pin=character
PORT(pin);
IO.output(17,1)
#PORTD|= (1<<RS);
IO.output(27,1)
#PORTD|= (1<<E);
time.sleep(0.001)
#_delay_ms(50);
IO.output(27,0)
#PORTD&= ~(1<<E);
pin=0
PORT(pin);

Department of CSE-IoT III B-Tech I Sem 34 | P a g e


Internet of Things (IoT Lab) Aditya College of Engineering
def PORT(pin): #assigning level for PI GPIO for sending data to LCD through D0-D7
if(pin&0x01 == 0x01):
IO.output(24,1)
else:
IO.output(24,0)
if(pin&0x02 == 0x02):
IO.output(23,1)
else:
IO.output(23,0)
if(pin&0x04 == 0x04):
IO.output(18,1)
else:
IO.output(18,0)
if(pin&0x08 == 0x08):
IO.output(26,1)
else:
IO.output(26,0)
if(pin&0x10 == 0x10):
IO.output(5,1)
else:
IO.output(5,0)
if(pin&0x20 == 0x20):
IO.output(6,1)
else:
IO.output(6,0)
if(pin&0x40 == 0x40):
IO.output(13,1)
else:
IO.output(13,0)
if(pin&0x80 == 0x80):
IO.output(19,1)
else:
IO.output(19,0)

def send_a_string(string_of_characters):
string_of_characters = string_of_characters.ljust(16," ")
for i in range(16):
send_a_character(ord(string_of_characters[i])) #send characters one by one through data port

while 1:

send_a_command(0x38); #16x2 line LCD


send_a_command(0x0E); #screen and cursor ON
send_a_command(0x01); #clear screen
time.sleep(0.1) #sleep for 100msec
IO.setup(21,1)
time.sleep(0.00001)
IO.setup(21,0) #sending trigger pulse for sensor to measure the distance

Department of CSE-IoT III B-Tech I Sem 35 | P a g e


Internet of Things (IoT Lab) Aditya College of Engineering

while (IO.input(16)==0):
start = time.time() #store the start time of pulse output

while (IO.input(16)==1):
stop = time.time() #store the stop time

distance = ((stop - start)*17150) #calculate distance from time


distance = round(distance,2) #round up the decimal values
if(distance<400): #if distance is less than 400 cm, display the result on LCD
send_a_command(0x80 + 0);
send_a_string ("Dist=%s cm"% (distance));
time.sleep(0.15)

if(distance>400): #If distance is more than 400cm, just print 400+ on LCD
send_a_command(0x80 + 0);
send_a_string ("Dist= 400+ cm");
time.sleep(0.15)

RESULT: Hence the distance measurement is done by interfacing Ultrasonic sensor with raspberry pi .

Department of CSE-IoT III B-Tech I Sem 36 | P a g e


Internet of Things (IoT Lab) Aditya College of Engineering

b. Basic LED functionality.


AIM:
To interface a basic LED with a Raspberry Pi and demonstrate its functionality.

APPARATUS REQUIRED:

Raspberry Pi (any model with GPIO pins)


LED (Light Emitting Diode)
Resistor (appropriate value for the LED)
Breadboard and jumper wires
WORKING PRINCIPLE:
The LED will be connected to one of the GPIO pins of the Raspberry Pi. GPIO pins can be controlled
by programming in Python using the RPi.GPIO library. When the GPIO pin is set to HIGH, the LED
will light up, and when it's set to LOW, the LED will turn off.

PROCEDURE:

Connect the longer leg (anode) of the LED to a GPIO pin on the Raspberry Pi using a resistor.
Connect the shorter leg (cathode) of the LED to the ground (GND) pin on the Raspberry Pi.
Ensure that the Raspberry Pi is powered on.
Write a Python script to control the GPIO pin:

CODE

import RPi.GPIO as GPIO


import time

# Set the GPIO mode and pin


GPIO.setmode(GPIO.BCM)
Department of CSE-IoT III B-Tech I Sem 37 | P a g e
Internet of Things (IoT Lab) Aditya College of Engineering
led_pin = 18 # Change this to the GPIO pin you are using

# Setup the GPIO pin as an output


GPIO.setup(led_pin, GPIO.OUT)

# Turn on the LED


GPIO.output(led_pin, GPIO.HIGH)
time.sleep(2) # Wait for 2 seconds

# Turn off the LED


GPIO.output(led_pin, GPIO.LOW)

# Cleanup GPIO settings


GPIO.cleanup()
Save the script and execute it using the terminal on the Raspberry Pi.
Code:

python
Copy code
import RPi.GPIO as GPIO
import time

# Set the GPIO mode and pin


GPIO.setmode(GPIO.BCM)
led_pin = 18 # Change this to the GPIO pin you are using

# Setup the GPIO pin as an output


GPIO.setup(led_pin, GPIO.OUT)

# Turn on the LED


GPIO.output(led_pin, GPIO.HIGH)
time.sleep(2) # Wait for 2 seconds

Department of CSE-IoT III B-Tech I Sem 38 | P a g e


Internet of Things (IoT Lab) Aditya College of Engineering
# Turn off the LED
GPIO.output(led_pin, GPIO.LOW)

# Cleanup GPIO settings


GPIO.cleanup()

RESULT:
Upon execution of the script, the LED should light up for 2 seconds and then turn off. This
demonstrates the basic functionality of interfacing an LED with a Raspberry Pi using GPIO pins and
programming in Python.

Department of CSE-IoT III B-Tech I Sem 39 | P a g e


Internet of Things (IoT Lab) Aditya College of Engineering

4. Raspberry Pi interact with online services through the use of public APIs and
SDKs
AIM:
To demonstrate the capability of Raspberry Pi in interacting with online services through the use of
public APIs and Software Development Kits (SDKs).

APPARATUS REQUIRED:

Raspberry Pi (Model 3 or later)


Internet connection
Sensors or peripherals compatible with Raspberry Pi (e.g., temperature sensor, camera)
Access to various online services with public APIs (e.g., weather API, Twitter API, Google Drive API)
WORKING PRINCIPLE:
Raspberry Pi will be programmed to communicate with various online services by utilizing their public
APIs and SDKs. The APIs will provide a standardized way for the Raspberry Pi to send requests and
receive data from the respective services. SDKs, if available, will be used to simplify the integration
process.

PROCEDURE:

Set up the Raspberry Pi with the required peripherals and ensure it has internet connectivity.
Identify and select online services with public APIs and SDKs that are relevant to the project (e.g.,
weather service, social media, cloud storage).
Install the necessary libraries and SDKs on the Raspberry Pi.
Develop a Python script on the Raspberry Pi to interact with the chosen online services using the
respective APIs and SDKs.
Implement functionalities such as fetching weather data, posting to social media, or uploading data to a
cloud storage service.
Test the script and debug if necessary.
Document the procedure and any challenges faced during the integration process.
Department of CSE-IoT III B-Tech I Sem 40 | P a g e
Internet of Things (IoT Lab) Aditya College of Engineering

CODE:
# Python script for interacting with a weather API
import requests

def get_weather(api_key, city):


url = f'https://api.openweathermap.org/data/2.5/weather?q={city}&appid={api_key}'
response = requests.get(url)

if response.status_code == 200:
weather_data = response.json()
return weather_data
else:
return None

# Example usage
api_key = 'your_openweathermap_api_key'
city = 'New York'
weather_info = get_weather(api_key, city)
print(weather_info)

RESULT:
The Raspberry Pi successfully interacts with online services through the use of public APIs and SDKs.
The results may include weather information, social media posts, or uploaded data to cloud storage,
depending on the chosen online services. The experiment demonstrates the versatility of Raspberry Pi
in connecting with external services and retrieving or sending data.

Department of CSE-IoT III B-Tech I Sem 41 | P a g e


Internet of Things (IoT Lab) Aditya College of Engineering

5. Using Arduino
a. Calculate the distance using distance sensor.
b. Basic LED functionality.
c. Calculate temperature using temperature sensor

A. Calculate the distance using distance sensor.

AIM:
To design and implement a distance measurement system using an ultrasonic sensor interfaced with
an Arduino Uno.

APPARATUS REQUIRED:

Arduino Uno board


Ultrasonic distance sensor (HC-SR04)
Breadboard and jumper wires
Computer with Arduino IDE installed
Power source for Arduino Uno
WORKING PRINCIPLE:
The ultrasonic sensor emits ultrasonic waves and measures the time it takes for the waves to bounce
back after hitting an object. By knowing the speed of sound in the air, the Arduino can calculate the
distance to the object using the formula: Distance = (Speed of Sound * Time taken) / 2.

PROCEDURE:

Connect the VCC pin of the ultrasonic sensor to the 5V pin on the Arduino Uno.
Connect the GND pin of the ultrasonic sensor to the GND pin on the Arduino Uno.
Connect the Trig pin of the ultrasonic sensor to digital pin 2 on the Arduino Uno.
Connect the Echo pin of the ultrasonic sensor to digital pin 3 on the Arduino Uno.
Connect the Arduino Uno to the computer using a USB cable.
Open the Arduino IDE on the computer.

Department of CSE-IoT III B-Tech I Sem 42 | P a g e


Internet of Things (IoT Lab) Aditya College of Engineering
Write a program to read the distance from the ultrasonic sensor and display it on the Serial Monitor.
Upload the program to the Arduino Uno.
Place an object in front of the ultrasonic sensor and observe the distance measurement on the Serial
Monitor.

CODE:

const int trigPin = 2;


const int echoPin = 3;

long duration;
int distance;

void setup() {
Serial.begin(9600);
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
}

void loop(){
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance= duration*0.034/2;
Serial.print("Distance in CM:");
Serial.println(distance);
delay(600);
}

Department of CSE-IoT III B-Tech I Sem 43 | P a g e


Internet of Things (IoT Lab) Aditya College of Engineering

RESULT: The Arduino Uno successfully interfaces with the ultrasonic sensor to measure the
distance of an object. The distance is displayed on the Serial Monitor in centimeters. The
experiment demonstrates the practical application of ultrasonic sensors for distance measurement
and the ability of Arduino Uno to interface with external sensors for various engineering purposes.

Department of CSE-IoT III B-Tech I Sem 44 | P a g e


Internet of Things (IoT Lab) Aditya College of Engineering

B. Basic LED functionality.


Aim:
To interface a Light Emitting Diode (LED) with an Arduino Uno and control its state using Arduino
programming.

APPARATUS REQUIRED:

Arduino Uno board


LED (Light Emitting Diode)
Resistor (220 ohms)
Breadboard and jumper wires
Computer with Arduino IDE installed
Power source for Arduino Uno
WORKING PRINCIPLE:
An LED is a semiconductor device that emits light when an electric current is applied in the forward
direction. To interface an LED with Arduino, a series resistor is used to limit the current flowing
through the LED. The Arduino can control the LED by providing a HIGH or LOW signal to the pin
connected to the LED. When the pin is HIGH, current flows through the LED, and it lights up.
When the pin is LOW, the LED is turned off.

PROCEDURE:

Connect the longer leg (anode) of the LED to digital pin 13 on the Arduino Uno.
Connect the shorter leg (cathode) of the LED to the resistor.
Connect the other end of the resistor to the GND (ground) pin on the Arduino Uno.
Connect the Arduino Uno to the computer using a USB cable.
Open the Arduino IDE on the computer.
Write a program to control the LED state (ON or OFF) by toggling the digital pin 13.
Upload the program to the Arduino Uno.
Observe the LED turning ON and OFF based on the program logic.

Department of CSE-IoT III B-Tech I Sem 45 | P a g e


Internet of Things (IoT Lab) Aditya College of Engineering

CODE:

// the setup function runs once when you press reset or power the board
void setup() {
// initialize digital pin LED_BUILTIN as an output.
pinMode(LED_BUILTIN, OUTPUT);
}

// the loop function runs over and over again forever


void loop() {
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
}

RESULT:
The LED connected to digital pin 13 on the Arduino Uno should alternately turn ON and OFF at 1-
second intervals. The experiment demonstrates the basic concept of interfacing an LED with
Arduino and controlling its state using a simple Arduino program. This foundational experiment is
often the starting point for more complex projects involving LEDs and other electronic components.

Department of CSE-IoT III B-Tech I Sem 46 | P a g e


Internet of Things (IoT Lab) Aditya College of Engineering

C. Calculate temperature using temperature sensor

AIM:
To measure and display the temperature using a DHT11 sensor interfaced with an Arduino Uno.

APPARATUS REQUIRED:

Arduino Uno board


DHT11 temperature and humidity sensor
Breadboard and jumper wires
10kΩ resistor (pull-up resistor for DHT11)
Computer with Arduino IDE installed
Power source for Arduino Uno
WORKING PRINCIPLE:
The DHT11 sensor is a digital temperature and humidity sensor that uses a capacitive humidity sensor
and a thermistor to measure temperature. The sensor provides a digital signal that can be read by the
Arduino. The DHT11 has a built-in resistor, and a pull-up resistor is required to stabilize the
communication between the sensor and the Arduino. The Arduino reads the digital signal from the
DHT11 and converts it into temperature values.

PROCEDURE:

Connect the VCC pin of the DHT11 sensor to the 5V pin on the Arduino Uno.
Connect the GND pin of the DHT11 sensor to the GND pin on the Arduino Uno.
Connect the DATA pin of the DHT11 sensor to digital pin 2 on the Arduino Uno.
Connect a 10kΩ resistor between the VCC and DATA pins of the DHT11 sensor.
Connect the Arduino Uno to the computer using a USB cable.
Open the Arduino IDE on the computer.
Install the DHT sensor library in the Arduino IDE.
Write a program to read the temperature from the DHT11 sensor and display it on the Serial Monitor.
Upload the program to the Arduino Uno.
Observe the temperature readings on the Serial Monitor.
Department of CSE-IoT III B-Tech I Sem 47 | P a g e
Internet of Things (IoT Lab) Aditya College of Engineering

CODE:
#include <dht.h>
#include<LiquidCrystal.h>

#define DHT11_PIN 10

LiquidCrystal lcd(2, 3, 4, 5, 6, 7); //rs=2, en=3, D4=4, D5=5, D6=6, D7=7;

dht DHT;

void setup()
{
Serial.begin(9600);
lcd.begin(16, 2);
Serial.println("Display Temperature and Humidity on Serial Monitor");
}

void loop()
{
int chk = DHT.read11(DHT11_PIN);
Serial.print("Temperature");
Serial.print(DHT.temperature);
Serial.print(" Humidity:");
Serial.println(DHT.humidity);
lcd.setCursor(0,0);
lcd.print("TMP: ");
lcd.setCursor(0,1);
lcd.print("Hum: ");
lcd.setCursor(4,0);
lcd.print(DHT.temperature);
lcd.setCursor(4,1);
lcd.print(DHT.humidity);
delay(500);
}

RESULT:
The Arduino Uno successfully interfaces with the DHT11 sensor, reads the temperature data, and
displays it on the Serial Monitor. The temperature readings are updated every 2 seconds. The
experiment demonstrates the practical application of DHT11 sensors for temperature measurement and
the ability of Arduino Uno to interface with external sensors for real-world applications.

Department of CSE-IoT III B-Tech I Sem 48 | P a g e


Internet of Things (IoT Lab) Aditya College of Engineering

6. Using Node MCU


a. Calculate the distance using distance sensor.
AIM:
To design and implement a distance measurement system using an ultrasonic sensor interfaced with
NodeMCU, and to display the distance information on the Serial Monitor.

APPARATUS REQUIRED:

NodeMCU (ESP8266) board


Ultrasonic distance sensor (HC-SR04)
Breadboard and jumper wires
Computer with Arduino IDE installed
USB to Micro-USB cable for NodeMCU power
Power source for NodeMCU
WORKING PRINCIPLE:
The ultrasonic sensor emits ultrasonic waves and measures the time it takes for the waves to bounce
back after hitting an object. NodeMCU, based on the ESP8266 module, is programmed to read the
sensor data and calculate the distance. The distance information is then printed to the Serial Monitor,
which can be monitored through the Arduino IDE or any serial communication software.

PROCEDURE:

Connect the VCC pin of the ultrasonic sensor to the 5V pin on the NodeMCU.
Connect the GND pin of the ultrasonic sensor to the GND pin on the NodeMCU.
Connect the Trig pin of the ultrasonic sensor to digital pin D1 on the NodeMCU.
Connect the Echo pin of the ultrasonic sensor to digital pin D2 on the NodeMCU.
Connect the NodeMCU to the computer using a USB to Micro-USB cable.
Open the Arduino IDE on the computer.
Install the necessary board and library support for NodeMCU in the Arduino IDE.
Write a program to read the distance from the ultrasonic sensor and print it to the Serial Monitor.

Department of CSE-IoT III B-Tech I Sem 49 | P a g e


Internet of Things (IoT Lab) Aditya College of Engineering
Upload the program to the NodeMCU.
Power the NodeMCU using an external power source.
Open the Serial Monitor in the Arduino IDE to view the distance information.
CODE:

// Arduino code for Ultrasonic sensor interfacing with NodeMCU and Serial Monitor display

#include <ESP8266WiFi.h>

const int trigPin = D1;


const int echoPin = D2;

void setup() {
Serial.begin(115200);

pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);

Serial.println("Distance Measurement using Ultrasonic Sensor with NodeMCU");


}

void loop() {
long duration, distance;

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

duration = pulseIn(echoPin, HIGH);


distance = (duration * 0.0343) / 2;

Department of CSE-IoT III B-Tech I Sem 50 | P a g e


Internet of Things (IoT Lab) Aditya College of Engineering
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm");

delay(1000); // Delay for 1 second before the next reading


}
RESULT:
The NodeMCU successfully interfaces with the ultrasonic sensor to measure the distance of an object.
The distance information is printed to the Serial Monitor, updating every second. The experiment
demonstrates the integration of a sensor with a wireless module (NodeMCU) and the real-time display
of distance data on the Serial Monitor for monitoring and analysis.

Department of CSE-IoT III B-Tech I Sem 51 | P a g e


Internet of Things (IoT Lab) Aditya College of Engineering

b. Basic LED functionality.


Title: LED Interfacing with NodeMCU and Blinking LED

AIM:
To interface an LED with NodeMCU and design a program to make the LED blink.

APPARATUS REQUIRED:

NodeMCU (ESP8266) board


LED (Light Emitting Diode)
Resistor (220 ohms)
Breadboard and jumper wires
Computer with Arduino IDE installed
USB to Micro-USB cable for NodeMCU power
Power source for NodeMCU
WORKING PRINCIPLE:
An LED is a semiconductor device that emits light when an electric current is applied in the forward
direction. To interface an LED with NodeMCU, a series resistor is used to limit the current flowing
through the LED. The NodeMCU is programmed to control the LED by toggling the digital pin to
which the LED is connected. By turning the pin HIGH and LOW with appropriate delays, the LED can
be made to blink.

PROCEDURE:

Connect the longer leg (anode) of the LED to digital pin D1 on the NodeMCU.
Connect the shorter leg (cathode) of the LED to a 220-ohm resistor.
Connect the other end of the resistor to the GND (ground) pin on the NodeMCU.
Connect the NodeMCU to the computer using a USB to Micro-USB cable.
Open the Arduino IDE on the computer.
Install the necessary board and library support for NodeMCU in the Arduino IDE.
Write a program to make the LED connected to D1 blink.
Upload the program to the NodeMCU.
Department of CSE-IoT III B-Tech I Sem 52 | P a g e
Internet of Things (IoT Lab) Aditya College of Engineering
Power the NodeMCU using an external power source.
Observe the LED blinking at regular intervals.
CODE:

#define LED D0 // Led in NodeMCU at pin GPIO16 (D0).


void setup() {
pinMode(LED, OUTPUT); // LED pin as output.
}
void loop() {
digitalWrite(LED, HIGH);// turn the LED off.(Note that LOW is the voltage level but actually
//the LED is on; this is because it is acive low on the ESP8266.
delay(1000); // wait for 1 second.
digitalWrite(LED, LOW); // turn the LED on.
delay(1000); // wait for 1 second.
}

RESULT:
The LED connected to digital pin D1 on the NodeMCU should blink at 1-second intervals. The
experiment demonstrates the basic concept of interfacing an LED with NodeMCU and controlling its
state using a simple program. This foundational experiment is often the starting point for more complex
projects involving NodeMCU and other electronic components.

Department of CSE-IoT III B-Tech I Sem 53 | P a g e


Internet of Things (IoT Lab) Aditya College of Engineering

7.

AIM:

To interface a DHT11 Temperature and Humidity sensor with an Arduino Uno and display the
measured values on an LCD (Liquid Crystal Display).

APPARATUS REQUIRED:

Arduino Uno

DHT11 Temperature and Humidity sensor

LCD (16x2 or 20x4)

Potentiometer (for LCD contrast adjustment)

Breadboard and jumper wires

WORKING PRINCIPLE:

The DHT11 sensor communicates with the Arduino Uno using a single-wire digital protocol. It sends
temperature and humidity data to the Arduino, which then processes the information. The Arduino
controls the LCD through the LiquidCrystal library, displaying the temperature and humidity readings
in real-time.

PROCEDURE:

Connect the DHT11 sensor to the Arduino Uno:

Connect the sensor's VCC pin to 5V on the Arduino.

Connect the sensor's GND pin to GND on the Arduino.

Connect the sensor's DATA pin to a digital pin (e.g., D2) on the Arduino.

Connect the LCD to the Arduino:

Department of CSE-IoT III B-Tech I Sem 54 | P a g e


Internet of Things (IoT Lab) Aditya College of Engineering
Connect the LCD's VCC pin to 5V on the Arduino.

Connect the LCD's GND pin to GND on the Arduino.

Connect the LCD's SDA pin to a digital pin (e.g., D4) on the Arduino.

Connect the LCD's SCL pin to a digital pin (e.g., D5) on the Arduino.

Adjust the contrast of the LCD using the potentiometer.

CODE:

#include <LiquidCrystal_I2C.h>

#include <dht.h>

dht DHT;

LiquidCrystal_I2C lcd(0x27, 16, 2); // Change the address (0x27) according to your LCD

void setup() {

lcd.begin(16, 2);

void loop() {

int chk = DHT.read11(D2); // Change the pin (D2) according to your setup

lcd.setCursor(0, 0);

lcd.print("Temp: ");

lcd.print(DHT.temperature);

lcd.print("C");

Department of CSE-IoT III B-Tech I Sem 55 | P a g e


Internet of Things (IoT Lab) Aditya College of Engineering
lcd.setCursor(0, 1);

lcd.print("Humidity: ");

lcd.print(DHT.humidity);

lcd.print("%");

delay(2000); // Wait for 2 seconds before updating the display

RESULT:

Upon uploading the code to the Arduino Uno, the LCD should display real-time temperature and
humidity readings obtained from the DHT11 sensor. The values will be updated every 2 seconds,
providing a continuous display of environmental conditions.

Department of CSE-IoT III B-Tech I Sem 56 | P a g e


Internet of Things (IoT Lab) Aditya College of Engineering

8.

Aim:

To design a street light automation system using an LDR (Light Dependent Resistor) sensor interfaced
with an Arduino Uno, with the goal of automatically controlling street lights based on ambient light
levels.

APPARATUS REQUIRED:

Arduino Uno

LDR (Light Dependent Resistor)

Relay module

Street light simulation (LED or lamp)

Resistors

Breadboard and jumper wires

WORKING PRINCIPLE:

The LDR sensor's resistance changes with variations in ambient light. In the absence of sunlight (or
when it gets dark), the resistance of the LDR increases, signaling low light conditions. The Arduino
Uno reads the LDR value and, based on the programmed threshold, activates or deactivates the street
light using a relay module. The relay is used to control the high-power street light circuit.

PROCEDURE:

Connect the LDR to the Arduino Uno:

Connect one leg of the LDR to the 5V pin on the Arduino.

Connect the other leg of the LDR to the analog pin (e.g., A0) on the Arduino.

Department of CSE-IoT III B-Tech I Sem 57 | P a g e


Internet of Things (IoT Lab) Aditya College of Engineering
Connect a resistor from the analog pin to the ground to form a voltage divider.

Connect the relay module to the Arduino:

Connect the signal pin of the relay module to a digital pin (e.g., D7) on the Arduino.

Connect the VCC and GND pins of the relay module to the 5V and GND on the Arduino.

Connect the street light simulation (LED or lamp) to the relay module.

CODE

int LDR_Pin = A0; // Define the LDR pin

int relayPin = 7; // Define the relay control pin

void setup() {

pinMode(LDR_Pin, INPUT);

pinMode(relayPin, OUTPUT);

void loop() {

int LDR_Value = analogRead(LDR_Pin);

// Adjust the threshold value based on ambient light conditions

if (LDR_Value < 500) {

digitalWrite(relayPin, HIGH); // Turn on the street light

} else {

digitalWrite(relayPin, LOW); // Turn off the street light

Department of CSE-IoT III B-Tech I Sem 58 | P a g e


Internet of Things (IoT Lab) Aditya College of Engineering
delay(1000); // Adjust delay as needed

RESULT:

When the ambient light decreases below the threshold value (e.g., it gets dark), the relay will be
activated, turning on the simulated street light. Conversely, when there is enough ambient light, the

Department of CSE-IoT III B-Tech I Sem 59 | P a g e


Internet of Things (IoT Lab) Aditya College of Engineering
relay will be deactivated, turning off the street light. The system demonstrates street light automation
based on environmental lighting conditions.

9. AIM:

To interface an OLED (Organic Light Emitting Diode) display with an Arduino Uno and display
graphical information.

APPARATUS REQUIRED:

Arduino Uno

OLED Display (128x64 pixels or similar)

Breadboard and jumper wires

WORKING PRINCIPLE:

OLED displays are capable of showing graphical information by illuminating individual pixels. The
Arduino communicates with the OLED display using the I2C (Inter-Integrated Circuit) protocol. The
Arduino sends commands and data to the OLED, instructing it to display text, shapes, or custom
graphics on the screen.

PROCEDURE:

Connect the OLED display to the Arduino Uno:

Connect the VCC pin of the OLED to the 5V pin on the Arduino.

Connect the GND pin of the OLED to the GND pin on the Arduino.

Connect the SDA pin of the OLED to the A4 (SDA) pin on the Arduino.

Connect the SCL pin of the OLED to the A5 (SCL) pin on the Arduino.

Download and install the "Adafruit_SSD1306" library for OLED displays in the Arduino IDE.

CODE

#include <Wire.h>

#include <Adafruit_GFX.h>

#include <Adafruit_SSD1306.h>

Department of CSE-IoT III B-Tech I Sem 60 | P a g e


Internet of Things (IoT Lab) Aditya College of Engineering

#define SCREEN_WIDTH 128

#define SCREEN_HEIGHT 64

Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);

void setup() {

if(!display.begin(SSD1306_I2C_ADDRESS, OLED_RESET)) {

Serial.println(F("SSD1306 allocation failed"));

for(;;);

delay(2000);

display.clearDisplay();

void loop() {

display.clearDisplay();

// Draw a rectangle

display.drawRect(10, 10, 50, 30, WHITE);

// Draw text

display.setTextSize(1);

display.setTextColor(WHITE);

display.setCursor(20, 40);

display.print("Hello, OLED!");

Department of CSE-IoT III B-Tech I Sem 61 | P a g e


Internet of Things (IoT Lab) Aditya College of Engineering

// Update the display

display.display();

delay(2000); // Adjust delay as needed

RESULT:

Upon uploading the code to the Arduino Uno, the OLED display should show a rectangle and text. The
rectangle represents a simple graphic, and the text "Hello, OLED!" is displayed on the screen. This
experiment demonstrates the interfacing of an OLED display with an Arduino Uno and the ability to
showcase graphical information on the OLED screen.

Department of CSE-IoT III B-Tech I Sem 62 | P a g e

You might also like