ADS1115_differential.ino 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. // I2C device class (I2Cdev) demonstration Arduino sketch for ADS1115 class
  2. // Example of reading two differential inputs of the ADS1115 and showing the value in mV
  3. // 06 May 2013 by Frederick Farzanegan (frederick1@farzanegan.org)
  4. // Updates should (hopefully) always be available at https://github.com/jrowberg/i2cdevlib
  5. //
  6. // Changelog:
  7. // 2013-05-13 - initial release
  8. /* ============================================
  9. I2Cdev device library code is placed under the MIT license
  10. Copyright (c) 2011 Jeff Rowberg
  11. Permission is hereby granted, free of charge, to any person obtaining a copy
  12. of this software and associated documentation files (the "Software"), to deal
  13. in the Software without restriction, including without limitation the rights
  14. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  15. copies of the Software, and to permit persons to whom the Software is
  16. furnished to do so, subject to the following conditions:
  17. The above copyright notice and this permission notice shall be included in
  18. all copies or substantial portions of the Software.
  19. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. THE SOFTWARE.
  26. ===============================================
  27. */
  28. #include "ADS1115.h"
  29. ADS1115 adc0(ADS1115_DEFAULT_ADDRESS);
  30. void setup() {
  31. Wire.begin(); // join I2C bus
  32. Serial.begin(19200); // initialize serial communication
  33. Serial.println("Initializing I2C devices...");
  34. adc0.initialize(); // initialize ADS1115 16 bit A/D chip
  35. Serial.println("Testing device connections...");
  36. Serial.println(adc0.testConnection() ? "ADS1115 connection successful" : "ADS1115 connection failed");
  37. // To get output from this method, you'll need to turn on the
  38. //#define ADS1115_SERIAL_DEBUG // in the ADS1115.h file
  39. adc0.showConfigRegister();
  40. // We're going to do continuous sampling
  41. adc0.setMode(ADS1115_MODE_CONTINUOUS);
  42. }
  43. void loop() {
  44. // Sensor is on P0/N1 (pins 4/5)
  45. Serial.println("Sensor 1 ************************");
  46. // Set the gain (PGA) +/- 1.024v
  47. adc0.setGain(ADS1115_PGA_1P024);
  48. // Get the number of counts of the accumulator
  49. Serial.print("Counts for sensor 1 is:");
  50. // The below method sets the mux and gets a reading.
  51. int sensorOneCounts=adc0.getConversionP0N1(); // counts up to 16-bits
  52. Serial.println(sensorOneCounts);
  53. // To turn the counts into a voltage, we can use
  54. Serial.print("Voltage for sensor 1 is:");
  55. Serial.println(sensorOneCounts*adc0.getMvPerCount());
  56. Serial.println();
  57. // 2nd sensor is on P2/N3 (pins 6/7)
  58. Serial.println("Sensor 2 ************************");
  59. // Set the gain (PGA) +/- 0.256v
  60. adc0.setGain(ADS1115_PGA_0P256);
  61. // Manually set the MUX // could have used the getConversionP* above
  62. adc0.setMultiplexer(ADS1115_MUX_P2_N3);
  63. Serial.print("Counts for sensor 2 is:");
  64. Serial.println(adc0.getConversion());
  65. Serial.print("mVoltage sensor 2 is:");
  66. Serial.println(adc0.getMilliVolts()); // Convenience method to calculate voltage
  67. Serial.println();
  68. delay(500);
  69. }