Difference between revisions of "Arduino with linux"

From Organic Design wiki
m (Sample code)
(working command to flash the micro)
Line 20: Line 20:
  
 
==Building software for Arduino under linux==
 
==Building software for Arduino under linux==
The Arduino project provides an integrated IDE enviroment to work with your Arduino. This is great if you've never programmed before and are running Windows, Mac OS or Ubuntu desktop enviroments.
+
The Arduino project provides an integrated IDE enviroment to work with your Arduino. This is great if you've never programmed before and are running Windows, Mac OS or Ubuntu desktop enviroments.  
  
Some people prefer to do the programming with a text editor and use ''make'' to compile their code. There are a  number of free programs that help you to do this. The Arduino uses the Amtel AVR ATMEGA series of microcontrollers.  
+
Some people prefer to do the programming with a text editor and use ''make'' to compile their code. You may want to write a program that directly interfaces with some powerful features of the ATMEGA168 chip on the Arduino. An exampe of this may be to write interrupt handlers that allow you do something every time the state of a pin changes.
 +
 
 +
There are a  number of free programs that help you to do this. The Arduino uses the Amtel AVR ATMEGA series of microcontrollers.  
 
  apt-get -y install gcc-avr avr-libc avrdude
 
  apt-get -y install gcc-avr avr-libc avrdude
 +
You can use the Makefile linked below. You will need to edit it to fit the details of your Arduino setup. When working correctly the output was:
 +
<pre>
 +
colourbox:~/blink# make all
 +
avr-gcc -I. -I/usr/avr/include/avr -g -mmcu=atmega168 -Os -fpack-struct -fshort-enums -funsigned-bitfields -funsigned-char -Wall -Wstrict-prototypes -Wa,-ahlms=blink.lst -c blink.c -o blink.o
 +
In file included from blink.c:2:
 +
/usr/lib/gcc/avr/4.1.0/../../../../avr/include/util/delay.h:136:3: warning: #warning "F_CPU not defined for <util/delay.h>"
 +
avr-gcc -Wl,-Map,blink.out.map -mmcu=atmega168 -lm  -o blink.out blink.o   
 +
colourbox:~/blink# make writeflash
 +
avr-objcopy -j .text                    \
 +
                -j .data                      \
 +
                -O ihex blink.out blink.hex
 +
avr-objcopy -j .eeprom                  \
 +
                --change-section-lma .eeprom=0 \
 +
                -O ihex blink.out blink.ee.hex
 +
avrdude -v -F -c stk500v1  \
 +
        -p m168 -P /dev/ttyUSB0 -e        \
 +
        -b 19200 -u -U flash:w:blink.hex
 +
 +
avrdude: Version 5.2, compiled on Oct 19 2006 at 17:24:52
 +
        Copyright (c) 2000-2005 Brian Dean, http://www.bdmicro.com/
 +
 +
        System wide configuration file is "/etc/avrdude.conf"
 +
        User configuration file is "/root/.avrduderc"
 +
        User configuration file does not exist or is not a regular file, skipping
 +
 +
        Using Port            : /dev/ttyUSB0
 +
        Using Programmer      : stk500v1
 +
        Overriding Baud Rate  : 19200
 +
......
 +
</pre>
  
 
*[http://www.pcb-europe.net/pipermail/developers_arduino.cc/2007-March/000269.html About the command line approach]
 
*[http://www.pcb-europe.net/pipermail/developers_arduino.cc/2007-March/000269.html About the command line approach]

Revision as of 09:38, 26 September 2008

Arduino is a small computer that connects to the pc with usb. Under debian with a 2.6.18-6-686 kernel it is detected automatically by the usbcore driver. Make sure the link on the Arduino is set to usb power and plug it into the powered on pc. The green LED on the Arduino should light.

Type

dmesg

and you should see some messages like this at the end of the output.

usb 1-2: new full speed USB device using uhci_hcd and address 4
usb 1-2: configuration #1 chosen from 1 choice
ftdi_sio 1-2:1.0: FTDI USB Serial Device converter detected
drivers/usb/serial/ftdi_sio.c: Detected FT232BM
usb 1-2: FTDI USB Serial Device converter now attached to ttyUSB0

This means the Arduino has been detected. There will now be a device in /dev called ttyUSB0 or similar.

ls /dev/ttyUSB*

This is a normal character device and you can send text to it or read from it very simply.

echo foo > /dev/ttyUSB0

The text foo is written to the Arduino over the serial usb line. You should see the small orange LED flicker as the data is recieved by the Arduino. Of course data can be sent in both directions. If you want to read from the Arduino you can do:

tail -f /dev/ttyUSB0

In order for anything to happen, the Arduino must have some kind of program that sends output, otherwise you will see no result.

Building software for Arduino under linux

The Arduino project provides an integrated IDE enviroment to work with your Arduino. This is great if you've never programmed before and are running Windows, Mac OS or Ubuntu desktop enviroments.

Some people prefer to do the programming with a text editor and use make to compile their code. You may want to write a program that directly interfaces with some powerful features of the ATMEGA168 chip on the Arduino. An exampe of this may be to write interrupt handlers that allow you do something every time the state of a pin changes.

There are a number of free programs that help you to do this. The Arduino uses the Amtel AVR ATMEGA series of microcontrollers.

apt-get -y install gcc-avr avr-libc avrdude

You can use the Makefile linked below. You will need to edit it to fit the details of your Arduino setup. When working correctly the output was:

colourbox:~/blink# make all
avr-gcc -I. -I/usr/avr/include/avr -g -mmcu=atmega168 -Os -fpack-struct -fshort-enums -funsigned-bitfields -funsigned-char -Wall -Wstrict-prototypes -Wa,-ahlms=blink.lst -c blink.c -o blink.o
In file included from blink.c:2:
/usr/lib/gcc/avr/4.1.0/../../../../avr/include/util/delay.h:136:3: warning: #warning "F_CPU not defined for <util/delay.h>"
avr-gcc -Wl,-Map,blink.out.map -mmcu=atmega168 -lm  -o blink.out blink.o    
colourbox:~/blink# make writeflash
avr-objcopy -j .text                    \
                -j .data                       \
                -O ihex blink.out blink.hex
avr-objcopy -j .eeprom                  \
                --change-section-lma .eeprom=0 \
                -O ihex blink.out blink.ee.hex
avrdude -v -F -c stk500v1   \
         -p m168 -P /dev/ttyUSB0 -e        \
         -b 19200 -u -U flash:w:blink.hex 

avrdude: Version 5.2, compiled on Oct 19 2006 at 17:24:52
         Copyright (c) 2000-2005 Brian Dean, http://www.bdmicro.com/

         System wide configuration file is "/etc/avrdude.conf"
         User configuration file is "/root/.avrduderc"
         User configuration file does not exist or is not a regular file, skipping

         Using Port            : /dev/ttyUSB0
         Using Programmer      : stk500v1
         Overriding Baud Rate  : 19200
......

Sample code