Hi I am using an esp32, due to a lack of available pins, I need to use the same pin for a button that has a built-in LED. The idea is that when I press the button, the LED turns on for a few seconds.
After doing some research, I found that certain pins are suitable for this purpose, but I want to make sure they are safe to use this way. I tested it with pin 21, and it worked as I wanted, but I don’t want to risk damaging the board.
The pins I’m considering using are: p2, p4, p5, p16, p17, p18, p19, p21, p22, p23.
Are these pins safe to use for controlling a button with an integrated LED on the same pin?
Indeed. It would be helpful to know which board. And you say that the button has a built-in LED? Does it also have a built-in LED resistor? If not, then you will need an external current limiting resistor.
Perhaps you could provide a link to the board and and the button part you are using?
Look at a pinout diagram for ESP32. If those other pins are marked as GPIO pins like pin 21 then it should be ok.
When connecting a button and led to the same pin, if the led remains lit for some time, it will not be possible to read the pin to detect if the button is still pressed or has been pressed again. However, by changing the pin mode back to input, reading the pin and then immediately changing it back to output again, any flickering in the led should not be too noticeable.
@LarryD are resistors R7-12 necessary? Also why are R1-6 different values? Some of those values seem a little too high for led current limiting.
Possibly. But we normally recommend to connect an ordinary button (without an LED) from the Arduino pin to ground, with no resistor, without much concern that the pin might be accidentally configured as high.
With these buttons which have LEDs, it will be necessary to switch between input and output modes, which you could argue increases the risk. But pins default to low when set to output, so I don't see it as a significantly increased risk. I guess the danger is that a beginner thinks the pin must be set to high rather than input to switch off the LED.
Yes, this precaution can save a lot of headaches, especially for beginners who regularly connect LEDs without a resistor Somewhere, they even recommend always putting a resistor in series with a GPIO.
R7-12 are needed when the Arduino a GPIO pin is HIGH (LED OFF).
If the switch was pressed, pin is made GND, shorting the HIGH 5v to GND. R7-12 limits current.
I use very HIGH efficiency LEDs, the resistor values are selected so the LED intensity are balanced.
Blue and white LEDs light if you sneeze on them
If the GPIO pin is output and at a HIGH (LED is OFF), closing the switch does not turn ON the LED.
When GPIO is made input (to read the switch) and the switch is closed, the LED goes on for the read time, for about 8.6us, every 50ms (very dim).
But if it was not critical that the LEDs must be off while the button is pressed, then there would be no need to set the pin HIGH and so no need for those resistors?
Bought 200 each colour from either eBay or Ali Express a few years back. 1206 SMD red, green, blue, white, yellow, amber, green.
I think the site called them Ultra bright
The Arduino LED/switch pin is made an output for almost 99.9% of the time, it will be either HIGH or LOW, depending on our code.
Let’s assume the pin was output and set to LOW (LED ON).
Closing/opening the switch at any time will not be a problem.
Let’s assume the pin was output and set to HIGH (LED OFF).
If the switch was open, no problem. If the switch was closed, when the output is HIGH, that HIGH is forced to 0v, ouch.
Our 220R limits the current to 22mA therefore noouch.
Every 50ms (for only 8.6us) we make the GPIO pin input, we read the switch, it could be open or closed; if the switch was closed, the LED goes ON for only 8.6us.
But if it is not important for the led to be off while the button is pressed, then I was thinking that if the led should be on, the pin would be output+low for 99.9% of the time, as you describe. But if the led should be off, then the pin would be input for 100% of the time. So no need for the pin to be high at any time, and no need for the additional resistors.