Make a button work with ESP32 C3 Super Mini

Hi!

I'm trying to get my ESP32 C3 Super mini to work with both a mechanical switch from a mechanical keyboard and the typical electronics small button.

Part of the code is taken from the Blink example from the Arduino IDE and the rest of the code is trying to make the built in LED power off when the button is pressed and power it on when not pressing the button.

I don't know if the pull-up pin is not properly configured or if there's any problem with the code syntax.

I'll leave this page ESP32 C3 Super Mini - sudo.is/docs since it has a lot of information about the ESP32 C3 Super Mini as well as its datasheets.

#define BTN_PIN     9
#define LED_PIN     8

void setup() {
  delay(3000); // sanity delay
  pinMode(BTN_PIN, INPUT_PULLUP);
  pinMode(LED_PIN, OUTPUT);
  digitalWrite(LED_PIN, HIGH);

}

void loop() {
  //  digitalWrite(BTN_PIN, HIGH);

     if (digitalRead(BTN_PIN) == LOW) {
        digitalWrite(LED_PIN, HIGH);
     } else {
         digitalWrite(LED_PIN, LOW);
     };
     
   delay(100);
}

I would love to know what I may be doing wrong or if there is any tutorial online that works with the ESP32 C3 Super Mini and the buttons display where I can look at the code and learn with it.

Thanks!!

I don't see anything wrong with your code.

Post a schematic showing how you have wired the button.

I have the wiring like this:

To ensure that you have wired to the normally open connections of the pushbutton connect to two diagonally opposite pins otherwise you may have used two pins that are always connected together

I cannot tell from your picture but hopefully the switch is wired to take the input pin LOW when the button is pressed

I don't know if I fully understood it, but I have change the cable that come out of GPIO9 to a pin diagonal to the ground take on the pushbutton (and it still doesn't work).

If you need more photos or angles tell me, please.

The button is now wired as I suggested but I cannot tell where the connections to the ESP32 board go to

Presumably one is pin 9 but what about the other one ?

I think the other wire is connected to ground. Like you said, it's hard to be sure, the image is dark and blurry.

@smallerone test your Dupont cables. They are pretty unreliable.

Post #5 is not powered by anything.

Post #3 IS powered by USB, and IS working.

@smallerone

1. Do not place your ESP32C3 Super Mini Board on the Breadboard. I strongly suspect conatct problem.

2. OnBoard LED is already there on the Board and is connected at DPin-8.

3. Take male-female jumper. Connect the fmale side at DPin-9. Leave the male side hanging. This jumper will work as your external Switch/Button.

4. The connection diagram of onBoard LED and Button is something similar to Fig-1.


image
Figure-1:

5. Select Board as follows:
IDE 2.3.1 ---> Tools ----> ESP32 ----> LOLIN C3 Mini
USB CDC On Boot: "Enabled"

6. Upload the following sketch. Intially, the onBoard LED is Off. When Swicth is closed, the oBoard LED starts blinking at 2-sec interval.

#define LED_BUILTIN 8
#define BTN 9

void setup() {
  Serial.begin(9600);
  while (!Serial) {
    ;
  }
  pinMode(LED_BUILTIN, OUTPUT);
  pinMode(BTN, INPUT_PULLUP);      //input pull-up resistor is enabled
  digitalWrite(LED_BUILTIN, HIGH);  //onBoard is Off; I have got like this
}

void loop() 
{
  if (digitalRead(BTN) == LOW)  //Switch K1 is closed; blink onBoard LED
  {
    while (true) 
    {
      digitalWrite(LED_BUILTIN, HIGH);  //onBoard LED is Off; invert logic
      delay(1000);
      digitalWrite(LED_BUILTIN, LOW);  //onBoard LED is On; invert logic
      delay(1000);
    }
  }
}

7. Check that onBoard LED is Off.

8. Gently, touch the male side of the hangig wire/jumpwe with G-pin of the Mini Board.

9. Check that the onBoard LED is blinking at 2-sec interval.

10. Press RST Button of the Mini Board and repeat the process.

I connected the pins to pin 9 and ground, but after looking up some videos I think I also need to power the pushbutton with the 3.3v and use a resistor.

On post #5 I unplugged it so the LED wouldn't unfocus my camera xD but when plugged it still didn't work. And in post #3 the led works but not the button functionality.

Thanks!! That worked flawlessly!! How can I now adapt the board and cables so I can use my push-button or a mechanical switch?

Post the picture of your Button.

I would love if it could work with both type of buttons (not at the same time).

The first one is a typical push button and the second one a mechanical switch.

I've search on the internet and found that I need a resistor, but if we are working with the pin 9 that is configured as pulled-up, the resistor is not needed(?)

GPIO 2,8,9 are strapping pins, and need to be a certain logic level at boot.
Best to avoid them until you run out of other pins.
Leo..

1. Place your Button on Breadboard as per Fig-1 and mark their terminlas as 1, 2, 3, and 4.

ButtRLED
Figure-1:

2. Place/Connect R1 and LED with Button exactly the way it is shown on the Breadboard,

3. Perform LED Test.
Take a jumper and connect 5V of ESP32C3 Mini Board at Pin-2 of Button. Check that LED glows; otherwise, swap the terminals of LED. Check that LED glows.

4. Find Normally Open (N/O) contanct of Button.
(1) REmove 5V terminal from Buttoon PIn-2 and place it with Pin-4. Check that LED is Off. If the LED is off, press the BUtton. Check that LED is on. Pin-2 and Pin-4 makes the N/O contact.

(2) Repeat process of Step-4(1) for other Pin-1, Pin-3 until you find a N/O contact.

5. Now remove GND connection from the LED and put it on Pin-2 of Button.

6. Put a jumper between Pin-4 of Button with DPin-9 of Mini Board.

7. Upload sketch of post #10. Press Buton and check that onBoard LED is blinking.

Who can confirm me that the onBoard LED at DPin-8 of ESP32C3 Super Mini Board is really driven by an inverter or the GPIO pin itself delivers invert logic? (Pls, see sketch of post #10.)

When I execute the following codes, the onBoard LED turns On:

pinMode(8, OUTPUT);
digitalWrite(8, LOW);   //and NOT digitalWrite(8, HIGH);

I only managed to make it work from step 1 to step 4.

I have ended up looking for a tutorial in YouTube since my interest with the matter was to make both type of buttons work with a pull-up configured pin.

This tutorial shows a very simple project with just a button and a resistor. I replicated it with a 2K resistor and it worked prefectly.

Since my goal was to make it work with a pull-up configured pin I removed the resistor, changed pinMode(x, INPUT) to pinMode(x, INPUT_PULLUP) and connected GROUND to what would be the fourth pin in your picture and the pin number 6 (to avoid problems with 2, 8 and 9 as @Wawa said) connected to the third pin in your picture.

It works perfectly with both the push-button and the mechanical switch.

Also, I didn't get to try out your post #18.

Thanks a lot for the help!!