IoT All Codes

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 22

PRACTICAL ASSIGNMENTS - INTERNET OF THINGS (IoT)

Experiment No 5: Write a program using Arduino to control LEDs.

void setup()
{
pinMode(0, OUTPUT);
pinMode(1, OUTPUT);
pinMode(2, OUTPUT);
pinMode(4, OUTPUT);
}

void loop()
{
digitalWrite(0, HIGH);
delay(100);
digitalWrite(1, HIGH);
delay(100);
digitalWrite(2, HIGH);
delay(100);
digitalWrite(4, HIGH);
delay(200);

digitalWrite(0, LOW);
delay(100);
digitalWrite(1, LOW);
delay(100);
digitalWrite(2, LOW);
delay(100);
digitalWrite(4, LOW);
delay(200);
}

Experiment No 6: Create a program that illuminates the green LED if the counter is
less than 100, illuminates the yellow LED if the counter is between 101 and 200 and
illuminates the red LED if the counter is greater than 200.

const int green_led = 0;


const int yellow_led = 1;
const int red_led = 2;

unsigned int counter;

void setup()
{
pinMode(green_led, OUTPUT);
pinMode(red_led, OUTPUT);
pinMode(yellow_led, OUTPUT);

digitalWrite(yellow_led,HIGH);
digitalWrite(green_led,HIGH);
digitalWrite(red_led,HIGH);
}

void loop()
{
counter = counter+1;
delay(200);

if(counter <= 100)


{
digitalWrite(green_led,LOW);
digitalWrite(yellow_led,HIGH);
digitalWrite(red_led,HIGH);
}
else if((counter > 100)&& (counter <= 200))
{
digitalWrite(yellow_led,LOW);
digitalWrite(green_led,HIGH);
digitalWrite(red_led,HIGH);
}
else if(counter > 200)
{
digitalWrite(red_led,LOW);
digitalWrite(yellow_led,HIGH);
digitalWrite(green_led,HIGH);
}
if (counter > 210)
counter =0;
}
Experiment No 7: Create a program so that when the user enters ‘b’ the green light
blinks, ‘g’ the green light is illuminated, ‘y’ the yellow light is illuminated and ‘r’ the
red light is illuminated.

const int green_led = 0;


const int yellow_led = 1;
const int red_led = 2;

unsigned int counter;

void setup()
{
Serial.begin(9600);
}

unsigned char key=0;


void loop()
{
Serial.begin(9600);
while(1)
{
key = Serial.read();
if((key == 'g')||(key == 'G'))
break;
if((key == 'r')||(key == 'R'))
break;
if((key == 'y')||(key == 'Y'))
break;
delay(100);
}

Serial.end();
delay(200);

pinMode(green_led, OUTPUT);
pinMode(red_led, OUTPUT);
pinMode(yellow_led, OUTPUT);

if((key==’b’)||(key==’B”))
{
digitalWrite(green_led, LOW);
delay(2000);
digitalWrite(green_led, HIGH);
delay(2000);
key=0;
}

else if((key == 'g')||(key == 'G'))


{
digitalWrite(green_led,LOW);
digitalWrite(yellow_led,HIGH);
digitalWrite(red_led,HIGH);
delay(5000);
digitalWrite(yellow_led,HIGH);
digitalWrite(green_led,HIGH);
digitalWrite(red_led,HIGH);
key = 0;
}

else if((key == 'y')||(key == 'Y'))


{
digitalWrite(yellow_led,LOW);
digitalWrite(green_led,HIGH);
digitalWrite(red_led,HIGH);
delay(5000);
digitalWrite(yellow_led,HIGH);
digitalWrite(green_led,HIGH);
digitalWrite(red_led,HIGH);
key = 0;
}

else if((key == 'r')||(key == 'R'))


{
digitalWrite(yellow_led,HIGH);
digitalWrite(green_led,HIGH);
digitalWrite(red_led,LOW);
delay(5000);
digitalWrite(yellow_led,HIGH);
digitalWrite(green_led,HIGH);
digitalWrite(red_led,HIGH);
key = 0;
}
}

Experiment No 8: Write a program that asks the user for a number and outputs the
square and cube of the number.

void setup()
{
Serial.begin(9600);
Serial.println(“Enter a number: ");
}

void loop()
{
while(Serial.available()>0
{
int input = Serial.parseInt();
int inputSquare = sq(input);
int inputCube = input*input*input;
Serial.println(int(inputSquare));
Serial.println(int(inputCube));
delay(500);
}
while(Serial.available()==0)
{}
}

Experiment No 9: Write a program to control the colour of the RGB LED by turning 3
different potentiometers.

int red_light_pin= 5;
int green_light_pin = 6;
int blue_light_pin = 3;
unsigned int red,green,blue;
void setup()
{
pinMode(red_light_pin, OUTPUT);
pinMode(green_light_pin, OUTPUT);
pinMode(blue_light_pin, OUTPUT);
}

void loop()
{
red = analogRead(A0);
red = (red/4);

green = analogRead(A2);
green = (green/4);

blue = analogRead(A3);
blue = (blue/4);

RGB_color(255-red, 255-green, 255-blue);


delay(1000);
}

void RGB_color(int red_light_value, int green_light_value, int blue_light_value)


{
analogWrite(red_light_pin, red_light_value);
analogWrite(green_light_pin, green_light_value);
analogWrite(blue_light_pin, blue_light_value);
}

Experiment No 10: Write a program to read the temperature sensor and send the
values to the serial monitor.

#include<dht.h>
dht DHT;
#define DHT11_PIN A1

void setup()
{
Serial.begin(9600);
Serial.println("Humidity (%),\tTemperature (C)");
}

void loop()
{
int chk = DHT.read11(DHT11_PIN);
Serial.print(DHT.humidity, 1);
Serial.print("\t");
Serial.println(DHT.temperature, 1);
delay(1000);
}

Experiment No 11: Write a program so it displays the temperature in Fahrenheit as


well as the maximum and minimum temperatures it has seen and notify maximum
value with red LED and minimum value with Orange LED.

#include <dht.h>
dht DHT;
#define DHT11_PIN A1

float minimumTemp, maximumTemp;


void setup()
{
Serial.begin(9600);
Serial.println("Temperature (F)");
minimumTemp = 100;
maximumTemp = 0;
pinMode(2, OUTPUT);
pinMode(4, OUTPUT);
}

void loop()
{

digitalWrite(2,HIGH);
digitalWrite(4,HIGH);
int value = DHT.read11(DHT11_PIN);
float temp = (DHT.temperature*1.8)+32;
Serial.print("Humidity: ");
Serial.println(DHT.humidity);
delay(1000);
static float currentTemp;
if(currentTemp != temp)
{
currentTemp = temp;
if(temp < minimumTemp)
{
minimumTemp = temp;
Serial.print("Minimum Temperature: ");
Serial.println(minimumTemp);
digitalWrite(4,LOW);
}
else if(temp > maximumTemp)
{
maximumTemp = temp;
Serial.print("Maximum Temperature: ");
Serial.println(maximumTemp)
digitalWrite(2,LOW);
}
Serial.println(temp);
delay(1000);
}
delay(500);
}
Experiment No 12: Write a program to read the temperature sensor and show a graph
of recent measurements.

#include <dht.h>
dht DHT;
#define DHT11_PIN A1

float min_t, max_t;

void setup()
{
Serial.begin(9600);
Serial.println("Humidity (%),\tTemperature (F)");
min_t = 0xffff;
max_t=0x00;
}

void loop()
{
int chk = DHT.read11(DHT11_PIN);
Serial.print(DHT.humidity, 1);
Serial.print("\t");
Serial.println((DHT.temperature*1.8)+32, 1);
delay(1000);
}

Experiment No 13: Write a program using Piezo element and use it to play a tune after
someone knocks.

const int buzzer = A1;


const int knockSensor = A1;
const int threshold = 400;
int sensorReading = 0;

void setup()
{
pinMode(buzzer, INPUT);
}
void loop()
{
pinMode(buzzer, INPUT);
sensorReading = analogRead(knockSensor);

if (sensorReading >= threshold)


{
pinMode(buzzer, OUTPUT);

tone(buzzer,264); //C
delay(200);
noTone(buzzer);

tone(buzzer,293); //D
delay(200);
noTone(buzzer);

tone(buzzer,329); //E
delay(200);
noTone(buzzer);

tone(buzzer,349); //F
delay(200);
noTone(buzzer);

tone(buzzer,392); //G
delay(200);
noTone(buzzer);

tone(buzzer,440); //A
delay(200);
noTone(buzzer);

tone(buzzer,494); //B
delay(200);
noTone(buzzer);

tone(buzzer, 524); //C


delay(200);
noTone(buzzer);
}
delay(100);
}
Experiment No 14: Understanding the connectivity of raspberry pi with IR sensor.
Write an application to detect obstacles and notify users using LEDs.

import time
import RPi.GPIO as GPIO
From gpiozero import LED

RUNNING = true

HIGH=1
LOW=0
DetectPin=4
led=8
buzzer=17
led2=LED(27)

def InitSystem():
GPIO.setmode(GPIO.BCM)
GPIO.setup(DetectPin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(led, GPIO.OUT)
GPIO.setup(buzzer, GPIO.OUT)
return

def DetectPerson():
while True:
input_state=GPIO.input(DetectPin)
time.sleep(0.3)
if(input_state==0):
return LOW
else:
return HIGH

def buzz(times):
for _ in range(times):
GPIO.output(buzzer, GPIO.HIGH)
time.sleep(0.5)
GPIO.output(buzzer, GPIO.LOW)
time.sleep(0.5)

try:
print(“\nCounting Using IR LED\n”)
print()
InitSystem()
count=0
while RUNNING:
GPIO.output(buzzer, GPIO.HIGH)
state=DetectPerson()
if (state==LOW):
count+=1
print(“PERSON COUNT = %d” %count)
GPIO.output(led, LOW)
time.sleep(1)
GPIO.output(led, HIGH)
led2.off()
time.sleep(0.5)
led2.on()
buzz(2)
except KeyboardInterrupt:
RUNNING=False
finally:
GPIO.cleanup()
Experiment No 15: Understanding the connectivity of Arduino with IR sensor. Write an
application to detect obstacles and notify users using LED and buzzer.

#define BUZZER A1
void setup()
{
pinMode(2, OUTPUT);
pinMode(BUZZER, OUTPUT);
pinMode(9, INPUT);
Serial.begin(9600);
digitalWrite(2, HIGH);
}

void loop()
{
if(digitalRead(9) == 0)
{
digitalWrite(2, LOW);
delay(100);
tone(BUZZER, 4000);
delay(1000);
noTone(BUZZER);
digitalWrite(2, HIGH);
}
else
{}
}

Experiment No 16: Understanding the connectivity of raspberry pi with temperature


sensor. Write an application to note a threshold value and notify the user using LEDs.

import time
import board
import adafruit_dht
from gpiozero import LED

dhtDevice = adafruit_dht.DHT11(board.D17,use_pulseio=False)

led = LED(8)

while True:
try:
temperature_c = dhtDevice.temperature
temperature_f = temperature_c * (9 / 5) + 32
humidity = dhtDevice.humidity
print("Temp: {:.1f} F / {:.1f} C Humidity: {}% ".format(temperature_f,
temperature_c, humidity))

if(temperature_c > 33):


led.off()
else:
led.on()
except RuntimeError as error:
print(error.args[0])
time.sleep(2.0)
continue
except Exception as error:
dhtDevice.exit()
raise error
time.sleep(2.0)

Experiment No 17: Write an application to control the operation of hardware simulated


traffic signals using Arduino.

const int NG = A2;


const int EG = 0;
const int SG = 10;
const int WG = 12;

const int NY = 11;


const int EY = 5;
const int SY = 13;
const int WY = 9;

const int NR = A1;


const int ER = 6;
const int SR = A3;
const int WR = A0;

void NState(int r, int y, int g)


{
digitalWrite(NR, r);
digitalWrite(NY, y);
digitalWrite(NG, g);
}

void SState(int r, int y, int g)


{
digitalWrite(SR, r);
digitalWrite(SY, y);
digitalWrite(SG, g);
}

void EState(int r, int y, int g)


{
digitalWrite(ER, r);
digitalWrite(EY, y);
digitalWrite(EG, g);
}

void WState(int r, int y, int g)


{
digitalWrite(WR, r);
digitalWrite(WY, y);
digitalWrite(WG, g);
}

void setup()
{
pinMode(NG, OUTPUT);
pinMode(EG, OUTPUT);
pinMode(SG, OUTPUT);
pinMode(WG, OUTPUT);

pinMode(NY, OUTPUT);
pinMode(EY, OUTPUT);
pinMode(SY, OUTPUT);
pinMode(WY, OUTPUT);

pinMode(NR, OUTPUT);
pinMode(ER, OUTPUT);
pinMode(SR, OUTPUT);
pinMode(WR, OUTPUT);
}
void loop()
{
NState(HIGH, LOW, LOW);
SState(HIGH, LOW, LOW);
EState(HIGH, LOW, LOW);
WState(HIGH, LOW, LOW);

NState(HIGH, LOW, LOW);


SState(LOW, LOW, HIGH);
EState(HIGH, LOW, LOW);
WState(HIGH, LOW, LOW);
delay(4000);
SState(LOW, HIGH, LOW);
delay(500);

NState(HIGH, LOW, LOW);


SState(HIGH, LOW, LOW);
EState(LOW, LOW, HIGH);
WState(HIGH, LOW, LOW);
delay(4000);
EState(LOW, HIGH, LOW);
delay(500);

NState(HIGH, LOW, LOW);


SState(HIGH, LOW, LOW);
EState(HIGH, LOW, LOW);
WState(LOW, LOW, HIGH);
delay(4000);
WState(LOW, HIGH, LOW);
delay(500);

NState(LOW, LOW, HIGH);


SState(HIGH, LOW, LOW);
EState(HIGH, LOW, LOW);
WState(HIGH, LOW, LOW);
delay(4000);
NState(LOW, HIGH, LOW);
delay(500);
}
Experiment No 18: Write an application to control the operation of hardware simulated
traffic signals, using Raspberry Pi.

import time
import RPi.GPIO as GPIO

TRUE = 1

#north led definition


NRED = 17
NAMBER = 10
NGREEN = 6

#south led definition


SRED = 5
SAMBER = 11
SGREEN = 8

#east led definition


ERED = 18
EAMBER = 12
EGREEN = 7

#west led definition


WRED = 4
WAMBER = 16
WGREEN = 9

GPIO.setmode(GPIO.BCM)
GPIO.setup(NRED,GPIO.OUT)
GPIO.setup(NAMBER,GPIO.OUT)
GPIO.setup(NGREEN,GPIO.OUT)
GPIO.setup(SRED,GPIO.OUT)
GPIO.setup(SAMBER,GPIO.OUT)
GPIO.setup(SGREEN,GPIO.OUT)
GPIO.setup(WRED,GPIO.OUT)
GPIO.setup(WAMBER,GPIO.OUT)
GPIO.setup(WGREEN,GPIO.OUT)
GPIO.setup(ERED,GPIO.OUT)
GPIO.setup(EAMBER,GPIO.OUT)
GPIO.setup(EGREEN,GPIO.OUT)

def NState(r,a,g):
GPIO.output(NRED,r)
GPIO.output(NAMBER,a)
GPIO.output(NGREEN,g)
return
def SState(r,a,g):
GPIO.output(SRED,r)
GPIO.output(SAMBER,a)
GPIO.output(SGREEN,g)
return

def EState(r,a,g):
GPIO.output(ERED,r)
GPIO.output(EAMBER,a)
GPIO.output(EGREEN,g)
return

def WState(r,a,g):
GPIO.output(WRED,r)
GPIO.output(WAMBER,a)
GPIO.output(WGREEN,g)
return
try:
while TRUE:
NState(1,1,1)
SState(1,1,1)
EState(1,1,1)
WState(1,1,1)

#S on
NState(1,0,0)
SState(0,0,1)
EState(1,0,0)
WState(1,0,0)
time.sleep(4)
SState(0,1,0)
time.sleep(0.5)

#E on
NState(1,0,0)
SState(1,0,0)
EState(0,0,1)
WState(1,0,0)
time.sleep(4)
EState(0,1,0)
time.sleep(0.5)

#W on
NState(1,0,0)
SState(1,0,0)
EState(1,0,0)
WState(0,0,1)
time.sleep(4)
WState(0,1,0)
time.sleep(0.5)

#N on
NState(0,0,1)
SState(1,0,0)
EState(1,0,0)
WState(1,0,0)
time.sleep(4)
NState(0,1,0)
time.sleep(0.5)

# If CTRL+C is pressed the main loop is broken


except KeyboardInterrupt:
RUNNING = False
print "\Quitting"
# Actions under 'finally' will always be called
finally:
# Stop and finish cleanly so the pins # are available to be used again
GPIO.cleanup()

You might also like