Difference between revisions of "Arduino with linux"

From Organic Design wiki
m (Building software for Arduino under linux)
m
 
(10 intermediate revisions by one other user not shown)
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 the powerful features of the ATMEGA168 chip on the Arduino, to write interrupt handlers that allow you do something every time the state of a pin changes.
  apt-cache search avr
+
 
Gives you a list of some packages that can help you cross compile AVR binaries and flash them (avrdude) onto the Arduino.
+
There are a  number of free programs that help you to do this. The Arduino uses the Atmel AVR ATMEGA series of microcontrollers.  
*[http://www.pcb-europe.net/pipermail/developers_arduino.cc/2007-March/000269.html About the command line approach]
+
  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
  
==Sample code==
+
        Using Port            : /dev/ttyUSB0
This example program responds to a binary switch input and lights an LED in response at the same time sending a short message over the serial line.
+
        Using Programmer      : stk500v1
 +
        Overriding Baud Rate  : 19200
 +
......
 +
</pre>
  
 +
== See also ==
 +
*[[Raspberry Pi]]
 +
*[http://www.pcb-europe.net/pipermail/developers_arduino.cc/2007-March/000269.html About the command line approach]
 +
*[http://electrons.psychogenic.com/articles/Makefile.tpl Sample Makefile]
 +
*[http://javiervalcarce.es/wiki/Program_Arduino_with_AVR-GCC Program Arduino with AVR-GCC] (Sample code)
 +
*[http://www.nongnu.org/avr-libc/ AVR libc], [http://www.nongnu.org/avr-libc/user-manual/group__avr__interrupts.html About interrupts]
 +
*[http://javiervalcarce.es/wiki/TV_Video_Signal_Generator_with_Arduino TV video signal generator with Arduino]
 
[[Category:Hardware]]
 
[[Category:Hardware]]
 +
*[http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1187659197 Arduino Pong]
 +
*[http://www.micahcarrick.com/05-11-2006/avr-controlled-gp2d120-distance-sensor.html distance sensors]
 +
*[https://www.mainframe.cx/~ckuethe/avr-c-tutorial/ Tutorial on AVR C]

Latest revision as of 19:00, 1 July 2013

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 the powerful features of the ATMEGA168 chip on the Arduino, 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 Atmel 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
......

See also