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.

Join the newsletter

Subscribe

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 *

This site uses Akismet to reduce spam. Learn how your comment data is processed.