Skip to content

Commit

Permalink
update library.json, license, minor edits, unit test (#35)
Browse files Browse the repository at this point in the history
  • Loading branch information
RobTillaart authored Dec 11, 2021
1 parent fcd6ef6 commit 18ec9a1
Show file tree
Hide file tree
Showing 25 changed files with 168 additions and 32 deletions.
37 changes: 29 additions & 8 deletions ADS1X15.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//
// FILE: ADS1X15.cpp
// AUTHOR: Rob Tillaart
// VERSION: 0.3.3
// VERSION: 0.3.4
// DATE: 2013-03-24
// PUPROSE: Arduino library for ADS1015 and ADS1115
// URL: https://github.com/RobTillaart/ADS1X15
Expand All @@ -22,6 +22,8 @@
// 0.3.1 2021-04-25 #22, add get/setClock() for Wire speed + reset()
// 0.3.2 2021-10-07 fix build-CI; update readme + add new examples
// 0.3.3 2021-10-17 update build-CI (esp32), readme.md, keywords.txt
// 0.3.4 2021-12-11 update library.json, license, minor edits incl layout)
// add unit test constants.


#include "ADS1X15.h"
Expand Down Expand Up @@ -102,7 +104,7 @@ differs for different devices, check datasheet or readme.md
#define ADS1X15_COMP_QUE_1_CONV 0x0000 // trigger alert after 1 convert
#define ADS1X15_COMP_QUE_2_CONV 0x0001 // trigger alert after 2 converts
#define ADS1X15_COMP_QUE_4_CONV 0x0002 // trigger alert after 4 converts
#define ADS1X15_COMP_QUE_NONE 0x0003 // dosable comparator
#define ADS1X15_COMP_QUE_NONE 0x0003 // disable comparator


// _CONFIG masks
Expand Down Expand Up @@ -230,21 +232,21 @@ uint8_t ADS1X15::getGain()
}


float ADS1X15::toVoltage(int16_t val)
float ADS1X15::toVoltage(int16_t value)
{
if (val == 0) return 0;
if (value == 0) return 0;

float volts = getMaxVoltage();
if (volts < 0) return volts;

volts *= val;
volts *= value;
if (_config & ADS_CONF_RES_16)
{
volts /= 32767; // val = 16 bits - sign bit = 15 bits mantissa
volts /= 32767; // value = 16 bits - sign bit = 15 bits mantissa
}
else
{
volts /= 2047; // val = 12 bits - sign bit = 11 bit mantissa
volts /= 2047; // value = 12 bits - sign bit = 11 bit mantissa
}
return volts;
}
Expand Down Expand Up @@ -418,6 +420,7 @@ int16_t ADS1X15::_readADC(uint16_t readmode)
return getValue();
}


void ADS1X15::_requestADC(uint16_t readmode)
{
// write to register is needed in continuous mode as other flags can be changed
Expand All @@ -436,6 +439,7 @@ void ADS1X15::_requestADC(uint16_t readmode)
_writeRegister(_address, ADS1X15_REG_CONFIG, config);
}


bool ADS1X15::_writeRegister(uint8_t address, uint8_t reg, uint16_t value)
{
_wire->beginTransmission(address);
Expand All @@ -445,6 +449,7 @@ bool ADS1X15::_writeRegister(uint8_t address, uint8_t reg, uint16_t value)
return (_wire->endTransmission() == 0);
}


uint16_t ADS1X15::_readRegister(uint8_t address, uint8_t reg)
{
_wire->beginTransmission(address);
Expand Down Expand Up @@ -506,43 +511,49 @@ ADS1015::ADS1015(uint8_t address, TwoWire *wire)
_maxPorts = 4;
}


int16_t ADS1015::readADC_Differential_0_3()
{
return _readADC(ADS1X15_MUX_DIFF_0_3);
}


int16_t ADS1015::readADC_Differential_1_3()
{
return _readADC(ADS1X15_MUX_DIFF_1_3);
}


int16_t ADS1015::readADC_Differential_2_3()
{
return _readADC(ADS1X15_MUX_DIFF_2_3);
}


int16_t ADS1015::readADC_Differential_0_2()
{
return readADC(2) - readADC(0);
}


int16_t ADS1015::readADC_Differential_1_2()
{
return readADC(2) - readADC(1);;
}



void ADS1015::requestADC_Differential_0_3()
{
_requestADC(ADS1X15_MUX_DIFF_0_3);
}


void ADS1015::requestADC_Differential_1_3()
{
_requestADC(ADS1X15_MUX_DIFF_1_3);
}


void ADS1015::requestADC_Differential_2_3()
{
_requestADC(ADS1X15_MUX_DIFF_2_3);
Expand Down Expand Up @@ -593,44 +604,54 @@ ADS1115::ADS1115(uint8_t address, TwoWire *wire)
_maxPorts = 4;
}


int16_t ADS1115::readADC_Differential_0_3()
{
return _readADC(ADS1X15_MUX_DIFF_0_3);
}


int16_t ADS1115::readADC_Differential_1_3()
{
return _readADC(ADS1X15_MUX_DIFF_1_3);
}


int16_t ADS1115::readADC_Differential_2_3()
{
return _readADC(ADS1X15_MUX_DIFF_2_3);
}


int16_t ADS1115::readADC_Differential_0_2()
{
return readADC(2) - readADC(0);
}


int16_t ADS1115::readADC_Differential_1_2()
{
return readADC(2) - readADC(1);;
}


void ADS1115::requestADC_Differential_0_3()
{
_requestADC(ADS1X15_MUX_DIFF_0_3);
}


void ADS1115::requestADC_Differential_1_3()
{
_requestADC(ADS1X15_MUX_DIFF_1_3);
}


void ADS1115::requestADC_Differential_2_3()
{
_requestADC(ADS1X15_MUX_DIFF_2_3);
}


// --- END OF FILE

6 changes: 3 additions & 3 deletions ADS1X15.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// FILE: ADS1X15.H
// AUTHOR: Rob Tillaart
// VERSION: 0.3.3
// VERSION: 0.3.4
// DATE: 2013-03-24
// PUPROSE: Arduino library for ADS1015 and ADS1115
// URL: https://github.com/RobTillaart/ADS1X15
Expand All @@ -12,7 +12,7 @@
#include "Arduino.h"
#include "Wire.h"

#define ADS1X15_LIB_VERSION (F("0.3.3"))
#define ADS1X15_LIB_VERSION (F("0.3.4"))

// allow compile time default address
// address in { 0x48, 0x49, 0x4A, 0x4B }, no test...
Expand Down Expand Up @@ -53,7 +53,7 @@ class ADS1X15
uint8_t getGain(); // 0xFF == invalid gain error.

// both may return ADS1X15_INVALID_VOLTAGE if the gain is invalid.
float toVoltage(int16_t val = 1); // converts raw to voltage
float toVoltage(int16_t value = 1); // converts raw to voltage
float getMaxVoltage(); // -100 == invalid voltage error

// 0 = CONTINUOUS
Expand Down
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2013-2021 Rob Tillaart
Copyright (c) 2013-2022 Rob Tillaart

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
16 changes: 10 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

# ADS1X15

Arduino library for I2C ADC ADS1015, ADS1115,
Arduino library for I2C ADC ADS1015, ADS1115, and similar.


## Description
Expand All @@ -31,7 +31,7 @@ differential measurement.

## Interface

The address of the ADS1113/4/5 is determined by to which pin the ADDR
The address of the ADS1113/4/5 is determined by to which pin the **ADDR**
is connected to:

| ADDR pin connected to | Address | Notes |
Expand All @@ -42,7 +42,7 @@ is connected to:
| SCL | 0x4B | |


- **ADS1x15()** constructor, should not be used.
- **ADS1x15()** base constructor, should not be used.
- **ADS1013(address, TwoWire \*wire = &Wire)** Constructor with device address,
and optional the Wire interface as parameter.
- **ADS1014(address, TwoWire \*wire = &Wire)** Constructor with device address,
Expand Down Expand Up @@ -170,7 +170,7 @@ in terms of code
{
if (ADS.isReady())
{
val = ADS.getValue();
value = ADS.getValue();
ADS.requestADC(pin); // request new conversion
}
// do other things here
Expand All @@ -183,6 +183,7 @@ See examples
## ReadADC Differential
For reading the ADC in a differential way there are 4 calls possible.
- **int16_t readADC_Differential_0_1()** returns the difference between 2 ADC pins.
- **int16_t readADC_Differential_0_3()** ADS1x15 only
- **int16_t readADC_Differential_1_3()** ADS1x15 only
Expand All @@ -191,6 +192,7 @@ For reading the ADC in a differential way there are 4 calls possible.
- **int16_t readADC_Differential_1_2()** ADS1x15 only - in software (no async equivalent)
The differential reading of the ADC can also be done with asynchronous calls.
- **void requestADC_Differential_0_1()** starts conversion for differential reading
- **void requestADC_Differential_0_3()** ADS1x15 only
- **void requestADC_Differential_1_3()** ADS1x15 only
Expand All @@ -199,8 +201,8 @@ The differential reading of the ADC can also be done with asynchronous calls.
After one of these calls one need to call
- **int16_t getValue()** Read the result of the last conversion.
The readiness of a CONTINUOUS conversion can only be detected by the RDY line.
Use interrupt for this, see examples.
The readiness of a CONTINUOUS conversion can only be detected by the **RDY** line.
Best to use an interrupt for this, see examples.
#### ReadADC continuous mode
Expand All @@ -226,6 +228,7 @@ See examples.
If the thresholdHigh is set to 0x0100 and the thresholdLow to 0x0000
the **ALERT/RDY** pin is triggered when a conversion is ready.
- **void setComparatorThresholdLow(int16_t lo)** writes value to device directly.
- **void setComparatorThresholdHigh(int16_t hi)** writes value to device directly.
- **int16_t getComparatorThresholdLow()** reads value from device.
Expand Down Expand Up @@ -304,6 +307,7 @@ A value of 3 (or above) effectively disables the comparator. See table below.
Depending on the comparator mode **TRADITIONAL** or **WINDOW** the thresholds registers
mean something different see - Comparator Mode above or datasheet.
- **void setComparatorThresholdLow(int16_t lo)** set the low threshold; take care the hi >= lo.
- **void setComparatorThresholdHigh(int16_t hi)** set the high threshold; take care the hi >= lo.
- **int16_t getComparatorThresholdLow()** reads value from device.
Expand Down
3 changes: 3 additions & 0 deletions examples/ADS_async_16_channel/ADS_async_16_channel.ino
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ int idx = 0;

uint32_t last = 0, now = 0;


void setup()
{
Serial.begin(115200);
Expand Down Expand Up @@ -116,4 +117,6 @@ void ADS_print_all()
Serial.println();
}


// -- END OF FILE --

3 changes: 3 additions & 0 deletions examples/ADS_async_8_channel/ADS_async_8_channel.ino
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ int idx = 0;

uint32_t lastTime = 0;


void setup()
{
Serial.begin(115200);
Expand Down Expand Up @@ -133,4 +134,6 @@ void ADS_print_all()
// Serial.println();
}


// -- END OF FILE --

4 changes: 4 additions & 0 deletions examples/ADS_async_differential/ADS_async_differential.ino
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

#include "ADS1X15.h"


// choose you sensor
// ADS1013 ADS(0x48);
// ADS1014 ADS(0x48);
Expand Down Expand Up @@ -51,6 +52,7 @@ void setup()
ADS.requestADC_Differential_0_1();
}


void loop()
{
if (handleConversion() == true)
Expand Down Expand Up @@ -92,4 +94,6 @@ bool handleConversion()
return false; // default not all read
}


// -- END OF FILE --

7 changes: 6 additions & 1 deletion examples/ADS_continuous/ADS_continuous.ino
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

#include "ADS1X15.h"


// choose you sensor
// ADS1013 ADS(0x48);
// ADS1014 ADS(0x48);
Expand All @@ -39,9 +40,13 @@ void setup()
ADS.readADC(0); // first read to trigger
}


void loop()
{
Serial.println(ADS.getValue());
// optional other code here
}

// -- END OF FILE --

// -- END OF FILE --

Loading

0 comments on commit 18ec9a1

Please sign in to comment.