0

with a raspberry pi zero you use the temp sensor ids with python code. Not sure how to add 2 of the same DS18B20 sensor to pico. I am pissed at myself as I damaged the pi zero onewire input on the board and cant reroute it. So tried to do more or less the same thing with the pico in sending temp data from 2 ds18b20 sensors to server. The wifi module works with pico. Any help would be greatly appreciated

DS18x20 temperature sensor driver for MicroPython.

MIT license; Copyright (c) 2016 Damien P. George

from micropython import const

_CONVERT = const(0x44) _RD_SCRATCH = const(0xBE) _WR_SCRATCH = const(0x4E)

class DS18X20: def init(self, onewire): self.ow = onewire self.buf = bytearray(9)

def scan(self):
    return [rom for rom in self.ow.scan() if rom[0] in (0x10, 0x22, 0x28)]

def convert_temp(self):
    self.ow.reset(True)
    self.ow.writebyte(self.ow.SKIP_ROM)
    self.ow.writebyte(_CONVERT)

def read_scratch(self, rom):
    self.ow.reset(True)
    self.ow.select_rom(rom)
    self.ow.writebyte(_RD_SCRATCH)
    self.ow.readinto(self.buf)
    if self.ow.crc8(self.buf):
        raise Exception("CRC error")
    return self.buf

def write_scratch(self, rom, buf):
    self.ow.reset(True)
    self.ow.select_rom(rom)
    self.ow.writebyte(_WR_SCRATCH)
    self.ow.write(buf)

def read_temp(self, rom):
    buf = self.read_scratch(rom)
    if rom[0] == 0x10:
        if buf[1]:
            t = buf[0] >> 1 | 0x80
            t = -((~t + 1) & 0xFF)
        else:
            t = buf[0] >> 1
        return t - 0.25 + (buf[7] - buf[6]) / buf[7]
    else:
        t = buf[1] << 8 | buf[0]
        if t & 0x8000:  # sign bit set
            t = -((t ^ 0xFFFF) + 1)
        return t / 16

2 Answers 2

2

The following code will read the temperature values from all the DS18B20 sensors that it finds and print out the unique serial numbers of each sensor and the temperature that it is reading.

import machine
import onewire
import ds18x20
import time
import binascii

gp_pin = machine.Pin(26)

ds18b20_sensor = ds18x20.DS18X20(onewire.OneWire(gp_pin))

sensors = ds18b20_sensor.scan()

print('Found devices: ', sensors)

while True:
    ds18b20_sensor.convert_temp()
    time.sleep_ms(750)
    for device in sensors:
        s = binascii.hexlify(device)
        readable_string = s.decode('ascii')
        print(readable_string)
        print(ds18b20_sensor.read_temp(device))
    time.sleep(10)

Multiple DS18B20 Connected to Pico

The DS18B20 sensors needs to be connected with the black wires to ground, the red wires to the 3V3 pin and the blue or yellow (some sensors have blue and some have yellow) wires to GP26 (pin 31). A resistor between the value of 4.7k Ohms to 10k Ohms needs to be connected between the 3V3 and GP26 pins to act as a ‘pull-up’ resistor.

The sensors can come with a couple of different wire colour combinations. They will typically have a black wire that needs to be connected to ground. A red wire that should be connected to a voltage source (in our case a 3.3V pin from the Pico) and a blue or yellow wire that carries the signal.

Full details of the setup can be found here

0

take a look at onewire.py

onewire.py's def scan(self) function returns an array of roms (rom codes).

onewire.py's def select_rom(self, rom): selects the sensor via the rom code.

so scan, traverse and read values

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.