Serial communication
As we have seen before, we are able to send messages from Arduino to the computer to be displayed on serial monitor. Similarly, we can use the serial protocol to send messages from computer to Arduino.
Computer
We need to do nothing from the computer as we will be using the serial monitor as our input.
Arduino
The following code shows the basic message sending from computer to Arduino.
Serial.begin(...)
sets the baud rate of the serial communication between computer and Arduino, in this case, 9600. The baud rate on the computer and the Arduino has to be the same to ensure correct understanding of the transferred data.Serial.print(...)
andSerial.println(...)
send characters from Arduino to computer through the serial protocol.Serial.available()
provides the information whether there is any incoming serial messages. If any message is waiting to be received, this will returntrue
or else will befalse
.Serial.read()
reads one character of data from the incoming serial message. This one character is then saved into the variableinput
(defined with characterchar
data type).
What happens when you provide a sentence instead of just one character?
Sending a sentence
We can make Arduino to read more than one character by using Serial.readStringUntil(...)
.
This code will display the whole message until "\n
", that is the newline character that's at the end of a string sent from the serial monitor.
Sending command
The following code shows an example of sending a string to Arduino to start different tasks. Take note on the use of .equals
which is a function to compare the String
variable with another String
object. If you are using char
variable, you can use the ==
operator to compare with a single character.
This tutorial is adapted from https://www.norwegiancreations.com/2017/12/arduino-tutorial-serial-inputs/.