Arduino Mega 2560 DHT Sensor Control Code

DHT sensors can measure temperature and humidity. You can receive fresh measurements every 2 seconds in the form of digital signals. DHT11 can sense temperature from 0 to 50 °C and humidity from 20 to 80% RH. DHT22 can sense temperature from -40 to 80 °C and humidity from 0 to 100% RH. The DHT Controller Device manages communication between a DHT sensor and the current Ozeki software.

arduino board with dht sensors
Figure 1 - Arduino Mega 2560 with DHT11 or DHT22 sensor

Required hardware

  • Arduino Mega 2560
  • DHT11 or DHT22 sensor
  • Resistor 4.7kΩ

Source code to install on controller


Before you upload this code to your Arduino, please format the EEPROM...
#include <OzIDManager.h>
#include <OzDHTSensor.h>

// global pointers
OzIDManager* manager;
OzDHTSensor* dhtTemperature;
OzDHTSensor* dhtHumidity;

void setup()
{
  Serial.begin(115200);

  // instantiate objects
  manager = new OzIDManager;
  manager->_sendACK = true;
  manager->_checksum = true;

  OzCommunication::setIDManager(manager);
 
  //DHT11 sensor connected to D2 pin
  dhtTemperature = new OzDHTSensor(DHT11,DHTMode::Temperature,2);
  dhtHumidity = new OzDHTSensor(DHT11,DHTMode::Humidity,2);
  //Set it to DHT22 if using DHT22

  // welcome devices
  int x = 1;
  manager->sendLinkSetup();
  manager->PrintWelcomeLine(dhtTemperature, x++, "TemperatureSensor_1");
  manager->PrintWelcomeLine(dhtHumidity, x++, "HumiditySensor_1");
}

void loop()
{
  dhtTemperature->ownLoop();
  dhtHumidity->ownLoop();
  OzCommunication::communicate();
}

More information