秋月電子、スイッチサイエンス、Aliexpressなどで販売されているADS1015ブレークアウト基板を使ってみた。
ADCはAVR内蔵のものしか使ってみたことがないがADS1015はかなり安定しているように見える。これはいいかも。基準電源を内蔵しているので部品点数も少なくて済む。但し、I2Cなので高速サンプリングは期待できないが...って、それは気にしないことにして、いつものようにライブラリを作ってみた。ADS1015とADS1115の両方に対応してみた。
【続編】
ADS1015/ADS1115ライブラリを改良してみた。
【サンプル・スケッチ】
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
#include <Wire.h> #include "ADS1015.h" ADS1X15<> adc; void setup() { Serial.begin(115200); while (!Serial) continue; Wire.begin(); Wire.setClock(400000); // 400KHz adc.mux(ADS1X15_MUX_AIN0_GND); adc.pga(ADS1X15_PGA_6144V); adc.begin(); } void loop() { static int32_t next; switch (adc.handle()) { case ADS1X15_STATUS_NO_DEVICE: if (millis() - next >= 1000) { next += 1000; Serial.println("no device."); } break; case ADS1X15_STATUS_READY: if (millis() - next >= 1000) { next += 1000; Serial.print(adc.voltage()); Serial.println(" uV"); } break; default: break; } } |
【ライブラリ】
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
/* * ADC Library for ADS1015 * * Sasapea's Lab: 2019-11-10 */ #ifndef __ADS1015_H #define __ADS1015_H #define ADS1015 #define ADS1X15_RESOLUSION 12 #define ADS1X15_DR_DEFAULT ADS1X15_DR_1600SPS typedef enum { ADS1X15_PGA_6144V, ADS1X15_PGA_4096V, ADS1X15_PGA_2048V, ADS1X15_PGA_1024V, ADS1X15_PGA_0512V, ADS1X15_PGA_0256V, ADS1X15_PGA_0128V, } ADS1X15_PGA; #define ADS1X15_REFERENCES \ { 6144000, 4096000, 2048000, 1024000, 512000, 256000, 128000 } typedef enum { ADS1X15_DR_128SPS, ADS1X15_DR_250SPS, ADS1X15_DR_490SPS, ADS1X15_DR_920SPS, ADS1X15_DR_1600SPS, ADS1X15_DR_2400SPS, ADS1X15_DR_3300SPS, } ADS1X15_DR; #define ADS1X15_CONVERSION_TIMES \ { 1000127 / 128, 1000249 / 250, 1000489 / 490, 1000919 / 920, \ 1001599 / 1600, 1002399 / 2400, 1003299 / 3300 } #include "ADS1X15.h" #endif |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
/* * ADC Library for ADS1115 * * Sasapea's Lab: 2019-11-10 */ #ifndef __ADS1115_H #define __ADS1115_H #define ADS1115 #define ADS1X15_RESOLUSION 16 #define ADS1X15_DR_DEFAULT ADS1X15_DR_128SPS typedef enum { ADS1X15_PGA_6144V, ADS1X15_PGA_4096V, ADS1X15_PGA_2048V, ADS1X15_PGA_1024V, ADS1X15_PGA_0512V, ADS1X15_PGA_0256V, } ADS1X15_PGA; #define ADS1X15_REFERENCES \ { 6144000, 4096000, 2048000, 1024000, 512000, 256000 } typedef enum { ADS1X15_DR_8SPS, ADS1X15_DR_16SPS, ADS1X15_DR_32SPS, ADS1X15_DR_64SPS, ADS1X15_DR_128SPS, ADS1X15_DR_250SPS, ADS1X15_DR_475SPS, ADS1X15_DR_860SPS, } ADS1X15_DR; #define ADS1X15_CONVERSION_TIMES \ { 1000007 / 8, 1000015 / 16, 1000031 / 32, 1000063 / 64, \ 1000127 / 128, 1000249 / 250, 1000474 / 475, 1000859 / 860 } #include "ADS1X15.h" #endif |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 |
/* * ADC Library for ADS1X15 * * Sasapea's Lab: 2019-11-10 */ #ifndef __ADS1X15_H #define __ADS1X15_H #include <stdint.h> #include <stdbool.h> #ifndef ARDUINO #define makeWord(h, l) (((uint16_t)h << 8) | (uint8_t)l) #endif #ifndef lowByte #define lowByte(w) (uint8_t)(w) #endif #ifndef highByte #define highByte(w) (uint8_t)((w) >> 8) #endif #define ADS1X15_DEVICE_ADDRESS 0x48 #define ADS1X15_RETRY_INTERVAL 1000000 typedef enum { ADS1X15_STATUS_NO_DEVICE, ADS1X15_STATUS_IDLE, ADS1X15_STATUS_BUSY, ADS1X15_STATUS_READY, } ADS1X15_STATUS; typedef enum { ADS1X15_REG_CONVERSION, ADS1X15_REG_CONFIG, ADS1X15_REG_LO_THRESH, ADS1X15_REG_HI_THRESH, } ADS1X15_REG; typedef enum { ADS1X15_OS_NONE, ADS1X15_OS_CONVERSION, } ADS1X15_OS; typedef enum { ADS1X15_MUX_AIN0_AIN1, ADS1X15_MUX_AIN0_AIN3, ADS1X15_MUX_AIN1_AIN3, ADS1X15_MUX_AIN2_AIN3, ADS1X15_MUX_AIN0_GND, ADS1X15_MUX_AIN1_GND, ADS1X15_MUX_AIN2_GND, ADS1X15_MUX_AIN3_GND, } ADS1X15_MUX; typedef enum { ADS1X15_MODE_CONTINUOUS, ADS1X15_MODE_POWER_DOWN, } ADS1X15_MODE; typedef enum { ADS1X15_COMP_MODE_TRADITIONAL, ADS1X15_COMP_MODE_WINDOW, } ADS1X15_COMP_MODE; typedef enum { ADS1X15_COMP_POL_ACTIVE_LOW, ADS1X15_COMP_POL_ACTIVE_HIGH, } ADS1X15_COMP_POL; typedef enum { ADS1X15_COMP_LAT_DISABLE, ADS1X15_COMP_LAT_ENABLE, } ADS1X15_COMP_LAT; typedef enum { ADS1X15_COMP_QUE_ONE, ADS1X15_COMP_QUE_TWO, ADS1X15_COMP_QUE_FOUR, ADS1X15_COMP_QUE_DISABLE, } ADS1X15_COMP_QUE; typedef union { struct { ADS1X15_COMP_QUE comp_que :2; ADS1X15_COMP_LAT comp_lat :1; ADS1X15_COMP_POL comp_pol :1; ADS1X15_COMP_MODE comp_mode:1; ADS1X15_DR dr :3; ADS1X15_MODE mode :1; ADS1X15_PGA pga :3; ADS1X15_MUX mux :3; ADS1X15_OS os :1; } reg; uint16_t val; } ADS1X15_CONFIG; template<typename WIRE = TwoWire, WIRE& WOBJ = Wire> class ADS1X15 { private: uint8_t _addr; int32_t _value; uint32_t _start; ADS1X15_CONFIG _config; ADS1X15_CONFIG _active; ADS1X15_STATUS _status; uint8_t write(ADS1X15_REG reg, uint16_t data) { WOBJ.beginTransmission(_addr); WOBJ.write(reg); WOBJ.write(highByte(data)); WOBJ.write(lowByte(data)); return WOBJ.endTransmission(); } uint8_t read(ADS1X15_REG reg, uint16_t *data) { WOBJ.beginTransmission(_addr); WOBJ.write(reg); uint8_t rv = WOBJ.endTransmission(false); if (rv == 0) { if (WOBJ.requestFrom(_addr, sizeof(*data)) == sizeof(*data)) { uint8_t high = WOBJ.read(); uint8_t low = WOBJ.read(); *data = makeWord(high, low); } else rv = 4; } return rv; } uint8_t start(void) { uint8_t rv = write(ADS1X15_REG_CONFIG, _config.val); if (rv == 0) _active = _config; return rv; } uint8_t fetch(void) { uint16_t val; uint8_t rv = read(ADS1X15_REG_CONVERSION, &val); if (rv == 0) { if (_active.reg.mux < ADS1X15_MUX_AIN0_GND) _value = (int16_t)val; else _value = val; } return rv; } void os(ADS1X15_OS value) { _config.reg.os = value; } void mode(ADS1X15_MODE value) { _config.reg.mode = value; } public: ADS1X15(uint8_t addr = ADS1X15_DEVICE_ADDRESS) : _addr(addr) , _value(0) , _start(0) , _status(ADS1X15_STATUS_NO_DEVICE) { const ADS1X15_CONFIG _DEFAULT = { ADS1X15_COMP_QUE_DISABLE, ADS1X15_COMP_LAT_DISABLE, ADS1X15_COMP_POL_ACTIVE_LOW, ADS1X15_COMP_MODE_TRADITIONAL, ADS1X15_DR_DEFAULT, ADS1X15_MODE_POWER_DOWN, ADS1X15_PGA_2048V, ADS1X15_MUX_AIN0_AIN1, ADS1X15_OS_CONVERSION, }; _config = _active = _DEFAULT; } void mux(ADS1X15_MUX value) { _config.reg.mux = value; } void pga(ADS1X15_PGA value) { _config.reg.pga = value; } void dr(ADS1X15_DR value) { _config.reg.dr = value; } void comp_mode(ADS1X15_COMP_MODE value) { _config.reg.comp_mode = value; } void comp_pol(ADS1X15_COMP_POL value) { _config.reg.comp_pol = value; } void comp_lat(ADS1X15_COMP_LAT value) { _config.reg.comp_lat = value; } void comp_que(ADS1X15_COMP_QUE value) { _config.reg.comp_que = value; } uint8_t resolution() { return ADS1X15_RESOLUSION; } uint32_t reference() { static const uint32_t REFERENCES[] = ADS1X15_REFERENCES; return REFERENCES[_active.reg.pga]; } int32_t measured_value() { return _value; } int32_t voltage(void) { return (int32_t)((double)measured_value() * reference() / 32768); } void begin() { _start = micros() - ADS1X15_RETRY_INTERVAL; } ADS1X15_STATUS handle(void) { static const uint32_t CONVERSION_TIMES[] = ADS1X15_CONVERSION_TIMES; switch (_status) { default: case ADS1X15_STATUS_NO_DEVICE: if (micros() - _start < ADS1X15_RETRY_INTERVAL) break; case ADS1X15_STATUS_IDLE: _status = (start() ? ADS1X15_STATUS_NO_DEVICE : ADS1X15_STATUS_BUSY); _start = micros(); break; case ADS1X15_STATUS_BUSY: if (micros() - _start < CONVERSION_TIMES[_active.reg.dr]) break; if (fetch() == 0) { _status = ADS1X15_STATUS_IDLE; return ADS1X15_STATUS_READY; } _start = micros(); _status = ADS1X15_STATUS_NO_DEVICE; break; } return _status; } }; #endif |