Created
October 11, 2024 09:57
-
-
Save analogic/d19184ba2373d85a213f4c4ff3f79791 to your computer and use it in GitHub Desktop.
one-wire
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 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