I have an ESP32 and a 3 pin connector which is either connected to two switches or to a 3 pin potentiometer. Pin1 is connected to GND and Pin2 is connected to GPIO25.
Pin3 has to be connected dynamically (maybe using transistor, multiplexer or relay) to either GPIO27 (in case of dual switch) or to 3.3V (in case of potentiometer).
What would be the most elegant and simple way of automatically detecting which of these two types is connected and then automatically switching pin3 appropriately?
I'd like to be able to detect it without having to set or change the pot value and without having to press any switches.
The max potentiometer value is not known. It is likely to a 10k, 20k or 25k, but it may also be 50k, 100k or 250k.
To others your description might be clear but it's not to me. Can you please draw what you suggest; all components and all power connections (Vcc/GND).
There of course would also have to be some sort of multiplexer, transistor, relay or whatever on order to change the function of pin 3 of the connector. Maybe like this:
For the right hand switch, put a resistor over it.
Connect the input of the board with resistor to Vcc.
You need to use an analogue input and analogRead().
The input will read Vcc if the switch is not connected.
The input will read a medium value if the switch is connected and open (e.g. 10k/10k will create approx 1.5V).
The input will read 0V if the switch is closed.
I don't understand. What do you mean by "put a resistor over it"?
The ESP32 can programmatically set pullup/pulldown resistors and change the function of a GPIO pin from ditigal to analog.
int testPin = A0;
int result = 0;
void setup() {
pinMode(testPin, INPUT_PULLUP);
Serial.begin(115200);
}
void loop() {
result = analogRead(testPin);
Serial.println(result);
delay(1000);
}
I get a count of 1016 or 1017 when the analogue pin is open circuit. (Not the maximum 1023.)
The count drops to 124 when a 10kΩ resistor is connected between the pin and GND.
The potentiometer is on an other input (based on the first drawing in the opening post); the suggested schematic only applies to the switch that is not replaced by the potentiometer input (the right hand side switch in the original drawing).
Edit @JohnLincoln
I think that I now see what you mean. So that will not be as simple as I thought.
Just a note — I just did a test on my ESP32 (NodeMCU) and the pullup on pin 25 does not work.
Also it seems that analogRead attaches the pin to ADC channel, which remaps it off the pullup circuit so if you want to perform a digitalRead() after the analogRead() you need to re-issue the pinMode(testPin, INPUT_PULLUP);
It's a live music thing. I have a box containing an ESP32 and the box has a TRS-connector (aka stereo jack) which should allow for connecting a dual footswitch (equivalent to two buttons) or an expression pedal (equivalent to a 3 pin potentiometer).
But then you would have to connect the buttons/potentiometer before powering on the ESP. I'd prefer to be able to do the detection at any point in time.
@J-M-L I don't see how this would work without GND, but if it was possible to read the states of the buttons / the state of the pot this way, I'd be okay with this wiring.