Iot Practcal
Iot Practcal
Internet of Things
CS 801
1. Study and Install IDE of Arduino and different types of Arduino. 1-3
Aim: Study and Install IDE of Arduino and different types of Arduino.
Objectives: Student should get the knowledge of Arduino IDE and different types of Arduino Board
Outcomes: Student will be get knowledge of Arduino IDE and different types of Arduino Board
Arduino:
Arduino is a prototype platform (open-source) based on an easy-to-use hardware and software. It consists
of a circuit board, which can be programed (referred to as a microcontroller) and a ready-made software
called Arduino IDE (Integrated Development Environment), which is used to write and upload the
computer code to the physical board.
Arduino provides a standard form factor that breaks the functions of the micro-controller into a more
accessible package.
Arduino is a prototype platform (open-source) based on an easy-to-use hardware and software. It consists
of a circuit board, which can be programed (referred to as a microcontroller) and a ready-made software
called Arduino IDE (Integrated Development Environment), which is used to write and upload the
computer code to the physical board.
Arduino boards are able to read analog or digital input signals from different sensors and turn it into an
output such as activating a motor, turning LED on/off, connect to the cloud and many other actions.
You can control your board functions by sending a set of instructions to the microcontroller on the board
via Arduino IDE (referred to as uploading software).
Unlike most previous programmable circuit boards, Arduino does not need an extra piece of hardware
(called a programmer) in order to load a new code onto the board. You can simply use a USB cable.
Additionally, the Arduino IDE uses a simplified version of C++, making it easier to learn to program.
Finally, Arduino provides a standard form factor that breaks the functions of the micro-controller into a
more accessible package.
1
Download the Arduino Software (IDE)
Get the latest version from the arduino.cc web site. You can choose between the Installer (.exe) and the
Zip packages. We suggest you use the first one that installs directly everything you need to use the
Arduino Software (IDE), including the drivers. With the Zip package you need to install the drivers
manually. The Zip file is also useful if you want to create a portable installation.
When the download finishes, proceed with the installation and please allow the driver installation process
when you get a warning from the operating system.
Choose the installation directory (we suggest to keep the default one)
The process will extract and install all the required files to execute properly the Arduino Software (IDE)
When the Arduino Software (IDE) is properly installed you can go back to the
Arduino USB
1. Arduino uno
This is the latest revision of the basic Arduino USB board. It connects to the computer with a standard
USB cable and contains everything else you need to program and use the board.
2. Arduino NG REV-C
Revision C of the Arduino NG does not have a built-in LED on pin 13 - instead you'll see two small
unused solder pads near the labels "GND" and "13".
Arduino Bluetooth
The Arduino BT is a microcontroller board originally was based on the ATmega168, but now is supplied
2
With the 328, and the Bluegiga WT11 bluetooth module. It supports wireless serial communication over
bluetooth.
Arduino Mega
The original Arduino Mega has an ATmega1280 and an FTDI USB-to-serial chip.
Arduino NANO
The Arduino Nano 3.0 has an ATmega328 and a two-layer PCB. The power LED moved to the top of the
board.
Experiment No 2
Objectives: Student should get the knowledge of Arduino Board and different types of LED
Outcomes: Student will be Write program using Arduino IDE for Blink LED
Hardware Requirements:
1x Breadboard
1x 330Ω Resistor
With a simple modification of the breadboard, we could attach the LED to an output pin of the Arduino.
Move the red jumper wire from the Arduino5V connector to D13, as shown below
3
Connect LED's long leg (anode) to digital pin 13
void setup() {
pinMode(13, OUTPUT); // Set pin 13 as output
}
void loop() {
digitalWrite(13, HIGH); // Turn LED ON
delay(1000); // Wait for 1 second
digitalWrite(13, LOW); // Turn LED OFF
delay(1000); // Wait for 1 second
}
Experiment No 3
Aim: Write Program for RGB LED using Arduino.
Objectives: Student should get the knowledge of Arduino IDE and RGB Led
Outcomes: Student will be developed programs using Arduino IDE and Arduino Board for RGB Led
Hardware Requirements:
1x Breadboard
1x 330Ω Resistor
With a simple modification of the breadboard, we could attach the LED to an output pin of the Arduino.
Move the red jumper wire from the Arduino 5V connector to D13, as shown below:
4
Now load the 'Blink' example sketch from Lesson 1. You will notice that both the built-in 'L' LED
the external LED should now blink.
The following test sketch will cycle through the colors red, green, blue, yellow, purple, and aqua.
These colors being some of the standard Internet colors.
void loop()
{
analogWrite(redPin, red);
analogWrite(greenPin, green);
5
analogWrite(bluePin, blue);
}
The sketch starts by specifying which pins are going to be used for each of the colors:
int bluePin = 9;
The next step is to write the 'setup' function. As we have learnt in earlier lessons, the setup function runs
just once after the Arduino has reset. In this case, all it has to do is define the three pins we are using as
being outputs.
void setup()
{
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
}
Before we take a look at the 'loop' function, lets look at the last function in the sketch. void setColor(int red, int green,
int blue)
{
analogWrite(redPin, red);
analogWrite(greenPin, green);
analogWrite(bluePin, blue);
}
This function takes three arguments, one for the brightness of the red, green and blue LEDs. In each case
the number will be in the range 0 to 255, where 0 means off and 255 means maximum brightness. The
function then calls 'analogWrite' to set the brightness of each LED.
If you look at the 'loop' function you can see that we are setting the amount of red, green and blue light
that we want to display and then pausing for a second before moving on to the next color.
Void loop(){
setColor(255, 0, 0); // red
delay(1000);
setColor(0, 255, 0); //
green delay(1000);
setColor(0, 0, 255); // blue
delay(1000);
setColor(255, 255, 0);// yellow
delay(1000);
setColor(80, 0, 80); // purple
delay(1000);
setColor(0, 255, 255);// aqua
delay(1000);
}
6
Experiment No 4
Aim: Study the Temperature sensor and Write Program foe monitor temperature using Arduino.
Outcomes: Student will be developed programs using Arduino IDE and Arduino Board for Temperature
Sensor
These sensors have little chips in them and while they're not that delicate, they do need to be handled
properly. Be careful of static electricity when handling them and make sure the power supply is
connected up correctly and is between 2.7 and 5.5V DC - so don't try to use a 9V battery!
They come in a "TO-92" package which means the chip is housed in a plastic hemi-cylinder with three
legs. The legs can be bent easily to allow the sensor to be plugged into a breadboard. You can also solder
to the pins to connect long wires. If you need to waterproof the sensor, you can see below for an
Instructable for how to make an excellent case.
Reading the Analog Temperature Data
Unlike the FSR or photocell sensors we have looked at, the TMP36 and friends doesn't act like a resistor.
Because of that, there is really only one way to read the temperature value from the sensor, and that is
plugging the output pin directly into an Analog (ADC) input.
Remember that you can use anywhere between 2.7V and 5.5V as the power supply. No matter what supply
the analog voltage reading will range from about 0V (ground) to about 1.75V.
If you're using a 5V Arduino, and connecting the sensor directly into an Analog pin, you can use these
formulas to turn the 10-bit analog reading into a temperature:
This formula converts the number 0-1023 from the ADC into 0-5000mV (= 5V)
7
If you're using a 3.3V Arduino, you'll want to use this:
This formula converts the number 0-1023 from the ADC into 0-3300mV (= 3.3V)
Simple Thermometer
void setup()
{
Serial.begin(9600); //Start the serial connection with the computer
//to view the result open the serial monitor
}
void loop()// run over and over again
{
//getting the voltage reading from the temperature sensor
int reading = analogRead(sensorPin);
// converting that reading to voltage, for 3.3v arduino use
3.3
8
Experiment No 5
Aim: Write a program to study Bluetooth connectivity with NodeMCU.
Components Required: NodeMCU (ESP8266), HC-05 Bluetooth Module, LED, 220Ω Resistor, Jumper
Wires, Breadboard, Android Mobile with Bluetooth Terminal App
Circuit Connections:
HC-05 Bluetooth Module NodeMCU
VCC 3.3V
GND GND
TXD D7 (GPIO13)
RXD D8 (GPIO15) (Use voltage divider to reduce 3.3V to 3.0V)
Connect LED's anode (long leg) to D2 (GPIO4) through a 220Ω resistor
Connect cathode to GND
Arduino Code:
#define LED D2
void setup() {
Serial.begin(9600); // Start serial communication
pinMode(LED, OUTPUT); // Set LED pin as output
digitalWrite(LED, LOW); // LED off initially
}
void loop() {
if (Serial.available()) {
char data = Serial.read(); // Read incoming data
if (data == '1') {
digitalWrite(LED, HIGH); // Turn ON LED
} else if (data == '0') {
digitalWrite(LED, LOW); // Turn OFF LED
}
}
}
Working Explanation:
The NodeMCU receives data from the HC-05 Bluetooth module via serial communication.
If '1' is received, the LED turns ON.
If '0' is received, the LED turns OFF.
You can use apps like Bluetooth Terminal or Serial Bluetooth Terminal on Android to send data.
Result:
Bluetooth communication is successfully established with NodeMCU, and the LED is controlled using commands
sent from a smartphone.
9
Experiment No 6
Aim: Study and Implement RFID using Arduino.
Objective:
To understand the working of an RFID system and write a program to read RFID tag data using Arduino
An RFID reader (RC522).
Circuit Connections:
RFID Module (MFRC522) Arduino UNO
VCC 3.3V
GND GND
RST D9
SDA D10
MOSI D11
MISO D12
SCK D13
Theory:
RFID (Radio Frequency Identification) is a technology that uses electromagnetic fields to
automatically identify and track tags attached to objects. An RFID system consists of two components:
Reader (RC522 Module) – Emits radio waves and receives signals back from the RFID tag.
RFID Tag/Card – Contains a microchip and antenna which transmits data when activated by the reader.
The RC522 RFID module uses SPI communication to interface with microcontrollers like Arduino.
The unique ID of an RFID tag can be used for identification and security applications.
Arduino Code:
#include <SPI.h>
#include <MFRC522.h>
#define SS_PIN 10
#define RST_PIN 9
MFRC522 rfid(SS_PIN, RST_PIN); // Create MFRC522 instance
void setup() {
Serial.begin(9600); // Start serial communication
SPI.begin(); // Init SPI bus
rfid.PCD_Init(); // Init MFRC522 RFID reader
Serial.println("Place your RFID tag near the reader...");
10
}
void loop() {
if (!rfid.PICC_IsNewCardPresent() || !rfid.PICC_ReadCardSerial())
return;
Procedure:
Connect the RC522 module to the Arduino as per the connection
Open the Arduino IDE and install the MFRC522 library.
Upload the code to the Arduino UNO.
Open the Serial Monitor at 9600 baud.
Place an RFID card/tag near the RC522 reader.
Observe the UID (Unique Identifier) of the card printed on the Serial Monitor.
Result:
The RFID system was successfully implemented using Arduino. The UID of RFID cards was accurately read
And displayed on the Serial Monitor.
Conclusion:
The experiment demonstrated how to interface an RFID reader with Arduino and identify RFID cards using
their unique IDs. This forms the basis of access control, inventory systems, and other security applications.
11
Experiment No 7
Raspberry Pi
The Raspberry Pi is a series of small single-board computers developed in the United Kingdom by the
Raspberry Pi Foundationto promote the teaching of basic computer science in schools and in developing
countries.[4][5][6] The original model became far more popular than anticipated, selling outside of its
target market for uses such as robotics. Peripherals (including keyboards, mice and cases) are not
included with the Raspberry Pi. Some accessories however have been included in several official and
unofficial bundles.
According to the Raspberry Pi Foundation, over 5 million Raspberry Pis have been sold before February
2015, making it the best-selling British computer.[8] By November 2016 they had sold 11 million
units[9][10], reaching 12.5m in March 2017, making it the third best-selling "general purpose computer"
ever.
To get started with Raspberry Pi, you need an operating system. NOOBS (New Out Of Box Software) is
an easy operating system install manager for the Raspberry Pi.
GO to the https://www.raspberrypi.org/downloads/
Click on NOOBS, then click on the Download ZIP button under ‘NOOBS (offline and network install)’,
and select a folder to save it to.
It is best to format your SD card before copying the NOOBS files onto it. To do this:
Insert your SD card into the computer or laptop’s SD card reader and make a note of the drive letter
allocated to it, e.g. G:/
12
In SD Formatter, select the drive letter for your SD card and format it.
Once your SD card has been formatted, drag all the files in the extracted NOOBS folder and drop them
onto the SD card drive.
When this process has finished, safely remove the SD card and insert it into your Raspberry Pi.
FIRST BOOT
Your Raspberry Pi will boot, and a window will appear with a list of different operating systems that you
can install. We recommend that you use Raspbian – tick the box next to Raspbian and click on Install.
Raspbian will then run through its installation process. Note that this can take a while.
When the install process has completed, the Raspberry Pi configuration menu (raspi-config) will load.
Here you are able to set the time and date for your region, enable a Raspberry Pi camera board, or even
create users. You can exit this menu by using Tab on your keyboard to move to Finish.
The default login for Raspbian is username pi with the password raspberry. Note that you will not see
any writing appear when you type the password. This is a security feature in Linux.
To load the graphical user interface, type startx and press Enter.
13
Experiment No 8
Objectives: Student should get the knowledge of LED blinking using Raspberry Pi.
Outcomes: Student will be developed program of LED bilking using Raspberry Pi.
Hardware Requirements:
1x 330Ω Resistor
2x Jumper Wires
Semiconductor light-emitting diode is a type of component which can turn electric energy into light
energy via PN junctions. By wavelength, it can be categorized into laser diode, infrared light-emitting
diode and visible light-emitting diode which is usually known as light-emitting diode (LED).
When 2V-3V forward voltage is supplied to an LED, it will blink only if forward currents flow through
the LED. Usually there are red, yellow, green, blue and color-changing LEDs which change color with
different voltages. LEDs are widely used due to their low operating voltage, low current, luminescent
stability and small size.
LEDs are diodes too. Hence they have a voltage drop which usually varies from 1V to 3V depending on
their types. Generally they brighten if supplied with a 5mA-30mA current and we usually use 10mA-
20mA.Thus when an LED is used ,it is necessary to connect a current-limiting resistor to protect it from
being burnt.
In this experiment, connect a 220Ω resistor to the anode of the LED, then the resistor to 3.3 V and
connect the cathode of the LED to GPIO0 (See Raspberry Pi Pin Number Introduction). Write 1 to
GPIO0, and the LED will stay off; write 0 to GPIO0, and then the LED will blink, just as indicated by the
principle above
14
Components Required: Raspberry Pi (any model), LED, 220Ω resistor, Breadboard, Jumper wires,
Monitor, Keyboard, Mouse (for GUI) or SSH access
Circuit Connections:
LED Pin Connects To
Long leg (Anode) GPIO17 (Pin 11 on Raspberry Pi) via 220Ω resistor
Short leg (Cathode) GND (Pin 6 on Raspberry Pi)
try:
while True:
GPIO.output(led_pin, GPIO.HIGH) # Turn LED ON
time.sleep(1) # Wait 1 second
GPIO.output(led_pin, GPIO.LOW) # Turn LED OFF
time.sleep(1) # Wait 1 second
except KeyboardInterrupt:
GPIO.cleanup() # Reset GPIO pins when exiting
How to Run:
Open terminal on Raspberry Pi.
Save the code in a file, e.g., blink.py
Run the program using:
Working Explanation:
GPIO17 is set as an output.
The LED turns ON and OFF every 1 second in a loop.
KeyboardInterrupt (Ctrl+C) safely stops the program and cleans up GPIO settings.
Result:
The LED connected to the Raspberry Pi blinks ON and OFF continuously with a 1-second delay.
15
Experiment No 9
Aim: Study and Implement Zigbee Protocol using Arduino / Raspberry Pi.
Objectives: Student should get the knowledge of Zigbee Protocol using Raspberry Pi.
Outcomes: Student will be developed program of Zigbee Protocol using Raspberry Pi.
Hardware Requirements
Raspberry Pi2
XBee 1mW Wire Antenna- Series 1 (2 No:)
XBee Explorer Dongle (2 No:)
ZigBee is a communication device used for the data transfer between the controllers, computers, systems,
really anything with a serial port. As it works with low power consumption, the transmission distances is
limited to 10–100 meters line-of-sight. ZigBee devices can transmit data over long distances by passing
data through a mesh network of intermediate devices to reach more distant ones. ZigBee is typically used
in low data rate applications that require long battery life and secure networking. Its main applications
are in the field of wireless sensor network based on industries as it requires short-range low-rate wireless
data transfer. The technology defined by the ZigBee specification is intended to be simpler and less
expensive than other wireless networks.
Here we make use of an interface of Zigbee with Raspberry Pi2 for a proper wireless communication.
Raspberry Pi2 has got four USB ports, so it is better to use a Zigbee Dongle for this interface. Now we
want to check the communication between the two paired ZigBee module
The response showed inside a red box indicates the presence of a usb device in the module.
Write a python script to perform Zigbee communication which is given below.
import serial
# Enable USB Communication
ser = serial.Serial('/dev/ttyUSB0', 9600,timeout=.5)
while True:
ser.write('Hello User \r\n') # write a Data
incoming = ser.readline().strip()
print 'Received Data : '+ incoming
The two zigbee must be in a line of sight and check the results in the Python shell and in the
hyperterminal of the computer.
16
Experiment No 10
Aim: Study and implement MQTT protocol using Arduino.
Objectives: Student should get the knowledge of MQTT Protocol using Arduino.
Outcomes: Student will be developed programs using Arduino IDE and Arduino Board for MQTT
Protocol
Components Required:
NodeMCU (ESP8266) or ESP32
Arduino IDE
Wi-Fi connection
MQTT Broker (e.g., broker.hivemq.com)
LED (optional for testing output)
Jumper wires, breadboard
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
WiFiClient espClient;
PubSubClient client(espClient);
#define LED D2
void setup_wifi() {
delay(10);
Serial.println("Connecting to WiFi...");
WiFi.begin(ssid, password);
17
Serial.println("\nWiFi connected");
}
if (message == "ON") {
digitalWrite(LED, HIGH);
} else if (message == "OFF") {
digitalWrite(LED, LOW);
}
}
void reconnect() {
while (!client.connected()) {
Serial.print("Connecting to MQTT...");
if (client.connect("ArduinoClient")) {
Serial.println("connected");
client.subscribe("home/led"); // Subscribe to topic
} else {
Serial.print("Failed, rc=");
Serial.print(client.state());
delay(2000);
}
}
}
void setup() {
pinMode(LED, OUTPUT);
Serial.begin(115200);
setup_wifi();
client.setServer(mqtt_server, 1883);
client.setCallback(callback);
}
void loop() {
if (!client.connected()) {
reconnect();
}
client.loop();
}
18
MQTT:
MQ Telemetry Transport (MQTT) is an open source protocol for constrained devices and low-bandwidth,
high-latency networks. It is a publish/subscribe messaging transport that is extremely lightweight and
ideal for connecting small devices to constrained networks.
MQTT is bandwidth efficient, data agnostic, and has continuous session awareness. It helps minimize the
resource requirements for your IoT device, while also attempting to ensure reliability and some degree of
assurance of delivery with grades of service.
MQTT targets large networks of small devices that need to be monitored or controlled from a back-end
server on the Internet. It is not designed for device-to-device transfer. Nor is it designed to “multicast”
data to many receivers. MQTT is extremely simple, offering few control options.
MQTT methods
MQTT defines methods (sometimes referred to as verbs) to indicate the desired action to be performed
on the identified resource. What this resource represents, whether pre-existing data or data that is
generated dynamically, depends on the implementation of the server. Often, the resource corresponds to a
file or the output of an executable residing on the server.
Connect
Disconnect
Waits for the MQTT client to finish any work it must do, and for the TCP/IP session to disconnect.
Subscribe
UnSubscribe
Requests the server unsubscribe the client from one or more topics.
Publish
Returns immediately to the application thread after passing the request to the MQTT client.
19