0

I'm having a strange issue where in order to get the expected serial output on my serial monitor, I have to set the baud rate on my serial monitor to be 8 times higher than the rate that i set in my Serial.begin(). For example, Serial.begin(4800) works with a serial monitor baud rate of 38400.

The setup is an atmega328P and an atmega8u2 built up on a breadboard similar to an UNO R1. I followed Nick Gammon's tutorial and his scripts to flash the 328P with the bootloader (http://www.gammon.com.au/forum/?id=11637), and I used the DFU programmer (on OS X) to flash the 8u2. When I plug my computer into the USB connector on the breadboard, the device correctly shows up as an Arduino Uno in my computer's devices list using lsusb. I can also flash sketches to the 328P using the breadboard tutorial (https://www.arduino.cc/en/Tutorial/ArduinoToBreadboard).

The fact that I get the expected results on my serial monitor (even though it's at the wrong baud rate) leads me to believe my hardware is set up correctly. I'm thinking that it could be a bit/byte confusion in the firmware, has anyone encountered something like this or have ideas? I currently can't flash sketches over the USB connector and I believe it is due to the baud rate mismatch.

  • 1
    Sounds like F_CPU is not the same as the actual frequency of the chip. Check your board configuration and your fuse settings. – Majenko Nov 13 '18 at 22:52
  • The trick though is to find if it's the 8U2 or the 328P that is the wrong speed. You really need a scope or logic analyser for that. Or another USB to UART adaptor that you know can work at the right speed. – Majenko Nov 14 '18 at 10:14
1

There are two baud rates you need to diagnose. One is the baud rate the ATMega8U2 runs at, and the other is the baud rate the ATMega328P runs at.

You need to work out which one is being calculated wrong:

  • The ATMega8U2's baud rate is set by the USB connection when you open the port
  • The ATMega328P's baud rate is set by your sketch

Obviously, the two have to match. However, a baud rate isn't just set as a number, it's calculated as a fraction of the CPU clock.

If the CPU clock on just one of the chips is different to what the firmware or sketch expects then the calculation for the baud rate will be wrong.

An example:

Your ATMega328P is expected to be running at 1MHz - however, the fuse settings have it set to run from the internal 8MHz clock. That means everything on that chip will run 8× faster than expected, so you have to compensate with a lower baud rate.

Another example:

Your ATMega8U2 is expected to be running at 16MHz - however, the fuse settings have it set to run at 2MHz. That means everything on that chip (except the USB which is clocked separately) will run 8× slower than expected, so you have to compensate on the ATMega328P with a lower baud rate.

So check the fuse settings on both chips. Make sure they are set to the right speeds for your firmware and boards.txt file settings.

Also, you should look at getting a cheap oscilloscope or logic analyser. That would allow you to monitor precisely what is going on with the communication between the two chips and work out if the baud rate is actually 8× faster than expected (ATMega8U2 at fault), or the normal speed but you have to specify it as a lower number (ATMega328P at fault).

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.