14

I have a simple sketch that prints out "Hello World" to the serial, but I'm getting a bunch of gibberish instead.

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

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

Why isn't it printing "Hello World" like the code says?

2
  • Have you checked if initialization of serial port is proper and matches with PC serial terminal ? The setup() should have something like Serial.begin(9600);
    – Rajendra
    Feb 17, 2014 at 4:23
  • @Rajendra that line is in the code example I gave. Besides, I'm not actually having the problem. It's a common issue for beginners and I answered the question below.
    – sachleen
    Feb 17, 2014 at 4:29

3 Answers 3

10

The board and terminal are probably not using the same baud rate. Check the bottom right corner of the Arduino Serial Terminal and make sure it is set to 9600.

Serial Data

Serial communication works by sending the entire data stream one bit at a time. This is in contrast to the standard parallel port which sends one byte at a time. Both devices need to know at what speed the data is being sent. Furthermore, both devices need to be operating at the same speed. This is called the baud rate.

Baud Rate

Baud really just means pulses per second. In the context of serial communication, it means how many bits are transmitted per second. One of the most common values is 9600, but there are other common ones which are factors or multiples of 9600. It doesn't really matter, though, as long as both devices operate at the same speed.

If I'm sending you messages faster than you are getting them, you lose data. If I am sending you messages slower than you are receiving them, you get nonsense mixed in with what I sent you.

Data, Parity, Stop bits

If you look at the documentation for begin(), you'll see it can take two parameters:

Serial.begin(speed)
Serial.begin(speed, config)

Hopefully you understand speed now, it is the bits per second at which the Arduino will communicate.

The second parameter, config, lets you configure some other things about the serial data transmission. These are the Data, Parity, and Stop bits. You'll see on the page it has values like 5N1, 6N1 and SERIAL_8N1 (the default)

The first number represents how many data bits. The second character tells you what kind of parity bit there is. E for even parity, O for odd parity, or N for no parity. The third number tells you how many stop bits.

So really, with 8N1, we have 10 bits being sent for each 8 bits of "your" data. 1 start bit, 8 data bits, and 1 stop bit.

Anyway, both sides need to agree on this information as well as the speed. The Arduino Serial Terminal doesn't give you the option of changing these settings, but others do, so you'll need to be sure all of these configuration settings are the same.

More info on wiki: Asynchronous serial communication

2

Sachleen's answer (above) says:

The board and terminal are probably not using the same baud rate. 
Check the bottom right corner of the Arduino Serial Terminal
and make sure it is set to 9600.

If this number is 9600, and you still get garbled data, check the output you obtained when uploading the code. It should read something like:

Sketch uses 2754 bytes (8%) of program storage space. Maximum is 30720 bytes.
Global variables use 195 bytes (9%) of dynamic memory, leaving 1853 bytes for local variables. Maximum is 2048 bytes.
C:\Program Files (x86)\Arduino\hardware\tools\avr/bin/avrdude -CC:\Program Files (x86)\Arduino\hardware\tools\avr/etc/avrdude.conf -v -patmega328p -carduino -PCOM5 -b57600 -D -Uflash:w:C:\Users\Owner\AppData\Local\Temp\arduino_build_457862/LockCode.ino.hex:i 

avrdude: Version 6.3, compiled on Jan 17 2017 at 12:00:53
         Copyright (c) 2000-2005 Brian Dean, http://www.bdmicro.com/
         Copyright (c) 2007-2014 Joerg Wunsch

         System wide configuration file is "C:\Program Files (x86)\Arduino\hardware\tools\avr/etc/avrdude.conf"

         Using Port                    : COM5
         Using Programmer              : arduino
         Overriding Baud Rate          : 57600

This bottom line (of what I have posted) says what your device's default baudrate is. For me, I changed Serial.begin(9600); --> Serial.begin(57600); and checked that the bottom right corner of my Arduino Serial Terminal read 57600.

After I ran this, and it worked, I was able to switch back to 9600 and get non-garbled data. Arduino seems to be finicky...

0

I spent half an hour trying to solve that, and it was my Arduino Pro Mini that is 3.3V and I had selected 5V in the Arduino IDE (on PC). Serial print never works like that even with same baud rate defined.

Select the correct board in the Arduino IDE.

To be clear, the correct processor (voltage/clock speed) under the Tools tab has to be selected.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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