Exploring Arduino: Beginnings on Arch Linux

by Amit


I ordered the Arduino starter kit from Robot Gear which came with the Arduino UNO board and few other essentials – LEDs, Miniature breadboard, Connecting wires, etc. The generic instructions for Linux mentioned in the Arduino playground worked for me without any problems. You will also have to set the permissions for the normal user in Arch Linux to be able to access the serial ports. Please see the Arch Wiki page on Arduino here. I was blinking a LED in no time!

Arduino programming

My first impression of looking at Arduino code is that its based on C/C++, which it is (See here). The Foundations page looks a handy starting point. Personally, I found this booklet provided by Oomlout a very handy reference, you can just hook up the board and start tinkering around with the notes by your side and pretty much learn the basics.

Development Environment

For now, I am just using the Arduino SDK from here. I intend to switch to a relatively mouse free environment right after this post by following the tips on the Arch Wiki above and also here.

Next, I describe my first real experiment with the Arduino board.

Blinking  LEDs

This is a Arduino UNO connected to a miniature breadboard on which you can see a Yellow and a Red LED connected to two digital pins on the UNO via two 330 Ohm resistors. (Excuse the tissue paper base :-) ).

Arduino UNO connected with 2 LEDs on a miniature breadboard

Circuit
The schematic is the same as in CIRC-01, but for two LEDs. So basically you would have one wire going from Pin 12 to one LED, and from Pin 13 to another. Each of them would be connected to the Ground via a 330 Ohm resistor.

Arduino code

What we have here is very simple. The two LEDs shall blink alternately with a delay, which you can specify. This is the code for the above Arduino circuit:

/* Multiple LED Blinking program
  Amit Saha

*/  

// constants won't change. Used here to 
// set pin numbers:
const int numPins = 2;
const int ledPin [] =  {12,13};      // the number of the LED pins

int interval = 1000;           // interval at which to blink (milliseconds)

void setup() {
  // Iterate over each of the pins and set them as output
  for(int i=0;i<numPins;i++)
    pinMode(ledPin[i], OUTPUT);      
}

/* Loop until death */
void loop()
{ 
  for(int i=0;i<numPins;i++)
  {    
    digitalWrite(ledPin[i],HIGH);
    delay(interval); 
    digitalWrite(ledPin[i],LOW);
    delay(interval);        
  } 
}

The basic manuals for Arduino programming shall tell you that the setup() function is executed when you upload your sketch to the board and the loop() function shall continue indefinitely till the power is not plugged out of the board.

The two LEDs, denoted by the integer variable numPins, are connected to the Digital pins 12 and 13 on the UNO board and this is specified by the ledPin[] array. The pins are specified in OUTPUT mode using pinMode() function. That completes the setup() portion of the code.

In the loop ( ) function, what we do is just iterate over each of the LEDs, set to HIGH, wait using delay( ), set it to LOW and do the same for the all the LEDs serially. Once you Verify and Upload your code to the board, you should see the LEDs blinking ON and OFF, one after the other.

As a next step, if you have more LEDs at your disposal, you may want to experiment with CIRC-02.

Advertisements