Author Topic: [SOLVED] ATmega32u4 - Sending debug (Serial.println) info via USB  (Read 1577 times)

0 Members and 1 Guest are viewing this topic.

Offline gmcTopic starter

  • Regular Contributor
  • *
  • Posts: 116
  • Country: gb
I've got a project that is using a ATmega32u4. I'm using all the I/O pins which are connected to various things like switches/OLED/led's etc.

I started this about a year ago and with some holiday time now want to finally finish the project.

I would like to send debug info via the USB port which is helpful when running the code, however try as I might I can't get this to work.  (I've been using Arduino's for years but don't have too much experience with using the ATmega32u4 as a standalone)

From what read for the 32u4 I need to use Serial1 so have this simple code:
Code: [Select]
void setup() {
  Serial1.begin(115200);
  Serial1.print("TEST");
}

void loop() {

}


However after opening up Arduino IDE/Monitor (Baud settings are correct), nothing is displayed.  Code works find on a Arduino Uno with Serial.print.

The atmega32u4 in the circuit works as I can program it, read switches, write to OLED etc. just the serial is not working.

The circuit is based off the Arduino Leonardo schematic.


Any ideas?  Such a simple thing is driving me crazy. Could it disabled in the bootloader? I programmed the bootloader a while back and can't remember when I sourced it from. Maybe used Arduino IDE to upload?

« Last Edit: December 24, 2021, 10:03:59 am by gmc »
 

Online magic

  • Super Contributor
  • ***
  • Posts: 6788
  • Country: pl
Re: ATmega32u4 - Sending debug (Serial.println) info via USB
« Reply #1 on: December 24, 2021, 09:30:50 am »
Ordinary Arduinos have a USB-UART chip and talk to the AVR by serial.
Here USB goes to the 32U4 and you can only talk to the bootloader (which pretends to be a USB-UART chip, if you use the Leonardo one).
Once your firmware is running, the functionality is gone. You would need to link some USB library into your code and implement your own USB debug.

It looks like what you have done is set up the MCU's built-in UART. That's an option too, but you now need to connect some external USB-UART dongle to the TX/RX pins, because your board doesn't have that. Mind voltage levels - the dongles come in either 3.3V or 5V, some of them may work with both.
« Last Edit: December 24, 2021, 09:33:05 am by magic »
 
The following users thanked this post: gmc

Online tszaboo

  • Super Contributor
  • ***
  • Posts: 7392
  • Country: nl
  • Current job: ATEX product design
Re: ATmega32u4 - Sending debug (Serial.println) info via USB
« Reply #2 on: December 24, 2021, 09:36:21 am »
Replace Serial1 with Serial.
Good luck.
 

Offline gmcTopic starter

  • Regular Contributor
  • *
  • Posts: 116
  • Country: gb
Re: ATmega32u4 - Sending debug (Serial.println) info via USB
« Reply #3 on: December 24, 2021, 09:57:26 am »
I already tried Serial.print - that doesn't work.

Is my only option to use Serial1 and then have use separate USB/TTL converter like a CH340?

How does this work on the Arduino Leonardo as my circuit it almost identical that?

Update: Solved. I found a thread with someone having a similar issue and the solution was to use a while.

Code: [Select]
void setup() {

  Serial.begin(115200);

  while (!Serial) {
    Serial.print("TEST");
  }


}

void loop() {

}
« Last Edit: December 24, 2021, 10:03:41 am by gmc »
 

Online tszaboo

  • Super Contributor
  • ***
  • Posts: 7392
  • Country: nl
  • Current job: ATEX product design
Re: ATmega32u4 - Sending debug (Serial.println) info via USB
« Reply #4 on: December 24, 2021, 10:29:52 am »
I already tried Serial.print - that doesn't work.

Is my only option to use Serial1 and then have use separate USB/TTL converter like a CH340?

How does this work on the Arduino Leonardo as my circuit it almost identical that?

Update: Solved. I found a thread with someone having a similar issue and the solution was to use a while.

Code: [Select]
void setup() {

  Serial.begin(115200);

  while (!Serial) {
    Serial.print("TEST");
  }


}

void loop() {

}
Yes, you have to wait for the USB protocol to exchange niceties before sending anything.
 
The following users thanked this post: gmc

Online magic

  • Super Contributor
  • ***
  • Posts: 6788
  • Country: pl
Re: [SOLVED] ATmega32u4 - Sending debug (Serial.println) info via USB
« Reply #5 on: December 24, 2021, 11:44:06 am »
Nice, I didn't realize they made it that simple :palm:

I do have an issue with that code, though. According to this the Serial object becomes "true" when USB serial emulation is ready to transfer data. In your code it looks like printing is attempted for as long as serial is not ready and then once serial becomes ready the loop exists.

I think you want:
Code: [Select]
while (!Serial);
Serial.print("TEST);
 
The following users thanked this post: gmc


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf