Skip to content

Commit 5be42c6

Browse files
committed
0.2.1 DS2438
1 parent efb2e1e commit 5be42c6

10 files changed

Lines changed: 281 additions & 59 deletions

File tree

libraries/DS2438/CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,14 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/)
66
and this project adheres to [Semantic Versioning](http://semver.org/).
77

88

9+
## [0.2.1] - 2026-02-05
10+
- fix #7, add isDS2438()
11+
- fix readRemaining();
12+
- update examples
13+
- add performance example
14+
- update readme.md
15+
- minor edits
16+
917
## [0.2.0] - 2026-02-05
1018
- fix address bug in read/writeEEPROM()
1119
- fix #5, improve demo sketch

libraries/DS2438/DS2438.cpp

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//
22
// FILE: DS2438.cpp
33
// AUTHOR: Rob Tillaart
4-
// VERSION: 0.2.0
4+
// VERSION: 0.2.1
55
// DATE: 2023-07-28
66
// PURPOSE: Arduino Library for DS2438 battery monitor
77
// URL: https://github.com/RobTillaart/DS2438
@@ -68,6 +68,14 @@ bool DS2438::isConnected(uint8_t retries)
6868
return _addressFound;
6969
}
7070

71+
bool DS2438::isDS2438()
72+
{
73+
if (_addressFound || isConnected(3))
74+
{
75+
return (_address[0] == 0x26);
76+
}
77+
return false;
78+
}
7179

7280
///////////////////////////////////////////////////////////
7381
//
@@ -86,7 +94,7 @@ float DS2438::readTemperature()
8694
readScratchPad(0);
8795

8896
// sign MSB LSB step >> 3
89-
_temperature = (int(_scratchPad[2]) * 256 + _scratchPad[1]) * 0.03125 * 0.125;
97+
_temperature = (int(_scratchPad[2]) * 256 + _scratchPad[1]) * (0.03125f * 0.125f);
9098

9199
return _temperature;
92100
}
@@ -127,7 +135,7 @@ float DS2438::readVDD()
127135
readScratchPad(DS2438_PAGE_CORE);
128136

129137
// 10 mV resolution
130-
_vdd = ((_scratchPad[4] & 0x03) * 256 + _scratchPad[3]) * 0.01;
138+
_vdd = ((_scratchPad[4] & 0x03) * 256 + _scratchPad[3]) * 0.01f;
131139

132140
// convert current in same call?!
133141

@@ -152,7 +160,7 @@ float DS2438::readVAD()
152160
readScratchPad(DS2438_PAGE_CORE);
153161

154162
// 10 mV resolution
155-
_vad = ((_scratchPad[4] & 0x03) * 256 + _scratchPad[3]) * 0.01;
163+
_vad = ((_scratchPad[4] & 0x03) * 256 + _scratchPad[3]) * 0.01f;
156164

157165
// convert current in same call?!
158166

@@ -172,8 +180,8 @@ float DS2438::getVAD()
172180
//
173181
void DS2438::setResistor(float resistor)
174182
{
175-
_inverseR = 1.0 / (4096.0 * resistor);
176-
_RICA = 1.0 / (2048.0 * resistor);
183+
_inverseR = 1.0f / (4096.0f * resistor);
184+
_RICA = 1.0f / (2048.0f * resistor);
177185
}
178186

179187

@@ -245,9 +253,9 @@ float DS2438::readRemaining()
245253
{
246254
// datasheet p.7
247255
readScratchPad(DS2438_PAGE_ETM_ICA_OFFSET);
248-
// factor 2.0 from optimization (need to explain this factor)
249256
// Remaining Capacity = ICA / (2048 * RSENS)
250-
float remaining = _scratchPad[4] / _RICA; // mAhr
257+
// setResistor() : _RICA = 1.0/(2048 * Rsens);
258+
float remaining = _scratchPad[4] * _RICA; // mAhr
251259
return remaining;
252260
}
253261

@@ -391,15 +399,15 @@ float DS2438::readCCA()
391399
// datasheet p.8 + 16
392400
readScratchPad(DS2438_PAGE_CCA_DCA);
393401
uint16_t raw = (_scratchPad[5] * 256 + _scratchPad[4]);
394-
return raw * 15.625;
402+
return raw * 15.625f;
395403
}
396404

397405
float DS2438::readDCA()
398406
{
399407
// datasheet p.8 + 16
400408
readScratchPad(DS2438_PAGE_CCA_DCA);
401409
uint16_t raw = (_scratchPad[7] * 256 + _scratchPad[6]);
402-
return raw * 15.625;
410+
return raw * 15.625f;
403411
}
404412

405413
bool DS2438::setCCA(float CCA)
@@ -408,7 +416,7 @@ bool DS2438::setCCA(float CCA)
408416
if (CCA < 0) return false;
409417
clearConfigBit(DS2438_CONFIG_IAD);
410418
readScratchPad(DS2438_PAGE_CCA_DCA);
411-
uint16_t tmp = round(CCA / 15.625);
419+
uint16_t tmp = round(CCA * (1.0f / 15.625f));
412420
_scratchPad[4] = tmp & 0xFF;
413421
_scratchPad[5] = tmp >> 8;
414422
writeScratchPad(DS2438_PAGE_CCA_DCA);
@@ -423,7 +431,7 @@ bool DS2438::setDCA(float DCA)
423431
if (DCA < 0) return false;
424432
clearConfigBit(DS2438_CONFIG_IAD);
425433
readScratchPad(DS2438_PAGE_CCA_DCA);
426-
uint16_t tmp = round(DCA / 15.625);
434+
uint16_t tmp = round(DCA * (1.0f / 15.625f));
427435
_scratchPad[6] = tmp & 0xFF;
428436
_scratchPad[7] = tmp >> 8;
429437
writeScratchPad(DS2438_PAGE_CCA_DCA);
@@ -453,7 +461,7 @@ void DS2438::resetAccumulators()
453461
//
454462
void DS2438::setConfigBit(uint8_t bit)
455463
{
456-
if (bit > 3) return;
464+
if (bit > DS2438_CONFIG_AD) return;
457465
uint8_t mask = (0x01 << bit);
458466
readScratchPad(DS2438_PAGE_CORE);
459467
if ((_scratchPad[0] & mask) == mask) return; // already 1
@@ -463,7 +471,7 @@ void DS2438::setConfigBit(uint8_t bit)
463471

464472
void DS2438::clearConfigBit(uint8_t bit)
465473
{
466-
if (bit > 3) return;
474+
if (bit > DS2438_CONFIG_AD) return;
467475
uint8_t mask = (0x01 << bit);
468476
readScratchPad(DS2438_PAGE_CORE);
469477
if ((_scratchPad[0] & mask) == 0x00) return; // already 0
@@ -515,7 +523,12 @@ void DS2438::readScratchPad(uint8_t page)
515523
_oneWire->write(DS2438_READ_SCRATCH, 0);
516524
_oneWire->write(page, 0);
517525
for (uint8_t i = 0; i < 8; i++) _scratchPad[i] = _oneWire->read();
518-
// skip crc for now.
526+
// skip CRC for now
527+
// - update loop to 9
528+
// - change return type
529+
// - set error flag.
530+
// return _oneWire->crc8(_scratchPad, 8) != _scratchPad[8]);
531+
return;
519532
}
520533

521534

@@ -527,6 +540,7 @@ void DS2438::writeScratchPad(uint8_t page)
527540
_oneWire->write(DS2438_WRITE_SCRATCH, 0);
528541
_oneWire->write(page, 0);
529542
for (uint8_t i = 0; i < 8; i++) _oneWire->write(_scratchPad[i], 0);
543+
// no CRC when writing.
530544
_oneWire->reset();
531545
_oneWire->select(_address);
532546
_oneWire->write(DS2438_COPY_SCRATCH, 0);

libraries/DS2438/DS2438.h

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,20 @@
22
//
33
// FILE: DS2438.h
44
// AUTHOR: Rob Tillaart
5-
// VERSION: 0.2.0
5+
// VERSION: 0.2.1
66
// DATE: 2023-07-28
77
// PURPOSE: Arduino Library for DS2438 battery monitor
88
// URL: https://github.com/RobTillaart/DS2438
99
//
10-
// DS2438
11-
// +------------------+
12-
// | 1 GND |
13-
// | 2 Vsense+ |
14-
// | 3 Vsense- |
15-
// | 4 VAD |
16-
// | 5 VDD |
17-
// | 6 NC |
18-
// | 7 NC |
19-
// | 8 DQ |
20-
// +------------------+
10+
//
11+
// DS2438
12+
// +-----------------------+
13+
// | 1 GND DQ 8 |
14+
// | 2 Vsense+ NC 7 |
15+
// | 3 Vsense- NC 6 |
16+
// | 4 VAD VDD 5 |
17+
// +-----------------------+
18+
//
2119
//
2220
// | Pin | Description | Connect to |
2321
// |:--------:|:------------------------------------|:-------------|
@@ -33,7 +31,7 @@
3331
#include "Arduino.h"
3432
#include "OneWire.h"
3533

36-
#define DS2438_LIB_VERSION (F("0.2.0"))
34+
#define DS2438_LIB_VERSION (F("0.2.1"))
3735

3836
#define DS2438_INVALID -999
3937

@@ -55,7 +53,7 @@ class DS2438
5553

5654
bool begin(uint8_t retries = 3);
5755
bool isConnected(uint8_t retries = 3);
58-
56+
bool isDS2438();
5957

6058
// TEMPERATURE
6159
// unit is degrees Celsius

libraries/DS2438/README.md

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,18 @@ Arduino Library for the DS2438 1-wire battery monitor.
2020

2121
The DS2438 is a experimental library for the DS2438 battery management sensor.
2222

23-
The library is not tested by me in detail as I have no hardware (breakout board).
24-
So for the moment the library is written based upon the datasheet and provided as is.
25-
That said, the library is working (see e.g. #5) but use it with care as it is still
26-
work in progress.
23+
The library is tested in #5 and seems to work OK, but use it with care as
24+
it is still work in progress.
25+
As I have no hardware == breakout board (yet) I cannot tests.
2726

2827
The device supports the following functionality.
2928
- voltage measurement
3029
- temperature measurement
3130
- current measurement.
32-
- 40 bytes non-volatile EEPROM memory
31+
- 40 bytes non-volatile EEPROM memory (use only 36 see below)
3332
- Elapsed time meter in binary format
33+
- current accumulators
34+
- test deviceType
3435

3536
This library supports only one DS2438 per Arduino / MCU pin.
3637

@@ -40,7 +41,8 @@ As always, feedback is welcome.
4041
### 0.2.0 Breaking Change
4142

4243
The 0.2.0 version fixes a bug in the EEPROM that affects the proper working
43-
of the device. (See issue 5).
44+
of the device. (See issue 5). Furthermore there are many other fixes and
45+
enhancements.
4446

4547
Versions before 0.2.0 are therefore obsolete.
4648

@@ -52,17 +54,13 @@ Need to find a breakout.
5254
See datasheet for details for a measurement schema.
5355

5456
```
55-
DS2438
56-
+------------------+
57-
| 1 GND |
58-
| 2 Vsense+ |
59-
| 3 Vsense- |
60-
| 4 VAD |
61-
| 5 VDD |
62-
| 6 NC |
63-
| 7 NC |
64-
| 8 DQ |
65-
+------------------+
57+
DS2438
58+
+-----------------------+
59+
| 1 GND DQ 8 |
60+
| 2 Vsense+ NC 7 |
61+
| 3 Vsense- NC 6 |
62+
| 4 VAD VDD 5 |
63+
+-----------------------+
6664
```
6765

6866
| Pin | Description | Connect to |
@@ -73,7 +71,7 @@ See datasheet for details for a measurement schema.
7371
| VSENS- | Battery current monitor input (-) | Battery |
7472
| VDD | Power Supply (2.4V to 10.0V) | +5V |
7573
| GND | Ground | processor |
76-
| NC | No connect | - |
74+
| NC | No connected | - |
7775

7876

7977
### Related
@@ -101,6 +99,9 @@ Returns true if address / device is found and all is OK.
10199
There will be a number of retries to connect, default 3.
102100
- **bool isConnected(uint8_t retries = 3)** Returns true if address / device is found.
103101
There will be a number of retries to connect, default 3.
102+
- **bool isDS2438()** returns true if the device type == 0x26 which indicates
103+
an DS2438. Returns false if no device seen or other type.
104+
104105

105106
### Temperature
106107

@@ -247,14 +248,11 @@ This library supports only one DS2438 per Arduino / MCU pin.
247248

248249
#### Must
249250

250-
- Improve documentation (a lot).
251-
- test with hardware or simulator?.
252-
- examples for testing.
251+
- improve documentation
253252

254253
#### Should
255254

256-
- implement CRC
257-
- add device type check
255+
- add real live examples
258256

259257
#### Could
260258

@@ -269,13 +267,13 @@ only after testing and code works.
269267
- optimize the code.
270268
- cache registers?
271269
- improve magic masks and numbers
272-
- performance test.
273270
- getAddress() ?
274-
- debugging
275271
- error handling.
276272
- copy snapshot to EEPROM(page) -> datasheet p.8 EE bit.
277273
- copies page 0 to EEPROM page 0..4
278-
274+
- enable CRC in readScratchPad()
275+
- performance penalty
276+
- failing CRC must be handled otherwise no added value.
279277

280278
#### Wont
281279

libraries/DS2438/examples/DS2438_demo/DS2438_demo.ino

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,14 @@ void setup()
2525

2626
bm.begin();
2727
Serial.println(bm.isConnected());
28-
28+
if (!bm.isDS2438())
29+
{
30+
Serial.print("Pin ");
31+
Serial.print(ONE_WIRE_BUS);
32+
Serial.println(" has no DS2438 attached.");
33+
Serial.println("Check connections and restart.");
34+
while(1);
35+
}
2936

3037
Serial.println("\nTemperature");
3138
bm.readTemperature();
@@ -48,6 +55,7 @@ void setup()
4855
delay(30);
4956
bm.readCurrent();
5057
Serial.println(bm.getCurrent());
58+
Serial.println(bm.readCurrentOffset());
5159

5260

5361
Serial.println("\nTime");
@@ -57,7 +65,8 @@ void setup()
5765

5866
Serial.println("\nThreshold");
5967
Serial.println(bm.readThreshold());
60-
68+
Serial.println("Remaining: ");
69+
Serial.println(bm.readRemaining());
6170

6271
Serial.println("\nCCA/DCA");
6372
Serial.println(" Charge: ");
@@ -66,6 +75,8 @@ void setup()
6675
Serial.println(bm.readDCA());
6776
Serial.println(" Delta: ");
6877
Serial.println(bm.readCCA() - bm.readDCA());
78+
Serial.println("Remaining: ");
79+
Serial.println(bm.readRemaining());
6980

7081

7182
Serial.println("\nEEPROM");

0 commit comments

Comments
 (0)