PlatformIO Community

Serial print not working when declaring global object from SPIClass

I’ve been getting this error, when I test a serial print without the declaration and initialization of a global object, my serial monitor prints gibberish. However, without it, it works fine. Does anyone have an idea of the cause?
My monitor speed is 9600, i’ve confirmed that, since it works fine without the global declaration.

Here is the code (a lot of it I commented out, while trying to figure out the problem)
image

What board are you using please?

I’m assuming from the use on Arduino.h that you are using the Arduino framework, but it also looks like you are using some kind of RTOS as well. Can you post your platformio.ini please.

When posting code, you can easily do it by enclosing the code parts in three back ticks like this:

```
   Serial.begin(9600);
   // Some other code here...
```

Those are not the same character as the single quote by the way, in case the font in your browser makes it look the same. :wink:

On an Arduino Uno, Serial is part of the library. When Arduino.h is included, that file includes a number of other files, one of which is HardwareSerial.h which defines the structure of a HardwareSerial class.

Also, it runs this code:

#if defined(UBRRH) || defined(UBRR0H)
   extern HardwareSerial Serial;
   #define HAVE_HWSERIAL0
#endif

Which will work fine on an Arduino Uno and declares Serial to be an object of the HardwareSerial class which will be found somewhere else. The actual code for the class is compiled into the library which is linked with your sketch code to create the final binary file for uploading.

So for the Uno, Serial is known about and is already a global object. This is why executing Serial.begin(9600); in setup() works even though you didn’t specifically declare what Serial is.

If you are using a different board, the code in HardwareSerial.h for your board will show something similar and should end up defining a global object of type HardwareSerial named Serial.

Now, the gibberish you report when you don’t execute a global initialisation is most likely down to the fact that if you don’t call Serial.begin() and pass a baud rate, the USART will not be correctly initialised. (Unless your board’s version of HardwareSerial::begin() defines a default baud rate, the Uno doesn’t.

Your code should work fine without needing a global to be declared and initialised. Does the following work for your board?

#include "Arduino.h"

void setup() {
   Serial.begin(9600);
}

void loop() {
    Serial.println("Hello World!");
    delay(1000);
}

So, in summary (because I do tend to go on and on a bit!):

  • Please tell us your board details;
  • Please post your platformio.ini file;
  • Are you using some form of RTOS?

Cheers,
Norm.

It may be possible that the ESP32 is crashing in the constructor of the SPI1 object and thus hasn’t even gotten to your setup() function where you set the baud to 9600. The default baud is 115200.

You should try adding / changing the platformio.ini to do

build_type = debug
monitor_filters = esp32_exception_decoder
monitor_speed = 115200

and see what it outputs. (see docs and related.)