Page 1 of 1

One ESP32 PWM output -> 2nd ESP32 ADC Input. Cannot get values

Posted: Wed Jun 04, 2025 7:11 pm
by RollingRocker
ESP32 Wroom 32d are the boards I am using.

I am using Arduino to experiment around with creation of a PWM based signal on one ESP32, which I do have up and running. I am outputting .959vDC via ledcAttach/ledcWrite using PIN 12. When I put a multimeter on the output, I do get 0.959v DC. This is a good result and what I want. (eventually board #1 will output a sine wave, but for now a steady output voltage is good enough for testing).

I then took a second identical ESP32. Wrote another small program to read the ACD. I am using the VP pin as the input.

When I put a 1.5v AA battery between GND and VP pin on the board, I get back ~1.56vDC.

When I take the outputs from board #1, outputting 0.959vDC and hook them to the inputs of Board #2 via the same VP input pin, I get readings all over the place, and never close to a rock steady 0.959vDC. Same code, totally different results.

I have tried powering both boards from a single 5vDC power supply in thinking that using two different power sources was problematic. That didn't make a difference.

I have tried a few different ESP32 boards and they all return the same result, so I think I am missing something code-wise.

Board #1 (signal output 0.959vDC = Good. Multimeter gets good reading)
Connect Board #1 to Board #2 via GND/VP input pin, ADC input readings all over the place.
Connect Board #2 to 1.5vDC AA battery, steading readings on the same input pins.

Why is this happening?

Note: I am not using any resistors, since all the voltages are <3.3 volts. Thus R1 and R2 below are zero.

Code: Select all

#define ANALOG_IN_PIN  36 // ESP32 pin GPIO36 (ADC0) connected to voltage sensor  "VP" PIN
#define REF_VOLTAGE    3.3
#define ADC_RESOLUTION 4096.0
#define R1             0.0 // resistor values in voltage sensor (in ohms)
#define R2             0.0  // resistor values in voltage sensor (in ohms)

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

  // set the ADC attenuation to 11 dB (up to ~3.3V input)
  analogSetAttenuation(ADC_11db);
}

void loop() {
  // read the analog input
  int adc_value = analogRead(ANALOG_IN_PIN);

  // determine voltage at adc input
  float voltage_adc = ((float)adc_value * REF_VOLTAGE) / ADC_RESOLUTION;

  // calculate voltage at the sensor input
  float voltage_in = voltage_adc * (R1 + R2) / R2;

  // print results to serial monitor to 2 decimal places
  Serial.print("adc_value= ");
  Serial.print(adc_value, 2);
  Serial.print(" Measured V1 = ");
  Serial.print(voltage_in, 2);
  Serial.print(" v@ADC = ");
  Serial.println(voltage_adc, 2);
  delay(200);
}

Re: One ESP32 PWM output -> 2nd ESP32 ADC Input. Cannot get values

Posted: Thu Jun 05, 2025 5:46 pm
by lbernstone
Use a capacitor or an RC filter to smooth out the signal. A PWM is literally jumping all over the place (well, in theory almost entirely between low and high). Capacitance near where you read the signal will resist the change in voltage.

Re: One ESP32 PWM output -> 2nd ESP32 ADC Input. Cannot get values

Posted: Thu Jun 05, 2025 9:41 pm
by RollingRocker
Thanks for your help. I read further about RC Low Pass filters and ordered a $6 PWM to Analog board from Amazon that handles this.

Re: One ESP32 PWM output -> 2nd ESP32 ADC Input. Cannot get values

Posted: Fri Jun 06, 2025 12:14 am
by lbernstone
The ESP32 has a DAC. It also has a cosine generator. PWM is a different peripheral for a different purpose.