# Using DS18B20 Temperature Sensor with CircuitPython

## Overview

![](https://cdn-learn.adafruit.com/assets/assets/000/049/953/medium800/temperature___humidity_IMG_6774.jpg?1515461764)

One-wire temperature sensors like the DS18B20 are devices that can measure temperature with a minimal amount of hardware and wiring.&nbsp; These sensors use a digital protocol to send accurate temperature readings directly to your development board without the need of an analog to digital converter or other extra hardware.&nbsp; You can get one-wire sensors in different form factors like waterproof and high temperature probes--these are perfect for sensing temperature in many different projects and applications.&nbsp; And since these sensors use the one-wire protocol you can even have multiple of them connected to the same pin and read all their temperature values independently.&nbsp; With just a few connections and some CircuitPython code you'll be sensing temperature in no time!

This guide explores how to connect a DS18B20 one-wire temperature sensor to a CircuitPython board and read its temperature from Python code using a few simple CircuitPython modules.

# Using DS18B20 Temperature Sensor with CircuitPython

## Hardware

# Hardware

To follow this guide you'll need the following parts:

- **DS18B20 Dallas one-wire temperature sensor. &nbsp;** You can use a simple standalone [DS18B20 sensor](https://www.adafruit.com/product/374), a [waterproof DS18B20 probe sensor](https://www.adafruit.com/product/381), or a [high temperature DS18B20 probe sensor](https://www.adafruit.com/product/642).
- **4.7 KΩ Resistor.** &nbsp; One-wire devices need a pull-up resistor connected to their signal line to be properly read by your board.&nbsp; Luckily all the DS18B20 sensors sold by Adafruit include the necessary pull-up resistor!
- **A board running CircuitPython.** &nbsp; You'll need a board capable of running CircuitPython like the&nbsp;[Feather M0 Express, Trinket M0 etc!](https://www.adafruit.com/category/957).
- [**Breadboard**](https://www.adafruit.com/product/65)**&nbsp;and&nbsp;[jumper wires](https://www.adafruit.com/product/153).**&nbsp; You'll need these parts to connect components to your development board.

# Wiring

Connect your DS18B20 sensor to a digital input on your development board.&nbsp; You'll also need to add the 4.7 KΩ pull-up resistor to the signal line to ensure the board can read the sensor.&nbsp; Here's an example of wiring a standalone DS18B20 to a Feather M0 board:

![](https://cdn-learn.adafruit.com/assets/assets/000/049/947/medium800/temperature___humidity_m0_ds18b20_to220_bb.png?1515457853)

- **Left-most leg of the sensor** (with the flat part facing you) to **board ground**.
- **Middle leg of the sensor** to **board D5**.
- **Right-most leg of the sensor** to **board 3.3V.**
- **4.7 KΩ resistor** connected to both the **middle leg** (data) and **right-most leg** (3.3V).

Or if you're using a waterproof or high-temperature probe here's an example of the wiring to a Feather M0 board:

![](https://cdn-learn.adafruit.com/assets/assets/000/049/948/medium800/temperature___humidity_m0_ds18b20_probe_bb.png?1515458006)

- **Black wire** (or solid white on high-temperature probe)&nbsp;to **board ground**.
- **Orange wire** (or white with blue stripe on high-temperature probe)&nbsp;to **board D5**.
- **Red wire** (or white with orange stripe on high-temperature probe)&nbsp;to **board 3.3V.**
- **4.7KΩ resistor** connected to both the data line on one side ( **D5** or whatever pin) and power line ( **3.3V** ) on the other side

# Using DS18B20 Temperature Sensor with CircuitPython

## CircuitPython

To use the DS18B20 you'll need to install both the&nbsp;[Adafruit CircuitPython OneWire](https://github.com/adafruit/Adafruit_CircuitPython_OneWire)&nbsp;and&nbsp;[Adafruit CircuitPython DS18X20](https://github.com/adafruit/Adafruit_CircuitPython_DS18X20)&nbsp;modules on your CircuitPython board.

First make sure you are running the&nbsp;[latest version of Adafruit CircuitPython](../../../../welcome-to-circuitpython/installing-circuitpython)&nbsp;for your board.

Next you'll need to install the necessary libraries&nbsp;to use the hardware--carefully follow the steps to find and install these libraries from&nbsp;[Adafruit's CircuitPython library bundle](https://github.com/adafruit/Adafruit_CircuitPython_Bundle).&nbsp; Our introduction guide has&nbsp;[a great page on how to install the library bundle](../../../../welcome-to-circuitpython/circuitpython-libraries)&nbsp;for both express and non-express boards.

Remember for non-express boards like the Trinket M0 or QT Py, you'll need to manually install the necessary folders and files from the bundle:

- **adafruit\_onewire**
- **adafruit\_ds18x20.mpy**

You can also download the&nbsp; **adafruit\_onewire** &nbsp;folder from&nbsp;[its releases page on Github](https://github.com/adafruit/Adafruit_CircuitPython_OneWire/releases), and the **adafruit\_ds18x20.mpy** from&nbsp;[its releases page on Github](https://github.com/adafruit/Adafruit_CircuitPython_DS18X20/releases).

Before continuing make sure your board's lib folder or root filesystem has the&nbsp; **adafruit\_onewire,&nbsp;** and **&nbsp;adafruit\_ds18x20.mpy**** &nbsp; **files and folders** &nbsp;**copied over.

Next&nbsp;[connect to the board's serial REPL&nbsp;](../../../../welcome-to-circuitpython/the-repl)so you are at the CircuitPython&nbsp; **\>\>\>** &nbsp;prompt.

# Usage

To demonstrate the usage of the sensor we'll initialize it and read the temperature from the Python REPL.

First run the following code to import the necessary modules and initialize the one-wire bus connection with the sensor:

```auto
import board
from adafruit_onewire.bus import OneWireBus
ow_bus = OneWireBus(board.D5)
```

Be sure to change **board.D5** to the appropriate pin you're using if you've connected your sensor to a different digital input than shown on the wiring diagram of this guide!

Before you use the temperature sensor you can scan the one-wire bus to see which devices are available.&nbsp; This is handy for example if you have multiple sensors connected to your board and want to choose the right sensor based on its ID.&nbsp; Run this code to print out the ROM (or unique ID) of each sensor on the bus:

```auto
devices = ow_bus.scan()
for device in devices:
    print("ROM = {} \tFamily = 0x{:02x}".format([hex(i) for i in device.rom], device.family_code))
```

![](https://cdn-learn.adafruit.com/assets/assets/000/049/957/medium800/temperature___humidity_multids.png?1515546179 Two DS18B20's hooked up in parallel)

If you only have one sensor is connected you should see one result printed with the ROM value (a list of hex values that has the unique identifier for that temperature sensor) and family (a byte that indicates the type of device).&nbsp;

If you multiple sensors connected you should see a line printed for each sensor.&nbsp; However if you don't see any devices printed double check your wiring and that the 4.7 KΩ pull-up resistor is connected as shown on the wiring diagrams!

Now you can import the DS18x20 module and use it to read the temperature from a sensor.&nbsp; Run the following code to import the module and create an instance of the DS18x20 sensor from the first device found on the one-wire bus:

```auto
import adafruit_ds18x20
ds18b20 = adafruit_ds18x20.DS18X20(ow_bus, devices[0])
```

Notice the first parameter to the DS18X20 initializer is the one-wire bus instance, and the second parameter is a device instance (found from calling **scan()** on the bus previously).&nbsp; If you have multiple sensors connected you can pick the appropriate one by changing the device instance to another value, like **devices[1]** would be the second sensor found (the 0 or 1 value in brackets is the index into the device list).

At this point you're ready to read the sensor's **temperature** property.&nbsp; This will return a value in degrees Celsius:

```auto
print('Temperature: {0:0.3f} °C'.format(ds18b20.temperature))
```

![](https://cdn-learn.adafruit.com/assets/assets/000/049/951/medium800/temperature___humidity_Screen_Shot_2018-01-08_at_5.14.10_PM.png?1515460468)

Finally there's one other property you can interact with, **resolution**.&nbsp; You can read and write this property to get or set the resolution in bits of the temperature sensor.&nbsp; Only a value of 9, 10, 11, or 12 is supported (the default is 12-bits of resolution).&nbsp; For example to change to a lower 9-bit resolution:

```auto
ds18b20.resolution = 9
print('Resolution: {0} bits'.format(ds18b20.resolution))
print('Temperature: {0:0.3f} °C'.format(ds18b20.temperature))
```

![](https://cdn-learn.adafruit.com/assets/assets/000/049/952/medium800/temperature___humidity_Screen_Shot_2018-01-08_at_5.17.27_PM.png?1515460662)

That's all there is to using the DS18B20 with CircuitPython!

Here's a complete example that will print the temperature every second from the first sensor found on the bus.&nbsp; Save this as **main.py** on your board and open the serial REPL to see the output:

https://github.com/adafruit/Adafruit_CircuitPython_DS18X20/blob/main/examples/ds18x20_simpletest.py

you can hold the sensor in your fingers to heat it up, watch the values go up!

![](https://cdn-learn.adafruit.com/assets/assets/000/049/958/medium800/temperature___humidity_temp.png?1515546407)

# Using DS18B20 Temperature Sensor with CircuitPython

## Python Docs


## Related Guides

- [Sewable NeoPixels](https://learn.adafruit.com/flora-rgb-smart-pixels.md)
- [Adafruit Feather M0 Express](https://learn.adafruit.com/adafruit-feather-m0-express-designed-for-circuit-python-circuitpython.md)
- [Adafruit Metro M0 Express](https://learn.adafruit.com/adafruit-metro-m0-express.md)
- [Adafruit Trinket M0](https://learn.adafruit.com/adafruit-trinket-m0-circuitpython-arduino.md)
- [Adafruit Circuit Playground Express](https://learn.adafruit.com/adafruit-circuit-playground-express.md)
- [Meeting Time Keeper Stick](https://learn.adafruit.com/meeting-time-keeper-stick-with-cpx.md)
- [Circuit Playground Gizmo Ornaments](https://learn.adafruit.com/display-ornaments-with-circuit-playground.md)
- [LED NeoPixel Corset with Circuit Playground Express and MakeCode](https://learn.adafruit.com/led-corset-with-circuit-playground-and-makecode.md)
- [LED Reactive Light-Up Hockey Puck in MakeCode](https://learn.adafruit.com/led-hockey-puck.md)
- [Spinning Logo](https://learn.adafruit.com/spinning-logo.md)
- [Combo Dial Safe with Circuit Playground Express](https://learn.adafruit.com/combo-dial-safe-with-circuit-playground-express.md)
- [Using EduBlocks with Circuit Playground Express](https://learn.adafruit.com/using-edublocks-with-circuit-playground-express.md)
- [Capacitive Touch Unicorn Horn](https://learn.adafruit.com/capacitive-touch-unicorn-horn.md)
- [USB Foot Switch Controller in CircuitPython](https://learn.adafruit.com/usb-foot-switch-circuit-python.md)
- [JOY Controller for Adafruit Feather](https://learn.adafruit.com/joy-controller-feather.md)
- [Phantom Mouse Jiggler](https://learn.adafruit.com/phantom-mouse-jiggler.md)
- [Make a Snow Globe with Circuit Playground Express & MakeCode](https://learn.adafruit.com/make-a-snowglobe-with-circuit-playground-makecode.md)
- [SpaceX Helmet](https://learn.adafruit.com/spacex-helmet.md)
- [NeoPixels with MakeCode](https://learn.adafruit.com/neopixels-with-makecode.md)
- [Adafruit DS2484 I2C to 1-Wire Bus Adapter Breakout](https://learn.adafruit.com/adafruit-ds2484-i2c-to-1-wire-bus-adapter-breakout.md)
