How to Upload an Arduino Sketch from Linux Terminal

April 9, 2024

Learn how to compile and upload Arduino sketches from the terminal, as well as view the serial output of your Arduino using the screen command, streamlining your development process and enhancing efficiency.

First of all we should update Raspberry Pi

sudo apt-get update

then we install the arduino-mk package

sudo apt-get install arduino-mk

create a folder named “sketchbook”

mkdir sketchbook

enter the sketchbook folder

cd sketchbook

create a blinky.ino

Write your Arduino code in a file with ino extension.

vim blinky.ino

create a libraries folder

mkdir libraries

create a Makefile

Specify the project settings related to your project in the Makefile. For example, you can define settings such as the Arduino board type, the port used, and the path to the Arduino IDE.

vim Makefile

write the following in the makefile

ARDUINO_DIR = /usr/share/arduino
ARDUINO_PORT = /dev/ttyUSB0
USER_LIB_PATH = /home/pi/sketchbook/libraries
BOARD_TAG = uno // board type
include /usr/share/arduino/Arduino.mk

and compile

To compile your project, use the make command. This command compiles your Arduino code and creates the corresponding .hexfile.

make

and upload sketch to Arduino

You can upload the compiled code to your Arduino board using the make upload command. This command compiles and uploads your Arduino code.

make upload

Additional Information

If you want to clean up temporary files and compilation outputs generated after the compilation and upload process, use this command:

make upload clean

Look for a line similar to the one representing the port to which your Arduino is connected. It will typically start with /dev/tty, followed by a designation such as /dev/ttyUSB0 or /dev/ttyACM0. This designation represents the port to which your Arduino is connected.

dmesg | grep tty

To monitor the serial messages from your Arduino in the Linux terminal, you can use the screen command.

screen /dev/ttyUSB0 9600

Replace /dev/ttyUSB0 with the appropriate serial port to which your Arduino is connected. 9600 is the baud rate, which should match the baud rate of your Arduino’s serial monitor.

Press Enter to open the serial monitor and observe the messages coming from your Arduino.

To exit the serial monitor window, press Ctrl + A followed by Ctrl + K.

References