Saturday, January 22, 2011

Connecting to Arduino & My First Sketch

I just received my first Arduino board (the Uno) as part of my spoils from the SparkFun Electronics free day.

My goal?  Toy around, clearly.  But seriously, I'd like to build some sort of autonomous robot eventually.  I'd love to get my kids involved at some level, but they're still very young.  We'll see how that works out eventually.

First things first, though... let's get a blinky app going.


I'm currently using the arduino environment to get started.  I may switch to the Eclipse AVR plugin, but that's for another day.

Here's my first sketch:
 
/*
 * First Arduino sketch - Blinky
 *
 */
 
 
#define LED_PIN 13

void turnOnLedShort(char pin);
void turnOnLedLong(char pin);


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

void loop() {
  // Morse Code "SOS"

  // S
  turnOnLedShort(LED_PIN);
  turnOnLedShort(LED_PIN);
  turnOnLedShort(LED_PIN);  

  // O
  turnOnLedLong(LED_PIN);
  turnOnLedLong(LED_PIN);
  turnOnLedLong(LED_PIN);  

  // S
  turnOnLedShort(LED_PIN);
  turnOnLedShort(LED_PIN);
  turnOnLedShort(LED_PIN);  
  
  delay(1000);
}


void turnOnLedShort(char pin)
{
  digitalWrite(pin, HIGH); 
  delay(500);
  digitalWrite(pin, LOW);
  delay(500);
}

void turnOnLedLong(char pin)
{
  digitalWrite(pin, HIGH); 
  delay(1000);
  digitalWrite(pin, LOW);
  delay(500);
}

Once the program was written, I needed to connect to the Arduino board.  Plugged it into my USB port and got this in my dmesg output:

 
usb 6-1: new full speed USB device using ohci_hcd and address 2
usb 6-1: New USB device found, idVendor=2341, idProduct=0001 
usb 6-1: New USB device strings: Mfr=1, Product=2, SerialNumber=220
usb 6-1: Product: Arduino Uno
usb 6-1: Manufacturer: Arduino (www.arduino.cc)
usb 6-1: SerialNumber: 012345689ABCDEF
usb 6-1: configuration #1 chosen from 1 choice
cdc_acm 6-1:1.0: ttyACM0: USB ACM device
usbcore: registered new interface driver cdc_acm
cdc_acm: v0.26:USB Abstract Control Model driver for USB modems and ISDN adapters

Nice, it connected...  The out of the box blinking test program was working fine, so the Uno had power, and it was functional.  Let's upload my program!

First, you've got to configure your arduino envionment.  To do that, change your Arduino preferences file (~/.arduino/preferences.txt).  Change the line:
serial.port=COM1
to:
serial.port=/dev/ttyACM0

Open the Arduino envionment, compile my sketch, and click upload... Here we go!

 
processing.app.SerialNotFoundException: Serial port '/dev/ttyACM0' not found.  Did you select the right one from the Tools > Serial Port menu?
        at processing.app.Serial.<init>(Serial.java:153)
        at processing.app.Serial.<init>(Serial.java:76)
        at processing.app.debug.Uploader.flushSerialBuffer(Uploader.java:75)
        at processing.app.debug.AvrdudeUploader.uploadViaBootloader(AvrdudeUploader.java:93)
        at processing.app.debug.AvrdudeUploader.uploadUsingPreferences(AvrdudeUploader.java:56)
        at processing.app.Sketch.upload(Sketch.java:1603)
        at processing.app.Sketch.exportApplet(Sketch.java:1568)
        at processing.app.Sketch.exportApplet(Sketch.java:1524)
        at processing.app.Editor$DefaultExportHandler.run(Editor.java:2293)
        at java.lang.Thread.run(Unknown Source)

Fail. Okay, Google can help me, and so I sought after an answer.  After a little while, it was apparent that the permissions for the device were wrong.  I could have just switched to root, and chmod it, but that'd be a temporary solution.  I decided to write a udev rule for arduino instead.  Here's what I came up with for /lib/udev/rules.d/90-arduino.rules (Your path may be different.  I'm running Slackware Linux.)

 
KERNEL=="ttyACM*", ATTRS{product}=="Arduino*", SYMLINK+="arduino%n", MODE="0666"

Restarted udev, and plugged the Uno back in, clicked the "Upload" button again.  "Done uploading." in the status bar.  Success!  My little arduino board is now blinking - - - __ __ __ - - -. 

1 comment:

  1. Thanks for you help configuring the udev rules.
    I continued to experience problems until I changed the connection from using a USB hub to one of the direct USB ports on my workstation.

    ReplyDelete