Arduino Serial Input Basics



The new stuff has to do with the serial monitor: In setup , look for Serial.begin (9600); which tells the Arduino to start communicating via text, at 9600 baud. The baud rate controls how much information is sent per second.

  1. Arduino Serial Input Basics Keyboard
  2. Arduino Forum Serial Input Basics
  3. Arduino Serial Port Basics

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.

Things should be looking pretty familiar by now. The new stuff has to do with the serial monitor: In setup, look for Serial.begin(9600); which tells the Arduino to start communicating via text, at 9600 baud. The baud rate controls how much information is sent per second. Digital Input -How to use the button with Arduino. Uncategorized 0 Comments Pushbutton is used to provide manual input to the microcontroller, we can program the microcontroller to perform a task when switch/button is pressed. A step by step illustrated basic tutorial for Arduino. Here we are taking analog input form a potentiometer. And this input is shown on LED as PWM and analog values on Serial monitor. Arduino gives analog output in range of 0 to 255. Technically the output is digital but in the form of PWM, but it seems to be analog. Arduino Boards have 6 PWM. The final chunk of the loop function prints the current value to the Arduino Serial Monitor, and sends the current value to the digital feed on Adafruit IO. We also set last = current; so we can tell if the state of the button has changed in the next run of the loop.

What Can Serial Input Be Used for?

Arduino Serial Input Basics Keyboard

Arduino Serial Input Basics

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.

If we send more than one character over serial with this code, the output will look like this:

Arduino Forum Serial Input Basics

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.

Sending Commands

Arduino Serial Port Basics

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:

Arduino Serial Input Basics

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

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().