Skip to content

Add multi sensor example #23

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Sep 21, 2021
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions examples/ds18x20_multi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# 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)

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