Our project is a real-time Temperature and Humidity Monitoring System built using an STM32 microcontroller, an AHT21 temperature and humidity sensor, and an ST7789 LCD display with an 8080 parallel interface.
The system continuously measures the surrounding temperature and humidity using the AHT21 sensor. Instead of using built-in libraries, the STM32 communicates with the sensor through a custom software I²C (bit-banging) driver, allowing full control over timing and data transfer.
When a measurement is triggered, the STM32 sends the required command to the AHT21 sensor and waits for the sensor to complete its internal conversion. After receiving the raw data bytes, the microcontroller processes and converts them into readable temperature (°C) and humidity (%) values.
The calculated values are then displayed in real time on the LCD screen. The display uses an 8-bit 8080 parallel interface, driven by a custom STM32 LCD driver that sends 16-bit RGB color data in two 8-bit transfers. This allows fast and smooth screen updates without relying on external graphics libraries.
The system refreshes the sensor readings every second, providing stable and accurate environmental data. A lightweight custom GUI and bitmap font renderer were implemented to display the values clearly and efficiently.
Step 1 — Pin Mapping and Hardware ConfigurationBefore writing any code, the first step was to carefully analyze the RT-Spark (STM32F407) pinout and understand how the external components are connected to the microcontroller.
The image above shows the STM32 pin assignment as configured in STM32CubeMX, highlighting the pins used for both the AHT21 sensor and the ST7789 LCD display.
AHT21 Sensor Connection (Software I²C)PE0 → AHT21_SDA (Data line)
PE1 → AHT21_SCL (Clock line)
These pins are configured as open-drain GPIO outputs to implement a software-based I²C (bit-banging) interface. This allows full control over the I²C timing instead of using the hardware I²C peripheral, which is useful for learning and debugging low-level communication.
LCD Interface (FSMC – 8080 Parallel Bus)The ST7789 LCD uses an 8080-style parallel interface, connected through the STM32’s FSMC (Flexible Static Memory Controller).
FSMC Data Bus (LCD – 8-bit Mode)The LCD display is connected using the FSMC (Flexible Static Memory Controller) in 8-bit parallel mode.
PD14 → FSMC_D0
- PD14 → FSMC_D0
PD15 → FSMC_D1
- PD15 → FSMC_D1
PD13 → FSMC_A18 is used as a control/address line for the LCD (often mapped as RS / DC)
- PD13 → FSMC_A18 is used as a control/address line for the LCD (often mapped as RS / DC)
Custom drivers are written to control both the sensor and the display without using external libraries.
LCD Driver
Supports 8-bit FSMC transfers
- Supports 8-bit FSMC transfers
Splits 16-bit RGB565 color data into two 8-bit writes
- Splits 16-bit RGB565 color data into two 8-bit writes
Includes custom drawing, text, and bitmap font functions
- Includes custom drawing, text, and bitmap font functions
- LCD DriverSupports 8-bit FSMC transfersSplits 16-bit RGB565 color data into two 8-bit writesIncludes custom drawing, text, and bitmap font functions
AHT21 Driver
Implements software I²C protocol (Start, Stop, ACK, Send/Read)
- Implements software I²C protocol (Start, Stop, ACK, Send/Read)
Sends the measurement command and reads raw sensor data
- Sends the measurement command and reads raw sensor data
Converts raw values into temperature (°C) and humidity (%)
- Converts raw values into temperature (°C) and humidity (%)
- AHT21 DriverImplements software I²C protocol (Start, Stop, ACK, Send/Read)Sends the measurement command and reads raw sensor dataConverts raw values into temperature (°C) and humidity (%)
All drivers are integrated in main.c to create a complete working system.
Sensor data is read every second
- Sensor data is read every second
A moving-average filter smooths temperature and humidity values
- A moving-average filter smooths temperature and humidity values
Processed values are displayed in real time on the LCD
- Processed values are displayed in real time on the LCD
A heartbeat pixel confirms the system is running
- A heartbeat pixel confirms the system is running
Error handling resets the sensor if communication fails
- Error handling resets the sensor if communication fails
The image above shows the system in operation, displaying live temperature and humidity readings on the ST7789 LCD.The STM32 continuously communicates with the AHT21 sensor every second, applies a moving-average filter to smooth the data, and updates the screen in real time.
Temperature is displayed in red with the unit °C.
- Temperature is displayed in red with the unit °C.
Humidity is displayed in blue with the unit %.
- Humidity is displayed in blue with the unit %.
The small blinking pixel in the corner acts as a heartbeat indicator, confirming that the main program loop is running correctly.
- The small blinking pixel in the corner acts as a heartbeat indicator, confirming that the main program loop is running correctly.
If a sensor read fails, an error message (“Read Err”) is displayed briefly, and the STM32 automatically reinitializes the sensor to maintain continuous operation.
- If a sensor read fails, an error message (“Read Err”) is displayed briefly, and the STM32 automatically reinitializes the sensor to maintain continuous operation.
This step demonstrates the integration of hardware, drivers, filtering, and display, showing how the system provides stable, readable, and real-time environmental data.











Comments