Arduino project 7 – buttons

arduino project with an LED and two buttons

two buttons and a light

The next arduino project in the book is the first to use input: it uses two buttons and an LED, so that pushing one button will turn the light on and the other turns it off again.  Still a simple idea, but one that illustrates how input can be used in a program to control a light, and could be extended theoretically to control any components by pushbutton input.

The loop part of the program is straightforward:

void loop(){
   if (digitalRead(inputPin1)==LOW){
     digitalWrite(ledPin,LOW); //turn LED off
 else if (digitalRead(inputPin2)==LOW){
   digitalWrite(ledPin, HIGH);//turn LED on

It was strange to be back writing in Java again with its curly braces and semicolons, not to mention having to declare variable types, but I’m finding it easier these days to switch between the two – and of course indenting comes through habit having dealt with Python so much!

What does frustrate me now is that theoretically I should be able to code a set of pelican crossing lights triggered by pressing a button, and I’m sure that I could reasonably easily figure out the code to use, but the knowledge I don’t have is how to join the components together, as the project book tells you what to use and where but not why or how they work. Comments like “there is one component that might seem out of place…” really don’t help, as its assuming more knowledge of the circuit than I have.

I’m starting to make a point of looking at the circuit diagram supplied as well as the schematic of the construction, but I’ve no idea when resistors are needed or what size to use, or when a capacitor or transistor is needed, and I would not be able to create my own project, although I’d be quite happy following the sorts of instructions I’ve come across so far, which are of the type of put X here and Y there and Z goes in this spot.  To be honest, I’m not sure what sort of time I’d have to learn any more about these components, although I have come to enjoy my weekly soldering sessions (last week I made a battery powered gadget that responds to sound by flashing lights on and off, and the timer I made the week before has somehow started working correctly – previously it wasn’t beeping when the time was complete, but just when it was turned off).

What I have gained over the summer, however, is a deeper understanding of what circuit boards are (I even looked up how to make them!), what the different components are, the skill needed to assemble and solder them and the sorts of things that can be done with an arduino and a handful of bits.

There are still four more projects in the book, and there are plenty more arduino books out there, but with summer drawing to a close I’m not sure how much time I’ll have to devote to playing with electronics.  My interest has only grown over the few weeks I’ve been playing, however, and I’ll be doing my best not to abandon the arduino and the soldering iron.

I may even try playing with bits and doing some research to see if I can mock up the three traffic lights, two pedestrian lights and button that would be needed for a pelican crossing prototype!

In the meantime, the weekend’s task has to be to get the Pi up and running and see what I can do with that, so I’m off to tidy my desk up a bit to make room.



Musical arduino

arduino with piezo element for sound

arduino with piezo for sound

Time for another arduino project – project 6 in the kit uses the piezo element to make sound.  While I fully admit to not knowing how the sound works, the program supplied with the kit maps note names to tones, meaning that it’s easy to adapt the lists for notes and beats to play whatever tune you want – as long as it’s in the key of C with no sharps, flats or really odd beats, that is! (although I’m sure it would be simple enough to either find a list of notes mapped to tones or experiment to find the right frequency)

The program originally plays twinkle twinkle little star, but I altered it to play Doe a Deer, by matching the correct list of notes and the correct list of beats.

void playNote(char note, int duration) {
char names[] = { 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C' };
int tones[] = { 1915, 1700, 1519, 1432, 1275, 1136, 1014, 956 };
char notes[] = "cdececedeffedf "; // a space represents a rest
int beats[] = { 3, 1, 3, 1, 2, 2, 4, 3, 1, 1, 1, 1, 1, 2, 4 };

The sound itself could do with amplifying, as it’s a little quiet, but that’s really not a drawback at this stage, possibly more of a mercy!

The code provided could easily be amalgamated into a larger project, I’m sure, making it easy to develop the system into something useful and interesting.  For now, I’m just learning about the different things the arduino can do.

While the coding is fairly straightforward, however, I still don’t have much idea yet of how to join my own components together in a circuit.  Ah well, another five projects to go from the starter kit, then there are plenty more resources out there.

Arduino – using a servo and shift register

servo connected to arduino

servo connected to arduino

The next project in the arduino kit, project 4, is to connect a servo.  In some ways, this is like a motor, but a servo provides much finer control – the arm on the top will rotate by a given angle.  This can then be attached to all sorts of things – servos are commonly used in radio controlled models, for example, to control steering.  The servo in the kit comes with different arms to put on top.  I used one with a double arm, but there is also a single arm and a cross-arm one.

servo arms

Servo arms

Wiring the servo to the arduino was simple with use of the breadboard circuit layout, and the program provided rotates the arm back and forth.  The code to move the arm is simple:

   for(pos = 0; pos < 180; pos += 1) // goes from 0 degrees to 180 degrees 
   {                       // in steps of 1 degree 
       myservo.write(pos); // tell servo to go to position in variable 'pos' 
       delay(15);          // waits 15ms for the servo to reach the position 

This will rotate the arm from 0 degrees to 180 degrees (well technically 179 degrees!).

Obviously the real power of the servo will come when it will rotate in response to input.

8 lights with a shift register

8 LEDs with a shift register

I then worked on project 5 – using a shift register.  This is a little integrated circuit that in this case allows me to control 8 LEDs but only using 3 pins on the arduino instead of the 8 pins I used for project 2. The advantage of this is that it leaves more pins free for other devices.

I hit major problems with this project – the wiring is more complicated than others I’ve done, and at first I had a wire in the wrong place.  This wasn’t helped by the fact there was no explanation of what I should see when the program was running, so although some lights came on I wasn’t sure if it was working right or not.  A rewire sorted most of the problems, but there was still one light that refused to come on.  Closer inspection revealed that one of the pins on the shift register was bent rather than going into its hole.

Once that was sorted as well, the lights lit up in sequence to illustrate binary numbers from 0 to 255, then a poke round the arduino support site found an alternative function which lit the lights in sequence and then turned them off again, and which showed that the lights could be turned on and off by changing digitalWrite()’s in the original wiring without the shift register to changeLED()’s in the new version, so the code itself is very similar.

What I don’t understand at the moment is how the shift register works.  While I can see the new code in action, and it looks straightforward, I would like to feel I understand how it works.  This is where I feel the frustration of not having had anything like this at school.  Still, I have the power of the internet at hand, and I’m sure somewhere out there is a simple explanation.

I feel at the moment the arduino is fairly straightforward when it comes to following the projects and writing the code, but I really don’t have the understanding of the circuitry that I would like, and no idea of how things are connected as the breadboard looks after it all.

I’ve now got a build it yourself FM radio kit (nothing to do with the arduino), so maybe working through that and having a play at proper soldering will give me more of an idea of circuitry, and in the meantime it’s a case of hitting the research before I move on.


Arduino – further experiments

arduino set up with 8 LEDs

Arduino with 8 LEDs

While I had my arduino set up with eight LEDs, I thought it was the ideal opportunity to write my own function to take in a decimal number and display the binary equivalent.

This proved extremely straightforward to do, leaving me with a pleasant sense of having got the idea with output code and basic C structure.

void displayBinary(int number)
   int delayTime=5000;
   int num=number;
   if (num>=128){
   if (num>=64){
   if (num>=32){
   if (num>=16){
   if (num>=8){
   if (num>=4){
   if (num>=2){
   if (num>=1){

Then I just called the function in the loop section, with different integers.  Something to aim for in future is to input a number on the computer and have that number pass to the arduino, which then displays it!

all the assembled parts for testing the motor

The parts for testing the motor

Then it was time to test a different output device, so I collected the pieces for circuit 3, the motor test.  This introduced some new pieces: a transistor, a different resistor and a diode, as well as the motor itself.  I still don’t have much of an idea yet what they do, but it was still straightforward to clip the pieces together to build the circuit. In a way, though, I think the breadboard hinders learning, as although it makes it very easy to connect the pieces together, it’s not obvious what’s going on underneath, and where circuits are being completed.  I also need to learn how to read an electronic circuit diagram sometime!  It’s been years since the batteries, switch and bulb days of my childhood science.

Testing the motor

Testing the motor

Still, the motor and other parts were soon connected (about 10 minutes max), and I downloaded the program from the web and uploaded it to the arduino. There were three sample functions, one to run the motor, one to accelerate it slowly and one to decelerate it.  All seemed to run very smoothly, using the same principle as the code to dim the lights, of sending different values of i in a loop in turn to analogWrite(motorPin, i) .  Now I need to figure out what sort of things I could connect the motor to.  This is where something like lego or k’nex might come in handy, to build a housing and purpose for the circuit.  There is a 9v adapter supplied with the kit, so that it doesn’t have to run connected to the computer.

So I’ve seen some of the outputs in action so far, lights and motor.  Still to come are the servo for finer control of movement and using a shift register, which apparently will reveal how to control a group of LEDs with less than one pin each.  Then there’s sound to add, and then I’m on to the input options.  That’s when things should start getting really interesting as it will then be possible to write interactive programs.

I have now also bought a book on programming the arduino, so there will be plenty more to come on this topic.


Arduino project 2

8 green lights

The 8 lights can be programmed to turn on and off

Project 2 for the arduino board is a series of 8 flashing lights, that can be programmed to turn on and off in different sequences.  A program is available to produce three different sequences, and I was able to add my own function fairly easily to add another sequence.  While I’m not fluent in the language by any means, the code was simple enough, and an array meant it was easy to cycle through the lights.  The setup itself was straightforward, although I did clip the wires on the resistors this time so they fitted more neatly.  The colour of the wires used doesn’t matter at this stage, although I suspect it would be easier to use colour coding on more complicated projects.

int ledPins[] = {2,3,4,5,6,7,8,9}; //this produces an array of ledPins - arrays 
                                   //start from 0, so ledPins[0] is the first
                                   //and ledPins[7] is the last light.

This code sets up the LED pins so each can be referred to by its index in the array.


void setup(){
    for(int i = 0; i < 8; i++){ //this is a loop and will repeat eight times
    pinMode(ledPins[i],OUTPUT); //we use this to set each LED pin to output

This is the setup function, that sets each of the connected pins on the arduino in turn to output mode.


void oneAfterAnotherLoop(){
  int delayTime = 100; //the time (in milliseconds) to pause between LEDs
  for(int i = 0; i < 8; i++){
    digitalWrite(ledPins[i], HIGH); //Turns on LED #i each time this runs i
    delay(delayTime); //delay so you can see effect then increase i and loop 

This is one of the functions that can be called in the loop() method.

I copied the structure of this function to write my own to turn the lights off in turn.  There were a few syntax errors (the beloved brackets and semi-colons!) but on the whole it was straightforward.

So I was left with a feeling of achievement, that was soon overwhelmed by a feeling that there’s still so much I can’t do.  However, I remind myself that people are using this quite confidently in a short amount of time, and of course if I don’t try I will never get anywhere.  I can at least see now how I could build simple flashing light sequences.

What would be interesting would be to get a large array of LEDs set up and write functions to draw different things with them.  This, of course, is how things like moving display signs are programmed – the end user will just type in the text they want, and the program will know how to display that text correctly, either still or moving.  So that’s something to head towards maybe.  An 8×8 bi-colour LED matrix is easily available from the same place I got the starter kit, at £3, and could be used as the basis for a project.  What I’m not sure about is how many pins it connects to.  It does cycle through each column of lights in turn, rather than controlling all 64 independently, which would be rather unwieldy.

I assume the limit for the arduino is the number of pins available to connect to – my uno has 14 digital input/output pins, 6 of which can be used as variable outputs (PWM) and 6 analogue inputs.  The arduino mega provides 54 digital I/O pins (14 provide PWM) and 16 analogue input.  Amazingly, I now understand what that all means, which is far more than I knew before I started, so I must be learning something!

Basically, the digital input/output can be used to turn something on or off (output) or receive an on or off signal (input).  Using one as PWM output means that the output load can be varied, rather than on/off, so can be used for dimming a light, for example.  The analogue input can be used to receive signals from sensors, which tend to be analogue (varying) rather than digital (on/off).

Arduino – first steps

arduino on base

Arduino bolted to its base

Time to get to grips with some technology!

First job was to set up the arduino and install the software.  The arduino bolted to the baseboard and the software was available from the arduino website.  The arduino connected to my computer via the USB port and I located and uploaded the first sample program, which flashes an LED on and off repeatedly.

I originally tried using a video to help me with my first project, but soon discovered the instructions from the arduino website and the experimenter’s guide were more than enough.

first program

This program turns the LED on and off repeatedly

The program seemed clear, although I haven’t tried writing my own yet – apparently it’s based on C.  It has two required functions, the setup function and the loop function.  The setup part tells the system what inputs and outputs to use, and then the loop function carries out the actual work.

Setting up the breadboard itself was rather fiddly at first, until I got the basic idea.  All the layouts for the starter experiments are included, and it’s a case of cutting one out, pinning it to the board and then pushing the right elements through the right holes.  The breadboard then joins to the arduino and sits neatly next to it on the base.

As soon as all the wires were connected, the red LED in the middle of the breadboard started obediently flashing on and off – a very satisfactory end to my first project, with no troubleshooting required.

flashing light project

This simple layout has a flashing LED

This is the project in action.  I need to clip the wires for the components, as at the moment they’re rather long, but I’ll save that for a day when I feel a little braver, as I feel it’s better at the moment to have them too long than too short.

flashing light layout

This shows the layout of the circuit

Instructions are provided for developing the project by connecting the LED to an analogue output instead, so that instead of on or off you can adjust the brightness of the light, and a program is provided to demonstrate this as well.

This shows a close up view of the circuit, including the breadboard with the printed template.  The LED is connected to pin 13 of the arduino, which corresponds to the code lines

int led = 13;

This all seems very straightforward so far, and I’m looking forward to learning more.

(Click on any image to see a larger version)


More about the arduino

inside the arduino starter kit

All the contents neatly laid out

The arduino is described as an opensource electronics prototyping platform based on flexible easy to use hardware and software.  The theory is that you can attach sensors and actuators to it, program it to run by itself or connect it to a computer to run it, and it’s a great introduction to electronics and robotics.  It’s a name that I’ve heard bandied about several times, particularly amongst the Computing At School group that I belong to, and I decided it was about time I found out what it was all about.   I have a starter kit for the arduino, that promises everything you need to get going.  It’s all neatly set out, with a guide in the lid of the box showing what goes where.  I recognised some of the components – LEDs, for example, and a small motor, and a bundle of wires, but others are currently a mystery, although the book that comes with it does seem to provide a very clear guide.

experimenters guide booklet

The book of words

I’m now entering a world with items such as piezo speakers, servos, transistors, potentiometers – I’ve never really had a chance to play with this sort of thing before, and it’s scary but also exciting.  Not sure yet what I’ll actually be able to achieve, but I’m expecting to at least learn what the different parts do  and the potential of the system, and I have a husband who’s interested in modelmaking and robotics who I might be able to enlist in helping with bigger projects at some point. I’m determined this will be something I work out for myself, though.


The arduino itself

This is the actual arduino – small and neat.  I guess I’m going to have to start doing some reading up on this, looking for ideas for simple projects.  I’ve seen one interesting idea, hooking it up to a wii nunchuck, that doesn’t look as if it would be too massively difficult.

So the next stage will be to actually unpack it and see if I can get things going.  I know computers are capable of absolutely amazing things – if I can set this up and manage to turn lights and motors on and off with it, it will be a good start.