Categories

Arduino more Easy Module Shield examples

In our previous look at the Easy Module Shield v1 – Arduino Easy Module Shield v1. We took a look at the shield and had a simple example looking at the LDR and the pot. In this example we will show examples for the LM35 and the DHT11. In both examples we will output via the serial monitor but we will also switch an LED on if the temperature rises above a certain value.

Here is the shield and you can clearly see the DHT11, LM35 and the red and blue LEDs

multi-purpose-shield-v1

Code

Example 1 : LM35 example

#define lm35Pin A2
#define ledPin 12
 
 
//this sets the ground pin to LOW and the input voltage pin to high
void setup()
{
Serial.begin(9600);
pinMode(ledPin, INPUT);
digitalWrite(ledPin,0);
}
 
//main loop
void loop()
{
  int rawvoltage= analogRead(lm35Pin);
  float millivolts= (rawvoltage/1024.0) * 5000;
  float celsius= millivolts/10;
  Serial.print(celsius);
  Serial.print(" degrees Celsius, ");
  Serial.print((celsius * 9)/5 + 32);
  Serial.println(" degrees Fahrenheit");
  if(celsius >25)
  {
    digitalWrite(ledPin,1);
  }
  else
  {
    digitalWrite(ledPin,0);
  }
  delay(1000);
 
}

Open up the serial monitor and you should see something like this, put your hand on the LM35 and that should push the temperature higher than 25 degrees and the LED will light

25.39 degrees Celsius, 77.70 degrees Fahrenheit
25.88 degrees Celsius, 78.58 degrees Fahrenheit
26.37 degrees Celsius, 79.46 degrees Fahrenheit
26.86 degrees Celsius, 80.34 degrees Fahrenheit
25.88 degrees Celsius, 78.58 degrees Fahrenheit
25.39 degrees Celsius, 77.70 degrees Fahrenheit
25.39 degrees Celsius, 77.70 degrees Fahrenheit
25.39 degrees Celsius, 77.70 degrees Fahrenheit
24.90 degrees Celsius, 76.82 degrees Fahrenheit
24.41 degrees Celsius, 75.95 degrees Fahrenheit

 

Example 2 : DHt11 Example

 

// Depends on the following Arduino libraries:
// - Adafruit Unified Sensor Library: https://github.com/adafruit/Adafruit_Sensor
// - DHT Sensor Library: https://github.com/adafruit/DHT-sensor-library
 
#include <Adafruit_Sensor.h>
#include <DHT.h>
#include <DHT_U.h>
 
#define DHTPIN 4 // Pin which is connected to the DHT sensor.
#define DHTTYPE DHT11 // DHT 11 
#define ledPin 13
 
DHT_Unified dht(DHTPIN, DHTTYPE);
 
uint32_t delayMS;
 
void setup() 
{
 Serial.begin(9600); 
 // Initialize device.
 dht.begin();
 Serial.println("DHTxx Unified Sensor Example");
}
 
void loop() 
{
 // Delay between measurements.
 delay(delayMS);
 // Get temperature event and print its value.
 sensors_event_t event; 
 dht.temperature().getEvent(&event);
 if (isnan(event.temperature)) 
 {
 Serial.println("Error reading temperature!");
 }
 else 
 {
 Serial.print("Temperature: ");
 Serial.print(event.temperature);
 Serial.println(" *C");
 if(event.temperature >25)
 {
 digitalWrite(ledPin,1);
 }
 else
 {
 digitalWrite(ledPin,0);
 }
 }
 delay(1000);
}

 

Testing, open the serial monitor and try and alter the temperature on the device (blow on it). Blue led should light

 

Share

DHT11 and DHT22 comparison

In this example I wanted to compare the readings of a DHT11 and a DHt22 (AM2032) temperature sensor. The sensors were positioned side by side with the same length of wire connecting them to my Arduino. Basically i was keeping the conditions as similar as possible.

This also shows how to connect 2 of these types of sensors.
The DHT11 costs about $1 using the links at the bottom, the DHT22 costs $5. So as you can see there is also a price difference that you may consider.

 

Schematic

Here is a simple schematic showing both devices connected to your Arduino

dhts breadboard

dhts breadboard

Code

This uses the adafruit DHT library

#include "DHT.h"
 
//DHT11
#define DHTPIN11 6     // what pin we're connected to
#define DHTTYPE11 DHT11   // DHT 11
DHT dht11(DHTPIN11, DHTTYPE11);
 
//DHT22
#define DHTPIN22 7     // what pin we're connected to
#define DHTTYPE22 DHT22   // DHT 22
DHT dht22(DHTPIN22, DHTTYPE22);
 
void setup() 
{
  Serial.begin(9600); 
  dht11.begin();
  dht22.begin();
}
 
void loop() 
{
  // Wait a few seconds between measurements.
  delay(2000);
 
  // Read DHT11 temperature as Celsius
  float tempDHT11 = dht11.readTemperature();
  // Check if readings have failed
  if (isnan(tempDHT11)) 
  {
    Serial.println("Failed to read from DHT 11 sensor!");
    return;
  }    
 
  Serial.print("DHT11 Temperature: "); 
  Serial.print(tempDHT11);
  Serial.println(" *C ");
 
  // Read DHT22 temperature as Celsius
  float tempDHT22 = dht22.readTemperature();
  // Check if readings have failed
  if (isnan(tempDHT22)) 
  {
    Serial.println("Failed to read from DHT 22 sensor!");
    return;
  }    
 
  Serial.print("DHT22 Temperature: "); 
  Serial.print(tempDHT22);
  Serial.println(" *C ");
 
}

 

Output

This was the output from the serial monitor, as you can see a slight difference in the readings

DHT11 Temperature: 26.00 *C
DHT22 Temperature: 25.40 *C
DHT11 Temperature: 26.00 *C
DHT22 Temperature: 25.40 *C
DHT11 Temperature: 26.00 *C
DHT22 Temperature: 25.40 *C
DHT11 Temperature: 26.00 *C
DHT22 Temperature: 25.40 *C
DHT11 Temperature: 26.00 *C
DHT22 Temperature: 25.40 *C

 

Links
1x DHT11 DHT-11 Digital Temperature and Humidity Temperature sensor with Cable for Arduino

DHT22 Digital Humidity AM2302 and Temperature Sensor Module For Arduino

Share

Temperature and humidity on a LCD4884 shield

In this example we connect a DHT11 sensor our Arduino, again we will display the temperature and this time the humidity as well on our LCD4884 shield. In this example we connected the data pin of the DHT11 to Pin 9. VCC and GND were derived from the shield.

Here’s a picture of the shield used

 

lcd4884

lcd4884

and here is the DHT11 breakout board, this simplifies things as it contains the necessary resistor already fitted otherwise you need to fit a 4k7 between Vcc and the data pin. You can also plug this into either a breadboard or simple connect dupont cables from the shield to the breakout

dht11 breakout

dht11 breakout

Code

You’ll need the LCD4884 library and the you will need the DHT11 library from the DHT library.

Download, extract and copy this to your Arduino libraries folder location


//required header files
#include <Wire.h>
#include "LCD4884.h"
#include <dht11.h>

//DHT11 sensor settings
dht11 DHT;
#define DHT11_PIN 9
int check;

//variables for readings from sensor
float tempC;
char tempstringC[10];
float humid;
char tempHumid[10];

void setup()
{
lcd.LCD_init();
lcd.LCD_clear();
init_MENU();
//debugging functionality
Serial.begin(9600);
Serial.print("DHT11 STATUS – \t");
}

void init_MENU(void)
{
//clear LCD and display initial display
lcd.LCD_clear();
lcd.LCD_write_string(10, 1, "Temperature", MENU_HIGHLIGHT );
lcd.LCD_write_string(10, 3, "Humidity", MENU_HIGHLIGHT );
}

void display_READINGS(void)
{
//get the temperature and humidity readings
tempC=DHT.temperature;
itoa(tempC,tempstringC,10);
humid=DHT.humidity;
itoa(humid,tempHumid,10);
//display values on LCD
lcd.LCD_write_string(10, 2, tempstringC, MENU_NORMAL);
lcd.LCD_write_string(22, 2, " (c)", MENU_NORMAL);
lcd.LCD_write_string(10, 4, tempHumid, MENU_NORMAL);
lcd.LCD_write_string(22, 4, " %", MENU_NORMAL);
delay(2000);
}

void loop()
{
//read and convert values
check = DHT.read(DHT11_PIN);
//check status - this is useful for debugging the sensor
switch (check)
{
case DHTLIB_OK:
Serial.print("OK\n");
display_READINGS();
break;
case DHTLIB_ERROR_CHECKSUM:
Serial.print("Checksum error\n");
lcd.LCD_clear();
lcd.LCD_write_string(10, 1, "Checksum error", MENU_HIGHLIGHT );
break;
case DHTLIB_ERROR_TIMEOUT:
Serial.print("Timeout error\n");
lcd.LCD_clear();
lcd.LCD_write_string(10, 1, "Timeout error", MENU_HIGHLIGHT );
break;
default:
Serial.print("Unknown error\n");
lcd.LCD_clear();
lcd.LCD_write_string(10, 1, "Unknown error", MENU_HIGHLIGHT );
break;
}

}

Links

DFRobot – Graphic LCD4884 Shield for (For Arduino)

DHT11 Digital Temperature and Humidity Sensor Module For Arduino

Share

DHT11 sensor example

In this example we will show a basic example of connecting a DHT11 Temperature and Humidity Sensor to your Arduino. This is a very nice little, low cost sensor which requires only a pull up resistor to VDD. This can easily be constructed on a mini bread board and then joined up to your Arduino. using some connecting cables

The DHT sensors are made of two parts, a capacitive humidity sensor and a thermistor. There is a chip inside that does some a/d conversion and outputs a digital signal with the temperature and humidity.

I wont go into the technical details of the DHT11, there is a link at the bottom to a PDF datasheet which explains all you need to know.

Schematic

Arduino and DHT11 schematic

Arduino and DHT11 schematic

Connections

Here is the breadboard layout

Arduino and DHT11 breadboard

Arduino and DHT11 breadboard

Code

You will need the DHT11 library from the DHT library. You could roll your own code but this saves a whole lot of time.

Download, extract and copy this to your Arduino libraries folder location. This is a simple example which outputs the humidity and temperature, place your fingers on the DHT11 and you will see the readings fluctuate

#include <dht11.h>

dht11 DHT;
//Pin 4 of Arduino to Data of DHT11
#define DHT11_PIN 4

void setup()
{
Serial.begin(9600);
int check;
Serial.print(“DHT11 STATUS – \t”);
check = DHT.read(DHT11_PIN);
//check status
switch (check)
{
case DHTLIB_OK:
Serial.print(“OK\n”);
break;
case DHTLIB_ERROR_CHECKSUM:
Serial.print(“Checksum error\n”);
break;
case DHTLIB_ERROR_TIMEOUT:
Serial.print(“Timeout error\n”);
break;
default:
Serial.print(“Unknown error\n”);
break;
}
}

void loop()
{

//humidity and temperature output
Serial.print(“Humidity is “);
Serial.print(DHT.humidity,1);
Serial.print(“\n”);
Serial.println(“Temperature is “);
Serial.println(DHT.temperature,1);

delay(1000);
}

Output

Upload the sketch above and open the Serial Monitor window

arduino and dht11 serial output

arduino and dht11 serial output

 

Parts List

Label	Part Type
DHT1	DHT11 Humitidy and Temperature Sensor	
Part1	Arduino Uno (Rev3)
R1	4.7k Ω

 

Links

DHT11 Humidity and Temperature Sensor from Amazon UK

DHT11 Humidity and Temperature Sensor from Amazon US

DHT11 Datasheet in PDF format

Share