/ examples / ds18x20_asynctest.py
ds18x20_asynctest.py
 1  # Simple demo of printing the temperature from the first found DS18x20 sensor every second.
 2  # Using the asynchronous functions start_temperature_read() and
 3  # read_temperature() to allow the main loop to keep processing while
 4  # the conversion is in progress.
 5  # Author: Louis Bertrand, based on original by Tony DiCola
 6  
 7  # A 4.7Kohm pullup between DATA and POWER is REQUIRED!
 8  
 9  import time
10  import board
11  from adafruit_onewire.bus import OneWireBus
12  from adafruit_ds18x20 import DS18X20
13  
14  
15  # Initialize one-wire bus on board pin D1.
16  ow_bus = OneWireBus(board.D1)
17  
18  # Scan for sensors and grab the first one found.
19  ds18 = DS18X20(ow_bus, ow_bus.scan()[0])
20  ds18.resolution = 12
21  
22  # Main loop to print the temperature every second.
23  while True:
24      conversion_delay = ds18.start_temperature_read()
25      conversion_ready_at = time.monotonic() + conversion_delay
26      print("waiting", end="")
27      while time.monotonic() < conversion_ready_at:
28          print(".", end="")
29          time.sleep(0.1)
30      print("\nTemperature: {0:0.3f}C\n".format(ds18.read_temperature()))
31      time.sleep(1.0)