Code examples for One-Wire library use for multiple ds18b20 sensors?

Hi everyone,

After getting my temp logger up and running with TMP35 analog sensors, I’d like to upgrade to using the ds18b20 sensors. I’d like to use the supplied One-Wire library, but all the code examples I can find are pre-library (including this one:

So as a noob, if anyone has a code resource that I could be forwarded that outlines the One-Wire library use on multiple sensors, I’d be grateful. I’ll need 3 sensors on my setup.


You could try the 1-Wire library accessible via the IDE:

Here’s what I use:

`ow<-Onewire(hardware.uart12); ow.init()
local dcount=ow.getDeviceCount(); local devices=ow.getDevices()

function DS18B20(device) {
ow.reset(); ow.matchRom(); for (local i=7; i>=0; i-- ) {ow.writeByte(device[i]); }
ow.writeByte(0x44); imp.sleep(0.8)
ow.reset(); ow.matchRom(); for (local i=7; i>=0; i-- ) {ow.writeByte(device[i]); }
local tempLSB=ow.readByte(); local tempMSB=ow.readByte() 
local tempCelsius=((tempMSB*256)+tempLSB)/16.0  ;
return tempCelsius      }`

Then call each DS18B20 like this:

newTemp[0]=DS18B20(devices[0]); newTemp[1]=DS18B20(devices[1])

Works like a charm for my two devices, but should work for any number, I would think.

Hey thanks Rfarmer. Although I have had numerous read throughs the onewire library documentation, I had been struggling to conceptualise how to put it into practice. Your chunk of code helps! My sensors will arrive this week and I look forward to giving it a go.

Thanks again!