Arduino hardware hacking: Part 1




Arduino is cool. It’s cool because it’s a tiny device – about three inches by two inches – that comes with a USB port and a programmable chip. It’s cool because you can program it using a very simple programming language known as Wiring.

But most of all, it’s cool because the entire reference design for the hardware is available under a Creative Commons licence, so you’re free to build your own if you want to. However, that’s probably a little extreme for most people, which is why you can also buy pre-built Arduino boards that are ready for action and available at very low prices.

In this tutorial, the first part of a mini-series, you’ll learn all you need to get started with Arduino hardware hacking…

The Arduino board is tiny, which means you can cram it into all sorts of cunning places.

Arduino hardware hacking - Part 1




The Arduino programming IDE is available under the GPL for Mac OS X, Windows and, of course, Linux, so the only things standing between you and your own pet hardware project are an Arduino board, a cool idea, and of course a Box O’ Tricks – some neat little parts you can plug into the Arduino to make it do more interesting things.

Arduino boards come in several varieties, but the main three are the Arduino NG (‘next generation’), the Arduino Diecimila and the Arduino Duemilanove. These aren’t competing boards – the NG came first, was replaced by the Diecimila, which in turn was replaced by the Duemilanove. We have an NG because we bought it a while ago, but if you try to buy one now you’ll probably only find the Diecimila or the Duemilanove for sale.

Ultimately the difference between these boards is very small: some have a ATmega168 CPU rather than the less-powerful ATmega8 found in the plain NG, and newer models have been tweaked to make it fractionally easier to load programs. The ATmega8 and ATmega168 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 Arduino board is suitable for following this tutorial.

Electronics and safety

Please remember that electronic parts can be very sensitive: static electricity can be fatal to your Arduino and many other small computing parts, so wear anti-static wristbands and try not to shuffle your feet. Equally, though, remember that electronic parts can be dangerous to you – they often have lead in them, so make sure you wash your hands thoroughly after working with your kit!

Arduino hardware hacking circuit- Part 1

Putting together little circuits is great fun, but please be careful!

Where to go shopping

As for where to get your Arduino board, we recommend E-Lioness (www.e-lioness.com) – they have the Duemilanove on sale there for €22, and the people there are very happy to answer questions if you’re unsure precisely what you want.

Now comes the slightly more complicated part: what do you want to plug into your Arduino? By itself, the board has 14 digital connectors and a further six 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 goodies for $30 or so.

If you’re in the UK, your best bet is to place an order at Maplin for the parts you want – although we recommend you throw in some of their wonderful Lucky Bags, because it’s surprising the ideas that you can get when you are 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 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 following if you want to complete this Arduino primer:

  • A solderless breadboard This is usually a rectangular white block of plastic, with lots of holes in, and carries current so you can hook things together.
  • Some jumper wires You can buy these pre-cut at different lengths, or make them yourself – it’s all the same.
  • A Type A to Type B USB connector In normal speak that’s standard USB on one side, and the chunky USB style on the other side.
  • Some LEDs Be colourful!
  • Resistors of various strengths You won’t need anything more than 10k Ohm resistors, but these are so cheap that you might as well buy a selection.
  • A potentiometer
  • A light-dependent resistor

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 have some fun later!

A USB type 2 (left) to USB type 1 (right) cable is required to connect the Arduino to your PC.

Blinkenlights

The first thing to do with any programming project – hardware or otherwise – is usually something akin to a ‘hello world’ test: make your program send a simple message to the outside world to show that 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 software is installed, run “sudo update-alternatives –config java” and select the number of the official Sun Java. That ought to be enough to get things working. 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 you probably won’t have to worry about the update-alternatives system with other distros.

Before you start coding, you need to download the latest Arduino IDE – extract it to your desktop, then run the command “arduino”. If you have problems, try running it from a 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 programs should be stored – a subdirectory of your home directory is fine.

The Arduino IDE is very simple, offering code highlighting, save and load, and not much more – but you can upload to your board straight from here.

Now that you have your Arduino software installed, go ahead and hook up the Arduino to your PC using a USB cable. If the board is working OK, the PWR (power) light will be on, and your 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 easy to get started with. We’re going to start with a simple project to make an LED flash on and off, and we want to show you the code and make sure it works before we look at how it works. Here’s the sketch:

int ledPin = 13;
void setup()
{
pinMode(ledPin, OUTPUT);
}
void loop()
{
digitalWrite(ledPin, HIGH);
delay(300);
digitalWrite(ledPin, LOW);
delay(100);
}

Now go to Tools > Microcontroller and choose either atmega8 (if you have the ArduinoNG) or atmega168 (if you have the Diecimila). If you’re not sure, ignore this step – Arduino will check the CPU when it connects, and report an error if it finds the wrong CPU. If you get such an error, just choose the other option!

With the CPU configured, go to Tools > Serial Port, and you’ll see a list of 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!

Before you try uploading to a board, make sure you select the correct serial port for your Arduino – it’s probably /dev/ttyUSB0 or something similar.

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 instructions are going to arrive, and finally your little test LED should start flashing. Success!

You can take this little project one step further by taking an LED from your kit bag and plugging it into digital pin 13. If you haven’t used an LED before, you should look closely at it before plugging it in: note that it has two wires coming out, that one wire is longer than the other, and that one side of the plastic-coloured rim is flat.

The longer wire and the flat side are there to indicate polarity: the side with the shorter wire and the flat rim is negative. When plugging your LED into your Arduino board, make sure the positive wire goes into pin 13, and the negative wire goes into GND (Ground), and all being well you should seeing it flash in time with the test LED. If 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, let’s have a look at 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 send data to pin number 13 rather than read data.
  • “loop()” is another default function for the Arduino, and is called every time the CPU is looking for some work to do.
  • “digitalWrite(ledPin, LOW)” turns the LED off.
  • “digitalWrite(ledPin, HIGH)” means ‘send the value HIGH to pin 13’ – HIGH is equivalent to a binary 1 versus a binary 0, or an ‘on’ compared with an ‘off’. In this case, it turns the LED on.
  • “delay(300)” makes the CPU pause for 300 milliseconds, or about a third of a second.
  • “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 can go, but having those delay() calls in there forces the CPU to pause for breath frequently – otherwise the LED would flash so quickly that it would just appear to be on! Remember that most LEDs have a little lens at the top that focuses their light – you’ll probably find your LED appears brightest when you look straight down at it.

 

For more detail: Arduino hardware hacking: Part 1




Clip to Evernote

Leave a Comment

(Spamcheck Enabled)

Read previous post:
Fluid In.Flux_3D Wax Printing In Water

Fluid In.Flux is a 3D printing experimental machine which prints wax in water. The Machine was a semester-wide exploration as...

Close
Scroll to top