Device agnostic library for the ADS1015 (4 channel, 12-bit ADC) written in C (compatible with C++ and Arduino).
There is only 1 configuration register for all 4 channels, this means each device connected to an input channel must set the config before taking a measurement (the current config could be set up for another device/channel that just used the ADS1015).
To take a measurement, call the function ads1015_measurement_get
with parameters:
ads1015_ctx_t
ads1015_config_reg_t
uint16_t measurement
Include the header file: #include "ADS1015.h"
Create an instance of ads1015_ctx_t
and set the mandatory read_reg
and write_reg
fields to the platform specific I2C read and write functions.
Create an instance of ads1015_config_reg_t
and set the values to work with a specific device on a channel.
Call the function ads1015_measurement_get
as described under heading Taking a measurement.
int32_t platform_read(void *handle, uint8_t reg, uint8_t *bufp, uint16_t len)
{
Wire.beginTransmission(i2cAddress);
Wire.write(reg);
Wire.beginTransmission(false);
Wire.requestFrom(i2cAddress, len);
for (uint16_t x = 0; x < len; x++)
{
bufp[x] = Wire.read();
}
return 0;
}
int32_t platform_write(void *handle, uint8_t reg, uint8_t *bufp, uint16_t len)
{
Wire.beginTransmission(i2cAddress);
Wire.write(reg);
for (uint16_t x = 0; x < len; x++)
{
Wire.write(bufp[x]);
}
return Wire.endTransmission(); // Will return 0 upon success
}
...
ads1015_ctx_t dev_ctx;
dev_ctx.write_reg = platform_write;
dev_ctx.read_reg = platform_read;
//dev_ctx.handle = (void*) this; // Used in a wrapper class
...
ads1015_config_reg_t config = ADS1015_DEFAULT_CONFIG();
// customise default config above with device specific settings below
config.mux = ADS1015_MUX_0; // Input channel: 0
config.pga = ADS1015_PGA_3; // Gain: +/- 1.024V
// etc...
...
uint16_t measurement;
int32_t error_response = ads1015_measurement_get(&dev_ctx, config, &measurement);
if (error_response == 0) {
// Success, use measurement here
}
else {
// Failure, handle error here
}
Create a single wrapper class
ads1015_config_reg_t
) and change the settings if needed.Create multiple different wrapper classes
ads1015_config_reg_t
) set to a specific channel.