Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Wire.h>
- #include <Adafruit_ADS1X15.h>
- // Create an instance of the ADS1115 class
- Adafruit_ADS1115 ads;
- // Global variables to set expected voltages
- float expectedVoltageA0 = 3.3; // Expected voltage on A0
- float expectedVoltageA1 = 5.0; // Expected voltage on A1
- float expectedVoltageA2 = 0.0; // Expected voltage on A2 (Set to 0 if not used)
- float expectedVoltageA3 = 0.0; // Expected voltage on A3 (Set to 0 if not used)
- // Uncomment the appropriate line based on where the ADDR pin is connected
- // #define ADS1115_ADDRESS 0x48 // ADDR pin connected to GND
- // #define ADS1115_ADDRESS 0x49 // ADDR pin connected to VDD
- // #define ADS1115_ADDRESS 0x4A // ADDR pin connected to SDA
- // #define ADS1115_ADDRESS 0x4B // ADDR pin connected to SCL
- // Function to determine the appropriate gain setting based on expected voltage
- adsGain_t chooseGain(float maxVoltage) {
- if (maxVoltage <= 0.256) {
- return GAIN_SIXTEEN; // ±0.256V
- } else if (maxVoltage <= 0.512) {
- return GAIN_EIGHT; // ±0.512V
- } else if (maxVoltage <= 1.024) {
- return GAIN_FOUR; // ±1.024V
- } else if (maxVoltage <= 2.048) {
- return GAIN_TWO; // ±2.048V
- } else if (maxVoltage <= 4.096) {
- return GAIN_ONE; // ±4.096V
- } else {
- return GAIN_TWOTHIRDS; // ±6.144V
- }
- }
- void setup(void)
- {
- Serial.begin(9600);
- Wire.begin(); // Start I2C communication
- if (!ads.begin(ADS1115_ADDRESS)) {
- Serial.println("Failed to initialize ADS1115!");
- while (1);
- }
- // Choose the correct gain based on the highest expected voltage
- float maxExpectedVoltage = max(max(expectedVoltageA0, expectedVoltageA1),
- max(expectedVoltageA2, expectedVoltageA3));
- ads.setGain(chooseGain(maxExpectedVoltage));
- Serial.println("ADS1115 initialized successfully.");
- }
- void loop(void)
- {
- int16_t adc0, adc1, adc2, adc3;
- float voltage0, voltage1, voltage2, voltage3;
- // Read ADC values from each channel
- adc0 = ads.readADC_SingleEnded(0); // Reading from A0
- delay(5); // Small delay to ensure stable reading
- adc1 = ads.readADC_SingleEnded(1); // Reading from A1
- delay(5);
- adc2 = ads.readADC_SingleEnded(2); // Reading from A2
- delay(5);
- adc3 = ads.readADC_SingleEnded(3); // Reading from A3
- delay(5);
- // Get the selected gain for voltage conversion
- float gainFactor = 0.0;
- switch (ads.getGain()) {
- case GAIN_TWOTHIRDS: gainFactor = 0.1875; break; // ±6.144V
- case GAIN_ONE: gainFactor = 0.125; break; // ±4.096V
- case GAIN_TWO: gainFactor = 0.0625; break; // ±2.048V
- case GAIN_FOUR: gainFactor = 0.03125; break; // ±1.024V
- case GAIN_EIGHT: gainFactor = 0.015625; break; // ±0.512V
- case GAIN_SIXTEEN: gainFactor = 0.0078125; break; // ±0.256V
- }
- // Convert ADC readings to voltage
- voltage0 = adc0 * gainFactor / 1000.0; // Convert mV to V
- voltage1 = adc1 * gainFactor / 1000.0;
- voltage2 = adc2 * gainFactor / 1000.0;
- voltage3 = adc3 * gainFactor / 1000.0;
- // Print the voltage values
- Serial.print("A0 Voltage: "); Serial.print(voltage0); Serial.println(" V");
- Serial.print("A1 Voltage: "); Serial.print(voltage1); Serial.println(" V");
- Serial.print("A2 Voltage: "); Serial.print(voltage2); Serial.println(" V");
- Serial.print("A3 Voltage: "); Serial.print(voltage3); Serial.println(" V");
- delay(1000);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement