import machine
import onewire
import ds18x20
import time
# Define the pin where the DS18B20 data line is connected
data_pin = machine.Pin(26, machine.Pin.PULL_UP) # Replace with your GPIO pin
# Set up the 1-Wire bus
ow = onewire.OneWire(data_pin)
# Set up the DS18B20 sensor
ds = ds18x20.DS18X20(ow)
# Scan for devices on the 1-Wire bus
devices = ow.scan()
if not devices:
print("No DS18B20 devices found on the bus!")
else:
print("Found DS18B20 devices:", devices)
while devices:
try:
# Initiate temperature conversion
ds.convert_temp()
time.sleep(1) # Wait for the conversion to complete
for device in devices:
# Read and print the temperature for each device
temp = ds.read_temp(device)
print("Temperature: ",temp,"°C")
time.sleep(2) # Delay before the next reading
except Exception as e:
print("Error reading temperature:", e)