Iot Manual
Iot Manual
Manual
INDEX
S.No Title Page
No.
1. Install IDE of Arduino and write a program using Arduino IDE to 3-6
blink LED.
2. Interface LED and buzzer with Arduino to buzz for a period of time. 6-7
4. a) Control a servo motor using Arduino with an input given through 16-21
a push button (e.g: When the push button is pressed the servo
motor has to rotate by 15 degrees). b) Rotate Stepper motor either
clockwise or anti clockwise at ‘n’ number of steps using Arduino.
10. Post/read the data to/from the cloud via MQTT broker 59-60
with a Raspberry Pi.
12. Implement an intruder alert system that alerts through email 65-68
2
WEEK-1
Install IDE of Arduino and write a program using Arduino IDE to blink LED.
1.
AIM: To implement a program to blink LED every second.
PROGRAM:
void setup(){
pinMode(8,OUTPUT);
}
void loop(){
digitalWrite(8,1);
delay(1000);
digitalWrite(8,0);
delay(1000);
}
CIRCUIT:
2.
AIM: To implement a program to blink 2 LEDs every second
alternatively. PROGRAM:
void setup(){
pinMode(8,OUTPUT);
3
pinMode(9,OUTPUT);
}
void loop(){
digitalWrite(8,1);
digitalWrite(9,0);
delay(1000);
digitalWrite(8,0);
digitalWrite(9,1);
delay(1000);
}
CIRCUIT:
3.
AIM: To implement a program to blink 2 LEDs every second alternatively for 10
times. PROGRAM:
void setup(){
pinMode(8,OUTPUT);
pinMode(9,OUTPUT);
int i=0;
}
void loop(){
while(i<10){
digitalWrite(8,1);
digitalWrite(9,0);
delay(1000);
4
digitalWrite(8,0);
digitalWrite(9,1);
delay(1000);
i++;
}
}
CIRCUIT:
4.
AIM: To implement a program to change the intensity of LED.
PROGRAM:
void setup()
{
pinMode(8,OUTPUT);
}
void loop()
{
for(int i=0;i<=255;i++)
{
analogWrite(8,i);
delay(100);
}
for(int i=255;i>0;i--)
{
analogWrite(8,i);
delay(100);
}
5
CIRCUIT:
WEEK-2
6
CIRCUIT:
WEEK-3
Interface RGB LED with Arduino to obtain different colours and brightness using
PWM 1.
AIM: To implement a program to obtain red, blue and green colors of RGB LED with user
input.
PROGRAM:
int c;
void setup() {
Serial.begin(9600);
pinMode(10,OUTPUT);
pinMode(8,OUTPUT);
pinMode(9,OUTPUT);
}
void loop()
{
c = Serial.parseInt();
if(c==1) {
digitalWrite(8,1);
digitalWrite(9,0);
digitalWrite(10,0);
}
else if(c==2) {
digitalWrite(9,1);
digitalWrite(8,0);
digitalWrite(10,0);
7
}
else if(c==3) {
digitalWrite(10,1);
digitalWrite(8,0);
digitalWrite(9,0);
}
Serial.println(c);
}
CIRCUIT:
8
2.
AIM: To implement a program to obtain different colors from RGB LED through user
input. PROGRAM:
String color = "";
void setup()
{
Serial.begin(9600);
pinMode(9,OUTPUT);
pinMode(10,OUTPUT);
pinMode(11,OUTPUT);
}
void loop()
{
color = Serial.readString();
color.trim();
9
}
}
CIRCUIT:
10
3.
AIM: To implement a program to change the intensity of RGB LED.
PROGRAM:
void setup()
{
pinMode(9,OUTPUT);
pinMode(10,OUTPUT);
pinMode(11,OUTPUT);
void loop()
{
for(int i = 0; i <=255; i++)
{
analogWrite(9,i);
analogWrite(10,i);
analogWrite(11,i);
delay(100);
}
for(int i = 255; i >=0; i--)
{
analogWrite(9,i);
analogWrite(10,i);
analogWrite(11,i);
delay(100);
}
}
CIRCUIT:
11
4.
AIM: To implement a program to increase the intensity of RGB LED.
PROGRAM:
void setup(){}
void loop()
{
for(i=0;i<=255;i++)
{
for(j=0;j<=255;j++)
{
for(k=0;k<=255;k++)
{
RGB(i,j,k);
delay(100);
}
}
}
}
void RGB(int R, int G, int B)
{
analogWrite(9, R);
analogWrite(10,G);
analogWrite(11,B);
}
CIRCUIT:
12
5.
AIM: To implement a program to first increase the intensity of RGB LED in the order
- green, red and blue.
PROGRAM:
int i;
void setup() {}
void loop()
{
for(int i=0;i<=255;i++)
{
RGB(0,i,0);
delay(100);
}
for(int i=0;i<=255;i++)
{
RGB(i,0,0);
delay(100);
}
for(int i=0;i<=255;i++)
{
RGB(0,0,i);
delay(100);
}
}
void RGB()
{
analogWrite(9,R);
analogWrite(10,G);
analogWrite(11,B);
}
13
CIRCUIT:
6.
AIM: To implement a program to switch ON any one of the 4
LEDs PROGRAM:
int c;
void setup(){
Serial.begin(9600);
pinMode(8,OUTPUT);
pinMode(9,OUTPUT);
pinMode(10,OUTPUT);
pinMode(11,OUTPUT);
14
}
void loop(){
c=Serial.parseInt();
if(c==1){
digitalWrite(8,1);
digitalWrite(9,0);
digitalWrite(10,0);
digitalWrite(11,0); }
else if(c==2){
digitalWrite(8,0);
digitalWrite(9,1);
digitalWrite(10,0);
digitalWrite(11,0); }
else if(c==3){
digitalWrite(8,0);
digitalWrite(9,0);
digitalWrite(10,1);
digitalWrite(11,0); }
else if(c==4){
digitalWrite(8,0);
digitalWrite(9,0);
digitalWrite(10,0);
digitalWrite(11,1); }
}
15
CIRCUIT:
WEEK-4
a) Control a servo motor using Arduino with an input given through a push button (e.g: When
the push button is pressed the servo motor has to rotate by 15 degrees). b) Rotate Stepper
motor either clockwise or anti clockwise at ‘n’ number of steps using Arduino.
1.
AIM: To implement a program to rotate servo motor 900 for every 500ms.
PROGRAM:
#include<Servo.h>
Servo s1;
int servopin=8;
16
void setup(){
s1.attach(servopin);
}
void loop(){
s1.write(90);
delay(500);
}
CIRCUIT:
2.
AIM: To implement a program to rotate servo motor 1800 in clockwise direction.
PROGRAM:
#include<Servo.h>
Servo s1;
int servopin=8;
void setup(){
s1.attach(servopin);
}
void loop(){
for(i=0;i<=180;i+=45){
s1.write(i);
delay(500);
17
}
}
CIRCUIT:
3.
AIM: To implement a program to rotating servo motor 1800 in anti-clockwise direction.
PROGRAM:
#include<Servo.h>
Servo s1;
int servopin=8;
void setup(){
s1.attach(servopin);
}
18
void loop(){ }
for(i=180;i>=0;i--){ s1.write(i);
delay(500); CIRCUIT:
}
4.
AIM: To implement a program to rotate stepper motor 3600 in clockwise and then in anti
clockwise direction.
PROGRAM:
#include<Stepper.h>
int steps_per_rev=32;
int gear_reduction=64;
int steps_req=gear_reduction*steps_per_rev;
Stepper motor(steps_per_rev,2,4,3,5);
void setup(){
Serial.begin(9600);
}
void loop(){
Serial.println(steps_req);
19
motor.setSpeed(900);
motor.step(steps_req);
delay(1000);
Serial.println(steps_req);
motor.setSpeed(900);
motor.step(-steps_req);
delay(1000);
}
CIRCUIT:
5.
AIM: To implement a program to rotate stepper motor 1800 in clockwise and then 900 in anti
clockwise direction.
PROGRAM:
#include<Stepper.h>
int steps_per_rev=32;
int gear_reduction=64;
int steps_req=gear_reduction*steps_per_rev;
Stepper motor(steps_per_rev,2,4,3,5);
void setup(){
Serial.begin(9600);
}
void loop(){
20
Serial.println(steps_req);
motor.setSpeed(900);
motor.step(steps_req/2);
delay(1000);
Serial.println(steps_req);
motor.setSpeed(900);
motor.step(-steps_req/4);
delay(1000);
}
CIRCUIT:
21
WEEK-5
Control any two actuators connected to the Arduino using
Bluetooth/Wifi. 1.
AIM: To implement a program to send and receive messages using Bluetooth module with
Arduino.
PROGRAM:
#include <SoftwareSerial.h>
SoftwareSerial EEBLUE(10,11);
void setup()
{
Serial.begin(9600);
EEBLUE.begin(9600);
Serial.println("Bluetooth is ready ");
}
void loop()
{
if(EEBLUE.available())
Serial.write(EEBLUE.read());
if(Serial.available())
EEBLUE.write(Serial.read());
}
OUTPUT:
22
2.
AIM: To implement a program using Bluetooth to interface LED based on Bluetooth
terminal input.
PROGRAM:
#include <SoftwareSerial.h>
SoftwareSerial EEBLUE(10,11);
int input;
void setup()
{
Serial.begin(9600);
EEBLUE.begin(9600);
Serial.println("BLUETOOTH IS READY");
pinMode(8,OUTPUT);
}
void loop()
{
if(EEBLUE.available()){
input=EEBLUE.parseInt();
if(input==1)
digitalWrite(8,1);
if(input==0)
digitalWrite(8,0);
}
}
CIRCUIT:
23
3.
AIM: To interface a servo motor using Bluetooth
terminal. PROGRAM:
#include <SoftwareSerial.h>
#include<Servo.h>
SoftwareSerial EEBLUE(10,11);
Servo s1;
int angle;
void setup()
{
Serial.begin(9600);
EEBLUE.begin(9600);
Serial.println("BLUETOOTH IS READY");
s1.attach(8);
}
void loop()
{
if(EEBLUE.available()){
angle=EEBLUE.parseInt();
for(int i=0;i<=angle;i+=20){
s1.write(i);
delay(500);
}
}
}
24
CIRCUIT:
25
WEEK-6
Interface analog/digital sensors with Arduino and analyse the corresponding readings.
(Sensors like temperature, alcohol, humidity, pressure, gas, sound pollution, level,
weight, flow, proximity, LDR, PIR, pulse, vibration, sound etc..)
1.
AIM: To interface DHT11 sensor for measuring humidity and
temperature. PROGRAM:
#include<DHT.h>
DHT dht(8,DHT11)
float t,h;
void setup(){
Serial.begin(9600);
dht.begin();
Serial.println("Starting DHT Test");
delay(2000);
}
void loop(){
h=dht.readHumidity();
t=dht.readTemperature();
if(isnan(h)||isnan(t)){
Serial.println("Failed to read");
}
else{
Serial.println(h);
Serial.println(t);
}
}
26
CIRCUIT: OUTPUT:
2.
AIM: To interface PIR sensor for detecting motion of an
object. PROGRAM:
27
int sensordata;
void setup(){
Serial.begin(9600);
pinMode(13,OUTPUT);
pinMode(8,INPUT);
}
void loop(){
sesnsordata=digitalRead(8);
if(sensordata==HIGH){
digitalWrite(13,HIGH);
Serial.println("Sensor Activated");
Serial.print("Motion detected at");
Serial.print(millis()/1000);
Serial.println("secs");
delay(50);
}
else{
digitalWrite(13,LOW);
}
delay(50);
}
CIRCUIT:
28
OUTPUT:
3.
AIM: To interface ultrasonic sensor for detecting the presence and measuring the distance of
an object.
PROGRAM:
int distance,duration;
void setup(){
Serial.begin(9600);
pinMode(8,OUTPUT);
pinMode(9,INPUT);
}
void loop(){
digitalWrite(8,HIGH);
delay(100);
digitalWrite(8,LOW);
duration=pulseIn(9,1);
distance=duration*0.032/2;
Serial.print("Distance");
Serial.print(distance);
Serial.println("cm");
29
delay(1000); }
CIRCUIT: OUTPUT:
4.
AIM: To implement a program to use joystick with serial
monitor. PROGRAM:
int VRx = A0;
int VRy = A1;
30
int xPosition = 0;
int yPosition = 0;
int SW_state = 0;
int mapX = 0;
int mapY = 0;
void setup() {
Serial.begin(9600);
pinMode(VRx, INPUT);
pinMode(VRy, INPUT);
}
void loop() {
xPosition = analogRead(VRx);
yPosition = analogRead(VRy);
mapX = map(xPosition, 0, 1023, -512, 512);
mapY = map(yPosition, 0, 1023, -512, 512);
Serial.print("X: ");
Serial.print(mapX);
Serial.print(" | Y: ");
Serial.print(mapY);
delay(100);
}
CIRCUIT:
31
OUTPUT:
RASPBERRY PI
32
AIM: To implement a program to blink LED with raspberry
pi. PROGRAM:
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
ledPinOne = 12
ledPinTwo = 13
GPIO.setup(ledPinOne, GPIO.OUT)
GPIO.setup(ledPinTwo, GPIO.OUT)
try:
while True:
GPIO.output(ledPinOne,HIGH)
GPIO.output(ledPinTwo,LOW)
time.sleep(100)
GPIO.output(ledPinOne,LOW)
GPIO.output(ledPinTwo,HIGH)
time.sleep(100)
except KeyboardInterrupt:
print("\nExiting Program\n")
GPIO.cleanup()
exit()
CIRCUIT:
33
WEEK-7
39
40
41
42
43
44
45
46
47
48
WEEK-8
Interface RGB LED with Raspberry Pi to obtain different colours and brightness
using PWM.
1.
AIM: To interface RGB LED brightness using PWM pins with Raspberry
Pi. PROGRAM:
import RPi.GPIO as g
import time
g.setmode(g.BCM)
49
g.setwarnings(False)
ground = 10
red = 11
green = 12
blue = 13
g.setup(red, g.OUT)
g.setup(green, g.OUT)
g.setup(blue, g.OUT)
g.setup(ground, g.OUT)
g.OUTPUT(ground,
g.LOW) p =
g.PWM(11,100)
q = g.PWM(12,100)
r = g.PWM(13,100)
p.start(0)
q.start(10)
r.start(50)
while True:
for i in range(50):
p.ChangeDutyCycle(i)
q.ChangeDutyCycle(i)
r.ChangeDutyCycle(i)
time.sleep(0.5)
for i in range(50):
p.ChangeDutyCycle(100-i)
q.ChangeDutyCycle(100-i)
r.ChangeDutyCycle(100-i)
time.sleep(0.5)
50
CIRCUIT: 2.
AIM: To interface RGB LED with Raspberry Pi to obtain different colors through user input.
PROGRAM:
import RPi.GPIO as g
import time
g.setmode(g.BCM)
g.setwarnings(False)
ledpinone = 11
ledpintwo = 12
ledpinthree = 13
g.setup(10, g.LOW)
g.setup(ledpinone, g.OUT)
g.setup(ledpintwo, g.OUT)
g.setup(ledpinthree, g.OUT)
while True:
color = input()
if color == 'red':
51
g.OUTPUT(ledpinone, g.HIGH)
g.OUTPUT(ledpintwo, g.LOW)
g.OUTPUT(ledpinthree, g.LOW)
g.OUTPUT(ledpinone, g.LOW)
g.OUTPUT(ledpintwo, g.HIGH)
g.OUTPUT(ledpinthree, g.LOW)
g.OUTPUT(ledpinone, g.LOW)
g.OUTPUT(ledpintwo, g.LOW)
g.OUTPUT(ledpinthree, g.HIGH)
CIRCUIT:
3.
AIM: To interface RGB led individual brightness using PWM pins with user
input. PROGRAM:
import RPi.GPIO as g
import time
g.setmode(g.BCM)
52
g.setwarnings(False)
ground = 10
red = 11
green = 12
blue = 13
g.setup(red, g.OUT)
g.setup(green, g.OUT)
g.setup(blue, g.OUT)
g.setup(ground, g.OUT)
g.OUTPUT(ground, g.LOW)
p = g.PWM(11,100)
q = g.PWM(12,100)
r = g.PWM(13,100)
p.start(0)
q.start(0)
r.start(0)
while True:
color = input()
if color == 'red':
for x in range(100):
p.ChangeDutyCycle(x)
time.sleep(0.05)
for x in range(100):
p.ChangeDutyCycle(100-x)
time.sleep(0.05)
for x in range(100):
53
q.ChangeDutyCycle(x)
time.sleep(0.05)
for x in range(100):
q.ChangeDutyCycle(100-x)
time.sleep(0.05)
elif color == 'blue':
for x in range(100):
r.ChangeDutyCycle(x)
time.sleep(0.05)
for x in range(100):
r.ChangeDutyCycle(100-x)
time.sleep(0.05)
CIRCUIT:
54
WEEK-9
a) Interface an ultrasonic sensor with Raspberry pi to print distance readings on the monitor
when the sensor changes its position.
b) Reading the data from an analog sensor with Raspberry using Arduino serial port or ADC
MCP3208 using SPI.
1.
AIM: To interface an ultrasonic sensor with Raspberry pi to print distance readings on the monitor
when the sensor changes its position.
PROGRAM:
import RPi.GPIO as g
import time
g.output(trigpin, True)
time.sleep(0.0001)
g.output(trigpin, False)
while g.input(echopin) == 0:
pulse_start = time.time()
while g.input(echopin) == 1:
pulse_end = time.time()
try:
print('Calibrating')
return -1
return distance
g.setmode(g.BCM)
trigpin = 24
55
echopin = 25
g.setup(trigpin, g.OUT)
g.setup(echopin, g.IN)
while True:
time.sleep(0.01)
CIRCUIT:
OUTPUT:
56
2.
AIM: To read the data from an analog sensor (joystick) with Raspberry using Arduino serial
port. PROGRAM:
# ## On Arduino
int xposition = 0;
int yposition = 0;
int mapx = 0
int mapy = 0;
void setup() {
Serial.begin(9600);
pinMode(VRx, INPUT);
pinMode(VRy, INPUT);
void loop()
xposition = analogRead(VRx);
yposition = analogRead(VRy);
Serial.print("X:");
Serial.print(mapx);
Serial.print("Y:");
Serial.print(mapy);
delay(100);
# ON Raspberry pi
import serial
57
import time
if _name_ == "_main_":
ser.reset_input_buffer()
while True:
if ser.in_waiting>0:
line = ser.readline().decode('utf-8').rstrip()
print(line)
CIRCUIT:
Post/read the data to/from the cloud via MQTT broker with a Raspberry Pi.
AIM: To post the data to the cloud via MQTT broker with a Raspberry Pi.
PROGRAM:
import urllib.request
import time
import Adafruit_DHT
writeAPIKey = 'FNAIGOOVH990KRLW'
baseURL =
"https://api.thingspeak.com/update?api_key={}".format(writeAPIKey) sensor =
Adafruit_DHT.DHT11
sensorPin = 18
GPIO.setmode(GPIO.BCM)
try:
while True:
humidity,temperature = Adafruit_DHT.read_retry(sensor,sensorPin) if
print(conn.read())
conn.close()
time.sleep(20)
except KeyboardInterrupt:
GPIO.cleanup()
exit()
59
CIRCUIT: OUTPUT:
60
WEEK-11
• Open Terminal and type sudo apt-get install bluetooth bluez blueman. Press enter. •
Once the packages have downloaded and installed, type sudo reboot. • To access this
menu, on the Raspberry Pi desktop click Menu, located in the upper
left corner of the screen, scroll down to Preferences with your cursor and click
Bluetooth Manager. From there, you can pair to any nearby devices, or you can
make your Raspberry Pi discoverable so you can pair your phone to it from your
phone's Bluetooth settings.
ledpin = 18
61
buzzerpin = 10
GPIO.setmode(GPIO.BCM)
GPIO.setup(ledpin, GPIO.OUT)
port = 1
server =
bluetooth.BluetoothSocket(bluetooth.RFCOMM) try:
server.bind((host, port))
except:
server.listen(1)
try:
while True:
data = client.recv(1024)
data = data.decode('utf-8').strip().upper()
if data =='L1':
GPIO.output(ledpin, True)
GPIO.output(ledpin, False)
GPIO.output(buzzerpin, True)
62
elif data =='B0':
GPIO.output(buzzerpin, Flase)
else:
except:
GPIO.cleanup()
client.close()
server.close()
exit()
2.
AIM: To implement a program to send real-time sensor data to smart phone using Raspberry
Pi onboard Bluetooth.
PROGRAM:
import bluetooth
import time
import RPi.GPIO as g
g.setmode(g.BCM)
trigpin = 24
echopin = 25
g.setup(trigpin, g.OUT)
g.setup(echopin, g.IN)
port = 1
server = bluetooth.BluetoothSocket(bluetooth.RFCOMM)
try:
server.bind((host, port))
63
except:
server.listen(1)
print('client', client)
g.output(trigpin, True)
time.sleep(0.0001)
g.output(trigpin, False)
while g.input(echopin) == 0:
pulse_start = time.time()
while g.input(echopin) == 1:
pulse_end = time.time()
try:
except:
print('Calibrating')
return -1
while True:
client.speed(str(dist))
time.sleep(2)
64
WEEK-12
Implement an intruder alert system that alerts through email
1.
AIM: To implement a program to send an email with Raspberry
Pi. PROGRAM:
import smtplib
from_email = '<my-email>'
receipients_list = ['<receipient-email>']
cc_list = []
username = '<Gmail-username>'
password = '<password>'
server = 'smtp.gmail.com:587'
inp=int(input())
server = smtplib.SMTP(smtpserver)
server.starttls()
65
server.login(login, password)
server.quit()
while True:
try:
if (inp == 1):
sleep(.01)
except KeyboardInterrupt:
exit()
OUTPUT:
66
2.
AIM: To implement an intruder alert system that alerts through email.
PROGRAM:
import smtplib
import time
from time import sleep
import RPi.GPIO as GPIO
from sys import exit
from_email = '<my-email>'
receipients_list = ['<receipient-email>']
cc_list = []
subject = 'Hello'
message = 'PIR with Raspberry Pi'
username = '<Gmail-username>'
password = '<password>'
server = 'smtp.gmail.com:587'
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(11, GPIO.IN)
server = smtplib.SMTP(smtpserver)
server.starttls()
server.login(login, password)
problems = server.sendmail(from_addr, to_addr_list, message)
server.quit()
while True:
try:
if (GPIO.input(11) == 1):
sendmail(from_email, receipients_list, cc_list, subject, message, username, password,
server)
sleep(.01)
67
except KeyboardInterrupt:
exit()
OUTPUT:
68