This is an example program to fetch data of DHT11 Sensors

Dependencies:   mbed

Fork of DHT11_with_Nucleo by Adatgy2014

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 
00003 //Tested with success on STM32 Nucleo L476
00004 
00005 DigitalOut myled(LED1); // Activate LED
00006 DigitalIn mybutton(USER_BUTTON); // Activate button
00007 DigitalInOut data_pin(A0); // Activate digital in
00008 Serial pc(SERIAL_TX, SERIAL_RX); // Initialize UART connection
00009 // Use a terminal program (eg. TeraTerm).
00010 Timer tmr; //initialize timer
00011 uint64_t adat; // 64 bit variable for temporary data
00012 int i;
00013 
00014 
00015 // Function to initialize DHT11
00016 void dht_read(void) {
00017     data_pin.output(); 
00018     // Initialize measurement > 18 ms low
00019     data_pin = 0;
00020     wait_ms(20);
00021     // After high and release the pin switch input mode
00022     data_pin = 1;
00023     
00024     wait_us(20);//Wait for the sensor to take control and set low level /!\ Important
00025     
00026     data_pin.input();
00027     // Wait until the end of 80 us low
00028     while(!data_pin.read());
00029     // Wait until end of 80 us high
00030     while(data_pin.read());
00031     // 40 bit, 40 read out cycle
00032     for(i=0; i<40; i++) {
00033         adat = adat << 1; // Shift for new number
00034         tmr.stop(); // Stop timer if runs
00035         tmr.reset();  // Reset timer
00036         // Wait until pin
00037         while(!data_pin.read());       
00038         tmr.start();            
00039         while(data_pin.read());
00040         // If DHT11 HIGH longer than 40 micro seconds (hopefully 70 us)
00041         if(tmr.read_us() > 50) {
00042             // bit is 1
00043             adat++;
00044         }
00045     }
00046 }
00047 
00048 int main() {
00049     pc.printf("Read the DHT11 temperature and humidity sensor!\n"); //Welcome message
00050     while(1) {
00051         if(mybutton == 0){ // Button is pressed
00052             // Reset adat variable
00053             adat = 0;
00054             myled = 1; // LED is ON
00055             dht_read(); // Call the function
00056             // Send result through UART result
00057             //pc.printf("Raw data:%10x\n\r",adat);
00058             pc.printf("Humidity:%d%%\n\r", (adat  & 0x000000ff00000000) >> 32); // Humidity
00059             pc.printf("Temperature:%d degrees\n\r", (adat & 0x0000000000ff0000) >> 16 ); // Temperature
00060             if((adat&0x00000000000000ff) == ((adat&0x000000ff00000000) >> 32) + ((adat&0x00000000ff000000) >> 24)
00061                 + ((adat&0x0000000000ff0000) >> 16) + ((adat&0x000000000000ff00) >> 8)){
00062                 pc.printf("Checksum : OK\n\r");        
00063             }
00064             else{
00065                 pc.printf("Checksum : Error\n\r");   
00066             }
00067             
00068             //pc.printf("Checksum:%d\n\r", adat & 0x00000000000000ff); // Checksum.
00069             /*pc.printf("Checksum calculated:%d\n\r",((adat&0x000000ff00000000) >> 32) + ((adat  & 0x00000000ff000000) >> 24)
00070                 + ((adat & 0x0000000000ff0000) >> 16) + ((adat & 0x000000000000ff00) >> 8));*/
00071             wait_ms(300); // Wait 0.2 sec till continue.
00072         } 
00073         else{
00074             myled = 0; // LED is OFF
00075         }
00076     }
00077 }