Skip to content

Commit 91d339d

Browse files
committed
Add DS2484 hosting DS18b20 temp sensor
1 parent b2901a6 commit 91d339d

File tree

4 files changed

+195
-4
lines changed

4 files changed

+195
-4
lines changed

platformio.ini

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ lib_deps =
2828
adafruit/Adafruit BMP280 Library
2929
adafruit/Adafruit BMP3XX Library
3030
adafruit/Adafruit DPS310
31+
adafruit/Adafruit DS248x
3132
adafruit/Adafruit INA219
3233
adafruit/Adafruit HTS221
3334
adafruit/Adafruit HTU21DF Library
@@ -107,9 +108,20 @@ board = rpipicow
107108
framework = arduino
108109
board_build.core = earlephilhower
109110
board_build.filesystem_size = 0.5m
110-
build_flags = -DUSE_TINYUSB
111+
build_flags =
112+
-DUSE_TINYUSB
113+
; -I"c:/Users/tyeth/.platformio/packages/framework-arduinopico/libraries/Adafruit_TinyUSB_Arduino"
114+
; -I"c:/Users/tyeth/.platformio/packages/framework-arduinopico/libraries/Adafruit_TinyUSB_Arduino/src"
115+
; -I"c:/Users/tyeth/.platformio/packages/framework-arduinopico/libraries/Adafruit_TinyUSB_Arduino/src/arduino"
111116
; Once https://github.com/platformio/platformio-core > 6.1.11 these can be removed
112117
lib_ignore = WiFiNINA, Adafruit Zero DMA Library
118+
;, Adafruit TinyUSB Library
119+
; lib_extra_dirs = /Users/tyeth/Projects/arduino-pico/libraries
120+
; src_filter =
121+
; +<*>
122+
; -<c:/Users/tyeth/.platformio/packages/framework-arduinopico/libraries/Adafruit_TinyUSB_Arduino/>
123+
; -<c:/Users/tyeth/.platformio/packages/framework-arduinopico/libraries/Adafruit_TinyUSB_Arduino/src>
124+
; -<c:/Users/tyeth/.platformio/packages/framework-arduinopico/libraries/Adafruit_TinyUSB_Arduino/src/arduino>
113125
lib_compat_mode = soft ; can be strict once pio detects SleepyDog on RP2040
114126

115127
; ESP32-x Boards ;
@@ -279,7 +291,7 @@ extends=common:esp8266
279291
board = huzzah
280292
build_flags = -DARDUINO_ESP8266_ADAFRUIT_HUZZAH
281293
board_build.filesystem = littlefs
282-
upload_port = /dev/cu.SLAB_USBtoUART
294+
; upload_port = /dev/cu.SLAB_USBtoUART
283295

284296
; SAMD51 Boards ;
285297

@@ -329,7 +341,7 @@ upload_protocol = cmsis-dap
329341
; board can use both Arduino cores -- we select Arduino-Pico here
330342
board_build.core = earlephilhower
331343
board_build.filesystem_size = 0.5m
332-
debug_init_break = tbreak runNetFSM
344+
debug_init_break = tbreak parseSecrets
333345
build_flags =
334346
-DDEBUG
335347
-DWIFICC=CYW43_COUNTRY_UK
@@ -355,4 +367,3 @@ build_flags =
355367
; ; No USB stack
356368
; build_flags = -DPIO_FRAMEWORK_ARDUINO_NO_USB
357369
; -DPIO_FRAMEWORK_ARDUINO_ENABLE_IPV6
358-

src/components/i2c/WipperSnapper_I2C.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,17 @@ bool WipperSnapper_Component_I2C::initI2CDevice(
296296
_dps310->configureDriver(msgDeviceInitReq);
297297
drivers.push_back(_dps310);
298298
WS_DEBUG_PRINTLN("DPS310 Initialized Successfully!");
299+
} else if (strcmp("ds2484", msgDeviceInitReq->i2c_device_name) == 0) {
300+
_ds2484 = new WipperSnapper_I2C_Driver_DS2484(this->_i2c, i2cAddress);
301+
if (!_ds2484->begin()) {
302+
WS_DEBUG_PRINTLN("ERROR: Failed to initialize DS2484!");
303+
_busStatusResponse =
304+
wippersnapper_i2c_v1_BusResponse_BUS_RESPONSE_DEVICE_INIT_FAIL;
305+
return false;
306+
}
307+
_ds2484->configureDriver(msgDeviceInitReq);
308+
drivers.push_back(_ds2484);
309+
WS_DEBUG_PRINTLN("DS2484 Initialized Successfully!");
299310
} else if (strcmp("ens160", msgDeviceInitReq->i2c_device_name) == 0) {
300311
_ens160 = new WipperSnapper_I2C_Driver_ENS160(this->_i2c, i2cAddress);
301312
if (!_ens160->begin()) {

src/components/i2c/WipperSnapper_I2C.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include "drivers/WipperSnapper_I2C_Driver_BMP280.h"
2929
#include "drivers/WipperSnapper_I2C_Driver_BMP3XX.h"
3030
#include "drivers/WipperSnapper_I2C_Driver_DPS310.h"
31+
#include "drivers/WipperSnapper_I2C_Driver_DS2484.h"
3132
#include "drivers/WipperSnapper_I2C_Driver_ENS160.h"
3233
#include "drivers/WipperSnapper_I2C_Driver_HTS221.h"
3334
#include "drivers/WipperSnapper_I2C_Driver_HTU21D.h"
@@ -132,6 +133,7 @@ class WipperSnapper_Component_I2C {
132133
// Sensor driver objects
133134
WipperSnapper_I2C_Driver_AHTX0 *_ahtx0 = nullptr;
134135
WipperSnapper_I2C_Driver_DPS310 *_dps310 = nullptr;
136+
WipperSnapper_I2C_Driver_DS2484 *_ds2484 = nullptr;
135137
WipperSnapper_I2C_Driver_ENS160 *_ens160 = nullptr;
136138
WipperSnapper_I2C_Driver_SCD30 *_scd30 = nullptr;
137139
WipperSnapper_I2C_Driver_BH1750 *_bh1750 = nullptr;
Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
/*!
2+
* @file WipperSnapper_I2C_Driver_DS2484.h
3+
*
4+
* Device driver the DS2484 I2C OneWire converter (hosting a DS18b20).
5+
*
6+
* Adafruit invests time and resources providing this open source code,
7+
* please support Adafruit and open-source hardware by purchasing
8+
* products from Adafruit!
9+
*
10+
* Copyright (c) Tyeth Gundry 2024 for Adafruit Industries.
11+
*
12+
* MIT license, all text here must be included in any redistribution.
13+
*
14+
*/
15+
16+
#ifndef WipperSnapper_I2C_Driver_DS2484_H
17+
#define WipperSnapper_I2C_Driver_DS2484_H
18+
19+
#define DS18B20_FAMILY_CODE 0x28
20+
#define DS18B20_CMD_CONVERT_T 0x44
21+
#define DS18B20_CMD_MATCH_ROM 0x55
22+
#define DS18B20_CMD_READ_SCRATCHPAD 0xBE
23+
24+
#include "WipperSnapper_I2C_Driver.h"
25+
#include <Adafruit_DS248x.h>
26+
27+
/**************************************************************************/
28+
/*!
29+
@brief Class that provides a sensor driver for the DS2484 I2C OneWire
30+
converter hosting a DS18b20 temperature sensor.
31+
*/
32+
/**************************************************************************/
33+
class WipperSnapper_I2C_Driver_DS2484 : public WipperSnapper_I2C_Driver {
34+
35+
public:
36+
/*******************************************************************************/
37+
/*!
38+
@brief Constructor for a DS2484 sensor.
39+
@param i2c
40+
The I2C interface.
41+
@param sensorAddress
42+
7-bit device address.
43+
*/
44+
/*******************************************************************************/
45+
WipperSnapper_I2C_Driver_DS2484(TwoWire *i2c,
46+
uint16_t sensorAddress = _DS2484_ADDRESS)
47+
: WipperSnapper_I2C_Driver(i2c, sensorAddress) {
48+
_i2c = i2c;
49+
_sensorAddress = sensorAddress;
50+
}
51+
52+
/*******************************************************************************/
53+
/*!
54+
@brief Destructor for an DS2484 sensor.
55+
*/
56+
/*******************************************************************************/
57+
~WipperSnapper_I2C_Driver_DS2484() { delete _ds2484; }
58+
59+
/*******************************************************************************/
60+
/*!
61+
@brief Initializes the DS2484 sensor and begins I2C.
62+
@returns True if initialized successfully, False otherwise.
63+
*/
64+
/*******************************************************************************/
65+
bool begin() {
66+
// initialize DS2484
67+
_ds2484 = new Adafruit_DS248x();
68+
if (!_ds2484->begin(_i2c, (uint8_t)_sensorAddress)) {
69+
WS_DEBUG_PRINTLN("Could not find DS2484");
70+
return false;
71+
}
72+
73+
// check bus is okay
74+
if (!_ds2484->OneWireReset()) {
75+
WS_DEBUG_PRINTLN("Failed to do a OneWire bus reset");
76+
if (_ds2484->shortDetected()) {
77+
WS_DEBUG_PRINTLN("\tShort detected");
78+
}
79+
if (!_ds2484->presencePulseDetected()) {
80+
WS_DEBUG_PRINTLN("\tNo presense pulse");
81+
}
82+
return false;
83+
}
84+
85+
// locate first DS18B20
86+
bool found_device = false;
87+
_ds2484->OneWireReset();
88+
while (!found_device && _ds2484->OneWireSearch(_rom)) {
89+
if (_rom[0] == DS18B20_FAMILY_CODE) {
90+
found_device = true;
91+
}
92+
}
93+
94+
if (!found_device) {
95+
WS_DEBUG_PRINTLN("Could not find DS18B20 attached to DS2484");
96+
return false;
97+
}
98+
99+
WS_DEBUG_PRINTLN("DS2484 found");
100+
return true;
101+
}
102+
103+
/*******************************************************************************/
104+
/*!
105+
@brief Processes a temperature event.
106+
@param tempEvent
107+
Pointer to an Adafruit_Sensor event.
108+
@returns True if the temperature was obtained successfully, False
109+
otherwise.
110+
*/
111+
bool processTemperatureEvent(sensors_event_t *tempEvent) {
112+
if (!_ds2484->OneWireReset()) {
113+
return false;
114+
}
115+
if (!_ds2484->presencePulseDetected()) {
116+
tempEvent->temperature = NAN;
117+
return true;
118+
}
119+
120+
_ds2484->OneWireWriteByte(DS18B20_CMD_MATCH_ROM); // Match ROM command
121+
for (int i = 0; i < 8; i++) {
122+
_ds2484->OneWireWriteByte(_rom[i]);
123+
}
124+
125+
// Start temperature conversion
126+
_ds2484->OneWireWriteByte(DS18B20_CMD_CONVERT_T); // Convert T command
127+
delay(750); // Wait for conversion (750ms for maximum precision)
128+
129+
// Read scratchpad
130+
_ds2484->OneWireReset();
131+
_ds2484->OneWireWriteByte(DS18B20_CMD_MATCH_ROM); // Match ROM command
132+
for (int i = 0; i < 8; i++) {
133+
_ds2484->OneWireWriteByte(_rom[i]);
134+
}
135+
_ds2484->OneWireWriteByte(
136+
DS18B20_CMD_READ_SCRATCHPAD); // Read Scratchpad command
137+
138+
uint8_t data[9];
139+
for (int i = 0; i < 9; i++) {
140+
_ds2484->OneWireReadByte(&data[i]);
141+
}
142+
143+
// Calculate temperature
144+
int16_t raw = (data[1] << 8) | data[0];
145+
tempEvent->temperature = (float)raw / 16.0;
146+
return true;
147+
}
148+
149+
/*******************************************************************************/
150+
/*!
151+
@brief Gets the DS2484's current temperature.
152+
@param tempEvent
153+
Pointer to an Adafruit_Sensor event.
154+
@returns True if the temperature was obtained successfully, False
155+
otherwise.
156+
*/
157+
/*******************************************************************************/
158+
bool getEventAmbientTemp(sensors_event_t *tempEvent) {
159+
return processTemperatureEvent(tempEvent);
160+
}
161+
162+
protected:
163+
Adafruit_DS248x *_ds2484; ///< DS2484 driver object
164+
uint8_t _rom[8]; ///< DS18B20 ROM
165+
};
166+
167+
#endif // WipperSnapper_I2C_Driver_DS2484

0 commit comments

Comments
 (0)