Hi everyone, I am currently working on a project where I am using an ESP-01S with a 0.91 inch SSD1306 OLED screen. GPIO1 and GPIO2 have already been used for the screen, and now I want to add two buttons. Trying to use the Rx pin as a GPIO does't seem to work.
Here is my code:
I know that using the Tx and Rx pin will prevent me from using the Serial Monitor, but I am okay with that. Since space is limited in my project, I cannot use anything else but ESP-01S.
to be honest that would be strictly coincidental, since the builtin LED is probably on that very pin, which means you are directly turning the led on but not through the code.\
pinMode(3, FUNCTION_3);
pinMode(3,INPUT_PULLUP);
It is either, or just
pinMode(3,INPUT_PULLUP);
will do the trick.
The extra function only come into play when you want use the pin for things like Serial, I2S Or for switching it back to Serial after you have switched it to INPUT etc.
FUNCTION_3 is the same as INPUT on GPIO 3, INPUT_PULLUP initiates the pullup, but the pin will be in the mode it was set to last.
Now assuming that you just selected 'esp8266 generic' and didn't modify the setting for the Builtin LED (which sets the macro for 'LED_BUILTIN' ) from 2 to 1,
how about you connect a LED + resistor between VCC & GPIO 2. Make sure it is active LOW (so yes as i say it it is) since GPIO 0, 1 & 2 cannot be pulled LOW at boot if you want to run the unit in normal boot mode.
Or you can change the setting for the builtin LED from 2 to 1 and upload again. (but don't connect the button to GPIO 1 now !! that would destroy the pin.)
Yes, I just select "Generic ESP8266 Moduel" and port. All other settings are as they come ("Builtin Led" is 2)
I don't understand this part, I'm newbie. Can you explain it in more detailes? What resistor to use? How to check if it's "active LOW"? Do I use same code?
You will need to limit the current going through any LED you connect to a GPIO, 470 Ohms will do the trick (anywhere between 270 and 1K is fine)
'ACTIVE LOW' means that if you set the pin LOW, the LED lights up, so that would work if you connect a 470R resistor to 3.3v, connect that to the Anode of the LED and connect the Cathode to GPIO 2. Of course you would need to set the LED_BUILTIN back to 2.
The confusion is caused by the default setting for the ESP8266 generic.
Thank you for detailed explanation, I will try everything you sad a bit later (not at home now)
The buttons in my project are not used control leds, leds are just as test to see if it's working. Buttons will change variables in code that will change output for OLED screen.
Will this work if I use screen as output and not leds?
Should work just fine, but keep in mind that a single button press & release may result in several presses recorded.
Have a look at this article. The tutorial uses a pulldown and is on an UNO, but the basic principle is the same.
This code DOES WORK with LED_BUILTIN back to 2! I had a faulty wire or button, not sure which one yet, but I changed it and it works perfectly now. Thank you so much for your help!