Write Arduino-code without Arduino-IDE

R.Devansh Shukla
6 min readMar 29, 2023

--

Yes, we are doing it today. In order to better understand it, we will take the difficult route. 🎓🎓 Also, it's Arduino Day. woohooo🥳🥳

The Arduino ♾️ is a widely used platform for building electronic projects, and the Arduino Integrated Development Environment (IDE) is the most popular way to write and upload code to an Arduino board. However, there are other ways to write Arduino code that don’t require the IDE. In this blog, we will discuss two alternative methods for writing Arduino code.

⚠️⚠️I use Linux Ubuntu (22.04 LTS). Commands may vary for other operating systems.

I am writing the blog on the same day

1️⃣ Method 1: Using Linux terminal to imitate an Arduino Makefile

The Arduino IDE is built on top of a command-line tool called “avr-gcc”, which is used to compile and upload code to the Arduino board. By using the terminal, we can imitate an Arduino makefile to compile and upload the code to the board.

Here are the steps to follow:

Step 1: Install the required tools🧰

To compile and upload code to the Arduino board, we need to install the avr-gcc toolchain, avrdude, and make. We can install these tools on Ubuntu or Debian by running the following command:

sudo apt-get install gcc-avr avr-libc avrdude make

Step 2: Create a new file 📁

Create a new file in your favorite text editor (Nano, Vim, gedit) and save it with a “.c” extension. For example, if you want to create a program to blink an LED connected to pin 13, you can create a file called “blink.c” with the following code:

I have a folder called “Projects” that I will use to organize my files. You may choose whatever suits you. .

blink.c looks like this:

#include <avr/io.h>
#include <util/delay.h>
int main(void)
{
DDRB |= (1 << PB5); // Set pin 13 as an output, refer schematics for the port configs
while (1) {
PORTB ^= (1 << PB5); // Toggle pin 13
_delay_ms(500);
}
return 0;
}

Step 3: Create a makefile 📁

Create a new file called “Makefile” in the same directory as your “blink.c” file, and add the following code:

MCU = atmega328p
F_CPU = 16000000UL
PROGRAMMER = arduino
PORT = /dev/ttyACM0
CC = avr-gcc
CFLAGS = -Wall -Os -DF_CPU=$(F_CPU) -mmcu=$(MCU)
OBJCOPY = avr-objcopy
OBJCOPYFLAGS = -j .text -j .data -O ihex
AVRDUDE = avrdude
AVRDUDEFLAGS = -p $(MCU) -c $(PROGRAMMER) -P $(PORT)
all: blink.hex
blink.hex: blink.elf
$(OBJCOPY) $(OBJCOPYFLAGS) $< $@
blink.elf: blink.o
$(CC) $(CFLAGS) -o $@ $<
blink.o: blink.c
$(CC) $(CFLAGS) -c $<
upload: blink.hex
$(AVRDUDE) $(AVRDUDEFLAGS) -U flash:w:$<
clean:
rm -f blink.o blink.elf blink.hex

Err: Don’t use space, instead use Tab in the necessary places, or else you may arrive at the following error message :

Step 4: Compile and upload the code 🗃️

Open a terminal and navigate to the directory where your “blink.c” and “Makefile” files are located. Run the following command to compile the code:

make

Terminal output for make:

This will generate a “.hex” file in the same directory. To upload the code to the Arduino board, run the following command:

make upload

Terminal output for upload:

This will upload the code to the board using the specified programmer and port.

Here are a few commands that might be useful if you are a beginner to Linux:

To view the arduino port and provide read/write access to the user, You can use 777 to give all users the same rights

2️⃣️ Method 2: Using VSCode Arduino Extension

VSCode (Visual Studio Code) is a popular code editor that supports many programming languages, including Arduino. The VSCode Arduino extension provides a powerful and easy-to-use environment for writing and uploading Arduino code.

Steps: Create a folder, add two files, blink.ino, and Makefile. They are the same as in the previous method. 🗄️🗃️📁

⚠️ The name of “*.ino” file should be similar to that of the folder name.

Verify and upload the code similar to arduino-ide using the icons on top right corner.

3️⃣ Method 3: Using Raspberry Pi with Arduino.mk

If you have a Raspberry Pi, you can use it to compile and upload Arduino code using the Arduino.mk “Makefile”. This method requires a bit of setup, but once it’s done, it provides a flexible and powerful way to develop Arduino projects.

Here are the steps to follow:

Step 1: Install the required tools🧰

On your Raspberry Pi, install the required tools by running the following command:

sudo apt-get install arduino arduino-mk

This will install the Arduino IDE and the Arduino.mk Makefile.

Step 2: Create a new project directory 🗄️

Create a new directory for your Arduino project by running the following command:

mkdir blink

Step 3: Create a new file 📁

Create a new file in your favorite text editor and save it with a “.ino” extension. For example, if you want to create a program to blink an LED connected to pin 13, you can create a file called “blink.ino” with the following code:

The code is similar to the arduino code of method 2.

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

Step 4: Create a Makefile 📁

Create a new file called “Makefile” in the same directory as your “blink.ino” file, and add the following code:

BOARD_TAG = uno
ARDUINO_PORT = /dev/ttyACM0

include /usr/share/arduino/Arduino.mk

This Makefile specifies the board and serial port to use, and includes the Arduino.mk Makefile.

Step 5: Compile and upload the code 🗃️

Open a terminal and navigate to the directory where your “blink.ino” and “Makefile” files are located. Run the following command to compile the code:

make

This will generate a “.hex” file in the same directory. To upload the code to the Arduino board, run the following command:

make upload

This will upload the code to the board using the specified serial port.

That’s it! With these steps, you can write and upload Arduino code using the Raspberry Pi and the Arduino.mk Makefile. This method can also be used with other Arduino-compatible boards, as long as you specify the correct board and serial port in the Makefile.

Run the following command after uploading if you want to reuse the code:

make clean monitor upload

Additionally, a serial monitor using “Screen” would be opened. This will be covered in later blogs.

You can find the repository here.

Ping me on LinkedIn for any further assistance. Seeeeee yaa!!! 😁👋🏾😁

--

--

R.Devansh Shukla

I am an IoT / Embedded Full Stack Developer with over 2 years of experience researching and developing IoT, Embedded, and Software solutions from the ground up.