Help please? Can read 2; Volt, Amp sensors separately, BUT not in same code

Hi, I'll try to be brief; I'm a noob but 2.0; -Been testing and coding 'couple of weeks now. I've managed to isolate some issues, and solved the calibration problems on ADC-ESP32 Wroom.
I had already combined code, from a page and from blynk, to make this work and send to the cloud and the blynk app. It works, not working when combined, and I totally suspect it's a code -location- issue. Or an extra/ or missing float line, or 'int' code (yes I've tried many combiations, comm out, etc.). So;
-I've moved the volt-specific code to just below the "#include" section, and also below the 'ACS712' section/.
-Can get amperage, current data with the most recent code uploaded, but not voltage.
-Can get serial feebdack from each sensor when the code is separated for each, and now have them calibrated to maybe 5%. (posting screenshots). Solved by changing the source volt level in the code, and using one ground line per sensor.
-Not getting any erros on loading the code.
-Sensors are the simple Volt divider >25v, and the ACS712, using the library from GitHub - RobTillaart/ACS712: Arduino library for ACS Current Sensor - 5A, 20A, 30A
-I tested disconnecting the ESP from USB and the sensors from source power, then reconnecting. That seemed to work when testing the ACS712 alone.
-I've changed the location several times, of the section "define SIGNAL_PIN 27" and its 7 lines, but not the lines inside 'void loop'
Apreciate any feedback!, including to go read more foum posts... I'm killing plants just reading for weeks. Thanks!

#define BLYNK_TEMPLATE_ID "TMP....."
#define BLYNK_DEVICE_NAME "test ..."
#define BLYNK_AUTH_TOKEN "RU....."

#define BLYNK_PRINT Serial
//code mixing test v1,2.27 print this to LCD

#define SIGNAL_PIN 27     //esp32 pin input analog syntax?
float adc_voltage = 0.0;  
float in_voltage = 0.0;   
float R1 = 30000.0;        
float R2 = 7500.0;        
float ref_voltage = 3.6;  //power source, chg to calibrate if divider not <1v
int adc_value = 0;
// float input_voltage = 0; // test this outside void loop, there's another instance line 82

#include <ACS712.h>
#include <Blynk.h>
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
#include <LiquidCrystal_I2C.h>

ACS712 ACS(34, 3.1, 4095, 100);

LiquidCrystal_I2C lcd(0x27, 16, 2);

BlynkTimer timer;

char auth[] = "RU.....;  //might be repeated
char ssid[] = "t.....";
char pass[] = "I.....";

unsigned long lastmillis = millis();  //changed loc. from line 26
void myTimerEvent() {                 // what does this do and del'd a '}' broke the void s.
}

void setup() {
  Serial.begin(115200);
  ACS.autoMidPoint();
  int sensorVal;  //right location? -edit to make Voltsensor work.
  Blynk.begin(auth, ssid, pass);
  timer.setInterval(5000L, myTimerEvent);
  lcd.begin(16, 2);
  lcd.init();
  lcd.backlight();
  lcd.setCursor(3, 0);
  lcd.print("Barbosa Worlds");
  lcd.setCursor(0, 1);
  lcd.print("Solar to Cloud");
  delay(1500);
  lcd.clear();  //review if necess
}

void loop() {
// adc_value = analogRead(27);  //changed
  adc_value = analogRead(SIGNAL_PIN);
  adc_voltage = (adc_value * ref_voltage) / 4096.0;  //esp32 resolution is 4096
  in_voltage = adc_voltage / (R2 / (R1 + R2));
  Serial.print("BattVolts = ");
  Serial.println(in_voltage, 2);

  {
    int mA = ACS.mA_DC();
    Serial.print("mA: ");
    Serial.println(mA);

    if (Serial.available() > 0)  // needs a ; ?
    {
      char c = Serial.read();
      if (c == '+') ACS.incMidPoint();
      if (c == '-') ACS.decMidPoint();
      if (c == '0') ACS.setMidPoint(512);
      Serial.println(ACS.getMidPoint());

      if (c == '*') ACS.setmVperAmp(ACS.getmVperAmp() * 1.05);
      if (c == '/') ACS.setmVperAmp(ACS.getmVperAmp() / 1.05);
      Serial.println(ACS.getmVperAmp());
    }
    delay(1000);

    float input_voltage = 0;  // test this!! location?

    lcd.begin(16, 2);
    lcd.init();
    //lcd.clear(); test this
    lcd.setCursor(0, 0);  //added these two up
    lcd.print("BV=");
    lcd.print(in_voltage, 2);

    // NOW EDITING SERIAL, LCD FOR ACS AMPS
    lcd.setCursor(9, 0);
    lcd.print(mA);
    lcd.print(" mAmp");

    lcd.setCursor(0, 1);  //2nd line lcd ident code v.
    lcd.print("Code mix1, V2.27");

    Blynk.run();
    timer.run();
  }
}

Is a ADC_2 pin. Which cannot be used when WiFI is being used.

https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/peripherals/adc_oneshot.html

1 Like

Also, this code cannot compile.

Thanks! -I'm aware that some ADC pins are unavailable while wifi is in use,,, thought I saw a chart that did not include 27 on it!! I'll change it to 35.

But what do you mean "cannot compile", I can see the device live on Blynk.

"RU...; where is the closing "?

Since it's repeated, I comm'd out, and an incomplete comment at that.

ALSO; Been testing pins 33, 35 for the last hour. No luck. Now it shows an irrelevant voltage level; same whether the source 13.2v is connected, or not. Schema pic attached. Also here I am attaching the isolated code that's for the DC Volt
sensor alone, that works perfectly.
Please take a look at 1st two lines inside void loop, I keep testing these two...

```cpp

#define SIGNAL_PIN 35     //was 27. (D**n you circuitschools dot com).
float adc_voltage = 0.0;  
float in_voltage = 0.0;   
float R1 = 30000.0;        
float R2 = 7500.0;        
float ref_voltage = 3.6;  //power source, chg to calibrate if divider not <1v
int adc_value = 0;
float input_voltage = 0; // test this outside void loop

void setup() {
Serial.begin(115200);
}

void loop() 
{
  adc_value = analogRead(35);  //changed 
 // adc_value = analogRead(SIGNAL_PIN);
  adc_voltage = (adc_value * ref_voltage) / 4096.0;  //esp32 resolution is 4096
  in_voltage = adc_voltage / (R2 / (R1 + R2)); 
  Serial.print("BattVolts = ");
  Serial.println(in_voltage, 2);
  // in_voltage= analogRead(27);   // check if this is necess.
  delay(500);

}
  
![20230227_205857|375x499](upload://mcfoSBbM4Nr834JcPUGHIfkDjVq.jpeg)

I actually setup the ADC converters of a ESP32 to be used; such as:
setup() code snippet

void setup()
{
  eg = xEventGroupCreate(); // get an event group handle
  x_message.topic.reserve(100);
  adc1_config_width(ADC_WIDTH_12Bit);
  adc1_config_channel_atten(ADC1_CHANNEL_6, ADC_ATTEN_DB_11);// using GPIO 34 wind direction
  adc1_config_channel_atten(ADC1_CHANNEL_3, ADC_ATTEN_DB_11);// using GPIO 39 current
  adc1_config_channel_atten(ADC1_CHANNEL_0, ADC_ATTEN_DB_11);// using GPIO 36 battery volts

using the adc

void fReadBattery( void * parameter )
{
  const float r1 = 50500.0f; // R1 in ohm, 50K
  const float r2 = 10000.0f; // R2 in ohm, 10k potentiometer
  const TickType_t xFrequency = 1000; //delay for mS
  float    adcValue = 0.0f;
  float    Vbatt = 0.0f;
  int      printCount = 0;
  float    vRefScale = (3.3f / 4096.0f) * ((r1 + r2) / r2);
  uint64_t TimePastKalman  = esp_timer_get_time(); // used by the Kalman filter UpdateProcessNoise, time since last kalman calculation
  SimpleKalmanFilter KF_ADC_b( 1.0f, 1.0f, .01f );
  TickType_t xLastWakeTime = xTaskGetTickCount();
  for (;;)
  {
    adc1_get_raw(ADC1_CHANNEL_0); //read and discard
    adcValue = float( adc1_get_raw(ADC1_CHANNEL_0) ); //take a raw ADC reading
    KF_ADC_b.setProcessNoise( (esp_timer_get_time() - TimePastKalman) / 1000000.0f ); //get time, in microsecods, since last readings
    adcValue = KF_ADC_b.updateEstimate( adcValue ); // apply simple Kalman filter
    Vbatt = adcValue * vRefScale;
    xSemaphoreTake( sema_CalculatedVoltage, portMAX_DELAY );
    CalculatedVoltage = Vbatt;
    xSemaphoreGive( sema_CalculatedVoltage );
    
      printCount++;
      if ( printCount == 3 )
      {
      //log_i( "Vbatt %f", Vbatt );
      printCount = 0;
      }
    
    TimePastKalman = esp_timer_get_time(); // time of update complete
    xLastWakeTime = xTaskGetTickCount();
    vTaskDelayUntil( &xLastWakeTime, xFrequency );
    //log_i( "fReadBattery %d",  uxTaskGetStackHighWaterMark( NULL ) );
  }
  vTaskDelete( NULL );
}
1 Like

I'm not sure I can use that. Altho yes eventually I want my setup to include sensors for 2 batteries, one APC UPS (wired as inverter), and a switch thru blynk to turn it off.
I've read about the ADC_ATTEN_DB function, just not sure I can implement it, yet.
2nd; I'm not certain at all about the 2nd part of your code here;

void fReadBattery( void * parameter )
{
  const float r1 = 50500.0f; // R1 in ohm, 50K
  const float r2 = 10000.0f; // R2 in ohm, 10k potentiometer
  const TickType_t xFrequency = 1000; //delay for mS
  float    adcValue = 0.0f;
  float    Vbatt = 0.0f;
  int      printCount = 0;
  float    vRefScale = (3.3f / 4096.0f) * ((r1 + r2) / r2);
  uint64_t TimePastKalman  = esp_timer_get_time(); // used by the Kalman filter UpdateProcessNoise, time since last kalman calculation
  SimpleKalmanFilter KF_ADC_b( 1.0f, 1.0f, .01f );
  TickType_t xLastWakeTime = xTaskGetTickCount();
  for (;;)
  {
    adc1_get_raw(ADC1_CHANNEL_0); //read and discard  /// etc. I re- posted incomplete just to quote

So I'll have to read more. For now I gotta get to my car's auto trans solenoid, I hope I can test it using the ADC, it's sending a P error to the car's OBD. Even tho it's just a switch, once hot, those things act up.
Thanks! I'll report back when I make changes on my volt-am setup.

Should be 10K for impedance matching.

LMTA! I'm actually right on that page calculating what resistors I need to keep the voltage sensed under 1v so there's a span available to read 10 to 40 volts -before- the volt divider. (esp32 volt limit being 3.3), nominal battery is 12v, 2 in series would be 34 max. (going by the solar charger). 34v going by a divider of R1=32k, R2=1k is 1.03v sensed.

However, going by your suggestion, with R2 at 10k, that doesn't seem to help. Please lmk!

so with a source V of 35 and r2 of 10K, and a output V of 3V3, the caclutator as linked to above produces a r1 value of 96.01K

I'm trying to understand "impedance matching". Is this what you're referring to?:

Yest. on starting IDE it notified of an update. Done. Now the board isn't being recognized. I fixed that problem. Then it restarted itself, now the icon is gone from the desktop. It's like it uninstalled itself!. I almost gave up the whole brand.
FFWWD to this AM. I am ordering a new board, and re installing the arduino IDE.

I worked support at IBM, Ambra in the 90s, and directed a team of server support dudes at Flextronics for years. We fixed a million issues over the years.
-THIS is the most frustrating little device I've ever encountered. I totally thought its development was mature.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.