IoT Lab Manual

Download as pdf or txt
Download as pdf or txt
You are on page 1of 32

Lab Manual

PCIT-111 Internet of Things Laboratory


Practical 1: Familiarization with Arduino/ Raspberry Pi and perform necessary software
installation
Raspberry Pi and Arduino are two very popular boards among electronics DIY builders,
hobbyists and even professionals. Raspberry Pi and Arduino are quite different boards. While
Arduino is aimed at quick programming and circuit prototyping, Raspberry Pi acts as a learning
tool for Computer Programming (but you can find Raspberry Pi is several DIY Projects as well).
Each board has its own advantages and disadvantages.
Let us take a closer look at these two boards, understand the differences between
Raspberry and Arduino and also build a comparison of Raspberry Pi vs Arduino in a tabular
format. If you want to decide between the two, then it depends on the requirement of your
project but we hope this article will helpful in understanding the differences between these two
boards and helps you in selecting the right board for your next project.
Arduino:
Let us start with Arduino. Arduino was developed by Massimo Banzi Et Al. in Ivrea,
Italy. Arduino is a simple electronics prototyping tool with open-source hardware and software.
Arduino is essentially a Microcontroller development board using which you can Blink LEDs,
accept inputs from Buttons, read data from Sensors, control Motors and many other
“Microcontroller” related tasks.
The most popular Arduino board is the Arduino UNO, which is based on ATmega328P
Microcontroller from Atmel (now Microchip). Coming to the software side of Arduino, all
Arduino boards can be programmed in C and C++ programming languages using special
software called Arduino IDE. The Arduino IDE consists of all the toolchains for editing source
code, compiling and programming the Microcontroller on the Arduino board.
If you have previous experience with Microcontrollers like 8051, Atmel or PIC
Microcontrollers, then you probably understand the lengthy process of developing applications
using these microcontrollers. If you are not familiar, then let us see the process briefly.
First, you have to write the application software (the main source code) in a dedicated
IDE (like Keil, Atmel Studio or PIC’s MPLAB IDE). Then you have to compile the code and
generate the binary file in the form of a .hex file. Now using a special hardware called
“Programmer”, you have to upload the hex file to the target microcontroller using programmer
software.
Arduino simplified this process with plug-and-play style quick programming. Using a
single software (the Arduino IDE), you can write the code, compile it and upload it to the
Microcontroller. You also don’t need separate hardware for uploading the program. Simply plug-
in the Arduino board to a Computer through USB Port, hit the upload button, et voila, the
Microcontroller on Arduino board is ready to do its tasks.
Another important thing about Arduino is it is open-source. This means the design files
and the source code for software and libraries are freely available. You can use the hardware
design files as a reference and essentially make your own Arduino board.
Raspberry Pi:
The Raspberry Pi was developed by Eben Upton at the University of Cambridge in the United
Kingdom with the aim of teaching and improving programming skills of students in developing
countries. While Arduino is a Microcontroller based development board, the Raspberry Pi is a
Microprocessor (usually an ARM Cortex A Series) based board that acts as a computer.
You can connect several peripherals like a Monitor (through HDMI or AV Port), Mouse
and Keyboard (through USB), connect to internet (through Ethernet or Wi-Fi), add a Camera
(through the dedicated Camera Interface), just like we do to our desktop computer.
Since the entire Computer (the Processor, RAM, Storage, Graphics, Connectors, etc.) is
sitting on a single Printed Circuit Board, the Raspberry Pi (and other similar boards) are called as
Single Board Computers or SBC.

As Raspberry Pi is essentially a full computer, it can run an Operating System. The


Raspberry Pi Foundation, the organization which is responsible for designing and developing
Raspberry Pi SBC, also provides a Debian based Linux Distribution called the Raspberry Pi OS
(previously known as the Raspbian OS).
Another important thing about Raspberry Pi is, as it is a Linux based Computer, you can
develop software using several Programming Languages like C, C++, Python, Java, HTML, etc.
Despite its original intentions, which is to promote programming (like Python and Scratch
Programming Languages) in schools, the original Raspberry Pi SBC became extremely popular
among DIY builders, hobbyists and enthusiasts for developing several applications like Robotics,
Weather Stations, Camera based security systems etc.
Due to its success and popularity, the Raspberry Pi Foundation is continuously updating
and releasing new versions of Raspberry Pi with the latest one being the Raspberry Pi 4 Model
B. The hardware design files and the firmware of Raspberry Pi are not open-source.
Differences between Raspberry Pi and Arduino:
Both Arduino and Raspberry Pi are good teaching tools for students, beginners and hobbyists.
Let us see some of the differences between Raspberry Pi and Arduino.
 The main difference between them is: Arduino is microcontroller board, while Raspberry
Pi is a microprocessor based mini computer (SBC).
 The Microcontroller on the Arduino board contains the CPU, RAM and ROM. All the
additional hardware on Arduino Board is for power supply, programming and IO
Connectivity. Raspberry Pi SBC has all features of a computer with a processor, memory,
storage, graphics driver, connectors on the board.
 Raspberry Pi needs an Operating System to run. Arduino doesn’t need any operating
system. All you need is a binary of the compiled source code.
 Raspberry Pi comes with a fully functional operating system called Raspberry Pi OS
(previously known as Raspbian OS). Although Pi can use different operating systems,
Linux is preferred by Raspberry Pi Foundation. You can install Android, if you want.
Arduino does not have any operating system. You just need a firmware instructing the
Microcontroller what task to do.
 The clock speed of Arduino is 16 MHz while the clock speed of Raspberry Pi is around
1.2 GHz.
 Raspberry Pi is good for developing software applications using Python, while Arduino is
good for interfacing Sensors and controlling LEDs and Motors.
 This doesn’t mean we cannot connect sensors and LEDs to Raspberry Pi. To encourage
learning programming by controlling hardware, the Raspberry Pi consists of a 40-pin
GPIO, through which you can connect different electronic components like LEDs,
Buttons, Sensors, Motors etc. On Arduino, the GPIO is called as Digital IO (for digital
Input and Output) and Analog IN (for Analog Input).
 Using Arduino Shields, which plug into the Arduino Pin headers, you can add a dedicated
feature or functionality like a Motor Driver, Ethernet Connection, SD Card Reader, Wi-
Fi, Touchscreens, cameras etc. to Arduino. While Raspberry Pi is a self-contained board,
you can add external hardware like Touchscreen, GPS, RGB panels etc. to Raspberry Pi.
The Raspberry Pi Hardware Attached on Top or HAT Expansion Boards are inspired by
Arduino Shields, using which you can add additional functionality to Raspberry Pi. They
are connected to the GPIO Pins.
 The power requirements of Raspberry Pi and Arduino are completely different. Even
though they both are powered by USB (micro-USB or USB Type C for Raspberry Pi and
USB Type B for Arduino), Raspberry Pi needs more more current than Arduino. So, you
need a power adapter for Raspberry Pi but you can power Arduino from the USB port of
a Computer.
 Power interruption for Raspberry Pi may cause damage to the hardware, software or
applications. In case of Arduino, if there is any power cut it again restarts. So, Raspberry
Pi must be properly shutdown before disconnecting power.
 Arduino uses Arduino IDE for developing the code. While Raspberry Pi can use Python
IDLE, Eclipse IDE or any other IDE that is supported by Linux. You can also program
using the terminal itself with any text editor like Vim.
 Using the open-source hardware and software files of Arduino, you can essentially create
your own Arduino board. This is not possible with Raspberry Pi as it is not open-source.
 The cost of original Arduino UNO is $23 but there are several clones of Arduino which
are available for less than $4. Coming to Raspberry Pi, the original Raspberry Pi SBC
was around $35, but the latest Raspberry Pi 4 Model B is available in different price
points ($35, $55 or $75) depending on the memory configuration.
References:
 https://www.electronicshub.org/raspberry-pi-vs-arduino/
 https://electropeak.com/learn/install-arduino-ide-on-raspberry-pi/
Practical 2: To demonstrate the communication modules like BLE, WIFI, XBEE
BLE:
For developers of IoT devices, Bluetooth Low Energy (BLE, aka, Bluetooth Smart) has many
advantages. As the name implies, low energy consumption is among them, as is the availability
of complete BLE modules that provide essentially drop-in wireless connectivity. The key to
maximizing the benefits of BLE modules, though, is to play to BLE's strengths.
By now most developers have at least heard of Bluetooth. The wireless protocol, named
for the Viking who converted the Danes to Christianity, originally appeared in wireless audio
streaming applications. The BLE version, which became available with Bluetooth v4.0, targets
low-power applications by reducing transmitter power and data rate comared to the core
specification. (Bluetooth's current version is v4.2.) Unlike the full core specification, BLE is
limited to a 50m range and 0.27 Mbps.
Developers new to using the BLE standard should treat these reductions are key points to
consider in setting system expectations. Just because a design uses the BLE protocols does not
mean that low energy consumption is guaranteed. The design's operating range and data rate
have a role to play in establishing energy demands, as does the system's overall communications
strategy. Developers will need to consider these factors carefully to achieve the promised low
energy attribute.
There are other factors to consider when designing a BLE IoT device, as well. The user
interface (UI) for the device, for instance, can involve built-in buttons and displays as with
traditional embedded systems. Alternatively, designs can depend on an app running on a
smartphone for its UI. Similarly, a device can use either a mobile phone or a fixed gateway
device as its link to the Internet.
Developers can also choose to have their BLE devices always pair with a master to
establish bidirectional communications, or operate in “advertising mode” for unverified one-way
transmission of data in small amounts (around 30 bytes). Working in the advertising mode
allows a BLE device such as a sensor to periodically send data to a listener without using the
energy needed to establish and maintain a full, two-way link. The advertising interval and use of
advertising modes such as scan request (which allows a listener to request additional data from
the device) can significantly affect the energy use of a BLE design, however.
As with any type of wireless communications, BLE places significant demands on a
design team's skills, particularly in the radio frequency (RF) arena. There is some help available.
Integrated radio/microcontroller ICs such as the Nordic Semiconductor nRF52832 and the
Silicon Labs BGM12x can handle details like RF signal generation and detection, modulation,
and BLE communications protocols all in an off-the-shelf package, but teams will still need RF
expertise to handle factors such as board layout and antenna design. Once a design is complete,
teams will then need to take their device through certification processes to prove compliance
with various national regulatory standards as well as with the Bluetooth interoperability
standards.
BLE_SoC_Source-Silicon_Labs deploys the BLE modules. A BLE module is a fully-
contained BLE transceiver with controller and built-in antenna that is preprogrammed to handle
all a design's radio interactions. Some modules are available that serve purely as an IO device for
a host controller, making the BLE connection the logical equivalent of a serial port for design
purposes. Other modules are able to operate in a stand-alone (hostless) manner and make
available their processor and other IO resources to developers to run application code, as well.
Both module types come precertified with both the Bluetooth SIG (for interoperability) and
various regulatory agencies.
The full design and precertification of modules has a substantial effect on the cost and
effort of creating a BLE-enabled IoT product. The need for RF expertise, for instance, drops
dramatically as the module is essentially a drop-in component that already has the tricky details
resolved. Similarly, precertification eliminates much of the effort and cost to get a product
approved by regulatory agencies and listed with the Bluetooth SIG as a valid Bluetooth device.
WiFi:
WiFi has the advantage of addressing a very wide variety of profiles because of the proliferation
of its family of standards. This means it will play a role in most IoT environments, alone or
interworking with more specialized protocols, or with cellular. Some IoT applications, such as
vehicular services, or video-based apps like connected security cameras, will need the bandwidth
of the wireless broadband network, implemented to enable other requirements like low latency
(In critical environments this may take place in a private network or slice). WiFi is uniquely
placed to support broadband and narrowband IoT applications from a common platform that can
work at varying levels of power consumption and signal range. The next release of 5G standards,
Release 16, will prioritize IoT-focused capabilities such as latency below four milliseconds and
very high availability, to support emerging cases in the URLLC (ultra-reliable low latency
communications) category.
XBEE:
The Xbee devices communicate with each other wirelessly over the air. They do not have any
microcontroller or processor in themselves, so they cannot manage the received or sent data.
They can simply transfer the information what they receive. But they can be interfaced with
other microcontrollers and processors like Arduino, Raspberry Pi or PC via serial Interface.
So, basically, Xbee modules are capable of two types of communication – wireless
communication and serial communication. The wireless communication takes place between
Xbee devices so that the devices act as radio frequency (RF) devices. For data to transmit and
receive from one Xbee module to another, both devices should be on same network. The data
between two devices is transmitted wirelessly. By serial communication (UART), the Xbee
modules can communicate with microcontrollers and processors.
A microcontroller, processor or PC can send data through the serial interface to the Xbee
module (transmitter) and the Xbee module wirelessly transmits the data to other Xbee module
(Receiver). The receiver Xbee module transmits the data through the serial interface to
controller, processor or PC to which it is interfaced. The controller interfaced to the Xbee
module processes the information received by the Xbee devices. This way, controllers can
monitor and control remote devices by sending messages through the local Xbee modules.
The Xbee modules communicate with each other in two modes – Transparent mode and
API (Application Peripheral Interface) mode. This project is based on Transparent mode. In
transparent mode, Xbee modules act as serial line replacement. All data received through serial
input is immediately transmitted over the air. When other Xbee module receives data wirelessly,
it sends that exactly as it receives through the serial interface and vice versa. Contrary to this, in
the API mode, the data is transmitted with some additional information.
References:
 https://www.embedded.com/ble-modules-simplify-iot-design/
 https://www.iotforall.com/wifi-role-iot
 https://www.engineersgarage.com/iot-communication-between-two-devices-over-zigbee-
protocol-iot-part-37/
Practical 3: To find the IP address of Computer/ other devices
Windows 7 Instructions:
First, click on your Start Menu and type cmd in the search box and press enter.
A black and white window will open where you will type ipconfig /all and press enter.
There is a space between the command ipconfig and the switch of /all. Your ip address will be
the IPv4 address.
Windows 10 Instructions:
Right click the windows button in the lower left of your screen. Select command prompt from
the list.
A black and white window will open where you will type ipconfig /all and press enter.
There is a space between the command ipconfig and the switch of /all.
When a technician requests the IP address of your Ethernet card, the information that
follows after the title of Ethernet Local Area Adapter gigabit connection. Your IP address will
be the IPv4 address.
The wireless card information will follow the title of Wireless Lan adapter connection.
Any description that includes the word ‘Virtual’ is not the information that is needed for
creating an internet connection for you.
References:
 https://www.med.unc.edu/it/guide/operating-systems/how-do-i-find-the-host-name-ip-address-
or-physical-address-of-my-machine/
Practical 4: To interface LED/ Buzzer with Arduino/ Raspberry Pi and write a program to
turn ON/OFF LED for specific duration.
Arduino circuit with an LED and a button
To build the circuit you will need those components:
 Arduino board (any board, if you don’t have Uno you can easily adapt by finding
corresponding pins).
 Breadboard.
 LED – any color.
 Push button.
 220 Ohm resistor for the LED. If you don’t have this specific value, any resistor from 330
to 1k Ohm will do.
 10k Ohm resistor for the push button. If you don’t have, you can go until 20k-50k Ohm.
 A bunch of male to male wires (including if possible black, red, and other colors).
Here’s the circuit you have to make.

Step by step instructions to build the circuit:


 First, make sure to power off your Arduino – remove any USB cable.
 Plug a black wire between the blue line of the breadboard and a ground (GND) pin on the
Arduino board.
 Plug the LED. You can notice that the LED has a leg shorter than the other. Plug this
shorter leg to the ground (blue line here) of the circuit.
 Connect the longer leg of the LED to a digital pin (here pin no 8, you can change it). Add
a 220 Ohm resistor in between to limit the current going through the LED.
 Add the push button to the breadboard, like in the picture.
 Connect one leg of the button to the ground, and put a 10k Ohm resistor in between. This
resistor will act as a “pull down” resistor, which means that the default button’s state will
be LOW.
 Add a red wire between another leg of the button and VCC (5V).
 Finally, connect a leg of the button (same side as the pull down resistor) to a digital pin.
Turn on the LED when button is pressed, turn it off otherwise:
What we want to achieve is simple: when the button is not pressed, the LED is off. And when we
press the button the LED should be on.
The code:
#define LED_PIN 8
#define BUTTON_PIN 5
void setup() {
pinMode(LED_PIN, OUTPUT);
pinMode(BUTTON_PIN, INPUT);
}
void loop() {
if (digitalRead(BUTTON_PIN) == HIGH) {
digitalWrite(LED_PIN, HIGH);
}
else {
digitalWrite(LED_PIN, LOW);
}
}
First, as a best practice, we use some defines to keep the pin number for the LED and push
button. That way, if you have used different pins than I, you just need to modify those 2 lines.
Also, in the future if you want to change the LED from pin 8 to pin 11 for example, you can
modify this line without touching anything else in the code. The setup function is executed once
at the beginning of the program. This is the perfect time to initialize our pins with the pinMode()
function:
 OUTPUT for the LED, as we’re going to write data to it.
 INPUT for the push button, as we’re going to read data from it.
Now, the digital pins are correctly set up.
In the loop function, we start by reading the button’s state with the digitalRead() function.
As we have a pull down resistor on the button, we know that the non-pressed state will give us
the value LOW.
(Note: if you were using a pull up resistor, or no resistor at all – with the INPUT_PULLUP
option for pinMode – this would be the opposite. HIGH when the button is not pressed, and
LOW when it’s pressed.)
So, once we get the button’s state, we check if it’s HIGH or LOW:
 HIGH (pressed): we power on the LED with digitalWrite() and the HIGH state.
 LOW (not pressed): we power off the LED with digitalWrite() and the LOW state.
References:
 https://roboticsbackend.com/arduino-turn-led-on-and-off-with-button/
 https://create.arduino.cc/projecthub/SBR/working-with-an-led-and-a-push-button-71d8c1
 https://create.arduino.cc/projecthub/reverendfuzzy/simple-on-off-pushbutton-f637a7
Practical 5: To interface DHT11/ DHT22 sensor with Arduino/ Raspberry Pi and write a
program to print temperature and humidity readings.
The DHT11 measures relative humidity. Relative humidity is the amount of water vapor in air
vs. the saturation point of water vapor in air. At the saturation point, water vapor starts to
condense and accumulate on surfaces forming dew.
The saturation point changes with air temperature. Cold air can hold less water vapor
before it becomes saturated, and hot air can hold more water vapor before it becomes saturated.
The formula to calculate relative humidity is:

Relative humidity is expressed as a percentage. At 100% RH, condensation occurs, and at


0% RH, the air is completely dry.
How the DHT11 Measures Humidity and Temperature:
The DHT11 detects water vapor by measuring the electrical resistance between two
electrodes. The humidity sensing component is a moisture holding substrate with electrodes
applied to the surface. When water vapor is absorbed by the substrate, ions are released by the
substrate which increases the conductivity between the electrodes. The change in resistance
between the two electrodes is proportional to the relative humidity. Higher relative humidity
decreases the resistance between the electrodes, while lower relative humidity increases the
resistance between the electrodes.
The DHT11 measures temperature with a surface mounted NTC temperature sensor
(thermistor) built into the unit.
With the plastic housing removed, you can see the electrodes applied to the substrate:
An IC mounted on the back of the unit converts the resistance measurement to relative
humidity. It also stores the calibration coefficients, and controls the data signal transmission
between the DHT11 and the Arduino:

The DHT11 uses just one signal wire to transmit data to the Arduino. Power comes from
separate 5V and ground wires. A 10K Ohm pull-up resistor is needed between the signal line
and 5V line to make sure the signal level stays high by default (see the datasheet for more info).
There are two different versions of the DHT11 you might come across. One type has four pins,
and the other type has three pins and is mounted to a small PCB. The PCB mounted version is
nice because it includes a surface mounted 10K Ohm pull up resistor for the signal line. Here are
the pin outs for both versions:
How to Set Up the DHT11 on an Arduino:
Wiring the DHT11 to the Arduino is really easy, but the connections are different depending on
which type you have.
Connecting a Three Pin DHT11:

Display Humidity and Temperature on the Serial Monitor:


Before you can use the DHT11 on the Arduino, you’ll need to install the DHTLib library. It
has all the functions needed to get the humidity and temperature readings from the sensor. It’s
easy to install, just download the DHTLib.zip file below and open up the Arduino IDE. Then go
to Sketch>Include Library>Add .ZIP Library and select the DHTLib.zip file. After it’s installed,
upload this example program to the Arduino and open the serial monitor:
#include <dht.h>
dht DHT;
#define DHT11_PIN 7
void setup(){
Serial.begin(9600);
}
void loop(){
int chk = DHT.read11(DHT11_PIN);
Serial.print("Temperature = ");
Serial.println(DHT.temperature);
Serial.print("Humidity = ");
Serial.println(DHT.humidity);
delay(1000);
}
You should see the humidity and temperature readings displayed at one second intervals.
If you don’t want to use pin 7 for the data signal, you can change the pin number in line 5 where
it says #define DHT11_PIN 7.
Display Humidity and Temperature on an LCD
#include <dht.h>
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
dht DHT;
#define DHT11_PIN 7
void setup(){
lcd.begin(16, 2);
}
void loop(){
int chk = DHT.read11(DHT11_PIN);
lcd.setCursor(0,0);
lcd.print("Temp: ");
lcd.print(DHT.temperature);
lcd.print((char)223);
lcd.print("C");
lcd.setCursor(0,1);
lcd.print("Humidity: ");
lcd.print(DHT.humidity);
lcd.print("%");
delay(1000);
}
References:
 https://www.circuitbasics.com/how-to-set-up-the-dht11-humidity-sensor-on-an-arduino/
 https://create.arduino.cc/projecthub/pibots555/how-to-connect-dht11-sensor-with-
arduino-uno-f4d239
 https://www.circuitbasics.com/how-to-set-up-the-dht11-humidity-sensor-on-the-
raspberry-pi/
Practical 6: To interface PIR Sensor with Arduino/ Raspberry Pi and write a program to
check the motion of PIR sensor.
Passive Infrared Sensors, often referred to as PIR Sensors (also IR Motion Sensors and
Pyroelectric Sensors), are Motions Detectors that basically detects the changes in Infrared
Radiations emitted by a person.
Every living and non-living thing which has a temperature greater than absolute zero will
emit infrared radiations. Since the emitted energy is in the form of infrared radiation, whose
wavelength is greater than that of our visible light, we humans cannot see those radiations.
But PIR Sensors are built to detect those infrared radiations. Hence, they are employed in
a variety of applications like Motion Detectors, Security Systems, Intruder Alert and so forth.
The term “Passive” in the PIR Sensor means that the sensor will not emit any infrared
energy but rather detects infrared radiations emitted by other objects. This is in contrast to active
sensors, which perform both the actions (emitting and detection).
The PIR Sensor used in this project consists of a Pyroelectric Infrared Sensor, BISS0001
PIR Motion Detector IC, Fresnel lens and a few other components.

For connections, the PIR Sensor has three pins namely VCC, DATA and GND. Also, the PIR
Sensor has two potentiometers: one for adjusting the sensitivity of the sensor (or rather the
sensing distance of the sensor) and the other for adjusting the time for which the Output stays
high upon detecting any human movement.
The Fresnel lens covering the Pyroelectric Sensor play an important role in focusing the
infrared energy onto the sensor. With the help of this lens, the PIR Sensor can detect objects in
1200 angle. The range of the sensor is 8 meters i.e. it can detect human movement up to 8 meters.
PIR Sensor Adjustments
As mentioned earlier, there are two potentiometers for manually adjusting the sensitivity
and output timing.
With the help of the first potentiometer, you can adjust the Sensitivity i.e. the Sensing
Distance of the PIR Sensor. The range can be adjusted from 3 meters up to 8 meters. To increase
the sensing distance, turn the POT in clockwise direction and to decrease, turn it in anti-
clockwise direction.
Coming to the second potentiometer, you can adjust the duration for which the Output of
the PIR Sensor stays HIGH. It can be varied anywhere between 0.3s to 600s. To increase the
time, turn the POT in clockwise direction and in anti-clockwise direction to decrease the time.
PIR Motion Sensor using Raspberry Pi
As mentioned in the introduction, the aim of this project is to interface a PIR Sensor with
Raspberry Pi and implement a PIR Motion Sensor using Raspberry Pi and Python Programming.
Circuit Diagram:
The following Fritzing based images shows all the connections with respect to the PIR Motion
Sensor using Raspberry Pi.
Components Required:
 Raspberry Pi 3 Model B
 PIR Sensor
 5V Buzzes
 Connecting Wires
 Mini Breadboard
 Power Supply
 Computer
Circuit Design:
Connect the VCC and GND pins of the PIR Motion Sensor to +5V and GND pins of the
Raspberry Pi. Connect the DATA Pin of the PIR Sensor to GPIO23 i.e. Physical Pin 16 of the
Raspberry Pi. A 5V Buzzer is connected to GPIO24 i.e. Physical Pin 18 of the Raspberry Pi. The
other pin of the buzzer is connected to GND.
NOTE:
 I have directly connected the Buzzer to Raspberry Pi. But if you are not sure, connect it
through an NPN Transistor.
 From the previous Raspberry Pi Projects, you already know that Raspberry Pi Input pins
are 3.3V tolerant i.e. they work on 3.3V Logic.
 If you are wondering why I connected the output Data pin of the PIR Sensor directly to
the Raspberry Pi, then you need to be confused as I have checked the output levels of the
PIR Sensor on HIGH state and got a result of around 3.5V.
 You can also check for the same and then proceed with a level converter circuit (voltage
divider) if it is required.
Code:
The Programming part of the project is implemented using Python. The following is the Python
Script for the PIR Motion Sensor using Raspberry Pi.
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 "Initialzing PIR Sensor......"

time.sleep(12)

print "PIR Ready..."

print " "

try:

while True:

if GPIO.input(sensor):

GPIO.output(buzzer,True)

print "Motion Detected"

while GPIO.input(sensor):

time.sleep(0.2)

else:

GPIO.output(buzzer,False)

except KeyboardInterrupt:

GPIO.cleanup()
Working:
The working of the PIR Motion Sensor using Raspberry Pi is very simple. If the PIR Sensor
detects any human movement, it raises its Data Pin to HIGH. Raspberry Pi upon detecting a
HIGH on the corresponding input pin, will activate the Buzzer.
Applications
The applications of the PIR Motion Sensor using Raspberry Pi project have already been
mentioned. Some of them are:
 Automatic Room Light
 Motion Detection
 Intruder Alert
 Automatic Door Opening
 Home Security System
References:
 https://www.electronicshub.org/pir-motion-sensor-using-raspberry-pi/
Practical 7: To interface PI Camera with Arduino/ Raspberry Pi and write a program to
start the camera and to place the clicked pictures on the desktop.
The Raspberry Pi camera module is a great addition to your Pi. It will allow you to develop more
advanced applications with vision. And if you thought that taking pictures with the Pi camera
would be hard, here you’ll see that it’s just the opposite.
Setup and enable the Pi camera:
First, make sure to shutdown and power off your Raspberry Pi. Localize the camera port (don’t
confuse it with the display port which has a similar connector). The camera port is between the
HDMIs ports and the jack port. You can see “CAMERA” written next to it. Once you’ve located
the camera port, now make sure to plug the connector in the right way. The blue part should face
the jack and USB ports. Now, power on your Raspberry Pi. To open the settings for the Pi
camera, click on the Raspberry Pi icon > “Preferences” > “Raspberry Pi Configuration”.

Select the “Interfaces” tab and click on “Enable” next to “Camera:”.


Then, click on OK and reboot your Pi so the change will be effective.
Take a picture with the Raspberry Pi camera:
Now that the camera is plugged and enabled, you can start to take pictures. Here we’ll use the
raspistill command in the terminal – already installed on the Raspberry Pi OS.
First, open a terminal. If you don’t have the terminal icon on the top bar, click on the Raspberry
Pi icon > “Accessories” > “Terminal”.
First picture with Raspberry Pi and raspistill:
To take a picture, you’ll need to use the raspistill command and also provide one argument: the
name of the file for the output, so raspistill can save the photo into that file.
Let’s give it a try.
$ raspistill -o ~/Pictures/first_image.jpg
This command will take a few seconds to execute, because the camera functionality needs some
time for initialization.
After writing
raspistill
, I have added
-o ~/Pictures/first_image.jpg
. What does it mean?
 -o is the option to specify an output file name. Here o is the abbreviation for output.
 ~/Pictures/first_image.jpg is the path + file name for saving the picture. “~” means that
we’ll start from the home directory (from the user you’re connected as), then we have
“/Pictures/” to go in the Pictures directory of that user, and finally “first_image.jpg” is the
file name. I have used a “.jpg” extension here, you can also use other common image
extensions such as “.png”. Don’t forget the extension otherwise you might have trouble
opening the file if you share it for example on a Google Drive or on Windows.
Note: if you execute the same command again with the same file name, the previous file will be
replaced. So, if you want to keep all pictures you take with the Pi camera, make sure to provide a
different file name every time. You can also provide a relative path to save the picture from
where you are in the terminal.
$ cd ~/Pictures/
$ raspistill –o second_image.jpg
This will also create a new picture file in your “~/Pictures/” directory.
Open the image you’ve taken with raspistill
Now that you’ve taken a photo, you might want to actually see that photo. There are 2 main ways
of doing that.
First, and this is the easiest way since you’re already in the terminal: use the xdg-open command,
which is basically the same thing as double clicking on the file to execute it or open it.
$ xdg-open ~/Pictures/first_image.jpg
Or, if you’re already in the picture directory:
$ cd ~/Pictures/
$ xdg-open first_image.jpg
You should now be able to see the picture you’ve just taken with the Raspberry Pi camera!
The second way to open the picture, is to open a file manager (click on it on the top bar), and use
your mouse to find the file + double click on it.
Change resolution and flip the picture:
When you open an image in the image viewer, the resolution will be displayed on top. For
example when I view the picture I see 3280 x 2464, which is quite big. Now let’s say you want
to take pictures with a specific resolution: 1280 x 720. Also, depending on how you placed your
camera, you may see that the image is upside down. It would be quite boring to have to resize
and apply changes for all pictures you take. Instead, you can provide some options with the
raspistill command, to directly take pictures with the correct configuration. No need to do any
post-processing. And one more thing: by default the raspistill command will wait for 5 seconds
before taking a photo. The camera needs about 2 seconds to initialize, so we could also reduce
this timeout. Let’s try one command to change the resolution, flip the image vertically, and wait
2 seconds instead of 5.
$ raspistill -o ~/Pictures/new_image.jpg -w 1280 -h 720 -vf -t 2000
Let’s break this command down:
 Raspistill: the command to take a picture.
 -o ~/Pictures/new_image.jpg: the option to add an output file name, as previously seen.
 -w 1280 -h 720: to set the resolution you have to provide 2 options. –w for the width in
pixels, and –h for the height in pixels.
 -vf: this option will vertically flip the picture.
 -t 2000: this is the timeout option. Basically, once you execute the raspistill command in
the terminal, it will wait for x amount of milliseconds before taking the picture and
exiting. By default the amount is 5 seconds, or 5000 milliseconds. Here we choose 2000,
which means 2000ms or 2 seconds. Make sure to always provide duration in milliseconds
for that option.
References:
 https://www.pyimagesearch.com/2015/03/30/accessing-the-raspberry-pi-camera-with-
opencv-and-python/
 https://opensource.com/life/15/6/raspberry-pi-camera-projects
 https://roboticsbackend.com/raspberry-pi-camera-take-picture/
 https://www.allaboutcircuits.com/projects/how-to-build-a-picamera/
Practical 8: To transmit and access the sensed data to any cloud platform.
There are several ways to connect devices( any sensor or mobile) to the cloud. The evolution of
various ways of sending data from a device to cloud started from 1970’s. The evolution is still in
process and we are approaching better ways of sending data from device to cloud. Given below
are some methods of sending data from one device to cloud. The complexity of methodology
adopted increases as you read.
Sensor To Cloud Over Ethernet:
One of the simplest , rather evolved in the 1970s and 1980s, before the development of all the
radio links. The Sensor includes a processor which is tough enough to configure the data
uploading to cloud. The Ethernet connection would connect to wired Internet service. The
problem: Some places don’t have wired Internet. The processor could also have the ability to
update or modify the functions of the sensor. There is no involvement of radio
link.

Sensor To Mobile-Phone Network To Cloud:


The mobile phone network began to develop in the early 1980s. These early cellular networks
were the first widely available radio link for connecting sensors to the cloud. The disadvantages
are that
 The sensor still needs a wired connection to a mobile phone or needs an expensive
custom radio in the mobile-phone band to connect to the phone tower.
 The uplink (sensor to the phone tower) radio transmitter needs a fair amount of power to
reach the tower
 The user needs to pay the mobile network provider for usage.
Sensor To Long-Range Radio To The Cloud:

Regulators established several license free radio bands as early as 1947. But these did not attract
much interest until mobile phones really caught on in the late 1990s. IEE 802.15.4 standard has
frequencies of two bands at 902-928 MHz and 2400-2483 MHz.(There are other standards, such
as Zigbee, in one or both of these bands.)
One configuration that uses these bands is a mesh network. It consists of many small,
low-power radios connected to each other to relay data from remote sensors at the outer edges of
an area to radios at a collection point. Each collection point has access to the cloud. This allows
for wide-area usage by deploying sensors connected to very low-power radios.
Sensor To Wi-Fi Router To Cloud:

The 2400-2483 MHz band and another license-free band at 5130-5835 MHz were the original
frequency bands of the 802.11 Wi-Fi Standard (created in 1997). They are used primarily for Wi-
Fi access points, which is widely available in cities these days. The largest number of these
routers are in homes, businesses, and public gathering places (coffee shops, malls, and airports).
Industry and infrastructure used a small number of more specialized routers. This is the most
widely-used way today to connect mobile devices (laptops, tablets, smart phones) to the cloud. In
fact, most applications in smartphones connect to the cloud primarily through a Wi-Fi router.
Shortly after Wi-Fi-capable smartphones became available, remote sensors that could connect
directly to a Wi-Fi router also began to appear. Small sensors with low power Wi-Fi radio are
placed within the range of wifi routor. Internet connection is provided later.
Sensor To Mobile Phone To Cloud:
The sensor just needs to connect to a mobile phone instead of connecting directly to a Wi-Fi
router. The main reason for this is to allow the mobile-phone user to interact directly with the
sensor before sending the information up to the cloud.
These applications are served by the Bluetooth standard, created in 1998. It was added to
the 802.15.4 standard in 2003 but continues to maintain its own independent working group. It
works in the same 2400-2483 MHz license-free band used by one of the Wi-Fi bands.
From Sensor to Cloud: A Plug and Play Approach Evolving
Today, more powerful, evolved gateways, can function either as dedicated devices or as a virtual
part of a system. They play a new role in receiving, translating, processing and transmitting data
as transparent information to the spectrum of cloud interfaces. So this is enabled by the new
cloud API for IOT gateways. It is essentially a middle-ware and glue logic solution to enable
simple orchestration of wired and wireless sensor networks as well as embedded system
configurations. The cloud API provides application-ready software modules. They act as
blueprints for original equipment manufacturers (OEMs) to develop their own applications.
Therefore it helps in removing complexity and creating a smart path to connect all types of
sensor networks to any cloud platform.
Amplifying the importance of gateways:

Gateways are complex devices with excellent transcoding and decision-making capabilities.
Using integrated logic, these collect, analyse and transcode sensor data. Later it determines
whether it goes to the field, the cloud or perhaps another gateway. Their secure end-to-end
encryption further allows them to structure and move data consistently. For example, enabling
bidirectional communication with a specific cloud solution.
Enabled with the new cloud API, the IoT gateway communicates locally with intelligent
sensors. Now it is capable of processing and converting the received sensor data. Embedded
driver modules (EDMs) interface with hardware and third-party expansion cards, providing the
glue logic that translates received data into the semantics of the application-specific IoT gateway
logic. This sensor engine, with EDM modules incorporated in its structure, was the first software
components which was standardised as a cloud function module. Its critical value is in moving
data from local sensors to a generic middleware, independent of protocols.
References:
 https://readwrite.com/2015/10/13/sensor-data-device-to-cloud/
 https://iot4beginners.com/how-does-a-sensor-data-travel-from-one-device-to-the-cloud/

You might also like