Page 1 of 1

OneWire with DS18B20

Posted: Wed Aug 11, 2021 6:02 am
by kiloWATT21
Hi,

i'm using a board with STM32L051K8, and i'm having trouble reading temperature from DS18B20.
I have tried all kinds of libraries and codes without the library, but non of it seems to work.
Is the L0 chips just too slow for OneWire protocol. Do i have to use the solution using USART solution with the diode ( https://cnnblike.com/post/stm32-OneWire/ )?

Re: OneWire with DS18B20

Posted: Wed Aug 11, 2021 7:30 am
by fpiSTM
Hi @kiloWATT21

if you use the STM32 core then simply use this library: https://github.com/PaulStoffregen/OneWire

Re: OneWire with DS18B20

Posted: Wed Aug 11, 2021 7:35 am
by ag123
many libraries may not work 'out of the box' if they use atmega specific features e.g. hardware timers

https://create.arduino.cc/projecthub/Th ... ino-9cc806
https://lastminuteengineers.com/ds18b20 ... -tutorial/

what can be done is to review the codes and adapt/port it across to use stm32timers
examples for using the stm32 hardware timer are here (STM core)
https://github.com/stm32duino/wiki/wiki ... er-library

it is quite easy once you get used to using the stm32 hardware timers, basically 'bit bang' the signals on a port.
you won't need a uart for it as the signals are pretty much 'slow' > 10us.
try the blink examples with stm32 hardware timers to figure out how to control duration, duty cycle, interrupt callback etc

hardware timer codes looks mostly like this

Code: Select all

HardwareTimer *Tim1 = new HardwareTimer(TIM1); 

void Update_IT_callback() {
	//timer calls this every period
	//e.g. digitalWrite(pin), digitalRead(pin);
}

void setup() {
	Tim1->pause();
	Tim1->setMode(channel, TIMER_OUTPUT_COMPARE_PWM1);
	Tim1->setOverflow(10, MICROSEC_FORMAT); // 10 microseconds
	Tim1->attachInterrupt(Update_IT_callback);
	Tim1->refresh();
	Tim1->resume();
}

void loop() {
	// other sketch codes goes here
	// you can still pause(), refresh(), resume() the timer from here
	// the connection between here and the timer callback is to use global variables or arrays
	// use a global variable to manage state
}