Introduction

Documentation Status Discord Build Status

CircuitPython driver for Dallas 1-Wire temperature sensor.

Dependencies

This driver depends on:

Please ensure all dependencies are available on the CircuitPython filesystem. This is easily achieved by downloading the Adafruit library and driver bundle.

Usage Example

import board
from adafruit_onewire.bus import OneWireBus
from adafruit_ds18x20 import DS18X20

ow_bus = OneWireBus(board.D2)
ds18 = DS18X20(ow_bus, ow_bus.scan()[0])
ds18.temperature

Contributing

Contributions are welcome! Please read our Code of Conduct before contributing to help this project stay welcoming.

Documentation

For information on building library documentation, please check out this guide.

Table of Contents

Simple test

Ensure your device works with these simple tests.

examples/ds18x20_simpletest.py
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# SPDX-FileCopyrightText: 2020 ladyada for Adafruit Industries
# SPDX-License-Identifier: MIT

# Simple demo of printing the temperature from the first found DS18x20 sensor every second.
# Author: Tony DiCola

# A 4.7Kohm pullup between DATA and POWER is REQUIRED!

import time
import board
from adafruit_onewire.bus import OneWireBus
from adafruit_ds18x20 import DS18X20


# Initialize one-wire bus on board pin D5.
ow_bus = OneWireBus(board.D5)

# Scan for sensors and grab the first one found.
ds18 = DS18X20(ow_bus, ow_bus.scan()[0])

# Main loop to print the temperature every second.
while True:
    print("Temperature: {0:0.3f}C".format(ds18.temperature))
    time.sleep(1.0)
examples/ds18x20_asynctest.py
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# SPDX-FileCopyrightText: 2020 ladyada for Adafruit Industries
# SPDX-License-Identifier: MIT

# Simple demo of printing the temperature from the first found DS18x20 sensor every second.
# Using the asynchronous functions start_temperature_read() and
# read_temperature() to allow the main loop to keep processing while
# the conversion is in progress.
# Author: Louis Bertrand, based on original by Tony DiCola

# A 4.7Kohm pullup between DATA and POWER is REQUIRED!

import time
import board
from adafruit_onewire.bus import OneWireBus
from adafruit_ds18x20 import DS18X20


# Initialize one-wire bus on board pin D1.
ow_bus = OneWireBus(board.D1)

# Scan for sensors and grab the first one found.
ds18 = DS18X20(ow_bus, ow_bus.scan()[0])
ds18.resolution = 12

# Main loop to print the temperature every second.
while True:
    conversion_delay = ds18.start_temperature_read()
    conversion_ready_at = time.monotonic() + conversion_delay
    print("waiting", end="")
    while time.monotonic() < conversion_ready_at:
        print(".", end="")
        time.sleep(0.1)
    print("\nTemperature: {0:0.3f}C\n".format(ds18.read_temperature()))
    time.sleep(1.0)

adafruit_ds18x20

Driver for Dallas 1-Wire temperature sensor.

  • Author(s): Carter Nelson

Software and Dependencies:

class adafruit_ds18x20.DS18X20(bus, address)[source]

Class which provides interface to DS18X20 temperature sensor :param bus: The bus the DS18X20 is connected to :param int address: The device address.

Quickstart: Importing and using the device

Here is an example of using the DS18X20 class. First you will need to import the libraries to use the sensor

import board
from adafruit_onewire.bus import OneWireBus
from adafruit_ds18x20 import DS18X20

Once this is done you can define your adafruit_onewire.bus.OneWireBus object and define your sensor object

ow_bus = OneWireBus(board.D5)
ds18 = DS18X20(ow_bus, ow_bus.scan()[0])

Now you have access to the temperature attribute

temperature = ds18.temperature
read_temperature()[source]

Read the temperature. No polling of the conversion busy bit (assumes that the conversion has completed).

resolution

The programmable resolution. 9, 10, 11, or 12 bits.

start_temperature_read()[source]

Start asynchronous conversion, returns immediately. Returns maximum conversion delay [seconds] based on resolution.

temperature

The temperature in degrees Celsius.

Indices and tables