Skip to content

Instantly share code, notes, and snippets.

@analogic
Created October 11, 2024 09:57
Show Gist options
  • Save analogic/d19184ba2373d85a213f4c4ff3f79791 to your computer and use it in GitHub Desktop.
Save analogic/d19184ba2373d85a213f4c4ff3f79791 to your computer and use it in GitHub Desktop.
one-wire
import time
import board
import busio
import adafruit_ds18x20
from adafruit_onewire.bus import OneWireBus
import microcontroller
ds18b20_pin = board.D2
ow_bus = OneWireBus(ds18b20_pin)
devices = ow_bus.scan()
sensors = []
for device in devices:
if adafruit_ds18x20.is_ds18x20(device):
sensors.append(adafruit_ds18x20.DS18X20(device))
if len(sensors) == 0:
print("No DS18B20 devices found.")
else:
print(f"Found {len(sensors)} DS18B20 sensors.")
while True:
try:
for i, sensor in enumerate(sensors):
temperature = sensor.temperature
print(f"Sensor {i+1}: {temperature:.2f} °C")
time.sleep(2)
except Exception as e:
print("Error: ", e)
time.sleep(5) # Wait before retrying
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment