Arduino Serial Monitor Input
Mar 19, 2015 A user can enter data in the input field in the serial monitor window to send values and data to the Arduino. Any serial program, or even a custom serial application can be used to send data to the Arduino instead of using the Serial Monitor window. Except for part 13 of this course, the Serial Monitor window has only been used for output purposes. It was used to display the results or outputs. Here, I will describe how to read the input from Serial Monitor and show the input. For this, we require Arduino IDE for writing the program. Here, I have my IDE.
- Arduino Input Through Serial Monitor
- Arduino Serial Monitor Download
- Arduino Serial Monitor Analog Input
In almost every Arduino tutorial we’ve written we’ve used serial output for either printing text to terminal or plotting values. It is also invaluable as a debugging tool. We have rarely written about serial input, however, which is what this post is about.
What Can Serial Input Be Used for?
The possibilites with serial inputs are endless. Maybe you want to display text on an LCD display, punch in numbers to controll LEDs, control motor movement with arrow keys or send commands to decide which functions to call. No matter what you decide to use it for, your system reaches a higher level of interactivity.
How?
Using serial inputs is not much more complex than serial output. To send characters over serial from your computer to the Arduino just open the serial monitor and type something in the field next to the Send button. Press the Send button or the Enter key on your keyboard to send.
Coding wise, let’s dive into an example.
There are two important functions related to the serial input in the code above, and that is Serial.available()
and Serial.read()
.
Serial.available()
returns the number of characters (i.e. bytes of data) which have arrived in the serial buffer and that are ready to be read.
Serial.read()
returns the first (oldest) character in the buffer and removes that byte of data from the buffer. So when all the bytes of data are read and no new serial data have arrived, the buffer is empty and Serial.available()
will return 0.
Whatsapp apk free download for pc. If we send more than one character over serial with this code, the output will look like this:
But what if you want to send more than one character in handle it in a sensible way? No problemo!
Here we’ve introduced the readStringUntil()
function, which makes it possible to combine all the characters in the sent message into a single Arduino string. In this case we’re waiting for the n
character, which is the newline character that comes at the end of a string sent in the Arduino serial monitor.
Arduino Input Through Serial Monitor
Sending Commands
Arduino Serial Monitor Download
A more usable scenario could be to send commands to the Arduino. Depending on what you send, the Arduino will perform different task. Here’s a somewhat abstract example on how to do this:
Notice the use of the equals()
function. This is a function in the Arduino String class which returns true
if the string in question is equal to the parameter string.
Summary
Arduino Serial Monitor Analog Input
Serial inputs can be very useful in your Arduino project. It’s a bit more complex than serial output, but not by much! The key functions are Serial.available()
and Serial.read()
.