Advertisement
microrobotics

Arduino ADS1115

Aug 14th, 2024
1,579
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <Wire.h>
  2. #include <Adafruit_ADS1X15.h>
  3.  
  4. // Create an instance of the ADS1115 class
  5. Adafruit_ADS1115 ads;
  6.  
  7. // Global variables to set expected voltages
  8. float expectedVoltageA0 = 3.3; // Expected voltage on A0
  9. float expectedVoltageA1 = 5.0; // Expected voltage on A1
  10. float expectedVoltageA2 = 0.0; // Expected voltage on A2 (Set to 0 if not used)
  11. float expectedVoltageA3 = 0.0; // Expected voltage on A3 (Set to 0 if not used)
  12.  
  13. // Uncomment the appropriate line based on where the ADDR pin is connected
  14. // #define ADS1115_ADDRESS 0x48  // ADDR pin connected to GND
  15. // #define ADS1115_ADDRESS 0x49  // ADDR pin connected to VDD
  16. // #define ADS1115_ADDRESS 0x4A  // ADDR pin connected to SDA
  17. // #define ADS1115_ADDRESS 0x4B  // ADDR pin connected to SCL
  18.  
  19. // Function to determine the appropriate gain setting based on expected voltage
  20. adsGain_t chooseGain(float maxVoltage) {
  21.   if (maxVoltage <= 0.256) {
  22.     return GAIN_SIXTEEN; // ±0.256V
  23.   } else if (maxVoltage <= 0.512) {
  24.     return GAIN_EIGHT; // ±0.512V
  25.   } else if (maxVoltage <= 1.024) {
  26.     return GAIN_FOUR; // ±1.024V
  27.   } else if (maxVoltage <= 2.048) {
  28.     return GAIN_TWO; // ±2.048V
  29.   } else if (maxVoltage <= 4.096) {
  30.     return GAIN_ONE; // ±4.096V
  31.   } else {
  32.     return GAIN_TWOTHIRDS; // ±6.144V
  33.   }
  34. }
  35.  
  36. void setup(void)
  37. {
  38.   Serial.begin(9600);
  39.   Wire.begin(); // Start I2C communication
  40.  
  41.   if (!ads.begin(ADS1115_ADDRESS)) {
  42.     Serial.println("Failed to initialize ADS1115!");
  43.     while (1);
  44.   }
  45.  
  46.   // Choose the correct gain based on the highest expected voltage
  47.   float maxExpectedVoltage = max(max(expectedVoltageA0, expectedVoltageA1),
  48.                                  max(expectedVoltageA2, expectedVoltageA3));
  49.   ads.setGain(chooseGain(maxExpectedVoltage));
  50.  
  51.   Serial.println("ADS1115 initialized successfully.");
  52. }
  53.  
  54. void loop(void)
  55. {
  56.   int16_t adc0, adc1, adc2, adc3;
  57.   float voltage0, voltage1, voltage2, voltage3;
  58.  
  59.   // Read ADC values from each channel
  60.   adc0 = ads.readADC_SingleEnded(0); // Reading from A0
  61.   delay(5); // Small delay to ensure stable reading
  62.   adc1 = ads.readADC_SingleEnded(1); // Reading from A1
  63.   delay(5);
  64.   adc2 = ads.readADC_SingleEnded(2); // Reading from A2
  65.   delay(5);
  66.   adc3 = ads.readADC_SingleEnded(3); // Reading from A3
  67.   delay(5);
  68.  
  69.   // Get the selected gain for voltage conversion
  70.   float gainFactor = 0.0;
  71.   switch (ads.getGain()) {
  72.     case GAIN_TWOTHIRDS: gainFactor = 0.1875; break; // ±6.144V
  73.     case GAIN_ONE: gainFactor = 0.125; break; // ±4.096V
  74.     case GAIN_TWO: gainFactor = 0.0625; break; // ±2.048V
  75.     case GAIN_FOUR: gainFactor = 0.03125; break; // ±1.024V
  76.     case GAIN_EIGHT: gainFactor = 0.015625; break; // ±0.512V
  77.     case GAIN_SIXTEEN: gainFactor = 0.0078125; break; // ±0.256V
  78.   }
  79.  
  80.   // Convert ADC readings to voltage
  81.   voltage0 = adc0 * gainFactor / 1000.0; // Convert mV to V
  82.   voltage1 = adc1 * gainFactor / 1000.0;
  83.   voltage2 = adc2 * gainFactor / 1000.0;
  84.   voltage3 = adc3 * gainFactor / 1000.0;
  85.  
  86.   // Print the voltage values
  87.   Serial.print("A0 Voltage: "); Serial.print(voltage0); Serial.println(" V");
  88.   Serial.print("A1 Voltage: "); Serial.print(voltage1); Serial.println(" V");
  89.   Serial.print("A2 Voltage: "); Serial.print(voltage2); Serial.println(" V");
  90.   Serial.print("A3 Voltage: "); Serial.print(voltage3); Serial.println(" V");
  91.  
  92.   delay(1000);
  93. }
  94.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement