Voltage Divider Explanation
A voltage divider is a simple circuit made of two resistors connected in series. It is used to scale down a voltage to a desired level. The basic principle is that the voltage is divided between the resistors in proportion to their resistance values.
In your example:
- The resistors in the divider are 10kΩ and 20kΩ.
- The voltage across the 10kΩ resistor (between GND and GPIO 34) will be your desired measurement.
Formula to Calculate Voltage
The voltage at the GPIO pin (V_out) is determined using the voltage divider formula:
$$
V_{out} = V_{in} \times \frac{R_2}{R_1 + R_2}
$$
Where:
- $V_{in}$ is the input voltage (in this case, 3.3V)
- $R_1$ is the resistor connected to GND (10kΩ)
- $R_2$ is the resistor connected to the GPIO pin (20kΩ)
Substituting the values:
$$
V_{out} = 3.3V \times \frac{20kΩ}{10kΩ + 20kΩ} = 3.3V \times \frac{20}{30} = 2.2V
$$
So, the voltage at GPIO 34 will be 2.2V.
Code to Read the Voltage Using ESP32
You can use the ADC (Analog-to-Digital Converter) feature of the ESP32 to read the voltage at GPIO 34.
Code Example (in Arduino IDE)
Code Breakdown:
- analogRead(ANALOG_PIN): Reads the voltage on GPIO 34 as a digital value between 0 and 4095, corresponding to 0V and 3.3V, respectively.
- Voltage Calculation: The result of analogRead() is mapped to the voltage range of 0 to 3.3V based on the ADC’s 12-bit resolution (0 to 4095).
- Serial Output: The calculated voltage is printed to the Serial Monitor for monitoring.
Important Considerations:
- Ensure that the voltage you’re measuring doesn’t exceed 3.3V, as ESP32’s ADC pins are not 5V tolerant.
- The analog input on the ESP32 uses a 12-bit resolution, meaning values will range from 0 to 4095.
This setup will allow you to measure the voltage at GPIO 34 using the voltage divider circuit and display it on the Serial Monitor.