>Use Cases>
/N32G031_OLED_DHT11

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.

Target MCU

Nations N32G031 Cortex-M0

Display Interface

OLED I2C (PB6: SCL, PB7: SDA)

Sensor

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:

DevicePinN32G031 Board PinNotes & Remarks
DHT11DATA / OUTPA5Requires a 4.7kΩ–10kΩ pull-up resistor (if not built into the module)
DHT11VCC3.3V / 5VCheck sensor voltage rating
DHT11GNDGNDCommon system ground
OLED (I2C)SCLPB6I2C Clock
OLED (I2C)SDAPB7I2C Data
OLED (I2C)VCC3.3VDisplay power supply
OLED (I2C)GNDGNDCommon system ground

Behaviour & Execution Logic

Screen Prompts & Output

1

Boot Screen

Upon startup, the screen displays a welcome message for 1.5 seconds.

Display:

System Ready

2

Normal Operation

The system updates the numbers only when the values change to prevent screen flickering.

Display:

Temp:    28 °C

Humidity: 65 %

3

Error State

If the DHT11 is disconnected or damaged, the system clears the screen and displays a warning.

Display:

Sensor Error!

Check Wiring.

main.c
#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(&current_temp, &current_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:

1

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.

2

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.

3

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.

4

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

1

Open Project: Open this project folder directly in the IDE.

2

Build & Flash: Simply click the Build and Flash buttons on the interface.