DIY Climate Control: Building Your Own Arduino Temperature and Humidity Sensor

For this tutorial, I’ll be showing you how to build a portable, digital thermometer using Arduino. To start, you’ll need to make sure you have all of the right components. I’ve added a list of supplies below and included a link for purchasing each component.

Supplies:

Once you have everything gathered, it’s time to start assembling the device. We’ll start by connecting the OLED to the breadboard. Be sure to leave some room for connecting jumper wires, so you can connect the OLED to the Arduino board. When you’ve found a good spot for your OLED, the next step will be wiring it to your Arduino.

To start, locate the pin labelled GND and connect a jumper wire, then connect that same jumper wire to the ground bus on the breadboard (this will be the pins with the blue stripe next to them and the - symbol at either end of the breadboard). Once that’s done, you can locate the VCC pin and connect a jumper wire to it and the power bus on the breadboard (this will be the pins with the red stripe next to them and the + symbol at either end of the breadboard). From there, locate the SCK pin and connect it with pin 13 on the Arduino board. After that, you’ll connect the SDA pin to pin 11, the RES pin to pin 8, the DC to pin 9 and the CS to pin 10.

With that, the OLED should be ready to go (once the code is uploaded) but we’ll still need to power the breadboard and connect the DHT11 before we can start coding.

To ensure the breadboard will get power from the Arduino, simply connect a jumper wire to the power bus on the breadboard and the 5V pin on the Arduino and connect another one to the ground bus and the GND pin on the Arduino.

Next comes the DHT11. There are a few variations of the DHT11 sensor but for this tutorial, I’m using one with four pins. This one will need to be connected differently than if you’re using a three pin version. Unfortunately the pins on this version of the sensor are not labelled so you can refer to the following diagram to get a better idea of what each pin is used for.

We’ll start by finding a suitable spot for the sensor and plugging it into the board. Once that’s done, connect a jumper wire to the ground pin and the ground bus. Then skip over the pin directly next to the ground as that will not be used. The data pin is slightly less straightforward to connect, as you’ll first need to add the 10K Ohm resistor to this pin and connect the other end to the power pin. From there, you can add a jumper wire next to the data pin and connect that to pin 7 on the Arduino and connect the power pin to the power bus on the breadboard.

If you’re feeling lost or need to check your connections, you can also refer to the following diagram to see where everything should be connected.

Note: Be sure to pay attention to the labels on the OLED pins as they may be arranged in a different order, depending on what type of OLED you’re using

Now that we’ve got everything connected, it’s time to add the code. For this, we’ll be using the Arduino IDE and you may need to add a library or two if you don’t have them downloaded already. In case you need to download anything before coding, the Arduino IDE can be downloaded here [Software | Arduino], the library for the OLED can be downloaded here [https://github.com/olikraus/u8g2] and the library for the DHT11 sensor can be downloaded here [https://github.com/adafruit/DHT-sensor-library] if it isn’t already included in the Arduino IDE. In case you’ve never downloaded a library from GitHub, here’s a quick video demo on how to do that [GitHub_Library_Download].

Once you have all of the software setup, it’s time to start adding the code. The first thing we want to do is to, right at the top of your sketch, is to add the libraries that we’ll be using for this project.

#include<U8glib.h>
#include <DHT.h>

This will allow us to simplify the code a bit and significantly cut down on the amount of work we’ll need to do to make our components work. The <U8glib.h> line refers to the library that we’ll be using for the OLED screen and the <DHT.h> is for the temperature/humidity sensor.

Once you’ve added the libraries, the next step is to setup your components so that you can tell them what to do.

//Setup OLED
U8GLIB_SH1106_128X64 u8g(13, 11, 10, 9, 8); 
//GND = GND, VCC = VCC, SCK = 13, SDA = 11, RES = 8, DC = 9, CS = 10

//Setup DHT11 (Temp/Hum) Sensor
#define datapin 7
#define DHTTYPE DHT11
DHT dht(datapin, DHTTYPE);

You may notice that I’ve included notes in these lines of code, this is done in order to help myself or anyone else who looks at the code later in the event something needs to be debugged or if one or more of the components needs to be replaced. It’s a good habit to get into as it will save you a lot of time and stress later on, especially in bigger projects or if you’re collaborating with others.

Anyway, the next step after the initial setup of our components is to initialize everything, take readings and display the data.

void setup() {

  //Initialize DHT Sensor
  dht.begin();

}

void loop() {
 
  //Display data on Screen
  u8g.firstPage();  
  do {
    u8g.setFont(u8g_font_helvR12);   // select font
    u8g.drawStr(0, 20, "Temp: ");   // put string of display at position X, Y
    u8g.drawStr(0, 60, "Hum: ");
    u8g.setPrintPos(72, 20);        // set position
    u8g.print(dht.readTemperature(), 0);  // display temperature from DHT11 in Celsius
    u8g.println("C"); 
    u8g.setPrintPos(60, 60);        // set position
    u8g.print(dht.readHumidity(), 0);     // display humidity from DHT11
    u8g.println("%");
  } while(u8g.nextPage() );
  delay(1000);

}

In the void setup, we’re telling the DHT11 sensor to start running so that the information can be displayed and the void loop contains the code that will repeatedly run, so the data shown on the screen can be constantly updated.

Now that we’ve completed the code, it’s time to compile and upload it and see if our sensor works. First, connect the USB to your Arduino and the USB drive on your computer. Next, click on the Sketch tab and click on upload. The code will then be automatically compiled before being uploaded to your Arduino. If there are any errors in your code, the compilation will likely fail and the IDE will give you more information as to why the compilation failed. If your compilation does fail, don’t panic. You can easily fix it by carefully looking through your code, line by line and comparing it to what’s shown here. I’ve also provided the completed code below if you need it for further assistance.

Once the code is successfully uploaded, your OLED should begin to display the data.

Congratulations, you’ve just completed your very own Arduino based temperature and humidity sensor! If you’d like to make it portable, you can add a mobile power bank and a few batteries. Just remember to be careful and follow all safety precautions if using batteries, especially lithium ion batteries.

Now that you have a feel for using sensors, see what else you can come up with. The only real limits with tech are your imagination and level of determination.

Here’s the complete code for the project, in order. I included code to initialize the Serial Monitor and view the sensor data, in case you’d like to experiment with using the Serial Monitor to test and debug.

//トラビス (my name in Japanese/a way to add my signature to all of my projects)
#include<U8glib.h>
#include <DHT.h>

//Setup OLED
U8GLIB_SH1106_128X64 u8g(13, 11, 10, 9, 8); //GND = GND, VCC = VCC, SCK = 13, SDA = 11, 
RES = 8, DC = 9, CS = 10

//Setup DHT11 (Temp/Hum) Sensor
#define datapin 7
#define DHTTYPE DHT11
DHT dht(datapin, DHTTYPE);


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

  //Initialize DHT Sensor
  dht.begin();

}

void loop() {
  //Display data in Serial Monitor
  Serial.println(String("Temperature: ") + (dht.readTemperature()) + String("°C"));
  Serial.println(String("Humidity: ") + (dht.readHumidity()) + String("%"));


  //Display data on Screen
  u8g.firstPage();  
  do {
    u8g.setFont(u8g_font_helvR12);   // select font
    u8g.drawStr(0, 20, "Temp: ");   // put string of display at position X, Y
    u8g.drawStr(0, 60, "Hum: ");
    u8g.setPrintPos(72, 20);        // set position
    u8g.print(dht.readTemperature(), 0);  // display temperature from DHT11 in Celsius
    u8g.println("C"); 
    u8g.setPrintPos(60, 60);        // set position
    u8g.print(dht.readHumidity(), 0);     // display humidity from DHT11
    u8g.println("%");
  } while(u8g.nextPage() );
  delay(1000);

}