Library 12- OneWire library for STM32F4

onewire devices1-Wire is a device communications bus system designed by Dallas Semiconductor Corp. that provides low-speed data, signaling, and power over a single signal.

It’s used in some temperature sensors, like DS18B20 temp sensor and many others products from Dallas.

OneWire key features

  • Single Contact Sufficient for Control and Operation
  • Unique ID Factory-Lasered in Each Device
  • Power Derived from Signal Bus (“Parasitically Powered”)
  • Multidrop Capable: Supports Multiple Devices on Single Line
  • Exceptional ESD Performance


17th May, 2014: There was some new functions added!



  • Read/write byte
  • Search for devices on 1-wire pin
  • Read rom from device
  • Version 2.0 – January 04, 2015
    • Library rewritten
    • It supports unlimited pins and gpios for OneWire
    • This allows you to group sensors to separate ports to prevent communication failures if one sensor is broken
  • Version 2.1 – March 10, 2015
    • Supported new GPIO system


    • STM32F4xx
    • STM32F4xx RCC
    • STM32F4xx GPIO
  • TM
    • TM DELAY
    • TO GPIO
    • defines.h
    • attributes.h


Before you can work with 1-wire bus, you have to initialize it

Reset pulse

If you need to reset 1-wire bus, simple call reset function

Write byte

Read byte

Search devices

Select device

Because you have more devices on one line, you have to somehow select device which you want to communicate with. You can do this with

Functions and enumerations


I connected 2 DS18B20 sensors to PD0 (make sure, you have external pullup resistor) and this code returns me on terminal.

One Wire devices connected to STM32F4

One Wire devices connected to STM32F4

Example 2

  • 4 devices are connected to STM32F4 via OneWire:
  • 2 OneWire ports:
    • PA3
    • PD0
OneWire example with multi instances

OneWire example with multi instances

Projects available on Github, download library below.

TM STM32F4 OneWire Library

OneWire library for onewire devices


Owner of this site. Also electronic enthusiasts, web developer, 3D printer fan, handball player and more. Big fan of STM32F4 devices. In anticipation of the new Discovery board for STM32F7 lines.

You may also like...

Read before commenting!

Before you make a new comment, make sure you agree with things listed below:

  • - Read post to make sure if it is already posted what you are asking for,
  • - Make sure you have the latest version of libraries used in your project,
  • - Make a clean and grammatically correct written message,
  • - Report as many details as possible, including what have you done so far,
  • - Do NOT post any code here. Use Pastebin,
  • - Do NOT post any error codes here. Use Pastebin,
  • - Specify STM32Fxxx family and used Discovery/EVAL/Nucleo or custom made board,
  • - Make sure your clock is set correct for PLL,
  • - If you are using my HAL drivers, please check this post how to start.
Comment will be deleted on breaking these rules without notification!
  • Thamanoon Kedwiriyakarn

    Thank you very much for your code, I do to finish one with stm32f103rb connect to DS18S20+.,12412.0.html

  • TC Zeynel Abidin Delikan

    Sory you are begining with systick but didnt write how can we use it !

    Please write about some example for systick controlling.

    Thank you for your sharing.

    • What?
      Please post this to post related to systick.

  • TC Zeynel Abidin Delikan

    Hi Tilen,

    Firstly thank you for your sharing.

    I try to make a delay time with systick timer. I’m using STM32F429 Disco kit for developing.

    And my code is…

    #include “stm32f4xx.h”
    #include “stm32f4xx_rcc.h”
    #include “stm32f4xx_gpio.h”
    #include “core_cm4.h” // SysTick Registerlarının bulunduğu kütüphane

    void Delay(__IO uint32_t time);
    __IO uint32_t TimingDelay;

    int main(void) {

    // Disco bordumuzun kırmızı ve yeşil ledlerimizi kullanmak için AHB1 i hazırlıyoruz

    // GPIO port tiplerini kullanabilmek için GPIO_InitDef değişkenimizi tanımlıyoruz
    GPIO_InitTypeDef GPIO_InitDef;

    // İlk olarak GPIOG portunun pinlerini düzenliyoruz

    // Pinlerimizi Belirliyoruz
    GPIO_InitDef.GPIO_Pin = GPIO_Pin_13 | GPIO_Pin_14;
    // Pinlerimizi çıkış tipini Push Pull olarak belirliyoruz
    GPIO_InitDef.GPIO_OType = GPIO_OType_PP;
    // Pinlerimizi çıkış olarak tanımlıyoruz.
    GPIO_InitDef.GPIO_Mode = GPIO_Mode_OUT;
    // Pinlerimizin çekme direncini ayarlıyoruz.
    // Pinlerimizin çalışma frekanslarını ayarlıyoruz
    GPIO_InitDef.GPIO_Speed = GPIO_Speed_2MHz;

    //Şimdi portlarımızı kuralım
    GPIO_Init(GPIOG, &GPIO_InitDef);

    // Şimdi ledlerimizin ilk değerlerini tanımlıyoruz.
    // Kırmızı Led “Düşük”, yeşilled ise “Yüksek” durumda olacak
    GPIO_SetBits(GPIOG, GPIO_Pin_14);
    GPIO_ResetBits(GPIOG, GPIO_Pin_13);

    //Son Olarak Systick Timer’ı 1ms’ye ayarlayalım. (sistem saatimiz ön tanımlı olarak 180Mhz dir)

    while (1)
    GPIO_ToggleBits(GPIOG, GPIO_Pin_13 | GPIO_Pin_14);

    return 0;

    void SysTick_Handler(void)
    if (TimingDelay != 0)
    TimingDelay –;
    void Delay(__IO uint32_t time)
    TimingDelay = time;
    while(TimingDelay != 0);

    In this case my “SysTick_Config(SystemCoreClock/11270);” command very nearly 1ms.

    But its not true !!!!

    How can fix it to the 1ms with using 180Mhz clock frequency ?

    when I try to use SystemCoreClock/100 its running on over the 1second !

    Thank you for your help !

    • Your config is wrong for systick.
      If you have 180MHz clock, then to get every 1ms interrupts, you need to divide your clock with 1000. This will do 1000interrupts in one second which is 1ms interval.

      PS: wrong post for that.

      • TC Zeynel Abidin Delikan

        Thank you for your feedback.

        I was checked it.
        if the coocox Ide can be select MCU type. All directions is true at the stm32f4xx.h files !

        But when I try to use to divide 1000 my led blink

        the led’s are burning more then the 1ms…

          Or I will ignore any future comments from you.

  • TC Zeynel Abidin Delikan

    Im so sory Majerle i think ım at the systick subject … Please delete my all post please :(.

  • Pingback: Library 03- STM32F4 system clock and delay functions - STM32F4 Discovery()

  • Jura

    Hey Tilen,

    I am trying to connect stm32f429I with DHT11 using your OneWire library but there seems to be no response from sensor. I am trying to use your devices = TM_OneWire_First(&OneWire1); function but response is always 0x00. I got 3 same sensors so there isn’t possibility that it’s broken. When I try to read data using
    data[0] = TM_OneWire_ReadByte(&OneWireStruct);

    It gives me fixed output, different on every pin. There shouldn’t be timer error cause it’s working fine with my other sensors. I tried connecting it with 4K7Ohms and without and there is no difference. Where could be error?
    LP, Jura

    • are you sure, this sensor works with OneWire?

      • Jura

        Well I checked deeper in datasheet and seems that it’s not totally compatible. I have written a simple program with putting GPIOx high and low and there is some response but it seems that it is timer problem. I can’t make it delay using only 1 us. Nvm it’s for some other discussion then.
        Thanks for reply 🙂

  • Glett

    You have a bug in the size of buf vector “char buf[30]”!!!
    When you use:
    sprintf(buf, “Devices found on 1-wire instance: %dn”, count);
    the size of the string copied to buf is 36.
    The same occur in all the examples

    Thank you for share, this library was very helpfull to me!!!!


    • Uau, totally sorry, its example, not lib 😉 no need to scream.

      • Glett

        Lol, is true!

  • guest

    how to read temperature

    • guest

      I am using stm32f407(discovery board)

      • guest

        I mean can you share keil configuration photos? (options for target photos?)

  • Christian Rangel

    Hello dear Tilen Majerle. I’ve written because i’ve a query. I want to use this library for implemented the One wire in my Nucleo-64 Board F401RE, it’s a STM32F4 Device. But i use the STM32Cubemx for the configuration GPIO’S, Clock, UART, ADC and all the peripherals and the system configuration.
    I notice that you do not use the STM32Cubemx and that you do the configuration and everything else directly in keil.
    I will want to know that if i can use only the one wire part of this library Without using the part of the configurations and that. Since I would do it through the Cube.

    • If you impelement macros for input/output on your own, you can easilynise this lib with hal. But you will need microseconds delay too.

      • Christian Rangel

        Yes, I’m using the HAL libraries and in this libraries there is the HAL_Delay that works for the delay. Well, i going to probe this library with the configuration through of the STM32Cubemx and then i will commented you if works or not. Thank you Tilen.
        Keep working as you go, excellent.

  • stm32fan

    I have a problem. I connected everything as i should and wrote the code up to devices = TM_OneWire_First(&OneWire1);. Devices read as 0, but what is more important, sensor and wires around him got really hot. I connected everything according to scheme online:

    Can you please help me understand what i did wrong ?

    • On the picture, you are looking at sensor from bottom side. Id you take your sensor and make pins in front, do you have correct wiring. If sensors are hot, you wrongly connected GND and VDD.

      • stm32fan

        I dont get it, i connected it as the picture says and sensor gets hot, i replace gnd and 3V and sensor stays cold. Strange, but i read the schematic in sensor manual and i connected it as i should.
        Anyway i get 0 devices now, i am guessing i burned out the sensor ?

        • stm32fan

          Nvm, now i understand what u mean, bottom side, i made a mistake :/ Anyway, sensor is burned i guess ?

          • stm32fan

            I managed to get it to work and display that code, but then i rearanged the code and suddenly it is only displaying 0x96. Any idea why ?

          • “but then i rearanged the code and suddenly it is only displaying 0x96”. No idea.

          • stm32fan

            It is working now. Now i have a different problem, my stm32 is running at 168Mhz, what do i have to change to get the temperature reading ? Right now “if (TM_DS18B20_Read(&OneWire1, device[i], &temps[i]))” throws me into else statement

          • stm32fan

            I figured it out. Turns out i have a sensor DS18S20 instead of DS18B20 and it fails at “check rom”. Do you think i can rework your library to work with DS18S20 or just buy DS18B20 ? Also question remains what to do about running clock (delays)

          • Clock is ok if it works.

          • stm32fan

            I ordered the correct sensor. Does that mean i dont have to change your code and it will give me the correct reading even at 168Mhz ?

          • Onewire communication works as I see. So no issue.