This article is the first in a series on Internet of Things (IoT) sensors. It teaches you about various sensors available and how to read data from them.

Hardware

  • Arduino Uno
  • DHT11 (Humidity + Temperature Sensor)
  • Jumper Cables

CodifyThings - Internet of Things (IoT) Sensors

Software

Circuit

Step 1: Make sure your Arduino is not connected to a power source.

Step 2: Using jumper cables connect power (VNC) and ground (GND) ports on Arduino to power (+) and ground (-) ports on breadboard.

Tip: It is a good practice to use red jumper cable for power (+ / VNC) and black jumper cable for ground (- / GND).

Step 3: Now that your breadboard has a power source, using jumper cables connect power (+) and ground (-) ports of your breadboard to power and ground ports of the DHT11 sensor.

Step 4: To read temperature and humidity values, you will need to connect a jumper cable from signal port of DHT11 sensor to D3 (Digital) port of your Arduino.

Your circuit is now complete and it should look similar to figures below.

CodifyThings - Internet of Things (IoT) Sensors

Arduino Code

Next you are going to write Arduino code that will read sensor data. Start your Arduino IDE and either type the code provided below or download it from GitHub. All the code goes into a single source file (*.ino) but in order to make it easy to understand and reuse, it has been divided into 3 sections.

  • External Libraries: includes all libraries required to run the program
    • <dht11.h> – http://playground.arduino.cc/main/DHT11Lib
  • Sensor Setup: code for reading sensor data
  • Standard Arduino Functions: implementation of standard Arduino functions setup() and loop()
/***************************************************************************
* External Libraries
**************************************************************************/

#include <SPI.h>;
#include <dht11.h>;

/*****************************************************************************
* Sensor Setup - Variables &amp; Functions
****************************************************************************/

dht11 DHT11;

#define DHT11PIN 3

float humidity = 0.0;
float tempC = 0.0;

void readSensorData()
{
int chk = DHT11.read(DHT11PIN);

Serial.print(&quot;[INFO] DHT11 Read: &quot;);

switch (chk)
{
case DHTLIB_OK:
Serial.println(&quot;OK&quot;);

humidity = (float)DHT11.humidity;
tempC = (float)DHT11.temperature;

Serial.print(&quot;[INFO] Humidity (%): &quot;);
Serial.print(humidity, 2);
Serial.print(&quot; Temperature (*C): &quot;);
Serial.println(tempC, 2);
break;
case DHTLIB_ERROR_CHECKSUM:
Serial.println(&quot;Checksum Error&quot;);
break;
case DHTLIB_ERROR_TIMEOUT:
Serial.println(&quot;Time Out Error&quot;);
break;
default:
Serial.println(&quot;Unknown Error&quot;);
break;
}
}

/***************************************************************************
* Standard Arduino Functions - setup(), loop()
**************************************************************************/

void setup()
{
// Initialize serial port
Serial.begin(9600);
}

void loop()
{
readSensorData();

delay(5000);
}

Final Product

Make sure your Arduino is powered on and code from above has been deployed. As soon as code has been deployed, open serial monitor.

CodifyThings - Internet of Things (IoT) Sensors