Advertisement
mikroavr

ads1115

Oct 17th, 2022 (edited)
1,254
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <Adafruit_ADS1X15.h>
  2.  
  3. Adafruit_ADS1115 ads;  /* Use this for the 16-bit version */
  4. //Adafruit_ADS1015 ads;     /* Use this for the 12-bit version */
  5.  
  6. void setup(void)
  7. {
  8.   Serial.begin(115200);
  9.   Serial.println("Hello!");
  10.  
  11.   Serial.println("Getting single-ended readings from AIN0..3");
  12.   Serial.println("ADC Range: +/- 6.144V (1 bit = 3mV/ADS1015, 0.1875mV/ADS1115)");
  13.  
  14.   // The ADC input range (or gain) can be changed via the following
  15.   // functions, but be careful never to exceed VDD +0.3V max, or to
  16.   // exceed the upper and lower limits if you adjust the input range!
  17.   // Setting these values incorrectly may destroy your ADC!
  18.   //                                                                ADS1015  ADS1115
  19.   //                                                                -------  -------
  20.   // ads.setGain(GAIN_TWOTHIRDS);  // 2/3x gain +/- 6.144V  1 bit = 3mV      0.1875mV (default)
  21.   // ads.setGain(GAIN_ONE);        // 1x gain   +/- 4.096V  1 bit = 2mV      0.125mV
  22.   // ads.setGain(GAIN_TWO);        // 2x gain   +/- 2.048V  1 bit = 1mV      0.0625mV
  23.   // ads.setGain(GAIN_FOUR);       // 4x gain   +/- 1.024V  1 bit = 0.5mV    0.03125mV
  24.   // ads.setGain(GAIN_EIGHT);      // 8x gain   +/- 0.512V  1 bit = 0.25mV   0.015625mV
  25.   // ads.setGain(GAIN_SIXTEEN);    // 16x gain  +/- 0.256V  1 bit = 0.125mV  0.0078125mV
  26.  
  27.   if (!ads.begin(0x48)) {
  28.     Serial.println("Failed to initialize ADS.");
  29.     while (1);
  30.   }
  31. }
  32.  
  33. void loop(void)
  34. {
  35.   int16_t adc0, adc1, adc2, adc3;
  36.   float volts0, volts1, volts2, volts3;
  37.  
  38.   adc0 = ads.readADC_SingleEnded(0);
  39.   adc1 = ads.readADC_SingleEnded(1);
  40.   adc2 = ads.readADC_SingleEnded(2);
  41.   adc3 = ads.readADC_SingleEnded(3);
  42.  
  43.   volts0 = ads.computeVolts(adc0);
  44.   volts1 = ads.computeVolts(adc1);
  45.   volts2 = ads.computeVolts(adc2);
  46.   volts3 = ads.computeVolts(adc3);
  47.  
  48.   Serial.println("-----------------------------------------------------------");
  49.   Serial.print("AIN0: "); Serial.print(adc0); Serial.print("  "); Serial.print(volts0); Serial.println("V");
  50.   Serial.print("AIN1: "); Serial.print(adc1); Serial.print("  "); Serial.print(volts1); Serial.println("V");
  51.   Serial.print("AIN2: "); Serial.print(adc2); Serial.print("  "); Serial.print(volts2); Serial.println("V");
  52.   Serial.print("AIN3: "); Serial.print(adc3); Serial.print("  "); Serial.print(volts3); Serial.println("V");
  53.  
  54.   delay(1000);
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement