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