Hello World Tutorial

Sending Serial Data From PC to Arduino

Today we’re going to learn how to talk to the Arduino. We have already established how to listen. Listening is cool and everything, but we all like to be able to talk sometimes too. This can be very useful when learning how to send commands to your Arduino. You can send text, and have the Arduino act based on what you sent. That’s not what we’re doing today, we’re just going to do a simple send from the serial monitor, and have the Arduino tell us what we said. Do I hear an echo?

What is it?

I always like to start with a what is it. What is it to send serial data from the PC to the Arduino? We all know what serial is, it’s a protocol which literally communicates in 0’s and 1’s, or highs and lows – very dang fast. The serial monitor makes it very easy to send things to the Arduino. There’s a send box built right in to it! All you do, is type in what you want to send, click send and it goes. The part we need to figure out is how to receive that data with the Arduino.

Uses

One of the common uses for this, is as a debugging tool. You can create smaller sections of a large program, and test them using this method. If you’re planning on acting based on received data from another device, you could simulate that data by hand in the serial monitor. Then you can see when and how it acts based off of your input.

Another use is for triggering functions. You may use this as your primary method of controlling the Arduino. We’ll get in to an example of that in a later post, in that post we’ll control an led using text.

Usage

This is the syntax from Arduino.cc:

Serial.readString() 

That’s it! Super complicated, huh? No arguments, nothing! This function simply reads the entirety of what you’ve entered, and you can add it to a variable.

Example

Here’s an example of the usage of Serial.readString().

String data;//initialize string variable

void setup() {
 Serial.begin(9600);//initialize serial
}

void loop() {
 while (Serial.available()){//loop while data is available
 Serial.print("You typed: ");
 data = Serial.readString();//read the data
 Serial.println(data);//print the data to serial monitor
 }
}

This code does nothing, until it sees serial data is available on the usb receive pin. It will simply loop and wait for serial data. Once it sees that there is serial data, with Serial.available(), it will parse that data. The next line you see is simple serial.print stating “you typed:”. Followed by data = Serial.readString();, this will assign the data to a string for later use. The final line prints out the data read from the string. Very simple!

Let’s upload, and run it!

Now, we can open the serial monitor, the output will be blank. In the top of the serial monitor, there’s a text box where you can type. Next to it is a send button.

In here, we can type what ever it is that you’d like to send for your test.

Let’s give it a whirl, and click send!

Now we’re well equipped for some serious conversation with our Arduino! Some of us aren’t able to make friends as quickly as others, so you can obviously see how this would be beneficial in the future.

Hello World Tutorial

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:

Pre-Setup:

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.

Setup:

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.

Loop:

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!

Arduino USB Serial

Uploading Code to Arduino and Using Serial Monitor

This post is assuming you’ve read my previous post, Arduino Serial Hello World. There you learned how to write the “Hello World” code, in preparation for output to the Serial Monitor. By now you should also understand the Basics of Arduino. These 2 are the prerequisites of this project. You could, of course, just copy my code and upload it.

Let’s get started!

I’ll be using the Arduino Uno, this is the most commonly known device that is used for Arduino programming. If you’ve read the Arduino Serial Hello World post, you’ll already have the code in your IDE.  If not, you can copy the following code and paste it in the Arduino IDE.

//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
}

Let’s get connected

If you haven’t already, you may want to configure your IDE for connection with the board. Following the configuration process allows the IDE to properly communicate with the board. It’s a very simple process, so it shouldn’t take too long.

After you do your one time configuration process, you’re ready to start uploading! Let’s plug USB in to the Arduino, and the other side into your computer.

You may notice I’m not using the default USB that came with the Arduino Uno. I choose to use a longer USB, because it helps keep my work area clutter free. Once you start more complex projects, you may run out of room in your work area.

Let’s upload

Now that you’re connected, you may see that the LED’s came on when plugging in your device. It’s alive! If you have it configured properly, you’re ready to upload. Click the arrow that points to the right, at the top left of your screen.

Now that you’re uploading, you can watch the progress in the black area at the bottom of your screen. There’s also a small progress bar that shows how it’s going.

This process should be very fast, on such a small sketch. When you start diving in to libraries, and using more complex code, it’ll take a little longer.

Where’s my output?

Now you’ll open the serial monitor. This is where the Arduino can send messages back to your computer. This is a very good area to try to use for debugging things. Click the icon that looks like a magnifying glass, at the top right of your screen.

If all is going well,  you’ll see the Arduino communicating with the Serial Monitor. It should say “Hello World!”. You’d think it would have more to say than that, but it really only talks about the things we tell it to. If you’re not talkative, neither is your Arduino, unfortunately. Don’t expect your Arduino to be the conversation starter, you’re the one in control here.

Thanks!

I hope you found this easy to follow, let me know in the comments if you have any questions. I’m here to help you, and I’d be more than happy to do so. Feel free to Subscribe below, you’ll find that I’m going to slowly work you in to more complex development. I feel you’d benefit from it a lot, if you follow along.