This project demonstrates how to interface a DS18B20 digital temperature sensor with an STM32F103C6 microcontroller using the 1-Wire protocol, displaying real-time temperature readings via UART.
The DS18B20 is widely used for precise temperature measurement due to its ±0.5°C accuracy, programmable resolution (9–12 bits), and multidrop capability. It can operate in parasite power mode (drawing power from the data line) or with an external power supply, making it flexible for embedded applications.
STM32CubeMX Setup- MCU Selection: STM32F103C6 (16 MHz clock)
- GPIO Configuration:
-
PA1 → 1-Wire Data Line (Open-Drain Output)
-
PC13 → LED (Output)
- UART1 Configuration:
-
Mode: Asynchronous
-
Baud Rate: 115200
-
Word Length: 8 bits
-
Parity: None
-
Stop Bits: 1
- Generate initialization code and open in STM32CubeIDE.
- Microsecond Delay (DWT Timer)
-
delay_us_dwt_init() – Initializes the DWT timer for precise microsecond delays.
-
delay_us_dwt(us) – Creates an accurate microsecond delay based on the DWT timer.
- DS18B20 Functions
-
DS18b20_temp() – Reads and returns the current temperature value from the DS18B20 sensor.
- OneWire Functions
-
onewire_reset() – Sends a reset pulse and checks for the presence of a OneWire device.
-
onewire_Write(dato) – Sends a single byte of data to the OneWire bus.
-
onewire_Read() – Reads a single byte of data from the OneWire bus.
- GPIO Functions
Required Components:
- STM32F103C6 microcontroller
- DS18B20 digital temperature sensor
- Virtual Terminal (UART)
- 4.7 kΩ pull-up resistor (on DATA line)
Connections:
- PA1 → DS18B20 Data Line (with 4.7 kΩ pull-up)
- PA9 → Virtual Terminal RX
- VDD → 3.3V or Parasite Power Mode
- GND → Common Ground
Steps:
- Load the.hex file from STM32CubeIDE.
- Run the simulation.
- Monitor real-time temperature readings on the Virtual Terminal.
That's all!
If you have any questions or suggestions don't hesitate to leave a comment below
Comments