Skip to content

Commit 042597a

Browse files
authored
Merge pull request #603 from adafruit/add-DS2484
Add DS2484 hosting DS18b20 temp sensor
2 parents aee903f + 868f17f commit 042597a

File tree

5 files changed

+191
-1
lines changed

5 files changed

+191
-1
lines changed

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@ paragraph=Arduino application for Adafruit.io WipperSnapper
77
category=Communication
88
url=https://github.com/adafruit/Adafruit_Wippersnapper_Arduino
99
architectures=*
10-
depends=Adafruit NeoPixel, Adafruit SPIFlash, ArduinoJson, Adafruit DotStar, Adafruit INA219, Adafruit LTR329 and LTR303, Adafruit LTR390 Library, Adafruit MCP3421, Adafruit NAU7802 Library, Adafruit SleepyDog Library, Adafruit TMP117, Adafruit TinyUSB Library, Adafruit AHTX0, Adafruit BME280 Library, Adafruit BMP280 Library, Adafruit BMP3XX Library, Adafruit DPS310, Adafruit SCD30, Adafruit SGP30 Sensor, Adafruit SGP40 Sensor, Sensirion I2C SCD4x, Sensirion I2C SEN5X, arduino-sht, Adafruit Si7021 Library, Adafruit MQTT Library, Adafruit MS8607, Adafruit MCP9808 Library, Adafruit MCP9600 Library, Adafruit MPL115A2, Adafruit MPRLS Library, Adafruit TSL2591 Library, Adafruit_VL53L0X, Adafruit VL53L1X, STM32duino VL53L4CD, STM32duino VL53L4CX, Adafruit_VL6180X, Adafruit PM25 AQI Sensor, Adafruit VCNL4020 Library, Adafruit VCNL4040, Adafruit VEML7700 Library, Adafruit LC709203F, Adafruit LPS2X, Adafruit LPS35HW, Adafruit seesaw Library, Adafruit BME680 Library, Adafruit MAX1704X, Adafruit ADT7410 Library, Adafruit HTS221, Adafruit HTU21DF Library, Adafruit HTU31D Library, Adafruit PCT2075, hp_BH1750, ENS160 - Adafruit Fork
10+
depends=Adafruit NeoPixel, Adafruit SPIFlash, ArduinoJson, Adafruit DotStar, Adafruit INA219, Adafruit LTR329 and LTR303, Adafruit LTR390 Library, Adafruit MCP3421, Adafruit NAU7802 Library, Adafruit SleepyDog Library, Adafruit TMP117, Adafruit TinyUSB Library, Adafruit AHTX0, Adafruit BME280 Library, Adafruit BMP280 Library, Adafruit BMP3XX Library, Adafruit DPS310, Adafruit DS248x, Adafruit SCD30, Adafruit SGP30 Sensor, Adafruit SGP40 Sensor, Sensirion I2C SCD4x, Sensirion I2C SEN5X, arduino-sht, Adafruit Si7021 Library, Adafruit MQTT Library, Adafruit MS8607, Adafruit MCP9808 Library, Adafruit MCP9600 Library, Adafruit MPL115A2, Adafruit MPRLS Library, Adafruit TSL2591 Library, Adafruit_VL53L0X, Adafruit VL53L1X, STM32duino VL53L4CD, STM32duino VL53L4CX, Adafruit_VL6180X, Adafruit PM25 AQI Sensor, Adafruit VCNL4020 Library, Adafruit VCNL4040, Adafruit VEML7700 Library, Adafruit LC709203F, Adafruit LPS2X, Adafruit LPS35HW, Adafruit seesaw Library, Adafruit BME680 Library, Adafruit MAX1704X, Adafruit ADT7410 Library, Adafruit HTS221, Adafruit HTU21DF Library, Adafruit HTU31D Library, Adafruit PCT2075, hp_BH1750, ENS160 - Adafruit Fork

platformio.ini

Lines changed: 1 addition & 0 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

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: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
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 ///< DS18B20 family code
20+
#define DS18B20_CMD_CONVERT_T 0x44 ///< Convert T command
21+
#define DS18B20_CMD_MATCH_ROM 0x55 ///< Match ROM command
22+
#define DS18B20_CMD_READ_SCRATCHPAD 0xBE ///< Read Scratchpad command
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, uint16_t sensorAddress)
46+
: WipperSnapper_I2C_Driver(i2c, sensorAddress) {
47+
_i2c = i2c;
48+
_sensorAddress = sensorAddress;
49+
}
50+
51+
/*******************************************************************************/
52+
/*!
53+
@brief Destructor for an DS2484 sensor.
54+
*/
55+
/*******************************************************************************/
56+
~WipperSnapper_I2C_Driver_DS2484() { delete _ds2484; }
57+
58+
/*******************************************************************************/
59+
/*!
60+
@brief Initializes the DS2484 sensor and begins I2C.
61+
@returns True if initialized successfully, False otherwise.
62+
*/
63+
/*******************************************************************************/
64+
bool begin() {
65+
// initialize DS2484
66+
_ds2484 = new Adafruit_DS248x();
67+
if (!_ds2484->begin(_i2c, (uint8_t)_sensorAddress)) {
68+
WS_DEBUG_PRINTLN("Could not find DS2484");
69+
return false;
70+
}
71+
72+
// check bus is okay
73+
if (!_ds2484->OneWireReset()) {
74+
WS_DEBUG_PRINTLN("Failed to do a OneWire bus reset");
75+
if (_ds2484->shortDetected()) {
76+
WS_DEBUG_PRINTLN("\tShort detected");
77+
}
78+
if (!_ds2484->presencePulseDetected()) {
79+
WS_DEBUG_PRINTLN("\tNo presense pulse");
80+
}
81+
return false;
82+
}
83+
84+
// locate first DS18B20
85+
bool found_device = false;
86+
_ds2484->OneWireReset();
87+
_ds2484->OneWireSearchReset();
88+
while (!found_device && _ds2484->OneWireSearch(_rom)) {
89+
if (_rom[0] == DS18B20_FAMILY_CODE) {
90+
found_device = true;
91+
} else {
92+
WS_DEBUG_PRINT("Found unwanted device with family code: 0x");
93+
WS_DEBUG_PRINTHEX(_rom[0]);
94+
WS_DEBUG_PRINTLN(" expected 0x28"); // DS18B20_FAMILY_CODE
95+
}
96+
}
97+
98+
if (!found_device) {
99+
WS_DEBUG_PRINTLN("Could not find DS18B20 attached to DS2484");
100+
return false;
101+
}
102+
103+
WS_DEBUG_PRINTLN("DS2484 found");
104+
return true;
105+
}
106+
107+
/*******************************************************************************/
108+
/*!
109+
@brief Processes a temperature event.
110+
@param tempEvent
111+
Pointer to an Adafruit_Sensor event.
112+
@returns True if the temperature was obtained successfully, False
113+
otherwise.
114+
*/
115+
bool processTemperatureEvent(sensors_event_t *tempEvent) {
116+
if (!_ds2484->OneWireReset()) {
117+
WS_DEBUG_PRINTLN("Failed to do a OneWire bus reset");
118+
return false;
119+
}
120+
if (!_ds2484->presencePulseDetected()) {
121+
tempEvent->temperature = NAN;
122+
return true;
123+
}
124+
125+
_ds2484->OneWireWriteByte(DS18B20_CMD_MATCH_ROM); // Match ROM command
126+
for (int i = 0; i < 8; i++) {
127+
_ds2484->OneWireWriteByte(_rom[i]);
128+
}
129+
130+
// Start temperature conversion
131+
_ds2484->OneWireWriteByte(DS18B20_CMD_CONVERT_T); // Convert T command
132+
delay(750); // Wait for conversion (750ms for maximum precision)
133+
134+
// Read scratchpad
135+
if (!_ds2484->OneWireReset()) {
136+
WS_DEBUG_PRINTLN(
137+
"Failed to do a OneWire bus reset after starting temp conversion");
138+
return false;
139+
}
140+
_ds2484->OneWireWriteByte(DS18B20_CMD_MATCH_ROM); // Match ROM command
141+
for (int i = 0; i < 8; i++) {
142+
_ds2484->OneWireWriteByte(_rom[i]);
143+
}
144+
_ds2484->OneWireWriteByte(
145+
DS18B20_CMD_READ_SCRATCHPAD); // Read Scratchpad command
146+
147+
uint8_t data[9];
148+
for (int i = 0; i < sizeof(data) / sizeof(data[0]); i++) {
149+
_ds2484->OneWireReadByte(&data[i]);
150+
}
151+
152+
// Calculate temperature
153+
int16_t raw = (data[1] << 8) | data[0];
154+
tempEvent->temperature = (float)raw / 16.0;
155+
return true;
156+
}
157+
158+
/*******************************************************************************/
159+
/*!
160+
@brief Gets the DS2484's current temperature.
161+
@param tempEvent
162+
Pointer to an Adafruit_Sensor event.
163+
@returns True if the temperature was obtained successfully, False
164+
otherwise.
165+
*/
166+
/*******************************************************************************/
167+
bool getEventAmbientTemp(sensors_event_t *tempEvent) {
168+
return processTemperatureEvent(tempEvent);
169+
}
170+
171+
protected:
172+
Adafruit_DS248x *_ds2484; ///< DS2484 driver object
173+
uint8_t _rom[8]; ///< DS18B20 ROM
174+
};
175+
176+
#endif // WipperSnapper_I2C_Driver_DS2484

0 commit comments

Comments
 (0)