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