Normally every Arduino board is programmed via the serial interface.
But not only the program code can be transferred to the Arduino. You can also send control commands to the Arduino or receive values from the Arduino.
How to do this and what options you have I describe in the following article.
Safety instructions
I know the following notes are always kind of annoying and seem unnecessary. Unfortunately, many people who knew "better" have lost eyes, fingers or other things due to carelessness or injured themselves. Data loss is almost negligible in comparison, but even these can be really annoying. Therefore, please take five minutes to read the safety instructions. Because even the coolest project is not worth injury or other trouble.
https://www.nerdiy.de/sicherheitshinweise/
Affiliate links/advertising links
The links to online shops listed here are so-called affiliate links. If you click on such an affiliate link and make a purchase via this link, Nerdiy.de will receive a commission from the relevant online shop or provider. The price does not change for you. If you make your purchases via these links, you support Nerdiy.de in being able to offer other useful projects in the future. 🙂
Prepare serial interface
To use the serial port in your program you have to initialize it.
Dies geschieht indem Ihr in der “setup()”-Funktion die Funktion
Serial.begin(115200);
aufruft. Die Zahl “115200” steht dabei für die Baudrate – also die Geschwindigkeit – mit der die Symbole Nicht Bits oder Bytes) über die serielle Schnittstelle übertragen werden. Diese Geschwindigkeit solltet ihr von der “Qualität” der Verbindungsleitung abhängig machen.
For example, if you have a shielded and very short cable, the speed can be set very high.
For unshielded and/or very long cables, this speed should not be set so high.
Common baud rates are for example the following values:
50 110 150 300 1200 2400 4800 9600 19200 38400 57600 115200 230400 460800 500000
Egal welche Geschwindigkeit Ihr einstellt, dieser Wert muss auch im “Seriellen Monitor” – also dem anderen Kommunikationspartner der seriellen Kommunikation – eingestellt werden. Nur dann werden die Zeichen korrekt empfangen und gesendet. Mehr dazu erfahrt Ihr im Laufe des Textes.
If you are unsure which baud rate to use I recommend values between 9600 baud and 115200 baud. These and the values in between are also the common values for many finished products like sensors and actuators.
Send data from the Arduino board to the connected computer
To send data from your Arduino board to the connected computer you have to call the following function.
Diese sendet den Text “Hallo Welt!” inklusive Zeilenumbruch an den Computer.
Serial.println("Hello world!");
As an example you can try the following code.
void setup() { Serial.begin(115200); } void loop() { Serial.println("Hello world!"); delay(1000); }

In der folgenden Kombination sendet die Funktion den Text “Hallo Welt!” ohne Zeilenumbruch an den Computer.
Serial.print("Hello world!");
There is another example of this.
void setup() { Serial.begin(115200); } void loop() { Serial.print("Hello world!"); delay(1000); }

Wollt Ihr Variabel-Werte – in diesem Fall den Wert der Variable “lustige_variable” – senden könnt Ihr dies wie folgt tun:
uint8_t funny_variable=5; Serial.println(String(funny_variable));
As an example, the whole thing looks like this again.
void setup() { Serial.begin(115200); } void loop() { uint8_t funny_variable=5; Serial.println(String(funny_variable)); delay(1000); }

Sending a line break means that in addition to the actual message content, a control character is also sent that signals the recipient to change to the next line.
Use serial monitor
The Serial Monitor is the counterpart to the Arduino board. It is the program which receives the data from the computer and makes it readable for you.
Ihr findet es als Teil der Arduino-IDE unter dem Menüpunkt “Werkzeuge/Serieller Monitor”. Tools wie dieser “Serielle Monitor” werden auch Terminalprogramme genannt. Eine gute Alternative zu dem “Seriellen Monitor” der Arduino-IDE ist auch das Programm HTERM.

In the window of the serial monitor you can set the baud rate. Here you have to set the same value that you have set before in the program code.
If you have used the command
Serial.begin(115200);
so you have to set a baudrate of 115200 baud in the serial monitor.
Besides the baud rate you can also set the line terminator. You only need this setting if you want to send data from the computer to the Arduino.
Here you can set the character that is automatically appended and sent after each value is sent.
You can use this in your code for example to detect on the Arduino if the transmission of a command or value is completed.
Im Bereich “Befehle und Parameter an den Arduino senden” wird dies ausführlich erklärt.
Use serial plotter
The serial plotter is a tool with which you can display numerical values (e.g. measured values) directly on the computer as a time curve. To do this, you must program the Arduino so that it sends the numerical values to the computer.
This can be done, for example, with the command:
Serial.println(VariableWithAReading);
Die Variable “VariableMitEinemMesswert” sollte natürlich euren Messwert als Zahl enthalten.
Auf dem Computer müsst ihr dann den Seriellen Plotter starten. Diesen findet ihr unter “Werkzeuge/Serieller Plotter”.
Ein simples Beispiel das auf dem seriellen Plotter eine Sinus-Kurve ausgibt ist das folgende. Probiert den Code einfach aus. Er sollte sich auf jedem Arduino ausführen lassen. Denkt daran Euren “Seriellen Plotter” auf 115200 baud einzustellen.
/* _ _ _ _ _ | | | | |(_) | | __ ____ ____ __ | | | ___ _ __ __| | _ _ _ __| | ___ / / / / / / / / / | . ` | / _ | '__|/ _` || || | | | / _` | / _ V V / V V / V /_ | | || __/| | (_| || |_| | _| (_| || __/ \_/\_/ \_/\_/ \_/\_/(_)|_| \_| \___||_| \__,_||_| \__, |(_)\__,_| \___| __/ | |___/ sinusTest by Fabian Steppat Info on https://www.nerdiy.de/ardunio-die-serielle-schnittstelle/ This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . You will find additional information to this project at the following address:HowTo: Ardunio – Use the serial portThis codesnippet prints a sine wave via serial console or serial plotter */ //variables and constants float degreeAngle = 0; float sinusValue = 0; const float pi = 3.1415; float radianAngle = 0; const uint8_t sinusFactor = 50; void setup() { Serial.begin(115200); } void loop() { radiantAngle = degreeAngle * pi / 180; //making deg in radians sinusValue = sin(radiantAngle); //calculate sine from radiant angle degreeAngle += 1.0; //increase the angle delay(5); //make output a bit slower if (degreeAngle > 360) //if degree boundary is reached reset it to zero { degreeAngle = 0; } Serial.println(sinusFactor * sinusValue); //output to serial monitor/plotter }

Of course, the plotter works not only with sine values but also with all other numerical values.
Send commands and parameters to the Arduino
So now you know how to send various status messages or values from the Arduino to the computer.
But it also works the other way around. This is especially useful or practical if you want to control the Arduino from the outside and do not want to connect input devices such as buttons and Co. to the Arduino (or can due to lack of GPIOs).
For example, you can set the brightness or color of an LED or even a motor speed of a connected motor via the serial interface alone.
For communication over the serial interface the Arduino IDE provides some useful functions. (The documentation with more details can be found here: https://www.arduino.cc/reference/en/language/functions/communication/serial/)
So that the reception and the evaluation of Befeheln works, there are now three tasks to solve:
1. how does the Arduino recognize received characters?
2. how to assemble complete commands from the received characters?
3. how can these received commands be evaluated?
1. how the Arduino recognizes received characters
Now to react on incoming characters there is the function
Serial.available()
Diese prüft ob neue Zeichen im Empfangspuffer des Arduino enthalten sind. Der Empfangspuffer ist dabei eine Art zwischenspeicher in dem empfangene Zeichen solange gespeichert werden bis der Mikrocontroller des Arduinos Zeit hat sich um die empfangegenen Zeichen zu “kümmern”.
In dem Fall, dass neue Zeichen empfangen, aber noch nicht aus dem Empfangspuffer gelesen wurden gibt die Funktion “Serial.available()” “true” zurück ansonsten “false”.
This function is perfect to check if characters have been received which have to be evaluated now. The command does not tell us how many characters were received. It only says that at least one character is in the receive buffer.
2. how to turn the received characters into complete commands
Good, now we have recognized thanks to the previous function that at least one character is available in the receive buffer. But how do we get the received characters now?
Characters from the receive buffer can be deleted with the command
Serial.read()
read out. In doing so, the function outputs the first character contained in the receive buffer and automatically removes it from the receive buffer.
To read all characters and write them into a variable, we now read from the receive buffer until it is empty and remember each character read.
This can be implemented with a loop, for example, like this:
void check_serial_receive() { String serialbuffer; while (Serial.available()) { char currentChar = (char)Serial.read(); if (currentChar == 13) //check if the read character is the line terminator { evaluate_received_characters(serialbuffer); } else { serialbuffer += currentcharacter; } } }
As you can see the code consists of a while loop that runs until the receive buffer is empty (then Serial.available()==false).
Innerhalb der while-Schleife werden währenddessen zeichenweise die auf dem Emfpangspuffer enthaltenen Zeichen ausgelesen (Serial.read()), der Variable “aktuellesZeichen” zugewiesen und an den String “seriellerPuffer” angehängt.
Hier kommt nun auch das im Abschnitt “Serieller Monitor” erwähnte Zeilenabschlusszeichen ins Spiel. In diesem (und jedem anderen Terminalprogramm) lässt sich nämlich ein Zeichen einstellen, welches nach jedem Absenden eines Zeichens oder einer Zeichenkette automatisch angehängt und mitgesendet wird.
This line terminator can now be used to detect whether the previously read string is complete and can therefore be evaluated.
This means that each character received must be checked to see if it is the line terminator character. In the code shown above this is done by
if (currentChar == 13)...
Lasst euch von der 13 nicht verwirren. Das Problem bei Zeilenabschlusszeichen ist nämlich, dass diese auf einer Tastatur nicht aufgeführt sind. Man kann sie also nicht als Vergleichszeichen in den Code “eintippen”.
To check the received character for the character terminator anyway, we use a trick here.
Thanks to the Ascii table we know that each character in the computer corresponds to a byte value (i.e. a numerical value between 0 and 255).
Wenn ihr im Arduino also zum Beispiel ein “A” speichert, so speichert dieser eigentlich den Zahlenwert “65”.
Würde man nun prüfen wollen ob ein empfangenes Zeichen einem “A” entspricht könnte man entweder
if (currentcharacter == "A")
or else
if (currentcharacter == 65)
write. Both comparisons would lead to the same result.
We now make use of this trick. To check whether the currently received character is a line terminator such as the CR (=CarriageReturn), we do not check the character itself but the value of the character in the ascii table: i.e. 13.
So as soon as this character has been received we know that all previously sent characters are supposed to represent a command and must now be evaluated.
3. how these received commands can be evaluated
Wir bzw. der Arduino hat nun also einen Befehl empfangen. Dieser ist in der String-Variable “seriellerPuffer” gespeichert und wird als Funktionsparameter an die Funktion “empfangene_zeichen_auswerten()” übergeben.
This function could look like this, for example:
void received_character_evaluate(String serialBuffer) { Serial.println("" + String(serialBuffer) + "" received."); //output the command just received. if (serialBuffer == "simplerCommandDerWasTut") { simpler_command_the_what_tut(); } else if (serialBuffer.indexOf('=') != -1) { uint16_t receivedValue = serialBuffer.substring(serialBuffer.indexOf('=') + 1).toInt(); String receivedParameter = serialBuffer.substring(0, serialBuffer.indexOf('=')); if (receivedParameter == "commandSetOneValue") { if (receivedValue = 0) { command_the_one_value_sets(receivedvalue ); } else { Serial.println(F("Value outside the allowed range of values.")); } } else { Serial.print(F("The command "")); Serial.print(serialBuffer); Serial.print(F("" was not recognized.")); } } else { Serial.print(F("The command "")); Serial.print(serialBuffer); Serial.print(F("" was not recognized.")); } }
After receiving a command it will be output again, this is very useful for debugging purposes. So you can check which string has finally arrived in the Arduino.
Danach kann mit einer einfachen IF-Abfrage geprüft werden welcher Zeichenkette der empfangene Befehl entspricht. Hier wird zum Beispiel geprüft ob der Befehl der Zeichenkette “simplerBefehlDerWasTut” entspricht.
Ist dies der Fall wird die Funktion “simpler_befehl_der_was_tut();” ausgeführt.
Mithilfe dieser “absoluten” Befehle könntet ihr nun einfache Befehle absetzen. Zum Beispiel um ein Licht ein- oder auszuschalten.
Um nun aber auch Werte übergeben zu können müssen auch Befehle der Form “helligkeitSetzenAuf=20” ausgewertet werden können.
Es muss also der Befehlsname (in diesem Fall “helligkeitSetzenAuf”) und der Wert (in diesem Fall “20”) erkannt werden.
This is done in the further part of the code shown above.
Sollte nämlich keiner der “absoluten” Befehle zu der empfangenen Zeichenkette passen wird geprüft ob in der empfangenen Zeichenkette ein “=”-Zeichen Vorhanden ist.
Solltet Ihr also zum Beispiel den Befehl “setzeHelligkeit=100” an den Arduino gesendet haben wird er die gesamte Zeichenkette “setzeHelligkeit=100” keinem “absoluten” Befehl zuordnen können.
In this case, the received string is examined for an equal sign and this is also found.
Then the received string is split into the part before and after the equal sign with the following commands.
uint16_t receivedValue = serialbuffer.substring(serialbuffer.indexOf('=') + 1).toInt(); String of received parameters = serialbuffer.substring(0, serialbuffer.indexOf('='));
Nun ist in der Variable “empfangenerWert” die “100” und in der Variable “empfangenerParameter” der Befehl “setzeHelligkeit” gespeichert.
Once extracted from the received string, the two values can then be further processed and acted upon.
Just try it 🙂
You can find the whole code to try out here:
/* _ _ _ _ _ | | | | |(_) | | __ ____ ____ __ | | | ___ _ __ __| | _ _ _ __| | ___ / / / / / / / / / | . ` | / _ | '__|/ _` || || | | | / _` | / _ V V / V V / V /_ | | || __/| | (_| || |_| | _| (_| || __/ \_/\_/ \_/\_/ \_/\_/(_)|_| \_| \___||_| \__,_||_| \__, |(_)\__,_| \___| __/ | |___/ serialTest by Fabian Steppat Info on https://www.nerdiy.de/ardunio-die-serielle-schnittstelle/ This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . You will find additional information to this project at the following address:HowTo: Ardunio – Use the serial portThis codesnippet is a basic concept of a serial communication to control an arduino via serial commands */ { Serial.begin(115200); } void loop() { check_serial_receive(); } void check_serial_receive() { String serialbuffer; while (Serial.available()) { char currentChar = (char)Serial.read(); if (currentChar == 13) { evaluate_received_characters(serialbuffer); } else { serialbuffer += currentcharacter; } } } void evaluate_received_characters(string serialbuffer) { Serial.println(""" + String(serialbuffer) + ""received."); if (serial buffer == "simpleCommandTheWhatDoes") { simpler_command_that_does_the_what(); } else if (serialBuffer.indexOf('=') != -1) { uint16_t receivedValue = serialBuffer.substring(serialBuffer.indexOf('=') + 1).toInt(); String of received parameters = serialbuffer.substring(0, serialbuffer.indexOf('=')); if (receivedParameter == "commandThatSetsAValue") { if (receivedValue <= 23 & & receivedValue >= 0) { command_that_sets_a_value(receivedValue ); } else { Serial.println(F("Value outside the allowed value range.")); } } else { Serial.print(F("The command "")); Serial.print(serial buffer); Serial.print(F("" was not recognized.")); } } else { Serial.print(F("The command "")); Serial.print(serial buffer); Serial.print(F("" was not recognized.")); } }
Have fun with the project
I hope everything worked as described for you. If not or you have questions or suggestions please let me know in the comments. I will then add this to the article if necessary.
Ideas for new projects are always welcome. 🙂
PS Many of these projects - especially the hardware projects - cost a lot of time and money. Of course I do this because I enjoy it, but if you think it's cool that I share the information with you, I would be happy about a small donation to the coffee fund. 🙂