Voltage sensor using esp32 showing in webpage

Hi guys, I am a newbie I have a project that uses a voltage sensor and measures voltage in esp32 as is shown on the webpage. I couldn't find the integration voltage sensor to esp32. Can you help me?

Describe this voltage you want to measure?

Are you using a ESP32 WROVER or WROOM or the ESP32-S? Note ESP32S and ESP32-S are different models.

/*
 5/12/2022, Idahowalker
 ESP32 code using the ESP32's AD API
 Using a freeRTOS task
 Using a Kalman filter
 */
#include "sdkconfig.h"
#include "esp_system.h"
#include "freertos/FreeRTOS.h" //https://www.freertos.org/a00106.html
#include "freertos/task.h"
#include "freertos/timers.h"
#include "freertos/event_groups.h"
#include <driver/adc.h>
#include <SimpleKalmanFilter.h> //https://www.arduino.cc/reference/en/libraries/simplekalmanfilter/
////
void setup()
{
  //https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/peripherals/adc.html
  adc1_config_width(ADC_WIDTH_12Bit);
  adc1_config_channel_atten(ADC1_CHANNEL_7, ADC_ATTEN_DB_11);// using GPIO 35
  //
  xTaskCreatePinnedToCore( fReadBattery, "fReadBattery", 4000, NULL, 3, NULL, 1 );
}
////
void fReadBattery( void * parameter )
{
  float adcValue = 0.0f;
  //https://ohmslawcalculator.com/voltage-divider-calculator
  const float r1 = 50500.0f; // R1 in ohm, 50K. For a LiFeP04 battery under charge
  const float r2 = 10000.0f; // R2 in ohm, 10k, make it a habit to use a 10K for R2
  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();
  const TickType_t xFrequency = 1000; //delay for mS *NON-BLOCKING DELAY *
  for (;;)
  {
    adc1_get_raw(ADC1_CHANNEL_7); //read and discard
    adcValue = float( adc1_get_raw(ADC1_CHANNEL_7) ); //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;
    printCount++;
    if ( printCount == 3 )
    {
      log_i( "Vbatt %f", Vbatt );// you may have to edit the code to use serial print.
      printCount = 0;
    }
    TimePastKalman = esp_timer_get_time(); // time of update complete
    xLastWakeTime = xTaskGetTickCount();
    vTaskDelayUntil( &xLastWakeTime, xFrequency );
    //log_i( "fReadBattery %d",  uxTaskGetStackHighWaterMark( NULL ) );
  }
  vTaskDelete( NULL );
}
////////
void loop() { }
1 Like

I will measure the OUTPUT Voltage of the Solar Panel ( I think it will be around 10-12 V ), I use WEMOS D1 R32 Model ESP32

You should not measure the output of a 12V, loaded, solar cell.

Instead, you could measure the output of the solar charge controller and/or the charge of the battery. A solar cell is not a voltage device. Install a reverse current blocking diode between the solar cell and the charge controller. A solar cell will draw current at night. The solar cell can draw too much current from the battery and damage itself.

The code from post#2 is from code used to read a 12V LifePo4 charged by solar.

1 Like

Thanks for your reply, I really appreciate it . I will try and give feedback.

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