Communication with Arduino from Linux-Terminal

I already wrote how to send the message from/to Arduino with help Python. Now I want tell you, how to send/receive the messages from/to Arduino in Linux-Terminal.

As you know, all devices of serial ports are represented by device files in the /dev directory. It is through these files that Linux OS communicates with an external device on the serial port. To transfer something to an external device, you need to write data to this file, and to read information from the device, read data from the file. This can be done with the cat and echo commands, as for "usual" disk files.

How to work with a COM port from the command line? Three commands are used for this: stty, cat and echo.

The stty command sets the parameters and speed of the COM port. Its format is:

stty [-F DEVICE | --file=DEVICE] [SETTING]...

for our case:

$ stty 9600 -F /dev/ttyUSB0 raw -echo

The raw parameter establishes that the data is transferred to the computer byte-by-byte the same way, as they arrive at the port without changes (more see man stty).

Code Arduino:

void setup(){

void loop(){
   int bytesSent = Serial.println("hello from Arduino");

Enter the command in the console:

$ cat /dev/ttyUSB0

we can see:

To see the hex data codes coming from the device, use the hexdump command.

$ cat /dev/ttyUSB0|hexdump -C

To transfer data to Arduino need use the echo command and redirect output to the device file.

Code Arduino:

String inData;
int ledPin = 13;

void setup() {
    pinMode(ledPin, OUTPUT);
    Serial.println("Serial conection started, waiting for instructions...");

void loop() {
    while (Serial.available() > 0)
        char recieved =;
        inData += recieved; 

        // Process message when new line character is recieved
        if (recieved == '\n')
            Serial.print("Arduino Received: ");
            digitalWrite(ledPin, HIGH);

            // You can put some if and else here to process the message juste like that:

            if(inData == "OFF\n"){ // DON'T forget to add "\n" at the end of the string.
              Serial.println("LED OFF");
              digitalWrite(ledPin, LOW);

            inData = ""; // Clear recieved buffer

Enter in one console:

$ stty 9600 -F /dev/ttyUSB0 raw -echo
$ cat /dev/ttyUSB0

Enter in another console:

$ echo "ON">/dev/ttyUSB0

13 pin is ON

$ echo "OFF">/dev/ttyUSB0

13 pin is OFF

To remove the ending of the line in the transmitted data, need to use echo -n

To output data from the device to the screen and to a text file, need to use tee:

$ cat /dev/ttyACM0|tee output.txt


There are 4 comments below.

Post your comment

  • Feb. 24, 2023, 11:24 p.m. - Sultan  
    Hello, may I ask if the Arduino does actually run the commands (as if a user is writing them in command line)? Or is it just a visualization of the data from Arduino?
  • Oct. 30, 2022, 11:56 a.m. - Tomas Antola  
    Thank you for this code! It didn't fully work for me, maybe because i'm using Manjaro linux. It says that "Serial conection started, waiting for instructions..." but when I try to run "echo "ON">/dev/ttyUSB0" or "sudo echo "ON">/dev/ttyUSB0" it says "zsh: permission denied: /dev/ttyUSB0". So u have any tips?
    Oct. 30, 2022, 9:51 p.m. - Ihar moderator  
    Problem: Cannot open /dev/ttyUSB0: Permission denied error is caused by the user not having access to the serial ports as other commenters have mentioned. More specifically, the user is not in the dialout group. Solution: sudo usermod -a -G dialout $USER From:
  • April 14, 2021, 1:39 a.m. - Daniel Dantas  
    Thanks, man... I tried this code into ESP32-WiFi_kit32 and has worked pretty well.
  • Search
    Popular Posts