0% found this document useful (0 votes)
10 views6 pages

LXF100 Arduino

The document provides an introduction to Arduino, a small programmable hardware device that allows users to create electronics projects. It discusses the different types of Arduino boards, necessary components for projects, and basic safety precautions when working with electronics. Additionally, it includes a simple tutorial on programming an LED to flash using the Arduino IDE and explains the code structure involved in the process.

Uploaded by

polite71
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views6 pages

LXF100 Arduino

The document provides an introduction to Arduino, a small programmable hardware device that allows users to create electronics projects. It discusses the different types of Arduino boards, necessary components for projects, and basic safety precautions when working with electronics. Additionally, it includes a simple tutorial on programming an LED to flash using the Arduino IDE and explains the code structure involved in the process.

Uploaded by

polite71
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Open source electronics

Hardware hacking
made easy
Any old geek can program software, but it takes some serious commitment to
computers to want to program hardware. We show you how to get started…

A
rduino is cool. It’s cool because it’s a tiny device – parts you can plug into the Arduino to make it do more
about three inches by two inches – that comes interesting things.
with a USB port and a programmable chip. It’s
cool because you can program it using a very Getting set up
simple programming language known as Wiring. But most of Arduino boards come in several varieties, but the main three are
all, it’s cool because the entire reference design for the the Arduino NG (“next generation”), the Arduino NG Plus, and the
hardware is available under a Creative Commons licence, so Arduino Diecimila. These aren’t competing boards – rather the NG
you’re free to build your own if you want to. However, that’s came first, and was replaced by the NG Plus, then the Diecimila.
probably a little extreme for most people, which is why you We have an NG because we bought it a while ago, but if you try to
can also buy pre-build Arduino boards that are ready for buy one now you’ll probably only find the Diecimila for sale.
action, and they are available at very low prices too. The Ultimately the difference between these boards is very small: the
Arduino programming IDE is available under the GPL for NG Plus has a ATmega168 CPU compared to the ATmega8 found
multiple platforms, so the only things standing between you in the plain NG, and the Diecimila has been tweaked to make it
and your own pet hardware project are an Arduino board, a fractionally easier to load programs. The ATmega8 and ATmega168
cool idea, and of course a Box O’ Tricks – some neat little chips are very similar, with the primary difference being that the
168 has space for larger programs, but that won’t be a problem for
our purposes here – any of those three boards are suitable for
Electronics and safety following this tutorial.
As for where to get your Arduino board, we recommend PCB
Please remember that electronic parts can be very sensitive: static electricity can Europe (http://pcb-europe.net) – they have the Diecimila on
be fatal to your Arduino and many other small computing parts, so don’t shuffle sale there for 22 Euros (£15/$31 at the time of writing), and they
your feet and wear anti-static wrist bands. Equally, though, remember that are very happy to answer questions if you’re unsure precisely what
electronic parts can be dangerous to you – they often have lead in them, so make you want.
sure you wash your hands thoroughly after working with your kit! Now comes the slightly more complicated part: what do you
want to plug into your Arduino? By itself, the board has 14 digital

50 Linux Format Christmas 2007

LXF100.arduino Sec1:50 30/10/07 10:42:58


Open Source electronics

A USB type 2 (left) to USB type 1 (right) cable is required to


connect the Arduino to your PC.

connectors and a further 6 analogue connectors, as well as a


built-in LED and a reset button, so you need to buy extra parts to
make it do things. If you’re in the US, you’re probably lucky
enough to have a Radio Shack in close driving distance, and you’ll
find a mine of cool stuff there ready to play with – just talk to one
of the store assistants, explain you’re trying your first electronics
projects, and you should be able to walk away with a basketful of can hook things together. The Arduino IDE
goodies for $30 or so. If you’re in the UK, your best bet is to place Some jumper wires. You can buy these pre-cut at different is very simple,
offering code
an order at www.maplin.co.uk for the parts you want – although I lengths, or make them yourself – it’s all the same.
highlighting, save
recommend you throw in some of their wonderful Lucky Bags, A Type A to Type B USB connector. In normal speak that’s
and load, and not
standard USB on one side, and the fat USB style on the other side.
much more – but
“The first thing with any Some LEDs. Be colourful!
Resistors of various strengths. You won’t need anything more
you can upload to
your board straight
programming project is than 10K Ohm resistors, but these are so cheap that you might as
well buy a selection.
from here.

usually akin to performing A potentiometer.


A light-dependent resistor.
a ‘hello word’ test.” You should be able to buy all the above for £10 and still have
change afterwards, so don’t be afraid to buy a few extra parts to
because it’s surprising the ideas that you can get when you are have some fun later!
given random parts to work with! Outside of the US and the UK,
you can either try to track down a good electronics store in your
country (and by “electronics” we don’t mean “sells iPods”!), or you Blinkenlights
can go back to PCB Europe and buy one of its Arduino parts kits.
No matter which option you take, you need at least the The first thing to do with any programming project – hardware or
following if you want to complete this Arduino primer: otherwise – is usually something akin to “hello world”: make your
A solderless breadboard. This is usually a rectangular white program send a simple message to the outside world to show that
block of plastic, with lots of holes in, and carries current so you you have everything configured correctly.
The Arduino has several built-in LEDs, such as the TX and RX
LEDs that flash when data is being sent or received. We’ll be using
a special testing LEDs to show that the board is working OK.
Getting Arduino to work on Linux can be a little
tricky, because it uses Java. What’s more, it needs
the official Sun Java rather than a clone. If you’re
using Ubuntu, make sure you have the Multiverse
repository enabled, then install the
packages sun-java5-jre, gcc-avr and
avr-libc. Many users have trouble with
braille support clashing with Arduino,
so if you don’t need braille support
you should remove the brltty
package first – if you plugged in
your Arduino board while brltty was
installed, unplug it and try again once
brltty is removed. Once all the
The Arduino board
software is installed, run sudo
is tiny, which means update-alternatives --config
you can cram it into java and select the number of the
all sorts of cunning official Sun Java. That ought to be
places. enough to get things working.

Christmas 2007 Linux Format 51

LXF100.arduino Sec1:51 30/10/07 10:43:12


A full-size solderless breadboard usually is usually 60 or
so pins wide, by at least 10 high. In this picture, the rows are
connected and the columns are not.

test LED might already be flashing to show that something is on


the board already.
In the Arduino editor, go to File > New to enter some new code.
Code files in Arduinoland are known as “sketches”, and they are
very easy to get started with. We’re going to start with a simple
project to make an LED flash on and off, and I want to show you
USB in, wires out, the code and make sure it works before we look at how it works.
and a light sensor in Here’s the sketch:
between – once your int ledPin = 13;
Arduino is hooked up
to your breadboard and void setup()
your PC, the possibilities
{
are endless!
pinMode(ledPin, OUTPUT);
}
If you’re not using Ubuntu or another Debian-based system,
the same routing is required: Sun’s Java, gcc-avr and avr-libc, but void loop()
you probably won’t have to worry about the update-alternatives {
system with other distros. digitalWrite(ledPin, HIGH);
We’ve included the Arduino development environment (IDE) delay(300);
on your DVD this month – extract that to your desktop, then run digitalWrite(ledPin, LOW);
the arduino command. If you have problems, try running it from a delay(100);
terminal so you can see any error messages. The first time you }
run the IDE, you’ll be asked to choose a place where your Now go to Tools > Microcontroller and choose either atmega8 (if
The Arduino programs should be stored – a subdirectory of your home you have the ArduinoNG) or atmega168 (if you have the
board is dominated directory is fine. Diecimila). If you’re not sure, ignore this step – Arduino will check
by the ATmega
Now that you have your Arduino software installed, go ahead the CPU when it connects, and report an error if it finds the wrong
chip, but you can
and hook up the Arduino to your PC using a USB cable. If the CPU. If you get such an error, just choose the other option! With
see the analog pins
board is working OK, the PWR (power) light will be on, and your the CPU configured, go to Tools > Serial Port, and you’ll see a list of
at the top and the
digital pins at the
bottom.

My breadboard has numbers as shown above for the rows and


letters for the columns; I’ve referred to them throughout this
feature so that you can copy my circuits exactly.

52 Linux Format Christmas 2007

LXF100.arduino Sec1:52 30/10/07 10:43:20


Open Source electronics

A potentiometer reports higher analog values as you turn its


knob, so make sure you use analogRead()!

possible USB devices. On Linux, chances are it’s /dev/ttyUSB0,


so select that.
OK, that’s all the configuration done – it’s time to upload your
sketch to the board! On the IDE menu bar, you’ll see an icon with a
right-facing arrow on – that’s the Upload button. If you have a
Diecimila, you can click that now and your program will be
uploaded. Otherwise, you need to press the little reset button on
the Arduino board first so that the Arduino is ready to accept a
new sketch, then press the upload button. The TX and RX lights
should flicker for a second or so, followed by about five seconds of
nothingness as your Arduino waits to see if any further
Connect the LED
instructions are going to arrive, and finally your little test LED send data to pin number 13 rather than read data.
digital pin 13/GND,
should start flashing. Success! loop() is another default function for the Arduino, and is called but make sure you
every time the CPU is looking for some work to do. look at the wire
digitalWrite(ledPin, HIGH) means “send the value HIGH to pin
“Should you plug your 13” – HIGH is equivalent to a binary 1 versus a binary 0, or an “on”
length on the LED
to see which side is
LED in the wrong way compared to an “off”. In this case, it turns the LED on.
delay(300) makes the CPU pause for 300 milliseconds, or
positive!

around, nothing will about a third of a second.


digitalWrite(ledPin, LOW) turns the LED off.
happen; just reverse it.” delay(100) makes the CPU wait a tenth of a second
And that’s it! The loop() function gets called as fast as the CPU
You can take this little project one step further by taking an can go, but having those delay() calls in there
LED from your kit bag, and plugging it into digital pin 13. If you forces the CPU to pause for breath
haven’t used an LED before, you should look closely at it before frequently – otherwise the LED If you have
plugging it in: note that it has two wires coming out, that one wire would flash so fast it would just everything working
is longer than the other, and that one side of the plastic-coloured appear to be on! correctly, your light
rim is flat. The longer wire and the flat side are there to Remember that most blinking sketch
will result in the
indicate polarity: the side with the shorter wire LEDs have a little
power light being
and the flat rim is negative. When plugging your lens at the top
on (green), the
LED into your Arduino board, make sure the that focuses test LED being
positive wire goes into pin 13, and the negative their light – on (yellow) and
wire goes into GND (Ground), and all your LED (red in
being well you should seeing it flash in our example) also
time with the test LED. If being on.
you get it the wrong way
around, nothing will
happen, but don’t leave
the LED plugged in like
that too long!

How the code


works
Now that your Arduino board works
properly, I want to explain how the code
works so you can try modifying it yourself:
int ledPin = 13 defines a variable, ledPin, that
holds an integer (a whole number, ie not numbers like
3.1) and is giving the starting value of 13.
setup() is a default function for the Arduino. It gets called
when your program starts so you can set up some basic
configuration.
pinMode(ledPin, OUTPUT) tells the Arduino that you want to

Christmas 2007 Linux Format 53

LXF100.arduino Sec1:53 30/10/07 10:43:51


Open source electronics

Resistors are so
cheap you usually
buy at least five at
a time – make sure
and look closely
at the stripes to
determine how
much resistance
they offer. You can buy a bag of pre-cut wires from any good electrical
store. If you want to look really smart, you can even use colours
for certain types of connections!

you’ll probably find your LED appears brightest when you look one is marked 9V? That’s where we can get more power from, but
straight down at it. you need to be careful – if you put too much through an LED, you
can burn it out. That’s not a big problem if you bought a mountain
Using the breadboard of LEDs, but if you only have three or four you need to make them
The solderless breadboard you have has strips of metal hidden last! However, it’s safe to plug it in for a second or two just to make
away under the holes, connecting rows of pins together as a sure it works, so please do that now: unplug your Arduino so it has
circuit. The columns aren’t connected, which means they don’t no power, then connect 5V to J33 and the Gnd next to 5V to J32.
carry a connection horizontally – only vertically. If you have a full- Now plug it in for a second: your LED should light up much, more
size breadboard like ours, you’ll have a gap between rows; the brightly (and may even start to get warm!). Once you can see that
rows on either side of these gaps aren’t connected either. To give it works, unplug the USB cable again so the light goes back out.
you an example of how this works, we can modify the simple LED Now, let’s up the complexity a little: I want to introduce you to
system we have now by introducing the breadboard and two resistors. These little things produce a voltage drop of different
wires. My board has columns numbered 1 to 60 and rows lettered levels, depending on the variety of resistor you have. The exact
A to J, as is shown in the photos of it on the previous page, and I’ll resistance is shown in the little coloured stripes on the resistors,
be using these but these are absolutely meaningless for mere mortals – don’t be
“Refer to your resistor numbers to explain afraid to keep around a resistor cheat sheet showing what the
where to put your different stripes mean. For now, connect 5V to J38 so that the LED
‘cheat sheet’ so that you wiring. Even if yours no longer has power going to it. To complete the circuit, you need

can decipher what their doesn’t have similar


numbering, these
to connect a resistor from I38 to J33 , and now you should see the
LED light – just a bit dimmer. If the resistor colour codes are
coloured stripes mean.” numbers will still be
helpful for you because
meaningless to you, try using different resistors here and
arranging them by strength based on the LED brightness!
they tell you what row and column I’m using, and that’s all that
really matters. Reading input
So, to move the blinkenlights “project” over to the breadboard, In your kit bag you should have a light sensor, which reports
connect a wire from GND to J33, and another wire from Digital 13 different values depending on the amount of light it receives. This
to J32. Now, that has hooked up current to I33 and I32, H33 and takes a few more wires, an extra function call, as well as a
H32, G33 and G32, and F33 and F32. My breadboard jump is conditional statement, but it’s really not so hard – and it’s worth
between row F and row E, which means E33 and E32 aren’t part of the effort because you can start writing code to make the Arduino
the circuit. Now all you have to do is put your LED in the right aware of its environment! I have two special lanes on my
place to finish the circuit: where do you think it should go? breadboard designed to make it easy to conduct power, but I won’t
Unless you’re very fearful of electric circuits, you can probably be using that here because smaller breadboards don’t always
just have a dabble and try things until the LED lights up – you come with something similar.
learn a lot by dabbling, and it’s more fun, too! In this example, First, plug your light sensor into your breadboard. I’ve put mine
plugging your LED into F33 (negative; short wire) and F32 on F34 and F37. Plug a strong resistor into G34, and connect it to
(position; long wire) would make it light up, but so would plugging I29, then connect a wire from J29 to 5 – this gives your light
into column G, H or I. sensor power. You also need to connect a wire from G37 to the
Now, let’s try something else: over on the other side of your Gnd pin next to 5V, so that the circuit is complete. To read a value
Arduino board are Analog In pins, as well as a few marked Power. from the light sensor, connect another wire from H34 to Analog 0,
These carry a lot more current – note than one is marked 5V and and that’s your system ready to be programmed!
Reading from an analog input in an Arduino sketch is very easy:
you need to store which pin you’re reading from (0), as well as
Hardware in LXF store the value from the light sensor. This is all done using the
analogRead() function, which returns a value you can store and
We very rarely cover hardware like the Arduino in Linux Format – in fact, this is a bit of a work with. In a sketch it looks like this:
first for us! So, please write in and let us know what you think: do you want to see us int ledPin = 13;
write some follow ups to this primer, using more parts, more complex sketches, and int lgtPin = 0;
perhaps even some complete projects? Or would you rather we stayed away from int lgtVal = 0;
hardware altogether? Let us know! [email protected]
void setup()

54 Linux Format Christmas 2007

LXF100.arduino Sec1:54 30/10/07 10:44:22


Open Source electronics

“Reading from an analog


input in an Arduino sketch
is very easy – use the
analogueRead() function.”
{
pinMode(ledPin, OUTPUT);
}

void loop()
{
lgtVal = analogRead(lgtPin);
digitalWrite(ledPin, HIGH);
delay(lgtVal);
digitalWrite(ledPin, LOW);
delay(lgtVal);
}
What do you think that sketch will do? If you’re not sure, try
assembling it, uploading the sketch to your board, and covering/
Before you try
revealing the light sensor! the right pin. Make sure your potentiometer is very firmly plugged
uploading to a
into the breadboard – they usually have thicker connectors, so you
board, make sure
More to try... might have to give it a good push! you select the
If you managed to pick up a potentiometer, these are particularly Of course, this has only really been a very light overview of correct serial port
easy to drop in once you have your light sensor working: your what Arduino can do, and that’s because the real magic comes in for your Arduino
cable to Analog 0 needs to connect to the middle pin of the the software you build. We’ve shown you here how to put together – it’s probably
potentiometer; your cable to Ground needs to connect to the left circuits to read and write data, and the next question is: what are /dev/ttyUSB0 or
pin; and your cable to the power (via a resistor) should connect to you going to do with it? LXF something similar.

Get online... www.linuxformat.co.uk


... and get more from Linux Format!
Visit our ever-expanding wiki for in-depth info
covering masses of Linux subjects, including:
First steps with printing, making videos and web security
58 cool tips and tricks to improve your Linux box
Building your own custom Linux distribution
Creating a locked-down Linux desktop for kids
Programming with PHP, C#, Perl and Subversion
Producing cool music and sound effects with Jack
...and all editable so you can add your own guides and tips!

Christmas 2007 Linux Format 55

LXF100.arduino Sec1:55 30/10/07 10:44:30

You might also like