Skip to content

Commit 5b034d6

Browse files
committed
0.1.2 MCP3424
1 parent 3bf1f8b commit 5b034d6

File tree

8 files changed

+124
-13
lines changed

8 files changed

+124
-13
lines changed

libraries/MCP3424/CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,13 @@ 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.2] - 2024-10-25
10+
- fix #4, update readme.md conversion delay
11+
- fix **setChannel()** optimization
12+
- add **getConversionDelay()**
13+
- add example **MCP3428_setChannel.ino**
14+
- minor edits
15+
916
## [0.1.1] - 2024-09-18
1017
- add derived classes for MCP3421/2/3/6/7/8
1118
- extend unit tests.

libraries/MCP3424/MCP3424.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//
22
// FILE: MCP3424.cpp
33
// AUTHOR: Rob Tillaart
4-
// VERSION: 0.1.1
4+
// VERSION: 0.1.2
55
// PURPOSE: Arduino library for 18 bit ADC I2C MCP3424 and compatibles.
66
// URL: https://github.com/RobTillaart/MCP3424
77

@@ -164,6 +164,14 @@ uint8_t MCP3424::getResolution()
164164
}
165165

166166

167+
uint16_t MCP3424::getConversionDelay()
168+
{
169+
uint16_t _interval[4] = { 5, 17, 67, 267 };
170+
int idx = (_bits - 12) / 2; // map 12-> 0 ... 18-> 3
171+
return _interval[idx];
172+
}
173+
174+
167175
void MCP3424::setContinuousMode()
168176
{
169177
if (getMode() != 1)

libraries/MCP3424/MCP3424.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22
//
33
// FILE: MCP3424.h
44
// AUTHOR: Rob Tillaart
5-
// VERSION: 0.1.1
5+
// VERSION: 0.1.2
66
// PURPOSE: Arduino library for 18 bit ADC I2C MCP3424 and compatibles.
77
// URL: https://github.com/RobTillaart/MCP3424
88

99

1010
#include "Arduino.h"
1111
#include "Wire.h"
1212

13-
#define MCP3424_LIB_VERSION (F("0.1.1"))
13+
#define MCP3424_LIB_VERSION (F("0.1.2"))
1414

1515

1616
class MCP3424
@@ -32,12 +32,17 @@ class MCP3424
3232
float readMicroVolts();
3333

3434
// CONFIG
35+
// note that after changing a channel one has to wait before
36+
// a valid measurement is available, see readme.md.
3537
bool setChannel(uint8_t channel = 0);
3638
uint8_t getChannel();
3739
bool setGain(uint8_t gain = 1);
3840
uint8_t getGain();
41+
3942
bool setResolution(uint8_t bits = 12);
4043
uint8_t getResolution();
44+
uint16_t getConversionDelay();
45+
4146
// MODE
4247
void setContinuousMode(); // default
4348
void setSingleShotMode();

libraries/MCP3424/README.md

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,23 +39,31 @@ The user has to configure the ADC device (bits, gain) and can call
3939
Current implementation will probably change slightly in the future
4040
when related devices will be supported. (See future section).
4141

42+
As always feedback is welcome.
43+
44+
### Special chars
4245

4346
Alt-230 = µ
4447

4548

4649
### Resolution
4750

48-
| Bits | LSB (gain=1) | SPS | Raw range | Notes |
49-
|:------:|---------------:|:------:|:-------------------:|:-------:|
50-
| 12 | 1 mV | 240 | -2048 .. 2047 |
51-
| 14 | 250 µV | 60 | -8192 .. 8191 |
52-
| 16 | 62.5 µV | 15 | -32768 .. 32767 |
53-
| 18 | 15.625 µV | 3.75 | -131072 .. 131071 | not for 3426/27/28.
51+
SPS = Samples per second
52+
53+
Interval is the time between consecutive reads in milliseconds.
54+
This is also the time to wait after the switching of a channel.
55+
56+
| Bits | LSB (gain=1) | SPS | Interval | Raw range | Notes |
57+
|:------:|---------------:|:------:|:----------:|:-------------------:|:-------:|
58+
| 12 | 1 mV | 240 | 4.2 ms | -2048 .. 2047 |
59+
| 14 | 250 µV | 60 | 16.7 ms | -8192 .. 8191 |
60+
| 16 | 62.5 µV | 15 | 66.7 ms | -32768 .. 32767 |
61+
| 18 | 15.625 µV | 3.75 | 266.7 ms | -131072 .. 131071 | not for 3426/27/28.
5462

5563
The effective resolution also depends on the gain set.
5664
In theory with a gain of 8 the LSB of the 18 bit resolution represents
5765
1/8 of 15.625 µV == 1.95 µV.
58-
If this is feasible in practice is to be seen.
66+
If this is feasible in practice is to be verified.
5967

6068

6169
### I2C Address
@@ -140,16 +148,32 @@ The user has to configure the ADC device (bits, gain) and can call
140148
Correct settings will be written to the device immediately, but be aware of the fact
141149
that it will take some time before the conversion with new settings is done.
142150

151+
#### Channels
152+
143153
- **bool setChannel(uint8_t channel = 0)** not to be used for the MCP3421 as
144154
it has only one channel. Default is channel 0, parameter should be less than the
145155
value of **getMaxChannels()**.
156+
After changing a channel one has to wait an interval depending on the resolution used.
157+
See table above.
146158
- **uint8_t getChannel()** returns chosen channel (default 0).
159+
160+
#### Gain
161+
147162
- **bool setGain(uint8_t gain = 1)** set gain to 1,2,4, or 8.
148163
Other values will return false and not change the setting.
149164
- **uint8_t getGain()** returns the set gain (default 1).
165+
166+
#### Resolution
167+
150168
- **bool setResolution(uint8_t bits = 12)** set the bit resolution 12,14,16 or 18.
151169
Other values will return false and not change the setting.
152170
- **uint8_t getResolution()** returns the set resolution (default 12).
171+
- **uint16_t getConversionDelay()** returns the delay in ms one has to wait at the
172+
current resolution between calls to **setChannel()** and /or **read()**.
173+
See Resolution setcion above.
174+
175+
#### Mode
176+
153177
- **void setContinuousMode()** idem.
154178
- **void setSingleShotMode()** idem.
155179
- **uint8_t getMode()** returns 0 for singleShot and 1 for continuous.
@@ -176,7 +200,8 @@ This might be added in the future.
176200

177201
- test on different boards.
178202
- optimize performance if possible
179-
- check performance I2C with HW
203+
- dividing by ```_gain``` ==> multiplication (faster?).
204+
- check performance I2C with HW.
180205
- optimize setting all configuration in one function call.
181206
- **setConfig(channel, resolution, gain, mode)** ?
182207
- getter needed?
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
//
2+
// FILE: MCP3428_setChannel.ino
3+
// AUTHOR: Rob Tillaart
4+
// PURPOSE: demo to show the needed timeout between setChannel() and read()
5+
// URL: https://github.com/RobTillaart/MCP3424
6+
7+
#include "MCP3424.h"
8+
9+
MCP3428 mcp;
10+
11+
12+
void setup()
13+
{
14+
Serial.begin(115200);
15+
Serial.println();
16+
Serial.println(__FILE__);
17+
Serial.print("MCP3424_LIB_VERSION: ");
18+
Serial.println(MCP3424_LIB_VERSION);
19+
Serial.println();
20+
21+
Wire.begin();
22+
Wire.setTimeout(10000);
23+
24+
mcp.begin();
25+
26+
// dump configuration
27+
Serial.println();
28+
Serial.print("Address:\t");
29+
Serial.println(mcp.getAddress(), HEX);
30+
Serial.print("Connect:\t");
31+
Serial.println(mcp.isConnected());
32+
Serial.print("Channels:\t");
33+
Serial.println(mcp.getMaxChannels());
34+
Serial.print("Gain:\t");
35+
Serial.println(mcp.getGain());
36+
Serial.print("Bits:\t");
37+
Serial.println(mcp.getResolution());
38+
Serial.print("Mode:\t");
39+
Serial.println(mcp.getMode());
40+
Serial.println();
41+
42+
}
43+
44+
45+
void loop()
46+
{
47+
uint16_t del = mcp.getConversionDelay();
48+
49+
// sample all channels.
50+
for (int chan = 0; chan < mcp.getMaxChannels(); chan++)
51+
{
52+
mcp.setChannel(chan);
53+
// new measurement duration depends on resolution. See readme.md.
54+
delay(del);
55+
Serial.print(mcp.read());
56+
Serial.print("\t");
57+
Serial.println(mcp.readVolts(), 6);
58+
}
59+
60+
delay(1000);
61+
}
62+
63+
64+
// -- END OF FILE --

libraries/MCP3424/keywords.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,10 @@ setChannel KEYWORD2
2525
getChannel KEYWORD2
2626
setGain KEYWORD2
2727
getGain KEYWORD2
28+
2829
setResolution KEYWORD2
2930
getResolution KEYWORD2
31+
getConversionDelay KEYWORD2
3032

3133
setContinuousMode KEYWORD2
3234
setSingleShotMode KEYWORD2

libraries/MCP3424/library.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"type": "git",
1616
"url": "https://github.com/RobTillaart/MCP3424.git"
1717
},
18-
"version": "0.1.1",
18+
"version": "0.1.2",
1919
"license": "MIT",
2020
"frameworks": "*",
2121
"platforms": "*",

libraries/MCP3424/library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=MCP3424
2-
version=0.1.1
2+
version=0.1.2
33
author=Rob Tillaart <[email protected]>
44
maintainer=Rob Tillaart <[email protected]>
55
sentence=Arduino library for 18 bit ADC I2C MCP3424 et al.

0 commit comments

Comments
 (0)