Raspberry Pi Pico Project

Interfacing DHT11 Temperature and Humidity sensor with Raspberry Pi Pico and SSD1306 OLED

Introduction

Interfacing DHT11 Temperature and Humidity Sensor with Raspberry Pi Pico and SSD1306 OLED- The DHT11 temperature and humidity sensor is a popular choice for many DIY projects due to its affordability and ease of use. In this article, we will explore how to interface the DHT11 temperature sensor with Raspberry Pi Pico and SSD1306 OLED display. This combination allows us to monitor and display real-time temperature and humidity readings in a compact and user-friendly manner.

DHT11 temperature and humidity sensor and Raspberry pi pico




Required Materials

Raspberry Pi Pico

DHT11 Temperature and Humidity Sensor

SSD1306 OLED Display (typically 0.96”)

Breadboard

Jumper Wires

*Please Note: These are affiliate links. I may make a commission if you buy the components through these links. I would appreciate your support in this way!

Overview of Raspberry Pi Pico

A versatile microcontroller board powered by RP2040, featuring a dual-core ARM Cortex-M0+ processor, which makes it suitable for a wide range of electronic projects.

Processor

Dual-core ARM Cortex-M0+ processor, capable of running up to 133 MHz.

Memory

264KB of SRAM.

2MB of on-board Flash memory.

GPIO Pins

26 multi-function GPIO pins, including 3 analog inputs.

Interfaces

2 × SPI, 2 × I2C, 2 × UART, 3 × 12-bit ADC, 16 × controllable PWM channels.

1 × USB 1.1 controller and PHY, with host and device support.

Power Supply

1.8V to 5.5V DC operating voltage.

Additional Features

On-board LED: Programmable LED connected to a GPIO pin.

Supported Languages: MicroPython and C/C++.

Debugging: SWD port for debugging.

Dimensions: 21mm × 51mm.

Temperature Range: Operating temperature: -20°C to +85°C.



Understanding the DHT11 Sensor

DHT11 temperature and humidity sensor and Raspberry pi pico

A widely used, low-cost digital sensor for measuring temperature and humidity, known for its ease of interfacing and sufficient accuracy for most hobbyist applications.

Humidity Measuring

Humidity Range: 20-80% RH (Relative Humidity).

Humidity Accuracy: ±5% RH.

Humidity Resolution: 1% RH.

Temperature Measuring

Temperature Range: 0-50°C.

Temperature Accuracy: ±2°C.

Temperature Resolution: 1°C.

Other Specifications

Operating Voltage: 3 to 5.5 Volts.

Output: Digital signal.

Sampling Rate: Measurement can be taken every second.

Long-term Stability: <±1% RH/year.

Size and Dimensions: Small, easy to integrate into systems and devices.

Ease of Use: Simple digital control and output, straightforward interfacing with microcontrollers like Arduino and Raspberry Pi.

Power Consumption: Low, suitable for battery-operated applications.

Response Time: Humidity – 5s, Temperature – 1s.



The SSD1306 OLED Display

DHT11 temperature and humidity sensor and Raspberry pi pico

A small, monochrome OLED display, perfect for displaying data like temperature and humidity. It’s known for its high contrast, wide viewing angle, and low power consumption.

Display Characteristics

Display Type: Monochrome OLED (Organic Light-Emitting Diode).

Resolution: Typically 128×64 pixels, variants may differ.

Color: Monochrome (usually blue or white).

Communication Interface

I2C (Inter-Integrated Circuit) or SPI (Serial Peripheral Interface) options.

Driver IC: SSD1306, responsible for driving the display.

Technical Specifications

Operating Voltage: 3.3V to 5V.

Viewing Angle: Approximately 160 degrees.

Pixel Size: Small for high-resolution displays.

Display Size: Commonly 0.96 inches diagonal; other sizes available.

Brightness: High, adjustable via software.

Low Power Consumption: Ideal for battery-powered applications.

Operating Temperature: Typically -20°C to 70°C.

Dimensions: Screen Size – 0.96 inches (diagonal), Active Display Area – Approximately 21.74mm x 10.86mm, Module Size – Approximately 27mm x 27mm x 4mm (WxHxD).



Raspberry Pi Pico Pin Diagram:

DHT11 temperature and humidity sensor and Raspberry pi pico

Circuit Connections

DHT11 temperature and humidity sensor and Raspberry pi pico

Connecting the DHT11 Sensor and an SSD1306 OLED Display to the Raspberry Pi Pico is a straightforward process that enhances the Pico’s capability to display real-time environmental data. Begin by connecting the VCC pin of the DHT11 sensor to the 3.3V pin on the Pico, ensuring a stable power supply. The ground (GND) pin of the DHT11 must be connected to one of the Pico’s ground pins to complete the power circuit. For data transmission, the DHT11’s Data Out pin should be connected to a general-purpose input/output (GPIO) pin on the Pico, such as GP15. This setup allows the Pico to receive temperature and humidity readings from the DHT11.

Incorporating the SSD1306 OLED display involves similar connections. Start by attaching the display’s VCC pin to the 3.3V pin on the Raspberry Pi Pico to power the display. Connect the GND pin of the OLED display to a ground pin on the Pico to establish a common ground. For data communication, the display’s SCL (Serial Clock Line) pin should be connected to the Pico’s SCL pin, in this case it is connected with GP17. Similarly, connect the SDA (Serial Data Line) pin of the OLED display to the Pico’s SDA pin, in this case it is connected with GP16. This configuration allows the Pico to send commands and data to the OLED display, enabling it to show text, graphics, or the sensor data received from the DHT11 sensor.

Ensure all connections are secure, possibly using a breadboard for an easier setup.



Programming:

To run this project, make sure that you have Thonny IDE or any other IDE of your choice installed. In this project, we will use three scripts, which are: ssd1306.py, dht.py, and main.py. The ssd1306.py file will be used for the driver code of the SSD1306 OLED display module, which I have discussed in my previous article. The dht.py file will contain the library code for the DHT11 temperature and humidity sensor. As you know, the DHT11 Temperature and Humidity sensor does not work without the library. Finally, in the main.py file, we will write the actual code for our project, which will display the Temperature and Humidity on the SSD1306 OLED display module.

ssd1306.py:

First, open your Thonny IDE and create a new file, then copy and paste the code provided below, and then save this code on the Raspberry Pi Pico with the name ssd1306.py.

DHT11 temperature and humidity sensor and Raspberry pi pico




dht.py:

Now, create a new file again, this time copy and paste the code given below. And then save this code as dht.py on the Raspberry Pi Pico.

DHT11 temperature and humidity sensor and Raspberry pi pico



main.py:

Finally, create the third and final script file and paste the actual code of the project into it, and then save this file with the name main.py

DHT11 temperature and humidity sensor and Raspberry pi pico

After saving all three script files, just press the ‘Run’ button or the ‘F5’ button on the keyboard.

DHT11 temperature and humidity sensor and Raspberry pi pico

After clicking the Run button, you will be able to see the temperature and humidity on SSD1306 OLED display module.

DHT11 temperature and humidity sensor and Raspberry pi pico

As you can see, when I applied heat using a lighter, the temperature and humidity changed.

DHT11 temperature and humidity sensor and Raspberry pi pico



Code Explanation:

It begins with the statement from machine import Pin, I2C, which is crucial for importing the Pin and I2C classes from the machine module. This module is a standard in microcontroller programming, facilitating direct interaction with hardware components. Next, the script includes import ssd1306, which brings in the ssd1306 module. This module is specifically designed to control SSD1306-based OLED displays using the I2C communication protocol, highlighting its significance in handling display functionalities. Following this, from dht import DHT11 is used to import the DHT11 class from the dht module. This class is essential for interfacing with DHT11 sensors, which are commonly used to measure temperature and humidity. Lastly, the script imports the time module with import time. The time module is a fundamental part of Python’s standard library, offering various time-related functions, such as implementing delays.

The line i2c = I2C(0, scl=Pin(17), sda=Pin(16)) is pivotal, as it initializes the I2C communication protocol. This is done by designating GPIO pins 17 and 16 to serve as SCL (clock) and SDA (data) lines, respectively. These pins are crucial for enabling two-way communication between the microcontroller and I2C-compatible devices. Following this, the variables oled_width and oled_height are set, defining the dimensions of the OLED display, which in this case are 128×64 pixels. This specification is essential for ensuring that graphics and text are rendered correctly on the display. Finally, an instance of the SSD1306 OLED display is created with the statement oled = ssd1306.SSD1306_I2C(oled_width, oled_height, i2c). This line of code is significant as it not only instantiates the display but also passes the previously set dimensions and the initialized I2C instance to it. This configuration is essential for the proper functioning of the display, allowing it to receive data and commands over the I2C protocol.



The statement pin = Pin(15, Pin.OUT, Pin.PULL_DOWN) is instrumental in this process, where a General Purpose Input/Output (GPIO) pin, specifically pin 15, is configured for the DHT11 sensor. This configuration involves setting the pin as an output and enabling a pull-down resistor. The pull-down resistor is crucial as it ensures a stable low logic level when the pin is not actively driven high, providing a more reliable signal for sensor operation. Following this, the code dht_sensor = DHT11(pin) plays a vital role in creating an instance of the DHT11 sensor. By passing the configured pin to the DHT11 class, this line effectively initializes the sensor, making it ready to measure temperature and humidity. This initialization is a key step in enabling the microcontroller to interact with the DHT11 sensor and retrieve environmental data.

while True: statement, initiating an infinite loop that serves a crucial role in continuously reading and displaying data from a DHT11 sensor on an OLED display. Within this loop, several actions are sequentially performed. First, dht_sensor.measure() is called, which activates the DHT11 sensor to take a new set of temperature and humidity measurements. Following this, the temperature reading is retrieved and stored in the variable temp using temp = dht_sensor.temperature, and similarly, the humidity reading is acquired and stored in hum with hum = dht_sensor.humidity. After obtaining these readings, the OLED display is cleared with oled.fill(0) to ensure that previous data is not mixed with the new readings. Subsequently, oled.text(…) is used to display the current temperature and humidity values on the OLED screen. This function is responsible for formatting and positioning the text on the display. Once the text is set, oled.show() is invoked to refresh the display, ensuring the new data is visible. Finally, time.sleep(2) pauses the execution of the loop for 2 seconds. This delay is essential to prevent rapid and continuous sensor readings, which could lead to data flooding and potential sensor overheating, ensuring that the system operates reliably and the sensor readings are updated at a manageable rate.




exception handling block, except OSError as e:, which is specifically designed to catch OSError exceptions. These exceptions are typically raised in scenarios where there is a problem in communicating with the sensor. This could be due to various reasons such as wiring issues, sensor malfunctions, or communication protocol errors. By catching OSError, the program can gracefully handle these unforeseen errors instead of crashing or behaving unpredictably. When such an exception is caught, the code print(‘Failed to read sensor.’) is executed. This statement serves a critical role in informing the user or the developer about the failure in sensor reading. It outputs the error message ‘Failed to read sensor.’ to the console or the designated output stream. This kind of feedback is invaluable for debugging and maintenance purposes, as it provides a clear indication of the failure point in the sensor-reading process, allowing for timely and targeted troubleshooting.

Conclusion

Interfacing a DHT11 temperature and humidity sensor with Raspberry Pi Pico and SSD1306 OLED display can be a rewarding project. It not only allows you to monitor environmental conditions but also provides hands-on experience with popular components and the Python programming language. Whether you’re a hobbyist, student, or professional, this project offers valuable insights into the world of microcontrollers and sensors.

Related Articles:

Interfacing SSD1306 OLED Display with Raspberry Pi Pico

Display Analog Value of Potentiometer on SSD1306 OLED using Raspberry Pi Pico

HC-SR04 Ultrasonic Distance Sensor with Raspberry Pi Pico and SSD1306 OLED: Displaying

 

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button