0% found this document useful (0 votes)
57 views12 pages

CODE

This document has all the IOT Concepts and Application experiments from basic to advanced level.

Uploaded by

Harish Ragav B
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
57 views12 pages

CODE

This document has all the IOT Concepts and Application experiments from basic to advanced level.

Uploaded by

Harish Ragav B
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

LED BLINKING USING ARDUINO

CODE:
void setup()
{
pinMode(12, OUTPUT);
}
void loop() {
digitalWrite(12, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(12, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
}

TEMPERATURE AND HUMIDITY SENSOR TO ARDUINO

CODE:
#include <dht11.h> #define DHT11PIN 4 dht11 DHT11;

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

void loop()
{
Serial.println();
int chk = DHT11.read(DHT11PIN);

Serial.print("Humidity (%): "); Serial.println((float)DHT11.humidity, 2); Serial.print("Temperature


(C): "); Serial.println((float)DHT11.temperature, 2);

delay(2000);

}
OUTPUT SCREENSHOT:

IR SENSOR TO ARDUINO
CODE:
int IRSensor = 2;
int LED = 13;

void setup()
{
pinMode (IRSensor, INPUT);
pinMode (LED, OUTPUT);
}

void loop()
{
int statusSensor = digitalRead (IRSensor);

if (statusSensor == 1)
{
digitalWrite(LED, LOW);
}
else
{
digitalWrite(LED, HIGH);
}

}
ARDUINO TO LCD

CODE:
#include<LiquidCrystal.h>
const int rs=8, en=9,d4=10,d5=11,d6=12,d7=13;
LiquidCrystal lcd(rs,en,d4,d5,d6,d7);
void setup() {
lcd.begin(16,2);
lcd.print("KGR");
}
void loop() {
lcd.setCursor(0,1);
lcd.print("JANA");
}

BLOCK DIAGRAM:
ARDUINO TO ZIGBEE MODULE
CODE:

ZIGBEE_RECIVER:

#include<SoftwareSerial.h>
int led = 6;
int received = 0;
int i;
SoftwareSerial zigbee(2,3);
void setup()
{
Serial.begin(9600);
zigbee.begin(9600);
pinMode(led, OUTPUT);
}
void loop()
{
if (zigbee.available() > 0)
{
received = zigbee.read();
if (received == '0')
{
Serial.println("Turning off LED");
digitalWrite(led, LOW);
}
else if (received == '1')
{
Serial.println("Turning on LED");
digitalWrite(led, HIGH);
}
}
}

ZIGBEE_TRANSMITTER:

#include "SoftwareSerial.h"
SoftwareSerial XBee(2,3);
int BUTTON = 5;
void setup()
{
Serial.begin(9600);
pinMode(BUTTON, INPUT_PULLUP);
XBee.begin(9600);
}
void loop()
{
if (digitalRead(BUTTON) == HIGH )
{
Serial.println("Turn on LED");
XBee.write('1');
}
else if (digitalRead(BUTTON) == LOW )
{
Serial.println("Turn off LED");
XBee.write('0');
}
}

GSM
CODE:
#include <SoftwareSerial.h> SoftwareSerial mySerial(9, 10);

void setup()
{
mySerial.begin(9600); // Setting the baud rate of GSM Module
Serial.begin(9600); // Setting the baud rate of Serial Monitor (Arduino)
delay(100);
}
void loop()
{
if (Serial.available() > 0)
{
switch (Serial.read())
{
case 's':
SendMessage(); break;
case 'd':
DialCall(); break;
}
}
if (mySerial.available() > 0)
{
Serial.write(mySerial.read());
}
}

void SendMessage()
{
mySerial.println("AT+CMGF=1"); // Sets the GSM Module in Text Mode
delay(1000); // Delay of 1000 milliseconds or 1 second

mySerial.println("AT+CMGS=\"+xxxxxxxxxxx\"\r"); // Replace x with mobile number


delay(1000);
mySerial.println("I am SMS from GSM Module"); // The SMS text you want to send
delay(100);
mySerial.println((char)26); // ASCII code of CTRL+Z delay(1000);
}
void DialCall()
{
mySerial.println("ATD+xxxxxxxxxxxx;"); // ATDxxxxxxxxxx; -- watch out here for semicolon at
the end!!
delay(100);
}
BLUETOOTH

CODE:
#define ledPin 12
int data = 0;
void setup()
{
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW);
Serial.begin(9600);
}
void loop()
{
if(Serial.available() > 0)
{
data = Serial.read();
if (data == '0')
{
digitalWrite(ledPin, LOW);
Serial.println("LED: OFF");
}
else if (data == '1')
{
digitalWrite(ledPin, HIGH);
Serial.println("LED: ON");
}
}
}
OUTPUT:
LED BLINK RASPBERRY PI
CODE:

import RPi.GPIO as GPIO


import time

# Set the GPIO mode to BCM


GPIO.setmode(GPIO.BCM)

# Set the GPIO pin to an output


LED_PIN = 17
GPIO.setup(LED_PIN, GPIO.OUT)

try:
while True:
# Turn the LED on
GPIO.output(LED_PIN, GPIO.HIGH)
time.sleep(1) # 1 second delay

# Turn the LED off


GPIO.output(LED_PIN, GPIO.LOW)
time.sleep(1) # 1 second delay

except KeyboardInterrupt:
# Clean up GPIO settings on keyboard interrupt
GPIO.cleanup()

IR SENSOR AND DHT 11 RASPBERRY PI

CODE:
IR Sensor:
import RPi.GPIO as gpio
from time import sleep

gpio.setmode(gpio.BOARD)
gpio.setup(36, gpio.IN) # Raspberry pi 4 -GPIO16
while True:
sensor=gpio.input(36)

if sensor==1:
print(" not detect")
sleep(0.1)

elif sensor==0:
print(" detect")

DHT11 Sensor:

import time
import board
import adafruit_dht
dhtDevice = adafruit_dht.DHT11(board.D4, use_pulseio=False) ## Raspberry pi 4 - gpio4
while True:
try:
# Print the values to the serial port
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
)
)

except RuntimeError as error:


# Errors happen fairly often, DHT's are hard to read, just keep going
print(error.args[0])
time.sleep(2.0)
continue
except Exception as error:
dhtDevice.exit()
raise error

time.sleep(2.0)

OUTPUT SCREENSHOT:

IR Sensor:
DHT11 Sensor:

ARDUINO WITH RASPBERRY PI

RASPBERRY PI CODE:
import serial
import time
bluetooth=serial.Serial("/dev/rfcomm7",9600)
while True:
a=input("enter:-")
string='X{0}'.format(a)
bluetooth.write(string.encode("utf-8"))

ARDUINO CODE:

Int led=13;
Void setup()
{
pinMode(led, OUTPUT);
Serial.begin(9600); //default baud rate for bt 38400
}
Void loop()
{
If(Serial.available())
{
Int a=Serial.parseInt();
Serial.println(a);
If (a==1)
{
digitalWrite(led, HIGH);
}
If (a == 0)
{
digitalWrite(led, LOW);
}
}
}

IoT BASED SYSTEM

#include <DHT.h> // Including library for dht

#include <ESP8266WiFi.h>

String apiKey = "ZF25J6EFQ69A1HLH"; // Enter your Write API key from ThingSpeak

const char *ssid = "ANAND"; // replace with your wifi ssid and wpa2 key
const char *pass = "PASSWORD";
const char* server = "api.thingspeak.com";

#define DHTPIN 4 //pin where the dht11 is connected pin no D2

DHT dht(DHTPIN, DHT11);

WiFiClient client;

void setup()
{
Serial.begin(115200);
delay(10);
dht.begin();

Serial.println("Connecting to ");
Serial.println(ssid);

WiFi.begin(ssid, pass);

while (WiFi.status() != WL_CONNECTED)


{
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");

void loop()
{
float h = dht.readHumidity();
float t = dht.readTemperature();

if (isnan(h) || isnan(t))
{
Serial.println("Failed to read from DHT sensor!");
return;
}

if (client.connect(server,80)) // "184.106.153.149" or api.thingspeak.com


{

String postStr = apiKey;


postStr +="&field1=";
postStr += String(t);
postStr +="&field2=";
postStr += String(h);
postStr += "\r\n\r\n";
client.print("POST /update HTTP/1.1\n");
client.print("Host: api.thingspeak.com\n");
client.print("Connection: close\n");
client.print("X-THINGSPEAKAPIKEY: "+apiKey+"\n");
client.print("Content-Type: application/x-www-form-urlencoded\n");
client.print("Content-Length: ");
client.print(postStr.length());
client.print("\n\n");
client.print(postStr);
Serial.print("Temperature: ");
Serial.print(t);
Serial.print(" degrees Celcius, Humidity: ");
Serial.print(h);
Serial.println("%. Send to Thingspeak.");
}
client.stop();

Serial.println("Waiting...");

// thingspeak needs minimum 15 sec delay between updates


delay(500);
}

You might also like