Arduino development environment on Linux

Arduino development environment on Linux

Introduction

I was fourteen when I got my first Arduino Uno. I will never forget my excitement from the first blink of an LED. Projects I implemented back then are still one of the most emotional ones.

Install Arduino IDE

Installation on Ubuntu based distros is super simple. Just run:

sudo apt-get install arduino

Alternatively, you can get the software archive and do manual installation following the link.

Arduino IDE

Before you can fire up IDE we need to do one more last step. By default, Arduino installation doesn't change permissions on port /dev/ttyACM0. So the operating system's username cannot access it to upload changes.

If you try to upload you will get the following error:

Sketch uses 924 bytes (2%) of program storage space. Maximum is 32256 bytes.
Global variables use 9 bytes (0%) of dynamic memory, leaving 2039 bytes for local variables. Maximum is 2048 bytes.
avrdude: ser_open(): can't open device "/dev/ttyACM0": Permission denied
Problem uploading to board.  See http://www.arduino.cc/en/Guide/Troubleshooting#upload for suggestions.

To fix this run:

sudo chmod a+rw /dev/ttyACM0

Now let's run the most basic example ever, blinking a LED.

Go to File -> Examples -> 01.Basics -> Blink or copy paste code below in your sketch:

void setup() {
pinMode(LED_BUILTIN, OUTPUT);
}

void loop() {
digitalWrite(LED_BUILTIN, HIGH);
delay(1000);
digitalWrite(LED_BUILTIN, LOW);
delay(1000);
}

And the circuit sketch:

Blink LED Fritzing 2

Now you must see your first microcontroller project giving a sign of life. The next step is to conqueror the world!

Install Fritzing

In an electronic project, the code is only one part of the story. We also need circuits. And as we document project functionality via code, we need to document schematics. My favorite tool is Fritzing.

Fritzing is open-source software designed to develop electronic projects. You can install it by running:

sudo apt-get install fritzing

Alternatively, you can use the following link to download and install it manually.

Arduino IDE

Note: It's my first time seeing mandatory donation to proceed to download. Maybe this approach makes some people pay, but sorry devs, not for me.

Note: For happy non-Ubuntu users. If you go to the project's GitHub page and then download Release, the installation script will fail with a message:

Copying /etc/mime.types -> /etc/mime.types.bak
Registering Fritzing MIME types...
Copying Fritzing data...
Entering fritzing/

:: INSTALLING IN SYSTEM MODE. IT WILL TAKE A LONG TIME!
PLEASE BE PATIENT...

Installing Fritzing MIME types...
-*-*-*-*-*-*-*-
xdg-mime: file 'icons/x-fritzing-fz.xml' does not exist
-*-*-*-*-*-*-*-

Setting up default application -> org.fritzing.Fritzing.desktop
-*-*-*-*-*-*-*-
-- TASK ENDED SUCCESSFULLY! --
-*-*-*-*-*-*-*-

Installing Fritzing system icons...
-*-*-*-*-*-*-*-
-- TASK ENDED SUCCESSFULLY! --
-*-*-*-*-*-*-*-

Making symlinks...
Doing final touch...
grep: uninstall_fritzing.sh: No such file or directory
cp: cannot stat 'uninstall_fritzing.sh': No such file or directory
sed: can't read uninstall_fritzing.use: No such file or directory
sed: can't read uninstall_fritzing.use: No such file or directory
sed: can't read uninstall_fritzing.use: No such file or directory
sed: can't read uninstall_fritzing.use: No such file or directory
sed: can't read uninstall_fritzing.use: No such file or directory
sed: can't read uninstall_fritzing.use: No such file or directory
Activating uninstall script...
sed: can't read uninstall_fritzing.use: No such file or directory
Activated!

cp: cannot stat 'uninstall_fritzing.use': No such file or directory
Updating databases...
Error in file "/usr/share/applications/org.kde.kdeconnect_open.desktop": "*/*" is an invalid MIME type ("*" is an unregistered media type)
Done!

So please save your time and use apt!

Upload Arduino sketch via Linux terminal

You can upload an Arduino sketch to the board by command:

arduino --upload sketch/sketch.ino --port /dev/ttyACM0

Upload C program via Linux terminal

Another practical usage of Arduino Uno is to program Atmega328P chip with C or Assembler code. One of my courses at Jacobs University Bremen, Embedded Systems Lab, covers this topic and teaches the basics of microcontroller programming.

In the class's laboratory, we got Windows machines with preinstalled Atmel Studio. But what if you don't have a Windows machine at home like me?

Well, Linux is open and with great openness comes great power. Since Arduino IDE is just Java wrapping around GCC and AVR, we can upload C program files directly from the Linux terminal.

Install the required tools:

sudo apt-get install gcc-avr binutils-avr gdb-avr avr-libc avrdude

Create an example led.c file with the following content to blink a LED:

#include <avr/io.h>
#define F_CPU 8000000UL
#include <util/delay.h>
#include <avr/interrupt.h>

int main(void)
{
EICRA = 0x02;
EIMSK = 0x01;
sei();
DDRD = 0xF0;
while(1) {
PORTD = 0b10000000;
_delay_ms(2500);
_delay_ms(2500);
PORTD = 0b01000000;
_delay_ms(150);
_delay_ms(150);
PORTD = 0b00100000;
_delay_ms(350);
_delay_ms(350);
}
}

ISR(INT0_vect)
{
PORTD = 0b00000000;
_delay_ms(250);
PORTD = 0b11100000;
_delay_ms(250);

PORTD = 0b00000000;
_delay_ms(250);
PORTD = 0b11100000;
_delay_ms(250);

PORTD = 0b00000000;
_delay_ms(250);
PORTD = 0b11100000;
_delay_ms(250);
}

Now you can use these commands to compile and upload a C file:

avr-gcc -Os -DF_CPU=16000000UL -mmcu=atmega328p -c -o led.o led.c
$ avr-gcc -mmcu=atmega328p led.o -o led
$ avr-objcopy -O ihex -R .eeprom led led.hex
$ avrdude -F -V -c arduino -p ATMEGA328P -P /dev/ttyACM0 -b 115200 -U flash:w:led.hex
 
avrdude: AVR device initialized and ready to accept instructions
 
Reading | ################################################## | 100% 0.00s
 
avrdude: Device signature = 0x1e950f
avrdude: NOTE: FLASH memory has been specified, an erase cycle will be performed
 To disable this feature, specify the -D option.
avrdude: erasing chip
avrdude: reading input file "led.hex"
avrdude: input file led.hex auto detected as Intel Hex
avrdude: writing flash (88 bytes):
 
Writing | ################################################## | 100% 0.02s
 
avrdude: 88 bytes of flash written
 
avrdude: safemode: Fuses OK
 
avrdude done.  Thank you.

Link
No links yet.