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
10import board
11from adafruit_onewire.bus import OneWireBus
12from adafruit_ds18x20 import DS18X20
13
14
15# Initialize one-wire bus on board pin D5.
16ow_bus = OneWireBus(board.D5)
17
18# Scan for sensors and grab the first one found.
19ds18 = DS18X20(ow_bus, ow_bus.scan()[0])
20
21# Main loop to print the temperature every second.
22while True:
23    print("Temperature: {0:0.3f}C".format(ds18.temperature))
24    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
13import board
14from adafruit_onewire.bus import OneWireBus
15from adafruit_ds18x20 import DS18X20
16
17
18# Initialize one-wire bus on board pin D1.
19ow_bus = OneWireBus(board.D1)
20
21# Scan for sensors and grab the first one found.
22ds18 = DS18X20(ow_bus, ow_bus.scan()[0])
23ds18.resolution = 12
24
25# Main loop to print the temperature every second.
26while True:
27    conversion_delay = ds18.start_temperature_read()
28    conversion_ready_at = time.monotonic() + conversion_delay
29    print("waiting", end="")
30    while time.monotonic() < conversion_ready_at:
31        print(".", end="")
32        time.sleep(0.1)
33    print("\nTemperature: {0:0.3f}C\n".format(ds18.read_temperature()))
34    time.sleep(1.0)