Improved UNO DS18B20 sketch

For stand-alone microprocessors

Improved UNO DS18B20 sketch

Postby Doug Coulter » Wed May 13, 2015 3:18 pm

In keeping with my policy of generating more-useful demos for various arduino widgets - here's one for the Dallas DS18B20 sensor.
What's special here is yes, it's yet another state machine, and no, it doesn't block. It simply maintains an array of temperature measurements, and sets a flag when it updates them. I'm using the waterproof version here for soil temperatures. I've tested this with up to two sensors on the same wire, it seems to call them the same unit # and detect how many there are pretty reliably, though as always, YMMV. There are some interesting comments in the basic library that indicate possibilities of errors, which is why I set the delay a little extra long. But really, this is temperature. Do you really need to go faster than 1/second for a (possible array of) measurement here?

The idea behind this series of sketches is that you should be able to easily take them and incorporate them in your own work. They're supposed to be friendly, let you change things easily with #defines and contain state machines that do the work - or return immediatly - when called from "loop()" so your loop remains responsive to other things - no blocking here, though some things might take a few cycles, delay() is verboten outside of the setup()-called functions.

In theory, this is supposed to support up to 8 sensors on the same wire. Dunno if you need to keep reducing the pullup resistance. I'm doing two with one 4.7k pullup just fine here. Here it is running on my laptop with one sensor in cold water on the floor, and the other looking at the exhaust from the laptop fan.
DS18B20Sketch.png


And here's the sketch:
DS18b20.zip
zip of sketch for arduino IDE 1.6.3
(1.29 KiB) Downloaded 217 times


You might notice that at the top I have a define about REQUIRESALARMS commented out. The default is true, and if you don't need them, uncommenting this saves you a little ram space. The compiler already seems to be smart enough to leave out unreachable code...It seems to save a few bytes but not affect this sketch, since I'm not using alarms here - it's actually faster to just read the temperatures and do that in arduino code anyway.
dallas_temperature_control.zip
The basic Dallas library in case you can't find it.
(22.86 KiB) Downloaded 207 times
Posting as just me, not as the forum owner. Everything I say is "in my opinion" and YMMV -- which should go for everyone without saying.
User avatar
Doug Coulter
 
Posts: 3515
Joined: Wed Jul 14, 2010 7:05 pm
Location: Floyd county, VA, USA

Re: Improved UNO DS18B20 sketch

Postby solar_dave » Tue May 19, 2015 8:57 pm

That's pretty cool Doug! I have been thinking more about the automation of the homestead here. There are lots of opportunities for improvements in this operation.

Now tell us about the png in the lower left corner! :D :D ;) ;)
Dave Shiels

My TED 5000 power monitoring
http://phx-solar.no-ip.info:8081/Footprints.html
solar_dave
 
Posts: 108
Joined: Tue Aug 21, 2012 12:33 pm
Location: Phoenix, AZ

Re: Improved UNO DS18B20 sketch

Postby Doug Coulter » Thu May 21, 2015 3:56 am

Yes, this has already saved me quite a lot of donkey-work around the place, the water system being the biggie so far. I mean to extend it to other areas. For example, one issue I have that you don't is finite battery power. It would, in the worst part of winter, be cool to know how much power my system is "dumping on the ground" when the batteries are already full, so I could utilize it for things like say, heating my cistern so my plumbing doesn't freeze - but only when I'm power-good. The Xantrex stuff doesn't tell me that digitally (unless I do some serious reverse engineering of their proprietary CAN bus stuff). So I will be adding an insolation detector along with the other weather stuff I'm adding....Once the basics are here, it's fairly easy to add more things (like the above to sense soil temperatures for gardening).

Many of the examples for arduino "block" when called for a number, which in turn makes the serial comm not be as responsive as it could be. So I've adopted a systems design strategy (at least for this sort of not-time-critical stuff) of having them just keep some ram variables updated, and the master (a pi model 2 for this case) go around and poll them at around 5 second intervals. Processing on the pi then either low-passes, averages, or sums the results and stuffs them into a MySQL database about once per minute. From that, CGI's serve my LAN, or could do some things automatically. For now, the "water control" CGI page just lets me open/close/set-autofill valves manually, but from any computer on my lan. The reason is - I haven't put in all the sensors on the rain barrel yet - it might be empty, or have lousy water (pollen is the enemy a few weeks of the year and I don't want that in my system), but I will.
For now I use the Mark-1 eyeball after a rain to decide if the stuff is worthy before doing the fillup. But I can do that by looking out a window, instead of putting on snowshoes or going out in the rain and waiting while the water from the rain barrel slowly trickles into the cistern. Once I decide, that's all automatic now. It's a moderately big deal, and foreshadows things to come - there's a lot more I could do, but hey, one step at a time. This is also serving to work all the bugs out of the basic design - the fusor is one of the things I need remote control of - it's working all too well to twist knobs 2 feet from the reactor safely. That's a pretty critical app considering what's invested in the fusor stuff - and some of the safety things will still be hardwired just in case a computer should crash. Not quite there yet - hours in the day limited.

I admit, I do enjoy the utter anachronism of using this high tech stuff to help with my otherwise ancient-news homesteading quest....Wait till you see an arduino controlling a listeroid diesel backup generator, or better - a woodstove. I haven't had pushbutton/thermostat heat since I moved here in 1979 - it'd be nice.

I tend to collect pictures I see on the web that amuse me at the time. Not all are suitable for work, or not-cynical. Since my personal politics aren't supposed to be much of the show here, I don't always post them, but for those interested, I do put them in a spot online: http://www.coultersmithing.com/data/Icons/ for various uses.
This one...it was just funny. And true.
For those who don't want to bother chasing it down, here it is.
BeerSave.png
One must save those in need.


Life isn't all work, after all. And even most of this work counts as play, I like doing it, after all.
Posting as just me, not as the forum owner. Everything I say is "in my opinion" and YMMV -- which should go for everyone without saying.
User avatar
Doug Coulter
 
Posts: 3515
Joined: Wed Jul 14, 2010 7:05 pm
Location: Floyd county, VA, USA


Return to Embedded software

Who is online

Users browsing this forum: No registered users and 2 guests