Skip to content

Commit 22e5744

Browse files
committed
0.1.3 LTC2485
1 parent bfda213 commit 22e5744

File tree

14 files changed

+160
-77
lines changed

14 files changed

+160
-77
lines changed

libraries/LTC2485/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ 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.1.3] - 2025-03-04
10+
- add simple calibration for temperature
11+
- add **getLastError()** for minimal error handling
12+
- extended unit test with constants test
13+
914
## [0.1.2] - 2025-02-24
1015
- add delay() in examples for first conversion (speed dependent)
1116
- refactor code - several fixes

libraries/LTC2485/LTC2485.cpp

Lines changed: 76 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,31 @@
11
//
22
// FILE: LTC2485.cpp
33
// AUTHOR: Rob Tillaart
4-
// VERSION: 0.1.2
4+
// VERSION: 0.1.3
55
// DATE: 2025-02-21
66
// PURPOSE: Arduino library for LTC2485 I2C 24 bit ADC.
77
// URL: https://github.com/RobTillaart/LTC2485
88

99

1010
#include "LTC2485.h"
1111

12+
1213
// CONVERSION
1314
// needed as I2C is blocked during conversion.
1415
#define LTC2485_DELAY_1X 200
1516
#define LTC2485_DELAY_2X 100 // not used yet
1617

1718

19+
// CALIBRATION
20+
// adjust these numbers to your need
21+
// determine them by averaging multiple TC measurements.
22+
// Datasheet page 20: 27 C; 420 mV; 1.40 mV/°C
23+
//
24+
const float LTC2485_TAVERAGE = 27.0; // °C
25+
const float LTC2485_MVOLT_TAVERAGE = 420.0; // mV
26+
const float LTC2485_SLOPE = 1.40; // mV/°C
27+
28+
1829
/////////////////////////////////////////////////////
1930
//
2031
// CONSTRUCTORS
@@ -26,7 +37,7 @@ LTC2485::LTC2485(uint8_t address, TwoWire *wire)
2637
// defaults
2738
_lastAccess = 0;
2839
_timeout = LTC2485_DELAY_1X;
29-
_error = 0;
40+
_error = LTC2485_OK;
3041
_vref = 2.5;
3142
_config = LTC2485_SPEED_1X | LTC2485_REJECT_50_60_HZ;
3243
}
@@ -81,8 +92,7 @@ int32_t LTC2485::getADC()
8192
{
8293
if (configure(_config & 0x07) != 0)
8394
{
84-
// TODO cleaner error handling.
85-
Serial.println("FAIL TO CONFIG-A");
95+
_error = LTC2485_ERR_CONFIG_ADC;
8696
return 0;
8797
}
8898
}
@@ -113,7 +123,7 @@ float LTC2485::getTemperature()
113123
{
114124
if (configure(_config | LTC2485_INTERNAL_TEMP) != 0)
115125
{
116-
Serial.println("FAIL TO CONFIG-T");
126+
_error = LTC2485_ERR_CONFIG_TEMP;
117127
return 0;
118128
}
119129
}
@@ -122,20 +132,28 @@ float LTC2485::getTemperature()
122132
delay(_timeout);
123133
// read the ADC
124134
int32_t value = _read();
135+
// DEBUG
136+
// Serial.print(F("TRAW: \t"));
137+
// Serial.print(value, HEX);
138+
// Serial.print(F("\t"));
139+
// Serial.println(value, DEC);
140+
125141
// update lastAccess
126142
_lastAccess = millis();
127143
// make two complements.
128144
value ^= 0x80000000;
129145
value /= 128;
130146

147+
// 16777215 == 2^24 - 1
131148
// div 16777215 == mul 5.960464832810e-8
132149
// float milliVolts = value * _vref * 5.960464832810e-8 * 1000;
133150
float milliVolts = value * _vref * 5.960464832810e-5;
134151
_lastAccess = millis();
135152
// datasheet page 20
153+
// adjust constants at top of file.
136154
// 27 C == 420 mV
137155
// SLOPE == 1.40 mV / °C
138-
float TC = 27.0 + (milliVolts - 420) / 1.40;
156+
float TC = LTC2485_TAVERAGE + (milliVolts - LTC2485_MVOLT_TAVERAGE) / LTC2485_SLOPE;
139157
return TC;
140158
}
141159

@@ -146,6 +164,19 @@ uint32_t LTC2485::lastAccessed()
146164
}
147165

148166

167+
/////////////////////////////////////////////////////
168+
//
169+
// ERROR HANDLING
170+
//
171+
int LTC2485::getLastError()
172+
{
173+
int e = _error;
174+
_error = LTC2485_OK;
175+
return e;
176+
}
177+
178+
179+
149180
//////////////////////////////////////////////////////////////////
150181
//
151182
// PRIVATE functions
@@ -154,49 +185,54 @@ int LTC2485::_write(uint8_t value)
154185
{
155186
_wire->beginTransmission(_address);
156187
_wire->write(value);
157-
return _wire->endTransmission();
188+
int n = _wire->endTransmission();
189+
if (n != 0)
190+
{
191+
_error = LTC2485_ERR_I2C_W;
192+
}
193+
return n;
158194
}
159195

160196

161197
uint32_t LTC2485::_read()
162198
{
163-
uint32_t rv = 0;
164199
int n = _wire->requestFrom(_address, (uint8_t)4);
165-
if (n == 4)
200+
if (n != 4)
166201
{
167-
rv |= _wire->read();
168-
rv <<= 8;
169-
rv |= _wire->read();
170-
rv <<= 8;
171-
rv |= _wire->read();
172-
rv <<= 8;
173-
rv |= _wire->read();
174-
return rv;
175-
176-
// DEBUG
177-
// uint32_t A = _wire->read();
178-
// uint32_t B = _wire->read();
179-
// uint32_t C = _wire->read();
180-
// uint32_t D = _wire->read();
181-
// if (A < 0x10) Serial.print(0);
182-
// Serial.print(A, HEX);
183-
// Serial.print("\t");
184-
// if (B < 0x10) Serial.print(0);
185-
// Serial.print(B, HEX);
186-
// Serial.print("\t");
187-
// if (C < 0x10) Serial.print(0);
188-
// Serial.print(C, HEX);
189-
// Serial.print("\t");
190-
// if (D < 0x10) Serial.print(0);
191-
// Serial.print(D, HEX);
192-
// Serial.print("\t");
193-
194-
// rv = (A << 24) + (B << 16) + (C << 8) + D;
195-
// return rv;
202+
_error = LTC2485_ERR_I2C_R;
203+
return n;
196204
}
197-
return n;
198-
}
205+
uint32_t rv = 0;
206+
rv |= _wire->read();
207+
rv <<= 8;
208+
rv |= _wire->read();
209+
rv <<= 8;
210+
rv |= _wire->read();
211+
rv <<= 8;
212+
rv |= _wire->read();
213+
return rv;
199214

215+
// DEBUG
216+
// uint32_t A = _wire->read();
217+
// uint32_t B = _wire->read();
218+
// uint32_t C = _wire->read();
219+
// uint32_t D = _wire->read();
220+
// if (A < 0x10) Serial.print(0);
221+
// Serial.print(A, HEX);
222+
// Serial.print("\t");
223+
// if (B < 0x10) Serial.print(0);
224+
// Serial.print(B, HEX);
225+
// Serial.print("\t");
226+
// if (C < 0x10) Serial.print(0);
227+
// Serial.print(C, HEX);
228+
// Serial.print("\t");
229+
// if (D < 0x10) Serial.print(0);
230+
// Serial.print(D, HEX);
231+
// Serial.print("\t");
232+
233+
// rv = (A << 24) + (B << 16) + (C << 8) + D;
234+
// return rv;
235+
}
200236

201237

202238
// -- END OF FILE --

libraries/LTC2485/LTC2485.h

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// FILE: LTC2485.h
44
// AUTHOR: Rob Tillaart
5-
// VERSION: 0.1.2
5+
// VERSION: 0.1.3
66
// DATE: 2025-02-21
77
// PURPOSE: Arduino library for LTC2485 I2C 24 bit ADC.
88
// URL: https://github.com/RobTillaart/LTC2485
@@ -12,7 +12,7 @@
1212
#include "Wire.h"
1313

1414

15-
#define LTC2485_LIB_VERSION (F("0.1.2"))
15+
#define LTC2485_LIB_VERSION (F("0.1.3"))
1616

1717

1818
// CONFIGURATION BITS, TABLE 1
@@ -24,6 +24,14 @@
2424
#define LTC2485_INTERNAL_TEMP 0x08
2525

2626

27+
// ERROR
28+
#define LTC2485_OK 0
29+
#define LTC2485_ERR_CONFIG_ADC -1
30+
#define LTC2485_ERR_CONFIG_TEMP -2
31+
#define LTC2485_ERR_I2C_W -3
32+
#define LTC2485_ERR_I2C_R -4
33+
34+
2735
class LTC2485
2836
{
2937
public:
@@ -43,6 +51,8 @@ class LTC2485
4351

4452
uint32_t lastAccessed();
4553

54+
// ERROR HANDLING
55+
int getLastError();
4656

4757
private:
4858

libraries/LTC2485/README.md

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -27,21 +27,27 @@ In practice there will be noise and one need a very stable VREF and test
2727
setup to get accurate readings on that level.
2828

2929
The LTC2485 has an internal voltage that is proportional to temperature.
30-
The library allows to read that and has a non calibrated conversion.
30+
The library allows to read that voltage and converts it to temperature Celsius.
31+
The formula can be calibrated a bit at the top of the .cpp file.
3132

3233

3334
Read the datasheet for the details.
3435

3536
As always, feedback is welcome.
3637

38+
3739
### Tested
3840

3941
Tested with hardware during development.
4042
More tests are needed to see if and how it can be improved.
4143

4244
See - https://forum.arduino.cc/t/ltc2485-arduino-library/1356147
4345

44-
### Keycode
46+
At the release 0.1.3 there are still unsolved / unexplained fluctuations
47+
visible in the measurements during tests, especially in temperature.
48+
49+
50+
### Key code
4551

4652
ALT230 = µ (windows).
4753

@@ -71,13 +77,13 @@ Please use the output of example **LTC2485_performance.ino** and the board used.
7177

7278
### Compatibles
7379

74-
TODO
80+
Unknown so far, there exists a whole range of LTCxxxx ADC's.
7581

7682

7783
### Related
7884

7985
- https://gammon.com.au/adc tutorial about ADC's (UNO specific)
80-
- https://github.com/RobTillaart/LTC2485
86+
- https://github.com/RobTillaart/LTC2485 this library
8187
- https://github.com/RobTillaart/MCP_ADC SPI based ADC
8288
- https://github.com/RobTillaart/ADS1x15 (12 & 16 bit ADC, I2C, slow)
8389
- https://github.com/RobTillaart/PCF8591 (8 bit ADC + 1 bit DAC)
@@ -106,7 +112,7 @@ Note: do call **Wire.begin()** before **begin()**
106112
### Configure
107113

108114
- **int configure(uint8_t value)** set flags for next conversion.
109-
Returns status of I2C, 0 == success write.
115+
Returns status of I2C, 0 == success write, other values == error.
110116

111117
Configuration bit masks, should be OR-ed.
112118

@@ -119,7 +125,8 @@ Configuration bit masks, should be OR-ed.
119125
| LTC2485_REJECT_60HZ | 0x04 |
120126
| LTC2485_INTERNAL_TEMP | 0x08 |
121127

122-
LTC2485_REJECT_50HZ and LTC2485_REJECT_50HZ may **NOT** be set simultaneously.
128+
Flags LTC2485_REJECT_50HZ and LTC2485_REJECT_60HZ may **NOT** be used simultaneously.
129+
Use LTC2485_REJECT_50_60_HZ instead.
123130

124131
example
125132

@@ -133,14 +140,19 @@ LTC.configure(LTC2485_SPEED_2X | LTC2485_REJECT_60HZ);
133140
Read returns the last conversion and triggers a new conversion at the end.
134141
So it might be needed to discard a first read.
135142

136-
- **int32_t getADC()** returns ADC value. Will configure ADC mode automatically.
137-
Returned range == -16777215..+16777215
143+
- **int32_t getADC()** returns ADC value. Will configure ADC mode automatically
144+
if needed.
145+
Returned range == -16777215..+16777215
146+
+16777216 == overflow
147+
-16777216 == underflow
138148
- **float getVolts()** converts ADC value to volts.
139149
- **float getMilliVolts()** converts ADC value to millivolts.
140150
- **float getMicroVolts()** converts ADC value to microvolts.
141151
- **float getTemperature()** returns internal temperature.
142152
Will configure temperature mode automatically.
143-
153+
During first hardware tests it was impossible to get the temperature
154+
stable.
155+
This needs more investigation in the future.
144156
- **uint32_t lastAccesed()** track time in milliseconds of last access.
145157
used internally to determine maximum delay needed for conversion.
146158

@@ -154,29 +166,26 @@ used internally to determine maximum delay needed for conversion.
154166
- compatibles section
155167
- get hardware to test library
156168

157-
158169
#### Should
159170

160-
- fix TODO's in code and documentation.
161-
- check pin compatible devices as derived class?
162-
- LTC2481 (16 bits) and LTC2483 (16 bits)
163-
- improve error handling.
171+
- improve error handling
164172
- overflow / underflow
165173
- time out handling?
166-
- performance measurements
167-
- I2C bus speed?
168-
- check math for improvements
169-
- refactor for performance.
170-
174+
- fix TODO's in code and documentation
171175

172176
#### Could
173177

178+
- check pin compatible devices as derived class?
179+
- LTC2481 (16 bits) and LTC2483 (16 bits)
174180
- calibrate internal temperature, something like
175181
- **void calibrateTemperature(float A, float B)** 420.0 1.40
176182
- TC = 27 + (Voltage - A) x B;
177183
- create a 16 bit API which only gets 2 bytes instead of 4?
178184
- looses the "noisy" bits.
179-
185+
- performance measurements
186+
- I2C bus speed?
187+
- check math for improvements
188+
- refactor for performance
180189

181190
#### Wont
182191

libraries/LTC2485/examples/LTC2485_connect/LTC2485_connect.ino

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111

1212
// adjust address if needed
1313
// 0x14 CA1 = LOW CA0 = HIGH
14-
LTC2485 LTC(0x14);
14+
LTC2485 LTC(0x16);
1515

16-
float VREF = 5.0;
16+
float VREF = 1.8;
1717

1818
void setup()
1919
{

0 commit comments

Comments
 (0)