XIAO ESP32-C3 button don't work

Hi,
I try to use ESP32-C3 with the " Expansion Board Base for XIAO".
The buzzer, and the screen work but the button D1 doesn’t.
I try to use the button of the expansion.
I try to set the XIAO directly on a bread board with a physical pullup resistance and with internal pullup resistance. I try other PIN. Input buttons never works.

The code with the expansion board :
#include <Arduino.h>
#include <U8x8lib.h>
#include <Wire.h>

//U8X8_SSD1306_128X64_NONAME_HW_I2C u8x8(/* clock=/ PIN_WIRE_SCL, / data=/ PIN_WIRE_SDA, / reset=/ U8X8_PIN_NONE); // OLEDs without Reset of the Display
U8X8_SSD1306_128X64_NONAME_HW_I2C u8x8(/
reset=*/ U8X8_PIN_NONE);

const int BUTTON_PIN = 0;

void setup(void) {

u8x8.begin();
u8x8.setFlipMode(1); // set number from 1 to 3, the screen word will rotary 180
u8x8.setFont(u8x8_font_chroma48medium8_r);
u8x8.setCursor(0, 0);
u8x8.print(“Hello World4”);

// initialize the pushbutton pin as an input:
pinMode(BUTTON_PIN, INPUT_PULLUP);

}

int nb=0;
int buttonState2 = 0;
void loop(void) {

u8x8.setCursor(0, 0);
u8x8.print(“Hello World”);
u8x8.setCursor(10,0);
u8x8.print(nb);
nb++;

buttonState2 = digitalRead(BUTTON_PIN);
// check if the pushbutton is pressed. If it is, the buttonState is HIGH:
if (buttonState2 == HIGH) {
u8x8.setCursor(0, 3);
u8x8.print("HIGH ");

}else
{
u8x8.setCursor(0, 3);
u8x8.print("LOW ");
}

}

Hi there, Delphine and welcome,

Have a LQQK at this post and how to use the button.
HTH
GL :slight_smile: PJ

Hi,
I try with interupt as in your exemple (I just take the buttons parts), but it doesn’t work too.

#include <Arduino.h>
#include <U8x8lib.h>
#include <Wire.h>

U8X8_SSD1306_128X64_NONAME_HW_I2C u8x8( U8X8_PIN_NONE);


const int buttonPin = 1;     // the number of the pushbutton pin
int buttonState = 0;// variable for reading the pushbutton status
uint8_t binterruptCount = 0; // Amount of received interrupts

void setup() {
   pinMode(buttonPin, INPUT_PULLUP);// initialize the pushbutton pin as an input:
   attachInterrupt(digitalPinToInterrupt(buttonPin), myISR_Falling, FALLING);
   attachInterrupt(digitalPinToInterrupt(buttonPin), myISR_Rising, RISING);

    u8x8.begin();
    u8x8.setFlipMode(1); 
    u8x8.setFont(u8x8_font_chroma48medium8_r);
    u8x8.clearDisplay();
    u8x8.setCursor(0, 0);
    u8x8.print("setup");

}

void loop()
{
   u8x8.setCursor(0, 0);
   u8x8.print("loop");

   u8x8.setCursor(1, 1);
   if(buttonState == LOW)
   {
      u8x8.print("LOW");
   }else
   {
      u8x8.print("HIGH");
   }
}

void myISR_Falling() {        // Pressed Button Interrupt //
  binterruptCount++;
  buttonState = LOW;
  
}

void myISR_Rising() {        // Pressed Button Interrupt //
  binterruptCount++;
  buttonState = HIGH;
  
}

Do you have an idea ?

Hi there,
Yes that won’t work. Your not using the interrupt count, you want the state.
like this; also no debouncing…?

#include <Arduino.h>
#include <U8x8lib.h>
#include <Wire.h>

U8X8_SSD1306_128X64_NONAME_HW_I2C u8x8(/* clock=*/PIN_WIRE_SCL, /* data=*/PIN_WIRE_SDA, /* reset=*/U8X8_PIN_NONE);  // OLEDs without Reset of the Display


const int buttonPin = 1;     // the number of the pushbutton pin
int buttonState = 0;// variable for reading the pushbutton status
uint8_t binterruptCount = 0; // Amount of received interrupts

void setup() {
  Serial.begin(9600);
  while (!Serial);
  delay(3000);//relax...Get Ready for serial port
  Serial.println();
  Serial.println("Power ON \n ");  // Let's BEGIN!!
  Serial.println("Test program compiled on " __DATE__ " at " __TIME__);
  Serial.println();
  Serial.println("Processor came out of reset.");
  u8x8.begin();
  u8x8.setFlipMode(1);  // set number from 1 to 3, the screen word will rotary 180
  u8x8.setFont(u8x8_font_8x13B_1x2_r);
  u8x8.clearDisplay();
  u8x8.setCursor(0, 0);
  u8x8.print("Power ON ");
  delay(2000); 
   pinMode(buttonPin, INPUT_PULLUP);// initialize the pushbutton pin as an input:
   attachInterrupt(digitalPinToInterrupt(buttonPin), myISR_Falling, FALLING);
   //attachInterrupt(digitalPinToInterrupt(buttonPin), myISR_Rising, RISING);
buttonState = digitalRead(buttonPin);             // read the state of the pushbutton value:
    u8x8.clearDisplay();
    u8x8.setCursor(0, 0);
    u8x8.print("setup");

}


   //buttonState = digitalRead(buttonPin);             // read the state of the pushbutton value:
void loop()
{
   u8x8.setCursor(0, 0);
   u8x8.print(" Loop ");
   u8x8.setCursor(1, 3);
   if(buttonState == LOW)
     {
      u8x8.clearLine(3);
      u8x8.print("LOW");
       Serial.println("Button Push");
   }else
   {
      u8x8.clearLine(3);
      u8x8.print("HIGH");
      buttonState = HIGH;
       //Serial.println("Button Release");  // Turn on Released prompt
   }
   delay (500);
   buttonState = digitalRead(buttonPin);             // read the state of the pushbutton value:
  
}

void myISR_Falling() {        // Pressed Button Interrupt //
  binterruptCount++;
  buttonState = LOW;
  
}

void myISR_Rising() {        // Pressed Button Interrupt //
  binterruptCount++;
  buttonState = HIGH;
  
}

should get this when powered up and push the button.

Power ON 
 
Test program compiled on Nov  3 2023 at 20:45:46

Processor came out of reset.
Button Push
Button Push
Button Push

HTH
GL :slight_smile: PJ

Hi,
Thanks for the code. I set your code, push the button many time and I get this


Power ON 
 
Test program compiled on Nov  4 2023 at 13:15:03

Processor came out of reset.

On the screen, I read POWER ON then Loop and HIGH. It never change.

I must make one change , I replace

U8X8_SSD1306_128X64_NONAME_HW_I2C u8x8(/* clock=*/PIN_WIRE_SCL, /* data=*/PIN_WIRE_SDA, /* reset=*/U8X8_PIN_NONE);  // OLEDs without Reset of the Display

by

U8X8_SSD1306_128X64_NONAME_HW_I2C u8x8(U8X8_PIN_NONE); 

Because first line don’t compile (PIN_WIRE_SCL is not recognize) , and I see this solution on a forum.

Do you think the issue can come from a library ?
I use seeduino and arduino from years but it is my first time with an esp32. There is somthing special to do to have the entry ?

Thanks for your help !

It’s finaly works !
I have set the buttonPin to the GPIO number (3) insted of the Digital input (1).
But I don’t know why, the buzzer is on Digitial 3 /GPIO 5. And it works when I set “int speakerPin = A3;”.

Thanks for your help PJ_Glasso !

1 Like