#include <Wire.h>
#include <Adafruit_ADS1X15.h>
Adafruit_ADS1115 ads; /* Use this for the 16-bit version */
void setup(void)
{
Serial.begin(9600);
// Explicitly assign standard ESP32 I2C pins (SDA=21, SCL=22)
Wire.begin(21, 22);
Serial.println("Hello!");
Serial.println("Getting single-ended readings from AIN0..3");
Serial.println("ADC Range: +/- 6.144V (1 bit = 0.1875mV/ADS1115)");
// UNCOMMENTED THIS: Sets the ceiling to 6.144V so 5V inputs don't clip
ads.setGain(GAIN_TWOTHIRDS); // 2/3x gain +/- 6.144V 1 bit = 0.1875mV (default)
// COMMENTED THIS OUT: Removed the 4.096V restriction ceiling
// ads.setGain(GAIN_ONE); // 1x gain +/- 4.096V 1 bit = 0.125mV
if (!ads.begin()) {
Serial.println("Failed to initialize ADS.");
while (1);
}
}
void loop(void)
{
// 1. Read the first potentiometer (Channel 0)
int16_t adc0 = ads.readADC_SingleEnded(0);
float voltage0 = ads.computeVolts(adc0);
// 2. Read the second potentiometer (Channel 1)
int16_t adc1 = ads.readADC_SingleEnded(1);
float voltage1 = ads.computeVolts(adc1);
// 3. Print both values on the same line so they are easy to track
Serial.print("AIN0 (Pot 1): ");
Serial.print(voltage0, 2); // Force 2 decimal places
Serial.print("V | ");
Serial.print("AIN1 (Pot 2): ");
Serial.print(voltage1, 2); // Force 2 decimal places
Serial.println("V");
delay(300); // Small delay to keep the text readable
}