Hi,
I have the Seeed xiao nRF52480 and am trying to see in my serial monitor whether a button is pressed or not. However, if I try to use serial communication, I get error messages in arduino.
Button example code works fine, as no serial communication is used. And the LED responds to the button fine.
const int buttonPin = 7; // the number of the pushbutton pin
const int ledPin = 13; // the number of the LED pin
int buttonState = 0; // variable for reading the pushbutton status
void setup() {
pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT);
}
void loop() {
buttonState = digitalRead(buttonPin);
if (buttonState == HIGH) {
digitalWrite(ledPin, HIGH);
} else {
digitalWrite(ledPin, LOW);
}
}
Once the serial communication is written into that example arduino disconnects for a second and once the button is released, connects again and resumes. So the LED will turn back on and the serial monitor will get new values printed. I’ve added tinyusb, as otherwise the serial communication will give an error, and I read that this might resolve that issue. (Same goes for using SPI or Wire.)
#include <Adafruit_TinyUSB.h>
const int buttonPin = 7; // the number of the pushbutton pin
const int ledPin = 13; // the number of the LED pin
int buttonState = 0; // variable for reading the pushbutton status
void setup() {
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT);
}
void loop() {
buttonState = digitalRead(buttonPin);
if (buttonState == HIGH) {
digitalWrite(ledPin, HIGH);
Serial.println(“not pressed”);
} else {
digitalWrite(ledPin, LOW);
Serial.println(“pressed”);
}
}
Does someone know what is happening? I’ve used serial communication within arduino for a long time and never had this issue. The same code also works with for instance an arduino uno or firebeetle ESP32.
Thanks in advance!