Incosistent input on XIAO

I am getting inconsistent readings on the inputs to the XIAO. Attaching the code. At first I had 3 buttons to DI0,1,2 and I was getting strange inputs that were being read on the serial monitor. I removed the three buttons and I am still getting strange readings. Not sure why the input turns to a value 1. There is nothing connected to any other inputs.

Any suggestions on what I can do? I am connecting the XIAO to the USB-C.

#define BUTTON_0 0 
#define BUTTON_1 1 
#define BUTTON_2 2

boolean buttonState_0;
boolean buttonState_1;
boolean buttonState_2;

void setup()

  pinMode(BUTTON_0, INPUT);
  pinMode(BUTTON_1, INPUT);
  pinMode(BUTTON_2, INPUT);


  while (!SerialUSB) ;  

void loop()        

  buttonState_0 = digitalRead(BUTTON_0);
  buttonState_1 = digitalRead(BUTTON_1);
  buttonState_2 = digitalRead(BUTTON_2);

  Serial.println ();
  digitalWrite(LED_BUILTIN, buttonState_0);
  digitalWrite(LED_BUILTIN, buttonState_1);
  digitalWrite(LED_BUILTIN, buttonState_2);


I infer that you are expecting digitalRead() values to be zero when nothing is attached to a pin, and you have button switches connected to D0, D1, and D2 that pull their respective pins high (3.3Volts from the XIAO) when pressed.


Here’s the deal, from digitalRead() - Arduino Reference

Now, to make the pins read zero when nothing is connected, you can change your XIAO pinMode() statements to


The digitalRead() values for these pins will be zero when the corresponding switch is not pressed and will be one when the switch connects the pin to Vcc (3.3Volts).

You can use your original pinMode() assignments (without the second argument) if you install three external resistors (10K is a typical value) to the input pins, that pull down the pins to ground. See Footnote

A discussion of digital inputs relevant to your project can be found here (and a gazillion other places on the Web):



Footnote: Not all Arduino processors have internal pull-down resistors, but all have internal pullups, to use these as digital input pins with no external resistors, change pinMode function calls to


Now, when a switch is not pressed, its digitalRead() value will be one.
Connect each switch’s other terminal to Gnd from the XIAO, and when a switch is pressed, its digitalRead() value will be zero.

It seems to me after reviewing hundreds of OPC (Other People’s Code) sketches that this is, by far, the most common type of switch input connection and will work on all Arduino boards, as far as I know, whereas INPUT_PULLDOWN arguments may not be available for some Arduino CPUs.

Bottom line: Chacun à son goût!

1 Like

Thanks very much Dave! It did work, and thank you for teaching me something today.

1 Like

It was my pleasure.

Bottom line: Onward and upward!