It's easy to use the Chainable DS18B20 Extender Breakout with CircuitPython and the Adafruit_CircuitPython_DS18X20 module. This module allows you to easily write Python code for reading the DS18B20 1-wire sensor.
You'll need a DS18B20 sensor to use this example with the breakout:
CircuitPython Microcontroller Wiring
First wire up the DS18B20 sensor to the breakout exactly as follows. Then, connect the breakout to your microcontroller with a JST PH cable. The following is the breakout wired to a Feather RP2040 with a JST PH cable.
- Breakout GND to DS18B20 ground (blue wire)
- Breakout Signal to DS18B20 1-wire output (yellow wire)
- Breakout V+ to DS18B20 VIN (red wire)
-
Board 3V to breakout JST PH VIN (red wire)
-
Board GND to breakout JST PH GND (black wire)
- Board pin 5 to breakout JST PH Signal (white wire)
CircuitPython Usage
To use with CircuitPython, you need to first install the Adafruit_CircuitPython_DS18X20 library, and its dependencies, into the lib folder on your CIRCUITPY drive. Then you need to update code.py with the example script.
Thankfully, we can do this in one go. In the example below, click the Download Project Bundle button below to download the necessary libraries and the code.py file in a zip file. Extract the contents of the zip file, and copy the entire lib folder and the code.py file to your CIRCUITPY drive.
Your CIRCUITPY/lib folder should contain the following folders and file:
- adafruit_bus_device/
- adafruit_onewire/
- adafruit_ds18x20.mpy
Example Code
Once everything is saved to the CIRCUITPY drive, connect to the serial console to see the data printed out!
# 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)
In the code, the 1-wire bus is instantiated on pin 5. A scan is performed for the sensor and once it is found, the loop begins. In the loop, the temperature reading is printed to the serial console every second.
Multiple Sensors
What makes this breakout special though is right in its name: you can chain multiple DS18B20 sensors together! To do this, you'll wire up multiple breakouts with DS18B20 sensors to your microcontroller. Then, you'll scan for each sensors' ROM address. After updating the code with their addresses, you can read multiple sensors at once.
Each breakout will be wired to a DS18B20 sensor with its terminal block:
- Breakout GND to DS18B20 ground (blue wire)
- Breakout Signal to DS18B20 1-wire output (yellow wire)
- Breakout V+ to DS18B20 VIN (red wire)
The breakouts will be daisy chained together via their JST PH cables. The final breakout in the chain will be connected to the Feather:
-
Board 3V to breakout JST PH VIN (red wire)
-
Board GND to breakout JST PH GND (black wire)
- Board pin 5 to breakout JST PH Signal (white wire)
You can determine the ROM address for each sensor using this code snippet:
import board from adafruit_onewire.bus import OneWireBus, OneWireAddress ow_bus = OneWireBus(board.D5) for device in ow_bus.scan(): print(device.rom)
The ROM addresses will print to the serial console:
You need to update the code with the ROM addresses for your sensors!
After noting the ROM addresses, you can update the multiple DS18B20 example code with your sensors' addresses. Once everything is saved to the CIRCUITPY drive, connect to the serial console to see the data printed out!
# SPDX-FileCopyrightText: 2020 ladyada for Adafruit Industries # SPDX-License-Identifier: MIT # Example of specifying multiple sensors using explicit ROM codes. # These ROM codes need to be determined ahead of time. Use `ow_bus.scan()`. # # (1) Connect one sensor at a time # (2) Use `ow_bus.scan()[0].rom` to determine ROM code # (3) Use ROM code to specify sensors (see this example) import time import board from adafruit_onewire.bus import OneWireBus, OneWireAddress from adafruit_ds18x20 import DS18X20 # !!!! REPLACE THESE WITH ROM CODES FOR YOUR SENSORS !!!! ROM1 = b"(\xbb\xfcv\x08\x00\x00\xe2" ROM2 = b"(\xb3t\xd3\x08\x00\x00\x9e" ROM3 = b"(8`\xd4\x08\x00\x00i" # !!!! REPLACE THESE WITH ROM CODES FOR YOUR SENSORS !!!! # Initialize one-wire bus on board pin D5. ow_bus = OneWireBus(board.D5) # Uncomment this to get a listing of currently attached ROMs # for device in ow_bus.scan(): # print(device.rom) # Use pre-determined ROM codes for each sensors temp1 = DS18X20(ow_bus, OneWireAddress(ROM1)) temp2 = DS18X20(ow_bus, OneWireAddress(ROM2)) temp3 = DS18X20(ow_bus, OneWireAddress(ROM3)) # Main loop to print the temperatures every second. while True: print("Temperature 1 = {}".format(temp1.temperature)) print("Temperature 2 = {}".format(temp2.temperature)) print("Temperature 3 = {}".format(temp3.temperature)) print("-" * 20) time.sleep(1)
Text editor powered by tinymce.