0% found this document useful (0 votes)
10 views4 pages

Iot 3

The document outlines an experiment involving the interfacing of digital sensors (IR, LDR, DHT11) with Arduino/Raspberry Pi and includes code examples for various functionalities. It demonstrates how to display messages on an LCD, control LEDs based on humidity readings, and measure temperature and distance. The provided code snippets illustrate the setup and loop functions necessary for sensor readings and output display.
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)
10 views4 pages

Iot 3

The document outlines an experiment involving the interfacing of digital sensors (IR, LDR, DHT11) with Arduino/Raspberry Pi and includes code examples for various functionalities. It demonstrates how to display messages on an LCD, control LEDs based on humidity readings, and measure temperature and distance. The provided code snippets illustrate the setup and loop functions necessary for sensor readings and output display.
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/ 4

EXPT N0:3

INTERFACE DIGITAL SENSOR (IR/LDR/DHT11) WITH ARDUINO


/RASPBERRY PI AND WRITE A PROGRAM TO PRINT THE READINGS

1.HELLOWORLD
#include <Wire.h>
#include <LiquidCrystal_I2C.h>

// Set the LCD address to 0x27 for a 16 chars and 2 line display
LiquidCrystal_I2C lcd(0x27, 16, 2);

void setup()
{
// initialize the LCD
lcd.begin();

// Turn on the blacklight and print a message.


lcd.backlight();
lcd.print("WELCOME TO IOT");
lcd.begin();
lcd.print("EEE-A");
}

void loop()
{
// Do nothing here...
}

2.Write a program to turn on red LED when the humidity goes beyond 68%
and turn on green LED when the humidity is below 68 c.

// Example testing sketch for various DHT humidity/temperature sensors


// Written by ladyada, public domain

// REQUIRES the following Arduino libraries:


// - DHT Sensor Library: https://github.com/adafruit/DHT-sensor-library
// - Adafruit Unified Sensor Lib:
https://github.com/adafruit/Adafruit_Sensor

#include "DHT.h"

#define DHTPIN 2 // Digital pin connected to the DHT sensor


// Feather HUZZAH ESP8266 note: use pins 3, 4, 5, 12, 13 or 14 --
// Pin 15 can work but DHT must be disconnected during program upload.

// Uncomment whatever type you're using!


//#define DHTTYPE DHT11 // DHT 11
#define DHTTYPE DHT11 // DHT 22 (AM2302), AM2321
//#define DHTTYPE DHT21 // DHT 21 (AM2301)

// Connect pin 1 (on the left) of the sensor to +5V


// NOTE: If using a board with 3.3V logic like an Arduino Due connect
pin 1
// to 3.3V instead of 5V!
// Connect pin 2 of the sensor to whatever your DHTPIN is
// Connect pin 3 (on the right) of the sensor to GROUND (if your sensor
has 3 pins)
// Connect pin 4 (on the right) of the sensor to GROUND and leave the
pin 3 EMPTY (if your sensor has 4 pins)
EXPT N0:3
INTERFACE DIGITAL SENSOR (IR/LDR/DHT11) WITH ARDUINO
/RASPBERRY PI AND WRITE A PROGRAM TO PRINT THE READINGS
// Connect a 10K resistor from pin 2 (data) to pin 1 (power) of the
sensor

// Initialize DHT sensor.


// Note that older versions of this library took an optional third
parameter to
// tweak the timings for faster processors. This parameter is no
longer needed
// as the current DHT reading algorithm adjusts itself to work on
faster procs.
DHT dht(DHTPIN, DHTTYPE);

void setup() {
Serial.begin(9600);
Serial.println(F("DHTxx test!"));
pinMode(4,OUTPUT);
pinMode(13,OUTPUT);

dht.begin();
}

void loop() {
// Wait a few seconds between measurements.
delay(2000);

// Reading temperature or humidity takes about 250 milliseconds!


// Sensor readings may also be up to 2 seconds 'old' (its a very slow
sensor)
float h = dht.readHumidity();
if(h>68){
digitalWrite(4,HIGH);
digitalWrite(13,LOW);
}
else{
digitalWrite(13,HIGH);
digitalWrite(4,LOW);
}
// Read temperature as Celsius (the default)
float t = dht.readTemperature();
// Read temperature as Fahrenheit (isFahrenheit = true)
float f = dht.readTemperature(true);

// Check if any reads failed and exit early (to try again).
if (isnan(h) || isnan(t) || isnan(f)) {
Serial.println(F("Failed to read from DHT sensor!"));
return;
}

// Compute heat index in Fahrenheit (the default)


float hif = dht.computeHeatIndex(f, h);
// Compute heat index in Celsius (isFahreheit = false)
float hic = dht.computeHeatIndex(t, h, false);

Serial.print(F("Humidity: "));
Serial.print(h);
Serial.print(F("% Temperature: "));
Serial.print(t);
Serial.print(F("°C "));
EXPT N0:3
INTERFACE DIGITAL SENSOR (IR/LDR/DHT11) WITH ARDUINO
/RASPBERRY PI AND WRITE A PROGRAM TO PRINT THE READINGS
Serial.print(f);
Serial.print(F("°F Heat index: "));
Serial.print(hic);
Serial.print(F("°C "));
Serial.print(hif);
Serial.println(F("°F"));
}

3.Temperature and humidity

#include "DHT.h"
#include<Wire.h>
#include<LiquidCrystal_I2C.h>
#defineDHTPIN2
#defineDHTTYPEDHT11
DHTdht(DHTPIN,DHTTYPE);
LiquidCrystal_I2Clcd(0x27,16,2);
void setup(){
dht.begin();
lcd.init();
lcd.backlight();
lcd.print("TempandHumEXP");
}
void loop(){
//putyourmaincodehere,torunrepeatedly:
delay(2000);
floath=dht.readHumidity();
//Read temperatureasCelsius(thedefault)
floatt=dht.readTemperature();
if(isnan(h) || isnan(t)){
lcd.setCursor(0,0);
lcd.print(" checkSensor ");
return;
}
lcd.setCursor(0,0);
lcd.print("TEMP= ");
lcd.print(t);
lcd.setCursor(0,1);
lcd.print("HUM= ");
lcd.print(h);
}

4.Distance based led control

// Define pins for Trigger and Echo

const int trigPin = 5;

const int echoPin = 4;

long duration;

int distance;
EXPT N0:3
INTERFACE DIGITAL SENSOR (IR/LDR/DHT11) WITH ARDUINO
/RASPBERRY PI AND WRITE A PROGRAM TO PRINT THE READINGS

void setup() {

// Start the serial communication

Serial.begin(9600);

// Set the Trigger pin as an OUTPUT and Echo pin as INPUT

pinMode(trigPin, OUTPUT);

pinMode(echoPin, INPUT);

void loop() {

// Send a HIGH pulse to the Trigger pin for 10 microseconds to start the measurement

digitalWrite(trigPin, LOW);

delayMicroseconds(2);

digitalWrite(trigPin, HIGH);

delayMicroseconds(10);

digitalWrite(trigPin, LOW);

// Read the duration of the pulse on the Echo pin

duration = pulseIn(echoPin, HIGH);

// Calculate the distance in cm (speed of sound = 343 m/s = 0.0343 cm/µs)

distance = duration * 0.0343 / 2;

// Print the distance to the Serial Monitor

Serial.print("Distance: ");

Serial.print(distance);

Serial.println(" cm");

// Wait for a moment before repeating the measurement

delay(500);

You might also like