Skip to content

Compiling Arduino Sketch on Raspberry Pi

AJ but at Work edited this page Jun 4, 2025 · 9 revisions

🚀 Uploading Arduino Code from a Raspberry Pi (Full Guide)

This guide walks you through setting up and using arduino-cli to compile and upload Arduino sketches directly from a Raspberry Pi (e.g., Pi 4B). No Arduino IDE needed.


🧰 Prerequisites

📦 Install Arduino CLI

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

(Optional: move it to your path)

mkdir -p ~/.local/bin
mv arduino-cli ~/.local/bin/
echo 'export PATH="$HOME/.local/bin:$PATH"' >> ~/.bashrc
source ~/.bashrc

🧠 Setup Arduino CLI

✅ Initialize CLI

arduino-cli config init

This creates the ~/.arduino15/ config directory.

✅ Install AVR Core (Nano, Uno, etc.)

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

📂 Create a New Sketch

mkdir -p ~/Arduino/MySketch
arduino-cli sketch new ~/Arduino/MySketch

Edit the .ino file:

// ~/Arduino/MySketch/MySketch.ino
void setup() {
  pinMode(13, OUTPUT);
}

void loop() {
  digitalWrite(13, HIGH);
  delay(500);
  digitalWrite(13, LOW);
  delay(500);
}

🛠️ Uploading Manually

🔌 Find your Arduino's port:

ls /dev/ttyUSB* /dev/ttyACM*

Typical result: /dev/ttyUSB0 or /dev/ttyACM0

⚙️ Compile the sketch:

arduino-cli compile --fqbn arduino:avr:nano ~/Arduino/MySketch

For old-bootloader Nanos, use:
arduino:avr:nano:cpu=atmega328old

🔁 Upload to Arduino:

arduino-cli upload --fqbn arduino:avr:nano -p /dev/ttyUSB0 ~/Arduino/MySketch

🔄 Create a Reusable Upload Script

Create a file named upload.sh in the same folder as your sketch:

#!/bin/bash

SKETCH_DIR="$(dirname "$(realpath "$0")")"
BUILD_DIR="$SKETCH_DIR/build"
PORT="/dev/ttyUSB0"
FQBN="arduino:avr:nano"  # or nano:cpu=atmega328old

arduino-cli compile --fqbn $FQBN --build-path $BUILD_DIR $SKETCH_DIR
arduino-cli upload --fqbn $FQBN -p $PORT --input-dir $BUILD_DIR --verbose

Make it executable:

chmod +x upload.sh

Then run it:

./upload.sh

🧪 Tips & Troubleshooting

  • Nano using old bootloader? Use :cpu=atmega328old and -b57600 in manual avrdude
  • Board port keeps changing? Use ls /dev/ttyUSB* before upload
  • Don't see .hex file? Use --build-path to force a known output folder
  • Serial undefined in VS Code?
    • Include #include <Arduino.h>
    • Make sure IntelliSense knows the core paths and defines
  • Want stable port name? Use a udev rule or dynamic port detection script

✅ You’re Done!

You can now:

  • Write and edit Arduino code from your Pi
  • Compile it with full C++ power (g++, threads, etc.)
  • Upload it directly to your Nano or Uno with one script

All without opening the Arduino IDE ever again. 💥

Clone this wiki locally