# 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:

```
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:

```
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:

```
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:

```
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:

```
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


## Featured Products

### DS18B20 Digital temperature sensor + extras

[DS18B20 Digital temperature sensor + extras](https://www.adafruit.com/product/374)
These 1-wire digital temperature sensors are fairly precise (±0.5°C over much of the range) and can give up to 12 bits of precision from the onboard digital-to-analog converter. They work great with any microcontroller using a single digital pin, and you can even connect multiple...

In Stock
[Buy Now](https://www.adafruit.com/product/374)
[Related Guides to the Product](https://learn.adafruit.com/products/374/guides)
### Waterproof 1-Wire DS18B20 Digital temperature sensor

[Waterproof 1-Wire DS18B20 Digital temperature sensor](https://www.adafruit.com/product/381)
This is a pre-wired and waterproofed (with heat shrink) version of a 1 Wire DS18B20 sensor. Handy for when you need to measure something far away, or in wet conditions. While the sensor is good up to 125°C the cable is jacketed in PVC so we suggest keeping it under 100°C. Because they...

In Stock
[Buy Now](https://www.adafruit.com/product/381)
[Related Guides to the Product](https://learn.adafruit.com/products/381/guides)
### High Temp Waterproof DS18B20 Digital temperature sensor + extras

[High Temp Waterproof DS18B20 Digital temperature sensor + extras](https://www.adafruit.com/product/642)
This is a pre-wired and waterproofed version of the DS18B20 sensor made with a PTFE wire cable. Handy for when you need to measure something far away, or in wet conditions. This sensor is a little more expensive than [the other waterproof version...](http://www.adafruit.com/products/381)

In Stock
[Buy Now](https://www.adafruit.com/product/642)
[Related Guides to the Product](https://learn.adafruit.com/products/642/guides)
### Adafruit Feather M0 Basic Proto - ATSAMD21 Cortex M0

[Adafruit Feather M0 Basic Proto - ATSAMD21 Cortex M0](https://www.adafruit.com/product/2772)
Feather is the new development board from Adafruit, and like its namesake it is thin, light, and lets you fly! We designed Feather to be a new standard for portable microcontroller cores.

This is the&nbsp; **Feather M0 Basic Proto** ,&nbsp;it has a bunch of prototyping space...

In Stock
[Buy Now](https://www.adafruit.com/product/2772)
[Related Guides to the Product](https://learn.adafruit.com/products/2772/guides)
### Adafruit Feather M0 Express

[Adafruit Feather M0 Express](https://www.adafruit.com/product/3403)
At the Feather M0's heart is an ATSAMD21G18 ARM Cortex M0+ processor, clocked at 48 MHz and at 3.3V logic, the same one used in the new&nbsp;[Arduino Zero](https://www.adafruit.com/products/2843). This chip has a whopping 256K of FLASH (8x more than the Atmega328 or 32u4) and...

Out of Stock
[Buy Now](https://www.adafruit.com/product/3403)
[Related Guides to the Product](https://learn.adafruit.com/products/3403/guides)
### Adafruit METRO M0 Express - designed for CircuitPython

[Adafruit METRO M0 Express - designed for CircuitPython](https://www.adafruit.com/product/3505)
Metro is our series of microcontroller boards for use with the Arduino IDE. This new **Metro M0 Express** board looks a whole lot like our&nbsp;[original Metro 328](https://www.adafruit.com/product/2488), but with a huge upgrade. Instead of the ATmega328, this Metro...

In Stock
[Buy Now](https://www.adafruit.com/product/3505)
[Related Guides to the Product](https://learn.adafruit.com/products/3505/guides)
### Circuit Playground Express

[Circuit Playground Express](https://www.adafruit.com/product/3333)
 **Circuit Playground Express** is the next step towards a perfect introduction to electronics and programming. We've taken the original Circuit Playground Classic and made it even better! Not only did we pack even more sensors in, we also made it even easier to...

In Stock
[Buy Now](https://www.adafruit.com/product/3333)
[Related Guides to the Product](https://learn.adafruit.com/products/3333/guides)
### Adafruit GEMMA M0 - Miniature wearable electronic platform

[Adafruit GEMMA M0 - Miniature wearable electronic platform](https://www.adafruit.com/product/3501)
The **Adafruit Gemma M0** is a super small microcontroller board, with just enough built-in to create many simple projects. It may look small and cute: round, about the size of a quarter, with friendly alligator-clip sew pads. But do not be fooled! The Gemma M0 is incredibly...

In Stock
[Buy Now](https://www.adafruit.com/product/3501)
[Related Guides to the Product](https://learn.adafruit.com/products/3501/guides)

## Related Guides

- [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)
- [The Foul Fowl -- Keystroke Injection Attack Tool with Gemma M0](https://learn.adafruit.com/the-foul-fowl-keyboard-injection-payload-gemma-m0.md)
- [Princess Peach LED Crown](https://learn.adafruit.com/bowsette.md)
- [AdaBox 006](https://learn.adafruit.com/adabox006.md)
- [Make It Talk](https://learn.adafruit.com/make-it-talk.md)
- [Animatronic Hand](https://learn.adafruit.com/animatronic-hands.md)
- [CircuitPython Hardware: PCA9685 DC Motor & Stepper Driver](https://learn.adafruit.com/micropython-hardware-pca9685-dc-motor-and-stepper-driver.md)
- [Robotic Creatures ](https://learn.adafruit.com/robotic-creatures.md)
- [Make a Snow Globe with Circuit Playground Express & MakeCode](https://learn.adafruit.com/make-a-snowglobe-with-circuit-playground-makecode.md)
- [Trinket / Gemma Blinky Eyes](https://learn.adafruit.com/trinket-gemma-blinky-eyes.md)
- [Glowing Slime Lunchbox](https://learn.adafruit.com/glowing-slime-lunchbox.md)
- [Trellis Feather DSP-G1 Synthesizer](https://learn.adafruit.com/feather-trellis-dsp-g1-synthesizer.md)
- [Archimedes' Boat](https://learn.adafruit.com/archimedes-boat.md)
- [Pushrod Garage](https://learn.adafruit.com/pushrod-garage.md)
- [Building CircuitPython](https://learn.adafruit.com/building-circuitpython.md)
- [NeoPixie Dust Bag](https://learn.adafruit.com/neopixel-pixie-dust-bag.md)
