Programming an Arduino from the command line

Install the Arduino CLI tool from Github on Rasbian so you can program an Arduino
Install the Arduino CLI tool from GitHub on Rasbian so you can program an Arduino

As part of my K9 and ROV projects, I am using at least one Arduino to control components such as the K9’s ears. Since the Arduinos will be fixed to the robots, I will program them via the Raspberry Pi. The version of Raspbian I am running is Raspberry Pi OS Lite, which does not have a Graphical User Interface, so we will need to install the Arduino command-line tools.

Configure Arduino command-line IDE

After a fairly default install, you will still need to install the specific parts for working with the Arduino. So log into the Raspberry Pi Linux install via SSH, and do an update:

sudo apt-get update && upgrade -y

The next part is to install the actual Arduino-CLI IDE. This is a specific set of tools for use from the command line; it is not available from APT, so we need to download it from the GitHub project.

curl -fsSL https://raw.githubusercontent.com/arduino/arduino-cli/master/install.sh | sh

At the end of the install, it flashed up a message stating

install.sh: arduino-cli not found. You might want to add "/home/pi/bin" to your $PATH

I run ZSH rather than Bash, so I edit ~/.zshrc rather than ~/.bashrc, but the instructions should be the same.

nano ~/.zshrc

Scrolling down to the bottom of the file, I append the following to the bottom of the file:

export PATH=$PATH:/home/pi/bin

Saving the file, we need to load the changes. Since I am not doing anything else with the Raspberry Pi at the moment, I am just going to reboot it. “sudo reboot”

Configuring arduino-cli

Once the Raspberry Pi has turned back on, we need to create a configuration file. Arduino-cli has a command to initiate this, which will generate a YAML file. Running the command below will create a boilerplate configuration at ~/.arduino15/arduino-cli.yaml, then we will use the second command to update the CLI tool chain:

arduino-cli config init
arduino-cli core update-index
arduino-cli core install arduino:avr

I added some additional text to the ~/.arduino15/arduino-cli.yaml configuration file:

board_manager:
    additional_urls: []
directories:
  data: /home/pi/.arduino15
  downloads: /home/pi/.arduino15/staging
  user: /home/pi/Arduino
logging:
  file: ""
  format: text
  level: info

Running the command “arduino-cli board list” returns the message “No boards found,” despite my having two plugged in. This is not an error as such, since I have Node-RED installed and running as a service, and one of my flows takes the serial ports, to stop Node-RED we use the command “sudo systemctl stop nodered” (to restart Node RED we use “sudo systemctl start nodered”.

Upload the first Arduino Program from the command line

We need to quickly make an Arduino sketch to upload to the Arduino board:

arduino-cli sketch new blink

I run the above command in “~/ROV/Arduino”, but it can be run in any folder; it will create a new folder called blink, with blink.ino inside.

Just as a quick test, we will use the standard blink.ino code:

void setup() {
    pinMode(LED_BUILTIN, OUTPUT);
}

void loop() {
    digitalWrite(LED_BUILTIN, HIGH);
    delay(1000);
    digitalWrite(LED_BUILTIN, LOW);
    delay(1000);
}

Checking the attached Arduino boards, we have one on /dev/ttyACM0 and one on /dev/ttyUSB0, as shown by “arduino-cli board list”. The one on /dev/ttyACM0 has a FQBN of arduino:avr:uno, and because we installed the above core type, we can use the following command to compile and upload the code directly to the Arduino.

arduino-cli compile -b arduino:avr:uno ~/ROV/Arduino/blink

Leave a comment

Your email address will not be published. Required fields are marked *

The maximum upload file size: 20 MB. You can upload: image, audio, video, document, spreadsheet, interactive, text, archive, code, other. Links to YouTube, Facebook, Twitter and other services inserted in the comment text will be automatically embedded. Drop files here