[SOLVED] ATmega32u4 - Sending debug (Serial.println) info via USB - Page 1
Forum section: electronics / general

author: gmc
author_rank: Regular Contributor
timestamp: December 24, 2021, 09:07:25 am
content: 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: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?

author: magic
author_rank: Super Contributor
timestamp: December 24, 2021, 09:30:50 am
content: 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.

author: tszaboo
author_rank: Super Contributor
timestamp: December 24, 2021, 09:36:21 am
content: Replace Serial1 with Serial.Good luck.

author: gmc
author_rank: Regular Contributor
timestamp: December 24, 2021, 09:57:26 am
content: 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:void setup() {Serial.begin(115200);while (!Serial) {Serial.print("TEST");}}void loop() {}

author: tszaboo
author_rank: Super Contributor
timestamp: December 24, 2021, 10:29:52 am
content: Quote from: gmc on December 24, 2021, 09:57:26 amYes, you have to wait for the USB protocol to exchange niceties before sending anything.

author: magic
author_rank: Super Contributor
timestamp: December 24, 2021, 11:44:06 am
content: Nice, I didn't realize they made it that simpleI do have an issue with that code, though. According tothisthe 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:while (!Serial);Serial.print("TEST);