Arduino Hello World: Easily Understand How to do it Yourself! Find out How to Create the Hello World code, and Find out Exactly How and Why it works. It looks Simple but there's a lot going on Under The Hood!

Arduino Hello World: Making your Arduino output those words will give you confidence in using any Arduino and allows you to see how easy it is to begin using any Arduino board.

arduino hello world thumbnail

Arduino Hello World: Components

Creating the Arduino Hello World example only requires the following items:
  • An Arduino Uno Board (More information here: Arduino Uno),
  • A USB cable - to attach the Arduino to a PC,
  • The Arduino IDE running on the PC.
The Arduino Hello World example is one of the simplest programs you can write, made simpler by the fact that it uses the serial link that is normally used to program the board.

Arduino Hello World: Programming

Arduino boards are unique in that all Arduino's are shipped with a pre-programmed microcontroller that lets you communicate via the PC. This USB serial link allows the PC to send data to the Arduino so that you can program it with your program (written in C/C++).

For the Hello World example you will simply use this serial link to:
  1. First program the Arduino,
  2. Then use the same serial link to get data back to the PC from the Arduino.
Essentially, after a reset the Arduino waits for a signal from the PC that programming is going to start. When it gets the signal (a bit of serial data), it takes subsequent serial data, using it to program the Flash memory. After this, at the end of serial data, it starts your program.

Note that The Arduino chip will always wait a bit after a reset (its waiting for the serial signal to start programming) but after a few seconds it starts your program. You won't notice this delay in normal use so after each reset (or power down/up) your program will start.

The important point is that the serial link that was used to program your Arduino is now freed up to use in your program! This is how the Aduino "Hello World" output will be created.

Arduino Hello World: Setup for Arduino Uno

The Arduino IDE can program many different types of Arduino so to ensure the IDE is talking to an Arduino Uno, use the menu to select "Arduino Uno":

    Menu > Tools > Board: > Arduino AVR Boards >Arduino Uno

arduino hello world board selection

The Arduino interface will now update to show that the Arduino Uno board is now in use.

hello world arduino uno selected

Hover the mouse over the tools menu (as above) to see that the text in the Tools menu has changed to "Board: Arduino Uno". You can also see that Arduino Uno is in use from the text at the bottom right of the IDE.

Arduino Hello World: Select port

In order for the PC to communicate with the Arduino Uno, a virtual Serial RS232 port must be used. PC's used to have a physical serial I/O port on a 9-pin d-type for serial communication, but without this hardware the serial link is emulated in a USB port.

There could be multiple serial links in a system so you need to select the one that is attached to your Arduino Uno. Here that port is labelled COM4 - your port could have a different number.

The word COM is short for communication so it is serial communications port 4.

hello world select serial communications port

Select the Arduino Uno port (here COM4) to continue.

Arduino Hello World: Example Sketch

There are two parts to an Arduino program or two functions setup() and loop(). Setup() is usually used to initialise components in your system e.g objects associated with external hardware e.g. an accelerometer ADXL345 etc.

loop() is the function that is repeated so you would check the accelerometer for changes every time round the loop.

For the Arduino Hello World example we only need to output the text once, so the code is placed into the setup() function while the loop() function is left empty.

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

   Serial.println("Hello World");
}

void loop(void) {
}

Note the code:

Serial.begin(9600);

is an initialisation code the applies the method "begin" to the Class object Serial and informs the underlying hardware (microcontoller) that the serial interface will be operating at 9600 baud (bits per second).

Note this baud rate must be matched in the Arduino IDE otherwise gibberish will be printed. Fortunately the default IDE setting is 9600Baud.

Arduino Hello World: Type the sketch into the IDE

Now copy and paste (or type) the above program into the Arduino IDE.

How this looks in the Arduino IDE 2.0.4

arduino hello world

Arduino Hello World: Program the Sketch.

Push the programming button (top left arrow pointing right - it will turn yellow):
arduino hello world compiled uploaded

The lower status screen labelled "Output" shows you the output of the compiler (effectively it shows what is happening under the hood).

You can see a report of the amount of program storage space used (here 4%) and amount of SRAM (variable memory space) used (here 9%). This useful to see how much memory you have left to play with when you are developing your code.

You also get dynamic messages showing the state of compilation:
  • "Done Compiling" - The process of turning C/C++ code into hex bytes for your specific microcontroller chip.
  • "Done uploading" - The process of loading the hex bytes into the microcontroller.
TIP: If you are quick you will see the LEDs on the Arduino board flashing as the code is uploaded.

Arduino Hello World: Serial output

Finally push the top right button (magnifier and dots) to pull up the "Serial monitor" or use the menu:

    Menu > Tools > Serial Monitor

The new Arduino IDE incorporates the serial output into the same window (previous versions of the Arduino IDE pulled up a new window):

hello world serial output

You can now see the final output - this is the Arduino "hello world" text which has been sent back over the serial link from the Arduino Uno to the PC.


Comments

Have your say about what you just read! Leave me a comment in the box below.

Don’t see the comments box? Log in to your Facebook account, give Facebook consent, then return to this page and refresh it.



Privacy Policy | Contact | About Me

Site Map | Terms of Use