[go: up one dir, main page]

100% found this document useful (1 vote)
277 views20 pages

Complete Guide For DHT11

The document provides instructions for connecting and using a DHT11/DHT22 humidity and temperature sensor with an Arduino. It describes the specifications of the DHT11 and DHT22 sensors, how to connect them to an Arduino, and includes Arduino code to read temperature and humidity values from the sensor and print them to the serial monitor. It also provides a similar guide for using an ultrasonic sensor and a PIR motion sensor with an Arduino.

Uploaded by

amalija20
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
100% found this document useful (1 vote)
277 views20 pages

Complete Guide For DHT11

The document provides instructions for connecting and using a DHT11/DHT22 humidity and temperature sensor with an Arduino. It describes the specifications of the DHT11 and DHT22 sensors, how to connect them to an Arduino, and includes Arduino code to read temperature and humidity values from the sensor and print them to the serial monitor. It also provides a similar guide for using an ultrasonic sensor and a PIR motion sensor with an Arduino.

Uploaded by

amalija20
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/ 20

Complete Guide for DHT11/DHT22 Humidity and Temperature Sensor With Arduino

Description

These DHTXX sensors are very popular among the Arduino Tinkerers. The DHT sensors are relative cheap
sensors for measuring temperature and humidity.
These sensors inside contain a chip that does analog to digital conversion and spits out a digital signal with
the temperature and humidity.
With any microcontroller(MCU) these signals are easy to ready.

Specifications DHT11 vs DHT22

You have to two versions of the DHT sensor.


DHT11

Range: 20-90%

Absolute accuracy: 5%

Repeatability: 1%

Long term stability: 1% per yea

Price: $1 to $5

DHT22

Range: 0-100%

Absolute accuracy: 2%

Repeatability: 1%

Long term stability: 0.5% per year

Price: $4 to $10

As you can see from the specs above, the DHT22 is a little more accurate.
Arduino with DHT11 Temperature and Humidity Sensor

You need the following components to make this circuit:

Arduino

DHT11

Breadboard

10K Resistor

Heres how to connect the DHT11 to an Arduino:

Pins:

VCC (3V to 5V)

Data OUT

Dont connect

GND

Source code

Heres the code you need for this project:


1. Download the DHT11 library here
https://github.com/adafruit/DHT-sensor-library/archive/master.zip
2. Unzip the DHT library
3. Rename the extracted folder and remove the -. Otherwise your Arduino IDE wont
recognize your library
4. Install the DHT11 in your Arduino IDE
5. Restart your Arduino IDE
6. Go to Files / Examples / DHT_SENSOR_LIB / DHT Tester
7. Upload the code
// Example testing sketch for various DHT humidity/temperature sensors
// Written by ladyada, public domain
#include "DHT.h"
#define DHTPIN 2

// what pin we're connected to

// Uncomment whatever type you're using!


#define DHTTYPE DHT11
// DHT 11
//#define DHTTYPE DHT22
// DHT 22 (AM2302)
//#define DHTTYPE DHT21
// DHT 21 (AM2301)
// Initialize DHT sensor for normal 16mhz Arduino
DHT dht(DHTPIN, DHTTYPE);
void setup() {
Serial.begin(9600);
Serial.println("DHTxx test!");
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();
// Read temperature as Celsius
float t = dht.readTemperature();
// Read temperature as Fahrenheit

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("Failed to read from DHT sensor!");
return;
}
// Compute heat index
// Must send in temp in Fahrenheit!
float hi = dht.computeHeatIndex(f, h);

Serial.print("Humidity: ");
Serial.print(h);
Serial.print(" %\t");
Serial.print("Temperature: ");
Serial.print(t);
Serial.print(" *C ");
Serial.print(f);
Serial.print(" *F\t");
Serial.print("Heat index: ");
Serial.print(hi);
Serial.println(" *F");

Demonstration

In this project the Arduino is measuring the temperature and humidity. Those two measures are being
displayed in the serial monitor. Heres what you should see in your Arduino IDE serial monitor.

Complete Guide for Ultrasonic Sensor HC-SR04


Description

The HC-SR04 ultrasonic sensor uses sonar to determine distance to an object like bats do. It offers
excellent non-contact range detection with high accuracy and stable readings in an easy-to-use package.
From 2cm to 400 cm or 1 to 13 feet. It operation is not affected by sunlight or black material like Sharp
rangefinders are (although acoustically soft materials like cloth can be difficult to detect). It comes
complete with ultrasonic transmitter and receiver module.
Features

Power Supply :+5V DC

Quiescent Current : <2mA

Working Current: 15mA

Effectual Angle: <15

Ranging Distance : 2cm 400 cm/1 13ft

Resolution : 0.3 cm

Measuring Angle: 30 degree

Trigger Input Pulse width: 10uS

Dimension: 45mm x 20mm x 15mm

Sensor

Pins

VCC: +5VDC

Trig : Trigger (INPUT)

Echo: Echo (OUTPUT)

GND: GND

Arduino with HC SR04 Sensor


This sensor is really cool and popular among the Arduino Tinkerers. So Ive decided to post a project
example using this sensor. In this project the ultrasonic sensor read and write the distance in the serial
monitor. Its really simple.
Note: Theres an Arduino library called NewPing (http://playground.arduino.cc/Code/NewPing) that can
make your life easier when using this sensor.

Schematics

Source code
/*
* created by Rui Santos, http://randomnerdtutorials.com
*
* Complete Guide for Ultrasonic Sensor HC-SR04
*
Ultrasonic sensor Pins:
VCC: +5VDC
Trig : Trigger (INPUT) - Pin11
Echo: Echo (OUTPUT) - Pin 12
GND: GND
*/
int trigPin = 11;
//Trig - green Jumper
int echoPin = 12;
//Echo - yellow Jumper
long duration, cm, inches;
void setup() {
//Serial Port begin
Serial.begin (9600);
//Define inputs and outputs
pinMode(trigPin, OUTPUT);

pinMode(echoPin, INPUT);

void loop()
{
// The sensor is triggered by a HIGH pulse of 10 or more microseconds.
// Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
digitalWrite(trigPin, LOW);
delayMicroseconds(5);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Read the signal from the sensor: a HIGH pulse whose
// duration is the time (in microseconds) from the sending
// of the ping to the reception of its echo off of an object.
pinMode(echoPin, INPUT);
duration = pulseIn(echoPin, HIGH);
// convert the time into a distance
cm = (duration/2) / 29.1;
inches = (duration/2) / 74;
Serial.print(inches);
Serial.print("in, ");
Serial.print(cm);
Serial.print("cm");
Serial.println();
delay(250);
}

Source code with NewPing


Below is an example using the NewPing library. Download the library here:
http://playground.arduino.cc/Code/NewPing
/*
* Posted on http://randomnerdtutorials.com
* created by http://playground.arduino.cc/Code/NewPing
*/
#include <NewPing.h>
#define TRIGGER_PIN 11
#define ECHO_PIN 12
#define MAX_DISTANCE 200
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and
maximum distance.
void setup() {
Serial.begin(9600);
}
void loop() {

delay(50);
unsigned int uS = sonar.ping_cm();
Serial.print(uS);
Serial.println(cm);
}

NOTE: If the HC-SR04 does not receive an echo then the output never goes low. Devantec and Parallax
sensors time out after 36ms and I think 28ms respectively. If you use Pulsin as above then with no return
echo the program will hang for 1 second which is the default timeout for Pulsin. You need to use the
timeout parameter.
http://arduino.cc/en/Reference/PulseIn
The HC-SR04 barely works to 10 feet giving a total path length of 20 feet and a path time of about 20ms so
set the timeout to something above that, say 25 or 30ms.
If you put a resistor, say 2k2 between E and T then only connect to T you can use the HC-SR04 from just
one Arduino pin. Look up single pin operation of ultrasonic sensors.
Also if you are using a HC-SR04 with a PicAxe you need to up the clockspeed to at least 8MHz otherwise
they dont see the start of the echo pulse so pulsin never starts. The HC-SR04 works fine with a BS2. by
David Buckley

pulseIn()
Description

Reads a pulse (either HIGH or LOW) on a pin. For example, if value is HIGH, pulseIn() waits for the pin
to go HIGH, starts timing, then waits for the pin to go LOW and stops timing. Returns the length of the
pulse in microseconds or 0 if no complete pulse was received within the timeout.
The timing of this function has been determined empirically and will probably show errors in shorter
pulses. Works on pulses from 10 microseconds to 3 minutes in length. Please also note that if the pin is
already high when the function is called, it will wait for the pin to go LOW and then HIGH before it starts
counting. This routine can be used only if interrupts are activated. Furthermore the highest resolution is
obtained with short intervals.

Syntax

pulseIn(pin, value)
pulseIn(pin, value, timeout)
Parameters

pin: the number of the pin on which you want to read the pulse. (int)
value: type of pulse to read: either HIGH or LOW. (int)
timeout (optional): the number of microseconds to wait for the pulse to be completed: the function returns
0 if no complete pulse was received within the timeout. Default is one second (unsigned long).
Returns

the length of the pulse (in microseconds) or 0 if no pulse is completed before the timeout (unsigned long)
Example
int pin = 7;
unsigned long duration;
void setup()
{
pinMode(pin, INPUT);
}
void loop()
{
duration = pulseIn(pin, HIGH);
}

Arduino with PIR Motion Sensor


https://youtu.be/vJgtckLzoKM

Introducing the PIR Motion Sensor


The PIR motion sensor is ideal to detect movement. PIR stand for Passive Infrared. Basically, the PIR
motion sensor measures infrared light from objects in its field of view.
So, it can detect motion based on changes in infrared light in the environment. It is ideal to detect if a
human has moved in or out of the sensor range.

The sensor in the figure above has two built-in potentiometers to adjust the delay time (the potentiometer at
the left) and the sensitivity (the potentiometer at the right).

Pinout
Wiring the PIR motion sensor to an Arduino is pretty straightforward the sensor has only 3 pins.

GND connect to ground

OUT connect to an Arduino digital pin

5V connect to 5V

Parts required
Heres the required parts for this project

1x PIR Motion Sensor

1x Arduino

1x LED

Jumper Cables

Schematics
Assemble all the parts by following the schematics below.

Code
Upload the following code.
/*

Arduino with PIR motion sensor


For complete project details, visit: http://RandomNerdTutorials.com/pirsensor
Modified by Rui Santos based on PIR sensor by Limor Fried

*/
int
int
int
int

led = 13;
sensor = 2;
state = LOW;
val = 0;

void setup() {
pinMode(led, OUTPUT);
pinMode(sensor, INPUT);
Serial.begin(9600);
}
void loop(){

//
//
//
//

the pin that the LED is atteched to


the pin that the sensor is atteched to
by default, no motion detected
variable to store the sensor status (value)

// initalize LED as an output


// initialize sensor as an input
// initialize serial

val = digitalRead(sensor);
if (val == HIGH) {
digitalWrite(led, HIGH);
delay(100);

//
//
//
//

read sensor value


check if the sensor is HIGH
turn LED ON
delay 100 milliseconds

if (state == LOW) {
Serial.println("Motion detected!");
state = HIGH;
// update variable state to HIGH
}

}
else {
digitalWrite(led, LOW); // turn LED OFF
delay(200);
// delay 200 milliseconds

}
}

if (state == HIGH){
Serial.println("Motion stopped!");
state = LOW;
// update variable state to LOW

Arduino Ultrasonic Sensor with LEDs and buzzer


Whats this project about?
Basically we have an Ultrasonic sensor that measures the distance and the LEDs bar graph will light up
according to our distance from the sensor and as we get closer the buzzer beeps in a different way. This
circuit can work as a parking sensor! Its easy and cheap.

Parts Required

1x Arduino

1x 74HC595 8 Bit Shift Register

1x Breadboard

8x LEDs (for example: 3x red, 3x yellow, 2x green)

9x 220 Ohm Resistors

1x Buzzer

1x Ultrasonic Sensor (for exemple: HC-SR04)

Jumper Wires

Schematics

Upload the Code below


/*
* created by Rui Santos, http://randomnerdtutorials.com
* Ultrasonic Sensor with LED's bar graph and buzzer
*/
int tonePin = 4;
//Tone - Red Jumper
int trigPin = 9;
//Trig - violet Jumper
int echoPin = 10;
//Echo - yellow Jumper
int clockPin = 11; //IC Pin 11 - white Jumper
int latchPin = 12; //IC Pin 12 - Blue Jumper
int dataPin = 13;
//IC Pin 14 - Green Jumper
byte possible_patterns[9] = {
B00000000,
B00000001,
B00000011,
B00000111,
B00001111,
B00011111,
B00111111,
B01111111,
B11111111,

};
int proximity=0;
int duration;
int distance;
void setup() {
//Serial Port
Serial.begin (9600);

pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(clockPin, OUTPUT);
pinMode(latchPin, OUTPUT);
pinMode(dataPin, OUTPUT);
pinMode(tonePin, OUTPUT);

void loop() {
digitalWrite(latchPin, LOW);
digitalWrite(trigPin, HIGH);
delayMicroseconds(1000);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;
/*if (distance >= 45 || distance <= 0){
Serial.println("Out of range");
}
else {
Serial.print(distance);
Serial.println(" cm");
}*/
proximity=map(distance, 0, 45, 8, 0);
//Serial.println(proximity);
if (proximity <= 0){
proximity=0;
}
else if (proximity >= 3 && proximity <= 4){
tone(tonePin, 200000, 200);
}
else if (proximity >= 5 && proximity <= 6){
tone(tonePin,5000, 200);
}
else if (proximity >= 7 && proximity <= 8){
tone(tonePin, 1000, 200);
}
shiftOut(dataPin, clockPin, MSBFIRST, possible_patterns[proximity]);
digitalWrite(latchPin, HIGH);
delay(600);
noTone(tonePin);
}

Watch the video demonstration


https://youtu.be/7ZPc__5tL3c

Guide for DS18B20 Temperature Sensor with Arduino


In this guide Ill show you how to read the temperature with the DS18B20 temperature sensor with the
Arduino board.

The DS18B20 Temperature Sensor


The DS18B20 temperature sensor is a 1-wire digital temperature sensor. This means that you can read the
temperature with a very simple circuit setup. It communicates on common bus, which means that you can
connect several devices and read their values using just one digital pin of the Arduino.
The sensor has just three pins as you can see in the following figure:

The DS18B20 is also available in waterproof version:

Features

Heres some main features of the DS18B20 temperature sensor:

Comunicates over 1-wire bus communication

Operating range temperature: -55C to 125C

Accuracy +/-0.5 C (between the range -10C to 85C)

Read the temperature with the DS18B20 temperature sensor and the
Arduino
In this example, youll read the temperature using the DS18B20 sensor and the Arduino, and the values
will be displayed on the Arduino Serial Monitor.
Parts needed

1x Arduino Uno

1x Breadboard

1x DS18B20 temperature sensor

1x 4700 ohm resistor (you can use similar values)

Jumper wires

Schematics

The sensor can operate in two modes:

Normal mode: 3-wire connection is needed. Heres the schematic you need to follow:

Parasite mode: only 2 wires required, the data and ground. The sensor derives its
power from the data line. In this case, heres the schematic you need to follow:

You can read the temperature of more than one sensor at the same time using just one digital Arduino pin.
For that, you just need to connect together all the DQ pins to any digital Arduino pin.

Code
Youll need to install the OneWire Library and DallasTemperature Library.
Installing the OneWire Library
1. Click here to download the OneWire library.

https://github.com/PaulStoffregen/OneWire/archive/master.zip
2. Unzip the .zip folder and you should get OneWire-master folder
3. Rename your folder from OneWire-master to OneWire
4. Move the OneWire folder to your Arduino IDE installation libraries folder
5. Finally, re-open your Arduino IDE
Installing the DallasTemperature Library
1. Click here to download the DallasTemperature library.
https://github.com/milesburton/Arduino-Temperature-Control-Library/archive/master.zip
2. Unzip the .zip folder and you should get Arduino-Temperature-Control-Librarymaster folder
3. Rename your folder from Arduino-Temperature-Control-Library-master to
DallasTemperature
4. Move the DallasTemperature folder to your Arduino IDE installation libraries folder
5. Finally, re-open your Arduino IDE

After installing the needed libraries, upload the following code to your Arduino board.
/*********
Rui Santos
Complete project details at http://randomnerdtutorials.com
Based on the Dallas Temperature Library example
*********/
#include <OneWire.h>
#include <DallasTemperature.h>
// Data wire is conntec to the Arduino digital pin 2
#define ONE_WIRE_BUS 2
// Setup a oneWire instance to communicate with any OneWire devices
OneWire oneWire(ONE_WIRE_BUS);
// Pass our oneWire reference to Dallas Temperature sensor
DallasTemperature sensors(&oneWire);
void setup(void)
{
// Start serial communication for debugging purposes
Serial.begin(9600);
// Start up the library
sensors.begin();
}
void loop(void){
// Call sensors.requestTemperatures() to issue a global temperature and Requests to

all devices on the bus


sensors.requestTemperatures();
Serial.print("Celsius temperature: ");
// Why "byIndex"? You can have more than one IC on the same bus. 0 refers to the
first IC on the wire
Serial.print(sensors.getTempCByIndex(0));
Serial.print(" - Fahrenheit temperature: ");
Serial.println(sensors.getTempFByIndex(0));
delay(1000);
}

Finally, you should open the Arduino IDE serial monitor at a 9600 baud rate and youll see the temperature
displayed in both Celsius and Fahrenheit:

You might also like