Problem reading ADC when OLED is activated

I’m testing an sketch for WiFi Kit 32, that reads signal from two STC-013 current transformers via and ADC ADS1115, which uses I2C to connect Wiki Kit. I can see reads on serial monitor but if I activate display, signal from SCT’s is wrong, it’s continuously the same for both. I guess there’s some kind of conflict between ADS and OLED, but I don’t know how to avoid it. This is the code I’m using:
#include <Adafruit_ADS1015.h>

Adafruit_ADS1115 ads;

#include <SSD1306Wire.h>
SSD1306Wire display(0x3c, 4, 15);
const float FACTOR = 17.1;
const float multiplier = 0.0625F;

// Definir variables.
uint8_t pwm_porcentaje = 0;

// Set up the pwm output
uint8_t pin_pwm = 25;
uint16_t output_pwm = 0;          // a value from 0 to 65535 representing the pwm value

float getCorriente_fv() {
    
    float voltaje_fv;
    float corriente_fv;
    float sum_fv = 0;
    long tiempo_fv = millis();
    int counter_fv = 0;
 
    while (millis() - tiempo_fv < 1000) {
        
        voltaje_fv = ads.readADC_Differential_0_1() * multiplier;
        corriente_fv = voltaje_fv * FACTOR;
        corriente_fv /= 1000.0;

        sum_fv += sq(corriente_fv);
        counter_fv = counter_fv + 1;
    }
 
 corriente_fv = sqrt(sum_fv / counter_fv);
 return(corriente_fv);
 
}

float getCorriente_grid() {
    
    float voltaje_grid;
    float corriente_grid;
    float sum_grid = 0;
    long tiempo_grid = millis();
    int counter_grid = 0;
 
    while (millis() - tiempo_grid < 1000) {
        
        voltaje_grid = ads.readADC_Differential_2_3() * multiplier;
        corriente_grid = voltaje_grid * FACTOR;
        corriente_grid /= 1000.0;

        sum_grid += sq(corriente_grid);
        counter_grid = counter_grid + 1;
    }
 
 corriente_grid = sqrt(sum_grid / counter_grid);
 return(corriente_grid);
 
} 

void printMeasure(String prefix, float value, String postfix) {
    
    Serial.print(prefix);
    Serial.print(value, 3);
    Serial.println(postfix);
 
}

void setup() {

     // SERIAL    
    Serial.begin(115200);
    Serial.println("Arrancando...");
  
  //ADS1115
    ads.setGain(GAIN_ONE);
    ads.begin();

     // OLED
    pinMode(25,OUTPUT);   // pin que va a enviar la señal PWM al dimmer
    pinMode(16,OUTPUT);
    digitalWrite(16, LOW);    // set GPIO16 low to reset OLED
    delay(50);
    digitalWrite(16, HIGH); // while OLED is running, must set GPIO16 in high
    
  //Start display
//    display.init(); Cuando activo esta línea dejan de medir bien las pinzas, las dos marcan 215 W continuamente.
//    display.flipScreenVertically();
//    display.setFont(ArialMT_Plain_24);
//    display.clear();
//    display.setTextAlignment(TEXT_ALIGN_LEFT);
//    display.drawString(2, 5, "Arrancando");
//    display.drawString(0, 35, "derivador...");
//    display.display();
//
//    delay(2000);
           
    // PWM
    
    // Initialize channels 
    // channels 0-15, resolution 1-16 bits, freq limits depend on resolution
    ledcSetup(0, 1000, 16); // 1 kHz PWM, 8-bit resolution
    ledcAttachPin(pin_pwm, 0); // assign pins to chanel
    ledcWrite(0, output_pwm);  // Write new pwm value

}

void loop() {
  
    float currentRMS_fv = getCorriente_fv();
    float power_fv = 235.0 * currentRMS_fv;
  
    float currentRMS_grid = getCorriente_grid();
    float power_grid = 235.0 * currentRMS_grid;

    float power_exced = (power_fv - power_grid);

    printMeasure("Irms_fv: ", currentRMS_fv, "A ,");
    printMeasure("Power_fv: ", power_fv, "W");
    
    printMeasure("Irms_grid: ", currentRMS_grid, "A ,");
    printMeasure("Power_grid: ", power_grid, "W");
    Serial.println();

//   display.clear();
//   display.drawString(0, 0, (String)(int)(pwm_porcentaje)+"%");
//   display.drawString(30, 30, (String)(int)(pwm_porcentaje * 15)+" W");
//   display.display();

    Serial.print("%: ");
    Serial.println(pwm_porcentaje);
    
    delay(1000);

}

If I uncomment Start display section in setup I no longer get good values from ADS1115
Pins are connected as follows:
ADS1115 WiFi Kit
Vdd 5V
GND GND
SCL 22
SDA 21
ADDR GND

You can try to initialize ads.setGain (GAIN_ONE), ads.begin () of ADS1115,
Add it to void loop () and initialize it every time you read the data.

Hi Quency-D
Thanks for your suggestion. I did it, but I’m afraid it didn’t help

Maybe you can read the data first and add i2c_san to the loop to see if both I2Cs are successfully initialized after initialization.

/* Heltec Automation I2C scanner example (also it’s a basic example how to use I2C1)
*

  • ESP32 have two I2C (I2C0 and I2C1) bus
  • OLED is connected to I2C0, so if scan with Wire (I2C0), the return address should be 0x3C.
  • If you need scan other device address in I2C1…
  •  - Comment all Wire.***() codes;
    
  •  - Uncomment all Wire1.***() codes;
    
  • I2C scan example and I2C0
  • HelTec AutoMation, Chengdu, China
  • 成都惠利特自动化科技有限公司
  • www.heltec.org
  • this project also realess in GitHub:
  • https://github.com/HelTecAutomation/Heltec_ESP32
  • */

#include “Arduino.h”
#include “heltec.h”

void setup()
{
Heltec.begin(true, false, true);
Wire.begin(SDA_OLED, SCL_OLED); //Scan OLED’s I2C address via I2C0
//Wire1.begin(SDA, SCL); //If there have other device on I2C1, scan the device address via I2C1
}

void loop()
{
byte error, address;
int nDevices;

Serial.println("Scanning...");

nDevices = 0;
for(address = 1; address < 127; address++ )
{
	Wire.beginTransmission(address);
	error = Wire.endTransmission();

// Wire1.beginTransmission(address);
// error = Wire1.endTransmission();

	if (error == 0)
	{
		Serial.print("I2C device found at address 0x");
		if (address<16)
		Serial.print("0");
		Serial.print(address,HEX);
		Serial.println("  !");

		nDevices++;
	}
	else if (error==4)
	{
		Serial.print("Unknown error at address 0x");
		if (address<16)
			Serial.print("0");
		Serial.println(address,HEX);
	}
}
if (nDevices == 0)
Serial.println("No I2C devices found\n");
else
Serial.println("done\n");

delay(5000);

}

I have added I2C_scan code to my sketch (modified to scan both buses) and it seems they are correctly configured. Despite of that both power values remain the same and they don’t change over time:
Scanning…
I2C0 device found at address 0x3C !
done

I2C1 device found at address 0x48  !
done

Irms_fv: 0.001A ,
Power_fv: 0.251W
Irms_grid: 0.001A ,
Power_grid: 0.251W