Skip to content

Instantly share code, notes, and snippets.

@capsulecorplab
Last active July 17, 2022 00:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save capsulecorplab/8ba84af25d15da1de4f950c2ce0b19c8 to your computer and use it in GitHub Desktop.
Save capsulecorplab/8ba84af25d15da1de4f950c2ce0b19c8 to your computer and use it in GitHub Desktop.
micropython class for reading temperature using DS18B20
import machine, onewire, ds18x20, time
class DS18B20:
def __init__(self, ds_pin: int = 4):
self.ds_pin = machine.Pin(ds_pin, machine.Pin.IN)
self.ds_sensor = ds18x20.DS18X20(onewire.OneWire(self.ds_pin))
def read_temperature(self, verbose: bool = False) -> float:
if verbose == True:
self.scan_sensor(True)
elif verbose == False:
self.scan_sensor()
self.ds_sensor.convert_temp()
for rom in self.roms:
return self.ds_sensor.read_temp(rom)
def scan_sensor(self, verbose: bool = False) -> None:
self.roms = self.ds_sensor.scan()
if verbose == True:
print("Found DS devices: ", self.roms)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment