Complete Guide For DHT11
Complete Guide For DHT11
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.
Range: 20-90%
Absolute accuracy: 5%
Repeatability: 1%
Price: $1 to $5
DHT22
Range: 0-100%
Absolute accuracy: 2%
Repeatability: 1%
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
Arduino
DHT11
Breadboard
10K Resistor
Pins:
Data OUT
Dont connect
GND
Source code
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.
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
Resolution : 0.3 cm
Sensor
Pins
VCC: +5VDC
GND: GND
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);
}
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);
}
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.
5V connect to 5V
Parts required
Heres the required parts for this project
1x Arduino
1x LED
Jumper Cables
Schematics
Assemble all the parts by following the schematics below.
Code
Upload the following code.
/*
*/
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(){
//
//
//
//
val = digitalRead(sensor);
if (val == HIGH) {
digitalWrite(led, HIGH);
delay(100);
//
//
//
//
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
Parts Required
1x Arduino
1x Breadboard
1x Buzzer
Jumper Wires
Schematics
};
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);
}
Features
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
Jumper wires
Schematics
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
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: