Arduino Serial Hello World

Hey guys, this time I’m going to introduce a hello world example outputting to the serial monitor on your Arduino IDE. You’ll learn how to write a very simple code that sets your first variable, and output that variable on to your computer screen.

What you need:


This is where we create our variable. Our variable is a character array, more commonly known as a string. Arduino sends data via serial byte by byte, meaning 8 bits at a time. Later, you’ll find that you need to define the length of what you’re sending, when communicating with other devices some times.

The following code is our simple initialization of our character array. The key here is starting with “char”, this tells the IDE what type of variable we’re trying to set up. Next we name the variable “myString”, this is the second part of setting your variable. Then we set the char type to array by adding “[]” to the end of the name. Finally we set the value of our variable to “Hello World!”, and close the line out with a semi-colon. You’ll notice that every line has to be closed out with a semi-colon, this tells the compiler that you’re done with the current statement.


Next we’re going to start the setup function. Setup is only run once, right when the program is started on your device. This is a good place to start initializing things that need to be started prior to reading or writing. Here we will initialize the serial port, the first serial port is always linked to USB. It can be used for other things than USB, but I prefer to leave it that way.

You’ll see our complete code below, this is a very simple one-liner setup. We start by calling “void setup()”, this tells the compiler that we’re defining the setup function. You can define custom functions as well, but we’ll get to that later. To create a box for your setup to reside in, you need to enclose your code in curly braces, “{” and “}”. These curly braces tell the compiler what code belongs to the function you’ve created, which in this case is setup.

Now that Our setup function is defined we can call “Serial.begin”, capital letters do matter here – Serial needs to be capitalized. If we take this call apart, “Serial” is the object that we’re telling the compiler that we’re working with, and “begin” is the function we’re calling. Some functions have arguments that need to be passed to them, that is done inside the parentheses – “(9600)”. What we’re doing here is setting the baud rate to 9600. The baud rate is important, because it synchronizes the speed of data flow between your computer and the Arduino device.


Here’s the juicy part, finally the loop! The loop is where your main code is run, this code is run over and over and over until the end of time, or until the device is unplugged/reset. This is a very simple loop that prints the text we defined earlier in a variable, waits 1 second, then repeats.

We have 2 functions being called here. The first is “Serial.println(myString);”, this tells the Serial object to print a new line using the variable named “myString”. There is another function to Serial that is simply “Serial.print”, which would print everything on the same line. That is useful if you have a lot of code determining what to print, and you don’t want to do it on a new line yet. The second function is delay, note that it’s lower case. Most built in Arduino functions are lower case, and the objects are upper case. The argument we’re passing to the “delay(1000)” function is amount of time in milliseconds. So 1000 = 1 second.

The final product:

//Hello world example code
 //This will be an introduction to Arduino, via serial communication
 char myString[] = "Hello World!";

void setup()                    // This only runs once, right when you start the sketch
 Serial.begin(9600);           // Instantiate serial with a buad rate of 9600 bps


void loop()                       // Runs forever
 Serial.println(myString);  // Prints Hello World! via the string that we created pre-setup
 delay(1000); // Pauses for 1 second


Now let’s run the code! Go on to the next post to put your code to work!

Join the newsletter

Subscribe to get our latest content by email.
We won't send you spam. Unsubscribe at any time. Powered by ConvertKit

Leave a Reply

Your email address will not be published. Required fields are marked *