Last active
July 17, 2022 00:58
-
-
Save capsulecorplab/8ba84af25d15da1de4f950c2ce0b19c8 to your computer and use it in GitHub Desktop.
micropython class for reading temperature using DS18B20
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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