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.

 

Advertisements