If you want to start Arduino programming you’ll notice a lot of the documentation and tutorials are centred around the Arduino IDE. Now, obviously, as an Emacs user you’ll be loath to install something like Arduino IDE, let alone actually use it. The good news is it’s super easy to get started with Arduino with any editor, including Emacs and even Vim if you so desire.

All the Arduino IDE is doing is calling a cross-compiler toolchain then using avrdude to communicate with the Arduino to upload software. The Arduino Uno and Nano both use the Atmel AVR platform so what you need is a toolchain that can target that platform. Now, you could install your own toolchain and call avrdude directly. If you know how to do that then I guess you can stop reading now. But if you don’t, or aren’t interested in learning how (it’s not very interesting), then read on.

PlatformIO

PlatformIO is a project that makes it really easy to do embedded development.

First, install PlatformIO, I like to use pipx to install tools like this: pipx install platformio.

Now, start your project by making a directory for it:

1mkdir my_new_project
2cd my_new_project

And initialise a PlatformIO project:

1platformio project init --board uno --board nanoatmega328

This will configure your project for both Arduino Uno and Nano.

Now write some barebones C++ code that does nothing in src/main.cpp:

 1#include "Arduino.h"
 2
 3void setup()
 4{
 5    // your setup code here
 6}
 7
 8void loop()
 9{
10    // your main loop here
11}

This is, of course, totally standard C++ so you can use your normal C++ modes etc.

You should end up with a project structure like this:

 1.
 2├── include
 3│   └── README
 4├── lib
 5│   └── README
 6├── platformio.ini
 7├── src
 8│   └── main.cpp
 9└── test
10    └── README

Now you can simply run the following to build the software for all platforms specified in platformio.ini:

1platformio run

To build and upload the software to your Arduino, if you are on Linux you first have to install some udev rules: https://docs.platformio.org/en/latest/core/installation/udev-rules.html

Then you can run simply:

1platformio run -e nanoatmega328 -t upload # for arduino nano
2platformio run -e uni -t upload # for arduino uno

This tends to cleverly pick the right serial device but if you have more than one you might need to specify it with --upload-port.

You can adapt these as your command for M-x compile or write a Makefile if you prefer. Don’t forget it expects to be run from the top-level where platformio.ini lives, though.

Another super-useful command to be aware of is platformio device monitor. This gives you a serial terminal for communicating with your device. Really convenient. There’s a lot more too.

And that’s it! You’ll find the Arduino documentation here: https://www.arduino.cc/reference/en/ That’s all you should need to get started. Happy hacking!