The Arduino Launch Control System-Part 15

With the DHT11 hardware discussed in our last post, it is time to work on the coding that makes the sensor useful.

Coding the DHT11 Sensor

The library used for this sensor is, not surprisingly, called dht11. It can be found on  Github at https://github.com/adidax/dht11. This library was referenced earlier in the libraries section of the startup code.

// ================================================================
// library required for the DHT-11 Sensor
   #include <dht11.h>

Additionally, the following declarations were added in as well

// ================================================================
// declarations required for the DHT-11 sensor
   #define DHT11PIN A2

// create an DHT-11 object called "dhtSensor"
   dht11 dhtSensor;
   int checkDHT;
   float dhtHumidity;
   float dhtTemp;

These coding items were discussed earlier in Part 4.

Getting the Sensor Readings

As we work through the showSerialMonitor() subroutine, we are now at the Temp and Humidity section. The first thing that happens is that the program calls for the dht11Measurements() subroutine.

// Temp and Humidity
   dht11Measurements();
   dht11SerialMonitor();

The dht11Measurements() subroutine is short, consisting of only 3 commands.

/*****************************************************************
******************************************************************
*                                                                *
*                         DHT CALCULATIONS                       *
*                                                                *
******************************************************************
******************************************************************/
void dht11Measurements()
{
  // Get temp and humidity readings
     dhtSensor.read(DHT11PIN);
     dhtHumidity = dhtSensor.humidity;
     dhtTemp = dhtSensor.temperature;
}

The first line reads the data coming from the pin. The second line gets the humidity reading and stores it in the variable dhtHumidity. The third line obtains the temperature reading and it is stored in the variable dhtTemp.

The dht11 library really does all of the heavy lifting here. It calculates the temperature and humidity readings and returns them to us in a straightforward fashion, with humidity returned as a percentage and temperature in degrees Centigrade.

Now that we have completed the subroutine and have the data, the program returns back to the showSerialMonitor() subroutine. The next line of code calls the dht11SerialMonitor subroutine.

Displaying the Results

Displaying the results for the DHT11 sensor follows the same basic pattern as with the BMP180 pressure sensor (see Part 13).

// Display temp and humidity readings on the Serial Monitor
   Serial.print(F("Temperature and Humidity Readings from DHT11 Sensor")); 
   Serial.println();

// Display temperature
   Serial.print(F(" Temperature: "));
   Serial.print(dhtTemp,0);
   Serial.println(F(" C ")); 

// Display humidity
   Serial.print(F(" Humidity: "));
   Serial.print(dhtHumidity,0);
   Serial.println(F("% "));
   Serial.println();

As before, we start by printing a header that identifies the data displayed below is from the DHT11. Next we print out the current temperature, and the next line prints out the humidity.

The same Serial.print(F(“”)); statements are used on the string constants. On the variables, both include the ‘0’ (zero) qualifier after the variable. This indicates that no digits after the decimal point are to be included.

When this subroutine is completed, we once again return to the showSerialMonitor() subroutine.

In our next post we look at how the display is paused without using the delay() command and why there are two different wait periods.

Back to Part 14                                            Go to Part 16