Xiao nRF52840 serial monitor

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!

I tried the following code and it worked fine.

#include <Adafruit_TinyUSB.h>
const int buttonPin = 1; // 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);
  while(!Serial);
  pinMode(13, OUTPUT);
  pinMode(1, INPUT_PULLUP);
}

void loop() {
  buttonState = digitalRead(1);

  if (buttonState == HIGH) {
    digitalWrite(13, HIGH);
    Serial.println("not pressed");
  } else {
    digitalWrite(13, LOW);
    Serial.println("pressed");
  }
  delay(100);
}

What is the benefit the TinyUSB?

I have never used this?

Hi there,
AFAIK, It’s supports BSP 1.1.1 non-mbed setups for Serial.begin.
HTH
GL :slight_smile: PJ

Me neither but if I don’t add either TinyUSB, SPI or WIRE then I get an error that Serial is not defined. So I read somewhere that adding TinyUSB solves at least makes some sort of serial communication possible.

Turns out my wiring was wrong, as I put the resistor to the pushbutton pin rather than the ground or voltage. So it was short circuiting. Thanks for all the fast help tho!

2 Likes