Program an Arduino with a Raspberry Pi via SSH

It is very interesting to pair an Arduino on a Raspberry Pi. The arduino allows to interface with different systems (sensor, module, driver …). It can also serve as fully programmable device. The Raspberry realizes the advanced application part of a project (interface, connectivity, advanced program …).
In this configuration, it is necessary to program the Arduino directly from the Raspberry Pi.

In this article we will see how to program an arduino with a Raspberry Pi via SSH using the PlatformIO Core command line tool.. One of the best advantages of this solution is the compatibility with many onboard microcontroller boards (AVR, STM32, ESP …).
PlatformIO Core Documentation
If you are already using PlatformIO with VScode, you will not be confused. The main difference is in the editor and the management of source files. Indeed in our case we will work in a remote way. We will see how to proceed with a Raspberry Pi without a graphical interface with SSH access (Indispensable when using Raspberry Pi zero for example). You must use an editor on terminal or use an editor on another computer.

Raspberry Pi logo
PlatformIO logo
Arduino logo

Prerequisite

The rest of this article assumes that you have a Raspberry Pi operational in SSH and accessible via Ethernet / Wifi. Python3 and pip must be installed on the Raspberry. A working Arduino is connected to a USB port on the Raspberry.
The control computer must have an SSH client (Windows: MobaXterm, PuttY … / Linux: ssh command / OSX: ssh command)
It is helpful to have some knowledge of using linux from the command line, Arduino programming, and how to use PlatformIO.

Installing PlatformIO Core on Raspberry Pi

First, you must connect to the Raspberry PI via SSH:

ssh mylogin@addressIpRaspberry

If so, Python3 and pip must be installed. Otherwise enter the following command:

sudo apt-get install python3 python3-pip

Installation of PlatformIO Core on the Raspberry PI:

sudo python3 -m pip install -U platformio

PlatformIO project creation

PlatformIO contains a database with configurations for many on-board boards. Toolchains and build / download scripts are installed seamlessly. Therefore, it is possible to automatically generate pre-configured projects by specifying the name of the card.

First you need to create a directory for the Arduino program project.

mkdir ~/ArduinoProject && cd ~/ArduinoProject

PlatformIO project initialization:

platformio init -b uno

If necessary you can replace “uno” by the name of the card used. The “platformio boards” command returns the list of boards supported by PlatformIO

Arduino source code

The default main source file is: src/main.cpp

There are several solutions to edit the source code of the Arduino program.

Possible solutions:

– Edit source files directly on the Raspberry Pi via SSH using a terminal editor (vim, nano …)

nano src/main.cpp

– Edit source files on another computer and copy files via a “scp” command

scp main.cpp mylogin@addressIpRaspberry:ArduinoProject/src

– Using a version manager (svn, git …)

This solution is the cleanest and most modern. It allows better management of the evolution of the code and of teamwork.

Exemple de code pour Arduino :

#include "Arduino.h"

#define BUILT_LED   13

void setup() 
{
    Serial.begin(115200);
    pinMode(BUILT_LED,OUTPUT);
}

void loop() 
{
    Serial.println("hello");
    digitalWrite(BUILT_LED,LOW);
    delay(500);
    digitalWrite(BUILT_LED,HIGH);
    delay(500);
}

Compilation and Upload

To compile, you must go to the PlatformIO project directory and enter the command:

platformio run

During the first compilation for a card type, platformIO will download and install all the necessary libraries. It may take some time depending on the connection and the type of Raspberry Pi.

To compile and upload to the Arduino:

platformio run -t upload

Serial monitor

Add the line “monitor_speed = 115200” to the project’s platform.ini file to use the serial monitor.

Example for an Arduino Uno with serial communication at 115200 Bauds:

; PlatformIO Project Configuration File
;
;   Build options: build flags, source filter
;   Upload options: custom upload port, speed and extra flags
;   Library options: dependencies, extra library storages
;   Advanced options: extra scripting
;
; Please visit documentation for the other options and examples
; https://docs.platformio.org/page/projectconf.html

[env:uno]
platform = atmelavr
board = uno
framework = arduino

monitor_speed = 115200

The serial monitor is started with the following command:

platformio device monitor

To start the compilation, upload and serial monitor:

platformio run -t upload && platformio device monitor

Result obtained on the Raspberry Pi terminal with the example code for Arduino:

pi@raspberrypi:ArduinoProject $ platformio run -t upload && platformio device monitor
Processing uno (platform: atmelavr; board: uno; framework: arduino)
------------------------------------------------------------------------------------------------------------------------------------
Verbose mode can be enabled via `-v, --verbose` option
CONFIGURATION: https://docs.platformio.org/page/boards/atmelavr/uno.html
PLATFORM: Atmel AVR 2.2.0 > Arduino Uno
HARDWARE: ATMEGA328P 16MHz, 2KB RAM, 31.50KB Flash
DEBUG: Current (simavr) On-board (simavr)
PACKAGES:
 - framework-arduino-avr 5.0.0
 - tool-avrdude 1.60300.200527 (6.3.0)
 - toolchain-atmelavr 1.50400.190710 (5.4.0)
LDF: Library Dependency Finder -> http://bit.ly/configure-pio-ldf
LDF Modes: Finder ~ chain, Compatibility ~ soft
Found 5 compatible libraries
Scanning dependencies...
No dependencies
Building in release mode
Checking size .pio/build/uno/firmware.elf
Advanced Memory Usage is available via "PlatformIO Home > Project Inspect"
RAM:   [=         ]   9.5% (used 194 bytes from 2048 bytes)
Flash: [=         ]   6.1% (used 1970 bytes from 32256 bytes)
Configuring upload protocol...
AVAILABLE: arduino
CURRENT: upload_protocol = arduino
Looking for upload port...

Auto-detected: /dev/ttyACM0
Uploading .pio/build/uno/firmware.hex

avrdude: AVR device initialized and ready to accept instructions

Reading | ################################################## | 100% 0.00s

avrdude: Device signature = 0x1e950f (probably m328p)
avrdude: reading input file ".pio/build/uno/firmware.hex"
avrdude: writing flash (1970 bytes):

Writing | ################################################## | 100% 0.33s

avrdude: 1970 bytes of flash written
avrdude: verifying flash memory against .pio/build/uno/firmware.hex:
avrdude: load data flash data from input file .pio/build/uno/firmware.hex:
avrdude: input file .pio/build/uno/firmware.hex contains 1970 bytes
avrdude: reading on-chip flash data:

Reading | ################################################## | 100% 0.30s

avrdude: verifying ...
avrdude: 1970 bytes of flash verified

avrdude: safemode: Fuses OK (E:00, H:00, L:00)

avrdude done.  Thank you.

=================================================== [SUCCESS] Took 17.34 seconds ===================================================
--- Available filters and text transformations: colorize, debug, default, direct, hexlify, log2file, nocontrol, printable, send_on_enter, time
--- More details at http://bit.ly/pio-monitor-filters
--- Miniterm on /dev/ttyACM0  115200,8,N,1 ---
--- Quit: Ctrl+C | Menu: Ctrl+T | Help: Ctrl+T followed by Ctrl+H ---
hello
hello
hello
hello
hello

--- exit ---

Conclusion

You can now program an Arduino with a Raspberry Pi remotely with a terminal in SSH.
Consult the PlatformIO documentation to discover more advanced uses.

Leave a Reply

Your email address will not be published.