LowPowerLab Forum

Hardware support => Projects => Topic started by: LukaQ on February 04, 2017, 09:40:35 AM

Title: Temperature sensing with DS18B20
Post by: LukaQ on February 04, 2017, 09:40:35 AM
Hello,

I used WeatherNote as start sketch, added all that was need to read DS18B20 and to send data back to gateway. First I had to change the ID of flash, I found this site to be useful

https://102g.ru/electronics/datasheets/Memory/idcodes.txt

From there and from datasheet I know to set flash ID to 0x2020.
Data pin for DS is on pin3, which you can change in
Code: [Select]
OneWire myds(3);
You have option to enable or disable led flashing, serial with
Code: [Select]
#define BLINK_EN
#define SERIAL_EN 
by commenting/uncommenting

Also you have option to set DS resolution to 9,10,11,12 bit with
Code: [Select]
 resolution = 9;
And for battery operation or slow events, you have sleep option
Code: [Select]
#define SEND_LOOPS   1
with
Code: [Select]
period_t sleepTime = SLEEP_8S;
will define how often the temp is transmitted.
Quote
15 loops of 8sec cycles = 120sec
(https://lowpowerlab.com/forum/index.php?action=dlattach;topic=2419.0;attach=2022;image)
Title: Re: Temperature sensing with DS18B20
Post by: ChemE on February 04, 2017, 01:59:57 PM
I've spent a long time refining my DS18B20 code.  Anymore I use them in a non-blocking fashion so you can call for a temperature measurement, go to sleep, and then wake back up and collect the result.  I also changed my code so you don't need a pullup resistor on the sensor, my code uses the built-in pullup resistors of the 328p.  Finally, I hate big bloated slow code so mine has no dependencies and compiles to less than 1k.  Perhaps something in here will be useful for your project.

https://lowpowerlab.com/forum/moteino/improvedoptimized-ds18b201wire-read/msg14975/#msg14975
Title: Re: Temperature sensing with DS18B20
Post by: LukaQ on February 05, 2017, 07:51:25 AM
I will take a look for sure, that is a good goal to have, but for now, really low power is not up high in the list of things to learn in this ecosystem, since there are still things like power leds and regulators, which all they do is eat power and give nothing in return.
Do you use this sensors with battery power? that kind of battery? Any more info to read about?
Title: Re: Temperature sensing with DS18B20
Post by: perky on February 05, 2017, 08:38:06 AM
It is possible to use the UART peripheral for one-wire devices, a somewhat neater way IMO than using blocking code or even state machines driven by a timer:
https://www.maximintegrated.com/en/app-notes/index.mvp/id/214
Mark.
Title: Re: Temperature sensing with DS18B20
Post by: ChemE on February 05, 2017, 11:19:24 AM
Do you use this sensors with battery power? that kind of battery? Any more info to read about?

I don't currently but I will later this spring in order to monitor a pond.  This node will be powered by 2xAA LiFeS2 batteries since their entire usable life is spent between 3.6 and 2.4VDC.  I haven't yet done careful current measurements on a DS18B20 but I should be able to take a temperature every five minutes and get years from a set of batteries.  Basically that node will be a 328p probably running at 8MHz, a RFM69CW broadcasting at its lowest power, a DS18B20 in a food safe thermal probe (cheap on eBay), and a BME280 to collect ambient Temp, RH basically for free.
Title: Re: Temperature sensing with DS18B20
Post by: ChemE on February 05, 2017, 01:45:43 PM
It is possible to use the UART peripheral for one-wire devices, a somewhat neater way IMO than using blocking code or even state machines driven by a timer:
https://www.maximintegrated.com/en/app-notes/index.mvp/id/214
Mark.

This strikes me as a pretty interesting notion/thing to play with.  Do you have any functioning code for a DS18B20 or other one-wire device using the UART?
Title: Re: Temperature sensing with DS18B20
Post by: perky on February 05, 2017, 02:02:29 PM
No, I had no need at the time. The concept of using a UART is really good because it allows tight timing control and be interrupt driven, it just seems a natural way to go IMO.
Mark.
Title: Re: Temperature sensing with DS18B20
Post by: ChemE on February 05, 2017, 09:41:11 PM
Yeah, I've never been so short on pins or so busy with other work but it is interesting to let the hardware take care of the timings instead of all the _delay_us() that I use.  The UART can be cranked up to 2.0MHz, not sure if the DS18B20 could hang in there but there is one way to find out...MWAHAHAHAHA!
Title: Re: Temperature sensing with DS18B20
Post by: perky on February 05, 2017, 10:07:44 PM
yes. If you also wanted to sleep the processor to reduce power (with just the peripheral clocks running) you'd need to use timer interrupts to do your delay_us timings, and maybe even drive state machines with those interrupts. It starts to get a bit messy, the UART approach seems cleaner IMO.
Mark.
Title: Re: Temperature sensing with DS18B20
Post by: RoSchmi on February 18, 2017, 04:47:03 AM
Thanks @LukaQ for this contribution, it gave me a good start (Worked out of the box)
Thanks @ChemE an @perky for the valueable links.
Title: Re: Temperature sensing with DS18B20
Post by: LukaQ on February 18, 2017, 12:01:08 PM
Thanks @LukaQ for this contribution, it gave me a good start (Worked out of the box)
Thanks @ChemE an @perky for the valueable links.

I think you should have the latest I use, change what you want (and need to)
I fixed all the things that were not wanted, I used the fast non-blocking DS script and I have to say, it's pretty damn good. But I do have BME280 on the way, I think that will be far better.
Also note, that this is for one sensor only, didn't see that it would work for 2 or more.