Thursday, January 24, 2008

Success! Servos and Motors

It was a brutal fight but I coerced Sparky under the control of his little Basic Stamp 2 brain. As mentioned previously, Sparky's little DC motor is controlled by an IRF510A MOSFET which in turn is controlled by the BS2 which in turn kept resetting right after powering up the motor. As for the 3rd wheel, the HiTec HS303 I had on hand was broken. Or so I thought. I couldn't get it to center properly with the sample code from the Basic Stamp Manual.

Basic Stamp Servo Control
So that you don't struggle with this yourself, here's how to properly control a servo with a Basic Stamp 2. Timing is everything. Servos require pulses separated by 20ms. The pulse length determines the position of the servo.

A 1.5ms pulse should approximately center the servo. To get the right pulsetrain you have to know a bit more about how your Basic Stamp (or other MCU) works. I just copied source code out of the manual. Big mistake. The manual's source was for a slower Basic Stamp 1 so the timing was off by a factor of 5! You want to use the PULSOUT command but on the BS2, each value lasts 2us instead of 10us on the BS1. So here's the source:

PULSOUT 0, 750 ' The servo signal line is sitting on pin 0

Once I figured this out, the HS303 worked beautifully. So did the brand new servo I had just bought thinking the other was broken. Oh well, I will put the new servo to good use. It's a HiTec HS311 and a cheapie at $11.

Basic Stamp Motor Control
With one problem slain it was onto the motor control issue. DPRG had a thread going about driving inductive loads and MCU resets due to brown out detection kicking in when the motor, relay, or other inductive load was shut off, creating a voltage spike as the magnetic field in the coil collapsed. This voltage spike is seen by the MCU as a raised voltage on the ground line which reduces the effective voltage seen by the brown out circuit on the chip causing the reset.

That wasn't what was happening to me. The MCU was resetting when turning the motor on. I am still trying to figure out what was happening but I did find that putting a 100uF capacitor across the Vdd / Vss (regulated 5V) pins on the BS2 solved the problem (temporarily, I later found out). The capacitor decouples the chip from the batteries so if the ground line rises in voltage the capacitor and chip kind of ride the surge. So I understand. I'll update this if I get more insight.

So, with these two problems solved, Sparky is able to control motors and servo and is very, very close to autonomy. Just a question of tying down batteries and boards and he'll go for his first test run and then I can figure out what powertrain to use for the real firefighter.


  1. be interested to see this concluded and also to know why your capacitor only "temporarily" solved your brownout issue.

  2. Thanks for your note!

    Me too. It may be that the batteries were running low on top of other issues.

    I think I'm running into a related problem with a project I'm soon to post about. Namely, interfacing a BS2 to an old SP0256-AL2 speech synthesis chip.

    The problem appears when an 8-ohm speaker attached and it goes away either with the speaker detached or with the speaker and the addition of a 100uF power decoupling cap.

    Since this experiment isn't mobile :) I will throw it on the scope and see if I can't figure out what is going on.

    Also, I've moved the BS2 over to a new proto board and once I rebuild Sparky I can do some more testing.

    Finally: I'm running the BS2 off of a 9V battery and I think all these issues may be ultimately due to low current capacity.


Note: Only a member of this blog may post a comment.