Introduction
In this DIY electronics project, we will explore the process of building a simple temperature logging system using the STM32F103C8T6 microcontroller. This project is an excellent way to get hands-on experience with microcontroller programming, sensor integration, and data logging. The STM32F103C8T6 is a 32-bit ARM Cortex-M3 microcontroller, offering plenty of features for DIY enthusiasts and hobbyists, including GPIO pins, UART, I2C, SPI communication, and timers. We’ll be using the STM32F103C8T6 to read data from a temperature sensor (the widely-used DS18B20) and log this data for later analysis.
Project Overview
The goal of this project is to build a temperature logger that continuously measures the temperature at regular intervals, stores the data, and provides a way to access the logged information. We'll use the STM32F103C8T6 to:
1) Interface with the DS18B20 temperature sensor.
2) Store temperature readings in EEPROM or an SD card.
3) Optionally, display the readings on an LCD screen or send data via UART for logging.
Components Required
1) STM32F103C8T6 Microcontroller: The central processing unit for the project.
2) DS18B20 Temperature Sensor: A digital temperature sensor that communicates over a 1-Wire bus.
3) 16x2 LCD Display: To display the temperature readings.
4) EEPROM Module: To store temperature data.
5) Push Buttons: For user input (e.g., to start logging).
6) Resistors, Capacitors, and Jumpers: For general circuit construction.
7) Breadboard and Wires: For assembling the circuit.
8) Power Supply (5V): To power the microcontroller and sensors.
9) ST-Link/V2 Programmer: For programming the STM32F103C8T6.
Step-by-Step Build
1. Setting Up the STM32F103C8T6
The first step in building this temperature logger is to set up the STM32F103C8T6 microcontroller. This is a relatively easy task, but it’s essential to know how to program the STM32F103C8T6.
Programmer/Debugger Setup: The STM32F103C8T6 doesn’t have a built-in USB interface for programming, so you’ll need an external programmer/debugger, like the ST-Link/V2.
Development Environment: We will use STM32CubeIDE, which is an integrated development environment for STM32 microcontrollers. It comes with the STM32CubeMX tool for generating initialization code and setting up peripherals.
a. Download and install STM32CubeIDE from STMicroelectronics. b. Launch STM32CubeIDE and create a new project for the STM32F103C8T6. c. Select the appropriate settings for the board (e.g., the correct part number or custom board if you’re using a development board).
Pin Configuration: Using STM32CubeMX, configure the microcontroller’s pins for I2C (for the LCD) and GPIO for the DS18B20 temperature sensor. The DS18B20 uses a single-wire interface, so you'll need to assign a pin for the 1-Wire bus.
2. Wiring the Components
Once your microcontroller is set up and ready for programming, you can begin connecting the components to your STM32F103C8T6.
DS18B20 Temperature Sensor:
2) The DS18B20 communicates over a 1-Wire bus, which means you need to connect a single wire from the sensor to the selected GPIO pin on the STM32. Additionally, connect the VCC pin of the DS18B20 to 3.3V (or 5V, depending on the model) and the GND pin to the ground of the system. A 4.7kΩ pull-up resistor is also needed between the GPIO pin and VCC.
DS18B20 Connections:
2) VCC → 3.3V (or 5V)
3) GND → GND
4) Data → GPIO pin (configured in CubeMX)
5) Pull-up Resistor (4.7kΩ) between Data and VCC
6) 16x2 LCD Display:
7) The LCD display uses I2C communication, which simplifies wiring. The I2C interface uses two pins: SDA (data) and SCL (clock). Connect these pins from the STM32F103C8T6 to the corresponding pins on the LCD, with appropriate pull-up resistors (typically 4.7kΩ).
LCD Connections:
2) SDA → I2C Data pin (from STM32)
3) SCL → I2C Clock pin (from STM32)
4) VCC → 5V
5) GND → GND
6) EEPROM or SD Card:
7) For storing data, we will use an external EEPROM or an SD card module. This is an optional feature depending on how much data you need to log. The SD card uses SPI or SDIO communication, while the EEPROM usually communicates via I2C or SPI.
1) EEPROM Connections:
2) SDA → I2C Data pin (from STM32)
3) SCL → I2C Clock pin (from STM32)
4) VCC → 3.3V
5) GND → GND
6) SD Card Connections (if using SPI):
7) MISO, MOSI, SCK, CS → SPI pins on STM32
3. Writing the Code
Now that the hardware is set up, we can begin programming the STM32F103C8T6. Here's a breakdown of the code:
Initialization:
Initialize the I2C peripheral for communication with the LCD. Initialize the GPIO pin for the 1-Wire interface to communicate with the DS18B20 temperature sensor.
1-Wire Protocol:
Implement the 1-Wire protocol to read temperature data from the DS18B20 sensor. This involves sending a command to the sensor to start a conversion and then reading the raw temperature data. You can use the STM32 HAL library to make this process easier.
Reading Temperature Data:
Once the temperature is converted, the DS18B20 provides the result as a 16-bit value that needs to be processed into a human-readable temperature.
Logging the Data:
Store the temperature readings in EEPROM or an SD card, depending on the storage option you have selected. If you choose to log to EEPROM, you can store the data periodically, while the SD card may allow you to store more data for extended periods.
Display on LCD:
The temperature data can also be displayed on a 16x2 LCD screen. To do this, you’ll need to use the I2C interface to communicate with the LCD.
Data Upload:
Optionally, you can add functionality to upload the logged data to a computer via UART or even display it in real-time.
4. Sample Code Snippets
Here is a simplified version of the code to get you started:
#include "stm32f1xx_hal.h" #include "ds18b20.h" // Custom library for DS18B20 #include "lcd_i2c.h" // Custom library for LCD void SystemClock_Config(void); static void MX_GPIO_Init(void); static void MX_I2C1_Init(void); static void MX_USART1_UART_Init(void); I2C_HandleTypeDef hi2c1; UART_HandleTypeDef huart1; int main(void) { HAL_Init(); SystemClock_Config(); MX_GPIO_Init(); MX_I2C1_Init(); MX_USART1_UART_Init(); DS18B20_Init(); // Initialize DS18B20 sensor LCD_Init(); // Initialize LCD char temp[10]; while (1) { float temperature = DS18B20_ReadTemperature(); sprintf(temp, "Temp: %.2fC", temperature); LCD_DisplayString(temp); // Display temperature on LCD HAL_UART_Transmit(&huart1, (uint8_t*)temp, strlen(temp), 1000); // Optionally transmit via UART HAL_Delay(1000); // Delay for 1 second } } void SystemClock_Config(void) { // System Clock Configuration (auto-generated by STM32CubeMX) } static void MX_GPIO_Init(void) { // GPIO Initialization (auto-generated by STM32CubeMX) } static void MX_I2C1_Init(void) { // I2C Initialization (auto-generated by STM32CubeMX) } static void MX_USART1_UART_Init(void) { // UART Initialization (auto-generated by STM32CubeMX) }
5. Testing the System
Once the code is programmed into the STM32F103C8T6, it’s time to test the system. Power on the circuit and monitor the LCD screen. You should see the temperature displayed in real-time. If you have implemented data logging, the system will store the readings in EEPROM or SD card, which can later be accessed or analyzed.
Conclusion
This project provides a solid introduction to working with the STM32F103C8T6 microcontroller. By completing this temperature logger project, you will learn how to interface with sensors, use communication
Comments
participate in discussions
Please login ? to participate in the comments
New customer Start here.