Official website for Linux User & Developer
FOLLOW US ON:
Feb
11

Arduino programming guide – part 1

by Gareth Halfacree

Gareth Halfacree shows us how to get started with programming for Arduino and open hardware by create an electronic music box. The first of our three part guide assumes no prior experience in either programming or developing with Arduino…

This article originally appeared in issue 95 of Linux User & Developer magazine.Arduino programming guide - part 1 Subscribe and save more than 30% and receive our exclusive money back guarantee – click here to find out more.

Resources
Arduino Experimenter’s Starter Kit
Arduino IDE

The Arduino project is a hacker’s dream: originally designed as a way of getting students involved in microcontroller programming and electronic design, it offers a quick and easy route for programmers to take their skills out of the digital realm and into the physical.

Better yet, the Arduino’s plug and play nature and the C-based programming environment mean that it’s easy to get started, even if you lack experience in electronics.
To prove it, this three-part project is going to walk you through building a simple Arduino music box, programming it with the Arduino IDE, integrating it with your PC, and finally adding a useful LCD display to your project.

As a completely open source project, Arduino programming goes well with Linux development, and all the code and hardware designs behind Arduino are freely available for tinkering.
Before we can get started, we’ll want to install the Arduino development environment.  If you’re lucky enough to be running a version of Linux for which pre-packaged versions exist, it’s a simple case of downloading them from the Arduino website or installing directly from your distribution’s repositories. If not, there are a few steps we’ll have to go through before we can get started with building our first Arduino circuit.

Installing the Arduino IDE
1. Grab the dependencies
If you’re not using a pre-packaged version of the Arduino IDE, you’ll need to install certain dependencies. In Debian-based distributions, simply run:

$ apt-get install openjdk-6-jre gcc-avr avr-libc avrdude binutils-avr

For Fedora-based distributions, the dependency list is slightly different:

$ yum install uisp java-1.6.0-openjdk avr-libc avr-gcc-c++ rxtx avrdude

2. Change group membership
As the Arduino IDE requires access to the USB port to program the board, you’ll need to make sure that you’re a member of the right groups.  As root, change the group memberships as follows:

$ usermod -a -G uucp,dialout,lock $USER

3.Install Arduino
The easiest way to get the Arduino IDE running on your system in lieu of a suitable package is to download the pre-compiled binaries and extract them into your home directory:

wget http://arduino.googlecode.com/files/arduino-0021.tgz

If you’re running a different distribution, or if the steps outlined above don’t work for you, more detailed instruction are available on the Arduino Playground.

Arduino programming guide - part 1Wiring up your first circuit
Before we get started writing our project in the Arduino IDE, we need to build the circuit.  For that, we’ll need our Arduino development board, a piezoelectric buzzer, some wire and a breadboard. If you’re using the Arduino ARDX Kit from Oomlout, then you’ve already got all the parts required. The Arduino board features multiple pins, some of which are capable of acting as analogue inputs for sensors while others can act as pseudo-analogue outputs thanks to a technique known as ‘pulse width modulation’.

For our simple starter project, however, we’re only going to use a single digital output pin.

4. Connect the piezoelectric buzzer to Pin 8
Our program is going to control the buzzer through Pin 8, one of the Arduino’s digital output pins.  Before that can happen, we need to connect the buzzer to the output pin on the breadboard (Fig 1).  If you’re using wire instead of a breadboard, just connect the positive wire directly to the pin.

Arduino programming guide - part 1

5. Connect the piezoelectric buzzer to the Ground Pin
Before we can use the buzzer, it needs to have a path to ground in order to complete the circuit.  Thankfully, the Arduino includes a ground pin that we can connect directly into (Fig 2).

6. Check your circuit
The finished circuit should connect the buzzer to the Arduino, and look something like Fig 3. Double-check that the wires lead to the same row as the buzzer’s pins, as it’s easy to miss.

Arduino programming guide - part 1

Programming the music box
As it stands at the moment, we’ve got a simple circuit that doesn’t do anything.  In order to make our collection of components become the electronic music box that we originally envisaged, we need to tell the microcontroller at the heart of the Arduino exactly what we have plugged into it and how we want it to operate the components.

Thankfully, the processing-based Arduino IDE makes this easy.  The complexities of coding for a microcontroller are abstracted away thanks to a library called Wiring, which allows you to address each pin of the microcontroller in a simple manner. Anyone familiar with the C programming language will feel immediately at home writing for the Arduino, but there are some ‘gotchas’ that can trap the unwary. An Arduino program is known as a ‘Sketch,’ and to make our simple music box we’re going to load one of the example Sketches to customise rather than reinventing the wheel.

7. Load the sample Sketch
After loading the IDE, you can find the Sketch we want – written for the Arduino project  by Tom Igoe back in January – under:
File>Examples>Digital>toneMelody
This Sketch contains an included header file, called ‘pitches.h’, which will also load automatically (Fig 4).

Arduino programming guide - part 1
Load the sample toneMeloyd Sketech to customise

8. The empty loop
Usually the meat of a Sketch can be found in the loop section, which runs continuously while power is applied to the Arduino. Because a constantly repeating tune would be annoying, you’ll notice that this section is empty in our sample program.

void loop() {
  // no need to repeat the melody.
}

Continue to: Page 2 – The setup

Linux User & Developer is the magazine for the GNU Generation
Click here to try 3 issues for £1

Return to the homepage
See what features in our latest issue

twitter follow us

Pages: 1 2
  • Tell a Friend
  • Follow our Twitter to find out about all the latest Linux news, reviews, previews, interviews, features and a whole more.