October 05, 2005
Servo Motors and PWMing of LEDs
This week's lab: Using a servo motor (and doing analog out).
I had a few spare moments in the lab, so I created an entirely new (but basically same-as-before) breadboard setup with the 10K resistor on the PIC's reset (upper left pin), power to the board and to the PIC, and the LED indicator light on the bottom. I figure it will be nice to be able to have two projects going at the same time, instead of having to pull all the attached wires each time my project changes. Plus, I think I'm a better solderer now, and I have hindsight on my side, so I was able to make a slightly cleaner board (the real improvement over the last one is in my serial connector. I did a much better job of soldering the header pins and laying it out this time).
The most difficult part of this lab was finding the servo motor. I trekked all around Chelsea trying to find a store that would sell it to me (there was talk about some store on 22nd street but I never found). I eventually had to go up to 30th street by 7th Av, a place called American Hobby Center, or something like that.
So what is a servo motor? Seattle Robotics has a good beginner's guide to the servo. Basically, it's a small motor that has a roughly 180-degree range of motion, and the shaft of the motor moves according to the time that a voltage is pulsed to it. It is connected to +5V and ground, and there is a control wire as well (this is the input to tell the servo how far to turn).
Luckily, there is a command in PicBasic Pro, PULSOUT that we can use to specify a length of time to send a signal to the motor. This is how we can specify how far the motor should turn. Sending a short pulse will cause the motor to reset back to its starting state.
One other neat thing I found out about from the Seattle Robotics link above is that servos move at a speed proportional to how far they have to go. This makes sense considering what I observed while using the little motor, but I didn't realize it before.
So what did I build? I came up with two ways to use the motor: manual and automatic. In manual mode, twisting a potentiometer varies the analog input to the PIC, which then translates this into a length of time to pulse the motor, with the result being that you twist the pot, and the motor twists along with you.
In automatic mode, the motor rotates until it hits a trip-switch that I built. When the switch is tripped, it resets and starts over. I think there's something really fascinating about watching the motor twist until it hits the wire and then abruptly resetting once it does. You can watch the movie below to see for yourself.
This first image is a close-up of the motor with the trip-switch. The trip-switch is just the yellow and white wires wrapped around each other. When the motor pushes the first wire, it touches the second and closes the switch.

This second picture is an overview of the entire board. It shows the switch in the upper right corner of the board and the yellow and red leds in the middle on the right side of the board. The yellow and red LEDs act as indicators of which state (automatic or manual) the board is operating in.

The LEDs also dim and glow according to the state of the stepper motor. This is done using PWM, or pulse-width modulation. The LEDs are PWM'd a certain length of time depending on the position of the motor's gear. Pulse-width modulation is when the PIC outputs a square wave voltage; the proportion of time the wave is HIGH and LOW (or ON and OFF) creates an average voltage. If the modulation is fast enough, the eye cannot see the individual on-off changes and perceives the LED as being dim (or bright). I was never able to keep the LEDs from flickering a bit. It was a trade-off between faster response of the stepper motor and smoother lighting of the LEDs.
Posted October 5, 2005 12:56 AM. Categories: Labs , Week 4 | Permalink
