Devices & Components
Arduino Uno Rev3
DHT11 Temperature & Humidity Sensor (4 pins)
Alphanumeric LCD, 16 x 2
Project description
Code
Code
c_cpp
Make sure that you have installed the <dht_nonblocking.h> and < LiquidCrystal > libraries or re-install them, if necessary. Otherwise, your code won't work.
1#include <dht_nonblocking.h> 2#define DHT_SENSOR_TYPE DHT_TYPE_11 3#include <LiquidCrystal.h> 4 5static const int DHT_SENSOR_PIN = 2; 6DHT_nonblocking dht_sensor( DHT_SENSOR_PIN, DHT_SENSOR_TYPE ); 7 8// initialize the library with the numbers of the interface pins 9LiquidCrystal lcd(7, 8, 9, 10, 11, 12); 10 11/* 12 * Display fixe text . 13 */ 14void setup( ) 15{ 16 lcd.begin(16, 2); 17 lcd.print( "T = " ); 18 lcd.setCursor(10, 0); 19 lcd.print( "deg. C" ); 20 lcd.setCursor(0, 1); 21 lcd.print( "H = " ); 22 lcd.setCursor(10, 1); 23 lcd.print( "%" ); 24} 25 26/* 27 * Poll for a measurement, keeping the state machine alive. Returns 28 * true if a measurement is available. 29 */ 30static bool measure_environment( float *temperature, float *humidity ) 31{ 32 static unsigned long measurement_timestamp = millis( ); 33 34 /* Measure once every four seconds. */ 35 if( millis( ) - measurement_timestamp > 3000ul ) 36 { 37 if( dht_sensor.measure( temperature, humidity ) == true ) 38 { 39 measurement_timestamp = millis( ); 40 return( true ); 41 } 42 } 43 44 return( false ); 45} 46 47/* 48 * Main program loop. 49 */ 50void loop( ) 51{ 52 float temperature; 53 float humidity; 54 55 /* Measure temperature and humidity. If the functions returns 56 true, then a measurement is available. */ 57 if( measure_environment( &temperature, &humidity ) == true ) 58 { 59 /* 60 * Display changing text. 61 */ 62 lcd.setCursor(4, 0); 63 lcd.print( temperature ); 64 lcd.setCursor(4, 1); 65 lcd.print( humidity ); 66 67 } 68}
Comments
Only logged in users can leave comments