For a project i'm working on i have 4 buttons to be read with only one analog in pin in an ESP32C3. for which i'm thinking on doing the usual voltage divider setup. BUT i would like it to have SOME semblance of multi touch by detecting when i click any two buttons at the same time and i know this is possible by some testing and theoretically.
for example, the usual multi-button voltage divider input works as such: you have a series of resistors and switches conecting each node between these resistors to the analog input. when you press a button the input will read the tension at that node as given by: V * (Req_bottom / (Req_top + Req_bottom), where Req_top and Req_bottom are the Equivalent resistance of the "top" and "bottom" resistors. BUT this also works if you press two switches for current will flow up to switch A the go through their shared connection at the input pin to switch b then on. in this case Req_top is from the first resistor on the VDD to the first switch and Req_bottom is from the second switch to ground
The problem is: if all values are the same, you can't differentiate the switch at the halfway point to the first and last switches together, and if all values are different the distance between values might be too small for the microcontroller to reliably measure. how do i find these "goldilocks" resistor values?
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
You're absolutely correct in identifying the challenges of designing a voltage divider-based multi-button analog input system, especially when trying to support multi-touch (detecting two buttons pressed simultaneously). The key is to carefully select resistor values so that:
Each single button press produces a unique and distinguishable voltage.
Each valid two-button press combination also produces a unique and distinguishable voltage.
The voltage differences between all possible states (single presses and valid two-button presses) are large enough to be reliably detected by the ESP32C3's ADC, considering its resolution and noise.
Below, I'll guide you through the process of designing such a system, including how to find the "Goldilocks" resistor values.
Step 1: Understand the ESP32C3 ADC
The ESP32C3 has a 12-bit ADC, meaning it can measure 4096 discrete levels over its input voltage range (typically 0 to 3.3V, though this may depend on your specific setup and reference voltage). This gives a resolution of:
However, in practice, the ADC is not perfectly accurate due to noise, non-linearity, and other factors. To ensure reliable detection, you should aim for voltage differences between states to be significantly larger than this resolution—ideally, at least 10–20 mV (or more) to account for noise and tolerances in resistors.
Step 2: Define the Circuit
For four buttons, the typical voltage divider setup looks like this:
When a single button (e.g., Btn1) is pressed, the voltage at the analog input is determined by the voltage divider formed by the resistors above and below that button.
When two buttons (e.g., Btn1 and Btn2) are pressed, the resistors between those buttons are effectively shorted together at the analog input, and the voltage is determined by the equivalent resistance of the top and bottom parts of the divider.
For example:
Single press of Btn1: Voltage = ( V_{DD} \cdot \frac{R2 + R3 + R4}{R1 + R2 + R3 + R4} )
Single press of Btn2: Voltage = ( V_{DD} \cdot \frac{R3 + R4}{R1 + R2 + R3 + R4} )
Two presses (Btn1 + Btn2): Voltage = ( V_{DD} \cdot \frac{R3 + R4}{R1 + R3 + R4} )
The challenge is to choose ( R1, R2, R3, R4 ) such that all single-button and two-button combinations produce distinct voltages, and the differences between these voltages are large enough to be reliably detected.
Step 3: Identify All Possible States
With 4 buttons, you need to consider the following states:
Single button presses: Btn1, Btn2, Btn3, Btn4 (4 states).
No buttons pressed: This can be handled separately (e.g., by adding a pull-down resistor or pull-up resistor to ensure a known voltage when no buttons are pressed).
This gives a total of 10 states (single + two-button combinations) that need to produce distinct voltages.
Step 4: Define the Voltage for Each State
For each state, the voltage at the analog input can be calculated as follows:
Notice that Btn4 and any combination involving Btn4 results in 0V because Btn4 shorts the analog input directly to ground. This is a limitation of the circuit: you cannot distinguish Btn4 alone from Btn1+Btn4, Btn2+Btn4, or Btn3+Btn4. To address this, you may need to modify the circuit (e.g., by adding a small resistor between Btn4 and ground) or accept that Btn4 cannot be part of a multi-touch detection scheme.
Step 5: Choose Resistor Values
To find the "Goldilocks" resistor values, you need to:
Ensure all non-zero voltages are distinct.
Ensure the differences between voltages are large enough to be reliably detected by the ADC.
A good starting point is to aim for voltages that are evenly spaced across the ADC range (0 to 3.3V). However, the two-button combinations introduce non-linearities, so you may need to use a numerical approach or trial-and-error to optimize the values.
Strategy: Evenly Spaced Voltages for Single Presses
Let's start by trying to space the single-button voltages evenly across the range. For example, with ( V_{DD} = 3.3V ):
Btn1: ~2.75V
Btn2: ~1.83V
Btn3: ~0.92V
Btn4: ~0V
The total resistance ( R_{total} = R1 + R2 + R3 + R4 ). The voltage for each button press is determined by the resistance below that button divided by the total resistance. Solving for the resistors:
To choose actual values, pick a total resistance ( R_{total} ) that is reasonable for your application (e.g., 10 kΩ to limit current while avoiding excessive noise from high impedance). For ( R_{total} = 10 , \text{kΩ} ):
Now, calculate the differences between adjacent voltages to ensure they are large enough:
2.75V – 2.64V = 0.11V (110 mV)
2.64V – 2.08V = 0.56V (560 mV)
2.08V – 1.83V = 0.25V (250 mV)
1.83V – 1.24V = 0.59V (590 mV)
1.24V – 0.92V = 0.32V (320 mV)
0.92V – 0V = 0.92V (920 mV)
These differences are all much larger than the ADC resolution (805 µV) and should be easily distinguishable, even with noise and resistor tolerances.
Step 6: Handle the Btn4 Issue
As noted, Btn4 and any combination involving Btn4 all produce 0V, making them indistinguishable. To fix this, you can add a small resistor ( R5 ) between Btn4 and ground:
Choose ( R5 ) to be small compared to the other resistors (e.g., 100 Ω) to keep the Btn4 voltage low but non-zero. Recalculate the voltages and adjust as needed.
Step 7: Test and Refine
Once you have a set of resistor values, simulate the circuit (e.g., using a tool like LTspice) or build a prototype to test the actual voltages. Measure the ADC readings for each state and ensure they are distinct and stable. If necessary, adjust the resistor values to increase the separation between voltages or to better fit the ADC range.
Step 8: Software Implementation
In your ESP32C3 code, you'll need to:
Read the analog input value (0–4095).
Map the ADC readings to the corresponding button states using thresholds. For example:
int adcValue = analogRead(ANALOG_PIN);
float voltage = adcValue * (3.3 / 4096.0);
if (voltage > 2.70) {
// Btn1
} else if (voltage > 2.60) {
// Btn1 + Btn2
} else if (voltage > 2.00) {
// Btn1 + Btn3
} else if (voltage > 1.75) {
// Btn2
} else if (voltage > 1.15) {
// Btn2 + Btn3
} else if (voltage > 0.85) {
// Btn3
} else if (voltage > 0.05) {
// Btn4 (if using R5)
} else {
// No buttons or Btn4 combinations
}
Add hysteresis or debouncing to handle noise and ensure stable readings.
Final Notes
Resistor Tolerances: Use resistors with 1% tolerance to minimize variations in the voltage levels.
Noise: Add a small capacitor (e.g., 10 nF) between the analog input and ground to filter out noise.
Current Consumption: Choose ( R_{total} ) to balance current consumption and noise immunity. A value of 10 kΩ is a good starting point, but you can adjust it based on your needs.
Limitations: This approach works well for two-button combinations but may not scale well to three or more buttons pressed simultaneously due to the increasing complexity of the voltage combinations.
By following this process, you should be able to find resistor values that allow reliable detection of single and two-button presses on your ESP32C3.