Show DHT11 on an OLED
N32G031_OLED_DHT11
Introduction
A real-time temperature and humidity monitoring system displaying data on an OLED screen, using the N32G031 microcontroller and a DHT11 sensor. This project is fully optimized for cross-platform workflows using UnityMbed.
Nations N32G031 Cortex-M0
OLED I2C (PB6: SCL, PB7: SDA)
DHT11 1-Wire Single Bus (PA5)
Video Demonstration
Wiring & Hardware Connection
Connect the DHT11 sensor and OLED (I2C) display pins to the N32G031 development board header according to the table below:
| Device | Pin | N32G031 Board Pin | Notes & Remarks |
|---|---|---|---|
| DHT11 | DATA / OUT | PA5 | Requires a 4.7kΩ–10kΩ pull-up resistor (if not built into the module) |
| DHT11 | VCC | 3.3V / 5V | Check sensor voltage rating |
| DHT11 | GND | GND | Common system ground |
| OLED (I2C) | SCL | PB6 | I2C Clock |
| OLED (I2C) | SDA | PB7 | I2C Data |
| OLED (I2C) | VCC | 3.3V | Display power supply |
| OLED (I2C) | GND | GND | Common system ground |
Behaviour & Execution Logic
Screen Prompts & Output
Boot Screen
Upon startup, the screen displays a welcome message for 1.5 seconds.
System Ready
Normal Operation
The system updates the numbers only when the values change to prevent screen flickering.
Temp: 28 °C
Humidity: 65 %
Error State
If the DHT11 is disconnected or damaged, the system clears the screen and displays a warning.
Sensor Error!
Check Wiring.
#include "n32g031.h"
#include "n32g031_gpio.h"
#include "n32g031_rcc.h"
#include "core_cm0.h"
#include "dht11.h"
#include "oled.h"
#include "utils.h"
#include <stdio.h>
int main(void)
{
uint8_t current_temp = 0;
uint8_t current_humi = 0;
uint8_t last_temp = 255;
uint8_t last_humi = 255;
uint8_t was_error = 1;
// 1. Add variable to track consecutive read error counts
uint8_t error_count = 0;
char buffer[20];
SystemInit();
SystemCoreClockUpdate();
delay_ms(500);
OLED_Init();
OLED_Clear();
DHT11_Init();
OLED_ShowString(10, 24, "System Ready", 16);
delay_ms(1500);
OLED_Clear();
while (1)
{
if (DHT11_Read_Data(¤t_temp, ¤t_humi) == 1)
{
// 2. If read succeeds, reset the error counter to 0 immediately
error_count = 0;
if (was_error) {
OLED_Clear();
OLED_ShowString(0, 0, "Temp: ", 8);
OLED_ShowString(0, 32, "Humidity: ", 8);
was_error = 0;
last_temp = 255;
last_humi = 255;
}
if (current_temp != last_temp) {
sprintf(buffer, "%02d C ", current_temp);
OLED_ShowString(0, 12, buffer, 16);
OLED_ShowString(24, 12, "o", 8);
last_temp = current_temp;
}
if (current_humi != last_humi) {
sprintf(buffer, "%02d %% ", current_humi);
OLED_ShowString(0, 44, buffer, 16);
last_humi = current_humi;
}
}
else
{
// 3. If read fails, increment error counter by 1
error_count++;
// 4. If failed 3 times consecutively (actual wiring disconnect), clear screen and display error
if (error_count >= 3) {
if (!was_error) {
OLED_Clear();
was_error = 1;
}
OLED_ShowString(0, 0, "Sensor Error! ", 16);
OLED_ShowString(0, 24, "Check Wiring. ", 16);
}
}
delay_ms(2000);
}
}Learning & AI Extension Ideas
Based on code analysis and state tracking variables (such as error_count, last_temp, last_humi), learners can explore several AI-assisted extensions:
Heat Index & Comfort Level
Use AI to structure main.c for the N32G031 with the Standard Peripheral Library (SPL), read DHT11 temperature/humidity, show it on a 128x32 OLED, classify comfort states with if-else logic (Warning, Hot, Cool, Comfort), and practice basic LED control through Direct Register access.
Maximum (Max) and Minimum (Min) Temperature Statistics
Use AI to implement global Max/Min statistics that update after every successful sensor read, then organize the OLED layout to show the current temperature alongside live statistics.
Non-blocking Blink Alert with Direct Register
Use AI to configure GPIO through Direct Register access without HAL and create a warning LED for temperature thresholds or sensor errors. A loop counter replaces delay() so other system work remains non-blocking.
Real-time Dual-line Graph (Mini Oscilloscope)
Use AI to build a framebuffer and a scrolling dual-line graph with Bresenham's Line algorithm for comparing temperature and humidity, plus a grid background that turns the OLED into a mini oscilloscope.
Build and Flash Guide
Open Project: Open this project folder directly in the IDE.
Build & Flash: Simply click the Build and Flash buttons on the interface.