Raspberry Pi - Temperature Humidity Sensor - OLED

This tutorial instructs you how to use Raspberry Pi to read the temperature and humidity from a DHT11/DHT22 sensor and display it on an OLED.

Raspberry Pi DHT11/DHT22 temperature humidity sensor OLED display

Hardware Preparation

1×Raspberry Pi 4 Model B
1×SSD1306 I2C OLED Display 128x64
1×SSD1306 I2C OLED Display 128x32
1×DHT11 Temperature and Humidity Sensor
1×DHT11 Temperature Humidity Sensor Module (short)
1×Jumper Wires
1×(Optional) Screw Terminal Adapter for Raspberry Pi
1×(Optional) USB-C Power Cable with On/Off Switch for Raspberry Pi 4B
1×(Optional) Plastic Case and Cooling Fan for Raspberry Pi 4B

You can use DHT22 sensor instead of DHT11 sensor.

Or you can buy the following sensor kits:

1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you.
Additionally, some of these links are for products from our own brand, DIYables.

Overview of OLED display, DHT11 and DHT22 Temperature Humidity Sensor

If you are unfamiliar with OLED display, DHT11 and DHT22 temperature humidity sensor (including pinout, functionality, programming, etc.), the following tutorials can help you out:

Wiring Diagram

Raspberry Pi - DHT11 Module LCD Wiring

The wiring diagram between Raspberry Pi and DHT11 Sensor OLED

This image is created using Fritzing. Click to enlarge image

Raspberry Pi - DHT22 Module LCD Wiring

The wiring diagram between Raspberry Pi and DHT22 Sensor OLED

This image is created using Fritzing. Click to enlarge image

Raspberry Pi Code - DHT11 Sensor - OLED

Detailed Instructions

  • Make sure you have Raspbian or any other Raspberry Pi compatible operating system installed on your Pi.
  • Make sure your Raspberry Pi is connected to the same local network as your PC.
  • Make sure your Raspberry Pi is connected to the internet if you need to install some libraries.
  • If this is the first time you use Raspberry Pi, See how to set up the Raspberry Pi
  • Connect your PC to the Raspberry Pi via SSH using the built-in SSH client on Linux and macOS or PuTTY on Windows. See to how connect your PC to Raspberry Pi via SSH.
  • Make sure you have the RPi.GPIO library installed. If not, install it using the following command:
sudo apt-get update sudo apt-get install python3-rpi.gpio
pip install Adafruit-SSD1306
  • Install the library for DHT11 temperature and humidity sensor by running the following command:
sudo pip3 install Adafruit_DHT
  • Create a Python script file dht11_oled.py and add the following code:
""" This Raspberry Pi code was developed by newbiely.com This Raspberry Pi code is made available for public use without any restriction For comprehensive instructions and wiring diagrams, please visit: https://newbiely.com/tutorials/raspberry-pi/raspberry-pi-temperature-humidity-sensor-oled """ import Adafruit_DHT import Adafruit_SSD1306 from PIL import Image, ImageDraw, ImageFont import time # Set up the DHT11 sensor DHT_SENSOR = Adafruit_DHT.DHT11 DHT_PIN = 20 # GPIO pin where the DHT11 sensor is connected # Set up the SSD1306 OLED display RST = None OLED = Adafruit_SSD1306.SSD1306_128_64(rst=RST) OLED.begin() OLED.clear() OLED.display() # Set up fonts font = ImageFont.load_default() while True: # Read temperature and humidity from DHT11 sensor humidity, temperature = Adafruit_DHT.read_retry(DHT_SENSOR, DHT_PIN) # Clear the OLED display OLED.clear() # Display temperature and humidity on OLED OLED.draw.text((5, 5), f'Temp: {temperature:.1f} C', font=font, fill=255) OLED.draw.text((5, 20), f'Humidity: {humidity:.1f}%', font=font, fill=255) # Update the OLED display OLED.display() # Wait for a moment before reading again time.sleep(2) except KeyboardInterrupt: # Clean up code before exiting the script OLED.clear() OLED.display() print("\nExiting the script.")
  • Save the file and run the Python script by executing the following command in the terminal:
python3 dht11_oled.py

The script runs in an infinite loop continuously until you press Ctrl + C in the terminal.

  • Make the enviroment around the temperature sensor hot, or hold the sensor in your hand.
  • Check out the results on the OLED display.

Raspberry Pi Code - DHT22 Sensor - OLED

""" This Raspberry Pi code was developed by newbiely.com This Raspberry Pi code is made available for public use without any restriction For comprehensive instructions and wiring diagrams, please visit: https://newbiely.com/tutorials/raspberry-pi/raspberry-pi-temperature-humidity-sensor-oled """ import Adafruit_DHT import Adafruit_SSD1306 from PIL import Image, ImageDraw, ImageFont import time # Set up the DHT22 sensor DHT_SENSOR = Adafruit_DHT.DHT22 DHT_PIN = 20 # GPIO pin where the DHT22 sensor is connected # Set up the SSD1306 OLED display RST = None OLED = Adafruit_SSD1306.SSD1306_128_64(rst=RST) OLED.begin() OLED.clear() OLED.display() # Set up fonts font = ImageFont.load_default() while True: # Read temperature and humidity from DHT22 sensor humidity, temperature = Adafruit_DHT.read_retry(DHT_SENSOR, DHT_PIN) # Clear the OLED display OLED.clear() # Display temperature and humidity on OLED OLED.draw.text((5, 5), f'Temp: {temperature:.1f} C', font=font, fill=255) OLED.draw.text((5, 20), f'Humidity: {humidity:.1f}%', font=font, fill=255) # Update the OLED display OLED.display() # Wait for a moment before reading again time.sleep(2) except KeyboardInterrupt: # Clean up code before exiting the script OLED.clear() OLED.display() print("\nExiting the script.")

※ OUR MESSAGES

  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!