Skip to content

Commit 64b85cd

Browse files
authored
Merge pull request #23 from caternuson/new_example
Add multi sensor example
2 parents de7a46c + ab0bc47 commit 64b85cd

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,5 @@ repos:
3030
name: pylint (examples code)
3131
description: Run pylint rules on "examples/*.py" files
3232
entry: /usr/bin/env bash -c
33-
args: ['([[ ! -d "examples" ]] || for example in $(find . -path "./examples/*.py"); do pylint --disable=missing-docstring,invalid-name $example; done)']
33+
args: ['([[ ! -d "examples" ]] || for example in $(find . -path "./examples/*.py"); do pylint --disable=missing-docstring,invalid-name,consider-using-f-string $example; done)']
3434
language: system

examples/ds18x20_multi.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# SPDX-FileCopyrightText: 2020 ladyada for Adafruit Industries
2+
# SPDX-License-Identifier: MIT
3+
4+
# Example of specifying multiple sensors using explicit ROM codes.
5+
# These ROM codes need to be determined ahead of time. Use `ow_bus.scan()`.
6+
#
7+
# (1) Connect one sensor at a time
8+
# (2) Use `ow_bus.scan()[0].rom` to determine ROM code
9+
# (3) Use ROM code to specify sensors (see this example)
10+
11+
import time
12+
import board
13+
from adafruit_onewire.bus import OneWireBus, OneWireAddress
14+
from adafruit_ds18x20 import DS18X20
15+
16+
# !!!! REPLACE THESE WITH ROM CODES FOR YOUR SENSORS !!!!
17+
ROM1 = b"(\xbb\xfcv\x08\x00\x00\xe2"
18+
ROM2 = b"(\xb3t\xd3\x08\x00\x00\x9e"
19+
ROM3 = b"(8`\xd4\x08\x00\x00i"
20+
# !!!! REPLACE THESE WITH ROM CODES FOR YOUR SENSORS !!!!
21+
22+
# Initialize one-wire bus on board pin D5.
23+
ow_bus = OneWireBus(board.D5)
24+
25+
# Uncomment this to get a listing of currently attached ROMs
26+
# for device in ow_bus.scan():
27+
# print(device.rom)
28+
29+
# Use pre-determined ROM codes for each sensors
30+
temp1 = DS18X20(ow_bus, OneWireAddress(ROM1))
31+
temp2 = DS18X20(ow_bus, OneWireAddress(ROM2))
32+
temp3 = DS18X20(ow_bus, OneWireAddress(ROM3))
33+
34+
# Main loop to print the temperatures every second.
35+
while True:
36+
print("Temperature 1 = {}".format(temp1.temperature))
37+
print("Temperature 2 = {}".format(temp2.temperature))
38+
print("Temperature 3 = {}".format(temp3.temperature))
39+
print("-" * 20)
40+
time.sleep(1)

0 commit comments

Comments
 (0)