I want to connect a Micro Limit Switch (Bump Lever) to an ESP32 Thing Plus (USB-C). I’ve seen videos of people using resistors on breadboards with buttons/switches, but I don’t really understand when they are or aren’t needed. Do I need a resistor between this switch and the ESP32?
Micro Limit switch (Bump Lever)
ESP32 Thing Plus (USB-C)
Resistors are used to limit current flow which, if excessive, can damage the ports on the ESP32.
If your switch is open circuit and connects GND to the ESP32 port, no resistor is needed because the current flow is determined by the input port. Usually a port can be configured to have a weak pullup resistor. This is inside the ESP32. You don’t really want a resistor either because you want the port to see a zero (GND) level.
If you are switching volts to the ESP32 port; then you absolutely need a resistor. Assume the port is at GND level and you are switching +5VDC. Using ohms law a current of 1 milliamp would need at least a 5K resistor. Rule of thumb, start with a large value and reduce it only if needed. Try a 10K resistor, if it works all good.
Not quite true BUT very good insurance.
Not sure about the ESP32 but when a pin is configured as an input the input impedance should be very high. The input current could be measured in nA or even pA. No damage would be done but at these currents a 10k resistor will make no difference to the input voltage supplied by the switch.
If by chance this I/O has been accidentally configured as an output and happens to pull LOW, then that is a different story. Without a current limiting resistor you could say goodby to your IC. That port anyway.
So Jacob. In this scenario do as James says and fit a resistor. Will do no harm and provide protection ai case of accidents. As James indicates if you are grounding this pin with a switch DO NOT use any resistor.
Agree in most cases.
Recently I was using the ADC pin on the Pi Pico which uses a RP2040 micro. I set up a voltage divider between VSYS (3.7V LiPo) and GND. I wanted to measure the battery voltage as it discharged. The RP2040 ADC pins will only tolerate up to 3.6V before being damaged. A LiPo fully charged is about 4.2V.
Using a 200k and 100k resistor I was not getting the readings I expected. The datasheet stated the following:
Changing to a 10k and 5k resistor improved the readings somewhat. Using a 2k and 1k was better but placed a unacceptable drain on the battery even if only for a short time while the Pico was awake.
The datasheet for the ATMega328P (Arduino micro) states:
I have not tried measuring battery discharge with the ATMega328P.
Anyway the adventure continues.
@James46717 Has already summarised the problem well, but I’d explain it a bit differently.
- If your pin is configured as an input, it has a high impedance, so the current is limited by this, so you don’t need a resistor. The ESP32-WROOM datasheet says the pullup/down resistors are ~45KOhm:
Oddly it doesn’t give an input impedance, but does give an input current measurement of 50nA, which mirrors Bob’s guess of nA or pA.
- When configured as an output, the pin will try and drive the voltage to 0V or VDD, and if there’s no resistor to limit the current, it’ll quickly exceed the maximum drive current of 20-40mA (refer to the datasheet for the exact number depending on a few factors)
Voltage measurement can be a problem when using resistive voltage dividers as if the divider resistors are too high the 10MΩ resistance of most meters can have a significant effect on the actual voltage present. The meter reading will show the voltage WHILE THE METER IS CONNECTED. Not the real life value.
The input resistance on the pin on the micro of choice will behave the same. If low enough the lower resistance of the divider will be modified by this input resistance. One reason why expected numbers are better with lower divider resistance values.
Back in Sept '21 I attempted with limited success to actually measure the input resistance of an Arduino analog input. I had a bit of bother as I do not have the luxury of an Electrostatic Voltmeter at home but I got what I think is a reasonable approximation by using the actual ADC readings as a volt meter. I emphasise here the word “approximation”.
It ended up being in the GΩ range.
If you are interested the method and problems are described in this post.
A bit slow to-day but the implications of your Par 3 about the ADC drawing 150µA have just sunk in.
Correct me if I am wrong but 150µA is 0.00015A is it not?
3.3V divided by this equates to 22kΩ. This is very very low compared to say the Arduino, refer my post above. And I find that a bit hard to believe.
If this is indeed true it would explain your unacceptable results with 100k in the lower leg of your divider. this would reduce to 18k where you have calculated for 100k. Also would explain why the situation improved when the divider resistors were decreased.
I know it might be a bit messy but I think the best way if you need a resistive divider is interface an op amp connected as a voltage follower between your divider and ADC.
PS: Your divider might be the other way around with 200k as the lower leg. No matter comments still apply.