3 Jan 2011

Getting to Grips with the Arduino

There are plenty of Arduino tutorials out there - try the Arduino website, LadyAda and, of course, Google. I don't plan to add to them.

The ubiquitous "Hello, World!" application is a blinking LED. Typically it blinks the onboard LED. If you're really daring you can plug in an external LED and make that blink. Right. As I said before, my kit (should have) shipped with 60 LEDs. I can't get excited about making LEDs flash whether it's one or sixty, yellow, red or blue. If I want a flashing LED, I'll buy one - it's way cheaper.

A lot of the tutorials build on the flashing/fading LED for several chapters. Unless you are obsessed with them or need simple concepts repeatedly driven home, I'd suggest you skip these chapters and start doing real stuff.

Fortunately for me, the LEDs and resistors had been omitted from my kit, so I was left with the handful of components I had kicking around - one LED, some switches, three resistors of mixed value, a servo, some variable resistors and a couple of LDRs (light dependent resistors).  Oh, and an LCD display I ordered with the kit (about £3.00). Time to get creative!

I said I wanted to make an enlarger timer and controller for photography. A really useful add-on to that would be an exposure meter that I could use to measure the darkness of the darks and the lightness of the lights to calculate the contrast as well as the exposure.

I've built the first bit and it only took about an hour! To achieve this I combined three tutorials from the Arduino website and hacked the sketch (the Arduino world's word for a program) a little. The tutorials are:
  • AnalogReadSerial - reads the value of an analog (non-digital) input, in my case the LDR.
  • Button - reads the state of a digital input. Possibilities are ON or OFF.
  • LiquidCrystal - uses library code to interface to an LCD display.
A light dependent resistor (LDR) normally has a high resistance. As light shines on them, the resistance drops by a known amount. Measuring the drop tells us the relative light intensity. There're some nice pictures here.

It's easier to understand the concept of analogue inputs if you first understand digital! As you probably know, computers work with ones and zeros. One equates to on or true, zero equates to off or false. That doesn't give us a lot to work with. It's okay for a switch, so we configure that as a digital input, but the LDR (which you can also think of, for these purposes, as a variable resistor or potentiomer) has a whole range of 'on' values. We need to take the value and convert it to a digital value. The Arduino cleverly does this for us using a built-in A/D (analogue-to-digital) converter. It represents the value as a number between 0 and 1023. If you have a 10k potentiometer, it'll measure in steps of about ten ohms.

Finally, liquid crystal displays (LCDs) are commonly used in calculators etc. They're more complex than switches and variable resitors so here we introduce the concept of libraries. A library is a bunch of code and functions that someone has written to deal with a specific situation - in this case LCDs. By including the library at the beginning of our sketch, we gain access to all those extra functions.

That's enough for now. Next time I'm going to include the circuit I came up with and the sketch.

No comments:

Post a Comment