How To Upload Files To An Arduino Uno Using The Linux Terminal With Vim, GCC, and C-Programming: Classic Blink Without The Arduino IDE
These are the written steps and code necessary to upload C programs to an Arduino Uno through the Linux terminal without needing to use the Arduino IDE. I am most certainly NOT the originator of this, and proper credit goes to the “Low Level Learning” Youtube channel, and specifically this video entitled “Getting Started with Baremetal Arduino C Programming | No IDE Required [Linux SDK]”. This methodology is entirely reproducible, as evidenced by my blinking Arduino taking up space on the TV tray in front of me.
- 1. Connect your Arduino to a usb port in your Linux machine.
- 2. I recommend testing “Blink” through an Arduino IDE on the Linux computer just to make sure it all actually works. You can also tell which TTY path (who knows if this is the proper terminology?!?) the USB is located at by checking here. Before beginning with the terminal method, upload “Bare Minimum” example to stop the blinking.
- 3. Close the Arduino IDE and open a Linux terminal instead. I am making the assumption you know how to use vim.
- 4. Type “sudo apt install avr-libc avrdude binutils-avr gcc-avr” to install these programs in the root ~$ directory.
- 5. Input your password when prompted.
- 6. Run “dmesg | grep tty” in the root directory (i.e., the main directory, meaning the one that shows up first when you open the terminal).
- 7. Take note of the “USB ACM device” and note down its “TTY” (location? port? directory?) In my case, it is “ttyACM0” that is associated with the Arduino.
- 8. Now create the directory where you want to store your files for this action. I called mine “FreeArduino” and added it to the “~/Desktop”. Remember mkdir FreeArduino would be the command line prompt.
- 9. Once in ~/Desktop/FreeArduino$ type in the command “vim Makefile”. This of course assumes that you already have vim installed on your Linux machine. It has become my go-to text editor for programming.
- 10. Now in vim, type in the following 4 separate lines of code. If you look, you will notice I highlighted some things in red, and others in green. The green text shows all of the files called “led”. This is because we are going to name the c-program we write “led.c” and put it in the ~/Desktop/FreeArduino$ directory. If you use another name besides “led.c”, then you need to rename these green text files in the code below accordingly. The red highlighted text below depends on your hardware. Check where your USB is connected; again in my case it is ttyACM0. Read the number of MHz off of the crystal on the Arduino Uno board (16MHz, i.e., 16000000). And check the type of chip on the board; mine is an atmega328p.
default:
avr-gcc -Os -DF_CPU=16000000UL -mmcu=atmega328p -c -o led.o led.c
avr-gcc -o led.bin led.o
avr-objcopy -O ihex -R .eeprom led.bin led.hex
sudo avrdude -F -V -c arduino -p ATMEGA328P -P /dev/ttyACM0 -b11520 -U flash:w:led.hex
- 11. You can exit vim and check your work in ~/Desktop/FreeArduino$ directory with command “cat Makefile”.
- 12. Now its time to write your LED blinky program. In the ~/Desktop/FreeArduino$ directory, type “vim led.c”.
- 13. Write the following code in vim:
#include <avr/io.h>
#include <util/delay.h>
int main(void) {
DDRB = DDRB | (1 << DDB5);
while(1)
{
PORTB = PORTB | (1 << PORTB5);
_delay_ms(1000);
PORTB = PORTB & ~(1 << PORTB5);
_delay_ms(1000);
}
}
- 12. Save and exit (:wq command) vim.
- 13. In the ~/Desktop/FreeArduino$ directory, if you perform an ls command, you should see now an led.c file and a Makefile.
- Enter “~/Desktop/FreeArduino$ make”
- Hopefully the AVR Dude will properly upload your code. He didn’t for me the first few times. I had an error: “avrdude: stk500_getsync(): not in sync: resp=0x00”. This turned out to simply be that I forgot a curly bracket in my program, so I guess this can be a sign that the code did not properly compile. I fixed and saved the code. Before trying to upload it again, I removed the led.bin, led.hex, and led.o files that running the Makefile had created using the command “rm led.bin led.hex led.o”. Then, after the code was fixed, I ran “make” again in the /FreeArduino$ directory. Voila! I got a blinky Arduino now!
So, now why am I even concerned with being able to upload to an Arduino without using the Arduino IDE? Well, a few reasons. My interest in the intersection between hardware and software is increasing, and I have developed a budding fascination with embedded systems. I do have a dive into the Arduino to thank for this. I decided some time last year to learn C programming so I could work “closer to the metal”, and finally managed to get through enough of the introductory material to feel like a well-rounded beginner. And I had taken the plunge by replacing an operating system of a cheapo computer with Linux last year as well. And, that little Mint machine is still running fine!
Get to the point! Ok, my goal, if you cannot tell already, is to be able to upload embedded C programs onto a microcontroller through the Linux terminal, using vim and gcc to create and compile respectively. It is looking very much like this is an accomplishable goal! Blinkety, blinkety, blink, blink, blink, little flashes of joy!
Until we meet again, you are on my mind!
KM1NDY
I am astonished by the thirst for knowledge.
I just bought a sBitx SDR radio.
After a life time of Windows and Mac I am faced, at 89 years old with learning Linux.
Keeps the gray matter functioning.
Always a delight to read your escapades !
Ira KB2DJJ
Hi Ira!!! Great to hear from you my friend!
Linux is definitely good for the noggin…its been taken up a lot of space in mine lately!
Hope all is well, and I’m so glad to hear you are still reading these.
Mindy