IoT All Codes
IoT All Codes
IoT All Codes
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.
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);
void setup()
{
Serial.begin(9600);
}
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;
}
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);
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);
}
#include <dht.h>
dht DHT;
#define DHT11_PIN A1
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
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.
void setup()
{
pinMode(buzzer, INPUT);
}
void loop()
{
pinMode(buzzer, INPUT);
sensorReading = analogRead(knockSensor);
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);
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
{}
}
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))
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);
import time
import RPi.GPIO as GPIO
TRUE = 1
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)