OneWire with DS18B20

Post Reply
kiloWATT21
Posts: 1
Joined: Wed Aug 11, 2021 5:54 am

OneWire with DS18B20

Post 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/ )?
User avatar
fpiSTM
Posts: 1738
Joined: Wed Dec 11, 2019 7:11 pm
Answers: 91
Location: Le Mans
Contact:

Re: OneWire with DS18B20

Post by fpiSTM »

Hi @kiloWATT21

if you use the STM32 core then simply use this library: https://github.com/PaulStoffregen/OneWire
ag123
Posts: 1655
Joined: Thu Dec 19, 2019 5:30 am
Answers: 24

Re: OneWire with DS18B20

Post 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
}    
Post Reply

Return to “STM32L0 based boards”