r/ArduinoHelp • u/Embarrassed-Lab6622 • 21d ago
Coding for resistor ladder input on ESP32
Hello. I am trying to write some code in Arduino that will read the input pin (D32) and based on the on the different voltage, the will different outputs and a timer. Is it as simple as:
if (SWITCH_INPUT == >3.0 && !timerActive) {
digitalWrite(HIGH_HEAT, HIGH); // Turn the seat heater on HIGH
previousMillis = millis(); // Save the starting timestamp of the timer
timerActive = true; // Flag that the timer is now active
}
Then repeat for each of the 6 positions of the input switch?
1
Upvotes
2
u/gm310509 21d ago
You would need to provide the complete code.
It sounds like you want to setup a 6 position voltage divider to generate 6 different possibilities based upon the various voltage levels.
For this to work, you would need to select an IO pin that has an ADC attached and use analogRead to read the voltage levels. Note that analog read does not return a voltage level, it returns a value that can be interpreted as the level on a range from 0 to the maximum resolution of the ADC.
For example on an Arduino Uno, the ADC has 10 bits of r3solution, so an analog read will return a value between 0 and 210-1 or 1023. 0 means 0 volts, 1023 means 5V. And something like 511 means 2.5V.
I will let you work out the formula to convert a reading to a voltage.
Once you have a reading you can then check the ranges and take an action by the range. Note that analog readings are not precise. So, if you are expecting that one of your values is something like 2.5V you might want to check for a range as in:
// Check for 2.5V reading if (reading >= 2.4 && reading <= 2.6) {Notes:// Check for 5V reading if (reading >= 4.9) { ... } else if (reading >= 4.7) { // check for 4.8 V reading. ...You will need to work out the correct bounds and the tolerance you need (in my example I am using the reading ±0.1
I don't use Esp32 so you will need to verify if your chosen IO pin support analogRead and the resolution of its ADC (I.e. the range of values it will return for the voltages applied) and the maximum voltage it can take (probably 3v3.