Basic Stamp 2 Fails to Follow Code
hiptechboy
Posts: 9
Hi, I believe I have the original Basic Stamp 2 microcontroller (there are no revisions on top of it) connected to a ~8V wall power supply. All I have connected are two parallax futaba servos which get ~5v from a 7805 voltage regulator. My problem is that when I run the attached code, it only follows the initial "For...Next" command and does so in a continuous loop without even executing the other "For...Next" commands. I confirmed this by using the debug terminal and after it got to 120, it would just reset to 1 and go up to 120 over and over again.
Other than that, I have no issues with the microcontroller. Running the blocks of "For...Next" commands individually works, it's just when I combine them that all but one of the blocks of code gets ignored. Unfortunately because of this, I won't be able to make my custom-made robot move in different directions with respect to the switches that I'm using as whiskers.
*The code didn't upload so here it is:
' {$STAMP BS2}
' {$PBASIC 2.5}
DEBUG "Program Running!"
counter VAR Word
FOR counter = 1 TO 120 ' Loop for three seconds
PULSOUT 7, 850 ' P13 servo counterclockwise
PULSOUT 6, 850 ' P12 servo counterclockwise
PAUSE 20
NEXT
FOR counter = 1 TO 124 ' Loop for three seconds
PULSOUT 7, 650 ' P13 servo clockwise
PULSOUT 6, 650 ' P12 servo clockwise
PAUSE 20
NEXT
FOR counter = 1 TO 122 ' Loop for three seconds
PULSOUT 7, 650 ' P13 servo clockwise
PULSOUT 6, 850 ' P12 servo counterclockwise
PAUSE 20
NEXT
FOR counter = 1 TO 122 ' Loop for three seconds
PULSOUT 7, 850 ' P13 servo counterclockwise
PULSOUT 6, 650 ' P12 servo clockwise
PAUSE 20
NEXT
END
Other than that, I have no issues with the microcontroller. Running the blocks of "For...Next" commands individually works, it's just when I combine them that all but one of the blocks of code gets ignored. Unfortunately because of this, I won't be able to make my custom-made robot move in different directions with respect to the switches that I'm using as whiskers.
*The code didn't upload so here it is:
' {$STAMP BS2}
' {$PBASIC 2.5}
DEBUG "Program Running!"
counter VAR Word
FOR counter = 1 TO 120 ' Loop for three seconds
PULSOUT 7, 850 ' P13 servo counterclockwise
PULSOUT 6, 850 ' P12 servo counterclockwise
PAUSE 20
NEXT
FOR counter = 1 TO 124 ' Loop for three seconds
PULSOUT 7, 650 ' P13 servo clockwise
PULSOUT 6, 650 ' P12 servo clockwise
PAUSE 20
NEXT
FOR counter = 1 TO 122 ' Loop for three seconds
PULSOUT 7, 650 ' P13 servo clockwise
PULSOUT 6, 850 ' P12 servo counterclockwise
PAUSE 20
NEXT
FOR counter = 1 TO 122 ' Loop for three seconds
PULSOUT 7, 850 ' P13 servo counterclockwise
PULSOUT 6, 650 ' P12 servo clockwise
PAUSE 20
NEXT
END
Comments
I tried your code and all that seemed to happen were pulses being sent in 1.5-2s intervals.
I think Mike Green has your problem nailed. Servos draw lots of current when they start to move. Your power supply isn't providing enough current which makes the voltage drop and the Stamp reset.
q2: Does "program running" appear multiple times in your debug screen?
q3: Did you try running it with one or both servos disconnected?
He's using a wall-wart.
hiptechboy, sometimes an extra capacitor will keep a uC from resetting when on the same power supply as a servo.
If I use a 1000uF with my QuickStart board, I can drive two micro servos using USB power. Without the extra cap, the servos cause a reset.
The failsafe solution to servo reset problems is to use separate power supplies for the servos and the uC. If you use separate power supplies, make sure the two power supplies share a common ground connection.
As for idea number 3, I'm not sure of how I would go about diagnosing that, but I would assume that noise would also be produced when running the blocks of code individually too (which results in no issues)?
q1: Variable "wall wart" @~9v. 9V battery has the same issue though.
q2: Yes program running appears multiple times whether regardless of the number of servos, except when I disconnect both the servos, the program running only displays once. I assume that this means the stamp is resetting itself.
q3: I tried running just one, and the same issue still occurred.
Alternatively, replace your wall wart with a big power supply or 6V gel cell and retest. I wouldn't run servos through a 7805, try running everything at 6V, let the Stamp regulate itself (use Vin) and couple your servos directly to the 6V power supply.
Edit: @erco (or anyone else), in terms of portability (e.g for use as the brain of an autonomous RC car), what would be the best solution for the servos? I do have a 9.6V NiCd rechargeable battery rated at 600mAh, but I'm not certain if that's enough, plus I would have to regulate the voltage using the 7805 (which you advised against).
Also, by big power supply, you mean one with a larger current correct?
P.S: Thanks to those who posted by the way.
Unfortunately, the wall-wart puts out a little too much voltage for the servos (max. about 7.2V). It's ok for a quick test, but the higher voltage causes arcing on the servo motors' brushes and shortens their lifetime. You could use another, lower voltage, wall-wart or put a power diode (rated for 3A) in series from the positive lead. That'll drop the voltage by about 0.7V which is much better for the servos. You might put a 500uF or 1000uF 10V electrolytic capacitor across the servo power leads. You could then attach the Stamp's Vin pin to the servo positive power lead and the Vss pin to the negative power lead. The Stamp's own regulator should provide some filtering.
The reason why I'm confused is that the voltage to the BS2 never dropped below 5V when running both servos at full speed for more than 5min. The current draw wasn't even half an amp, so the wall wart's 1.3A should have been plenty right? I feel as if I am missing something. Thanks for the time you all have spent answering my questions so far.
Also, what would I need to do in order to use a rechargeable 9V or RC 9.6V battery, instead of the AAs. I'm just trying to reduce my costs and make the entire car as light as possible when I assemble it together.
I don't really believe the 170mA combined current draw. That might be an average or they might be really small servo motors. Peak currents can certainly be 1A each. Most current meters measure an average current over some period of time, maybe a large fraction of a second, not the peak current. The large filter capacitor we've been suggesting is intended to provide large amounts of current for short periods of time. Capacitors can also filter out very short (noise) surges.
The wall-wart supply may be able to supply 1.3A, but whether that's an average or a peak current, it doesn't say.
I've found the problem with servos happen right when they start to move, not at full speed.
The current spike/voltage drop is very short. It's unlikely you'd see it on a multimeter.
When your servos were moving at full speed, what program was running in the BS2? Was the program one where you'd notice if it reset?
When choosing a battery, keep in mind higher capacity batteries usually can supply more current. You don't want a small recharable 9V (smoke alarm size) battery. AA NiMH are often a good option. LiPo batteries are even better but they require a special charger. A 9.6V RC battery would probably be good depending on its capacity (mAh).
I also like DE's voltage regulators. One thing to watch out for when using a switching regulator is radio interference.
The DE regulators I have, interfere with GPS signals and RC signals (using a Spektrum 2.4GHz radio). There are supposed to be ways to shield the switching regulators (so says Mike) but I haven't figured out how to do it yet (though I'm not done trying).
I was thinking about that too and now that I think about it, I wouldn't have noticed. So I decided to test the servos using the block of code listed below which would allow me to see the if the BS2 did reset. The result was an initial jitter and the "program running" message displaying 5x instead of once. When using a 9v battery for the BS2 and my AC adapter (regulated down to ~9.7V) through the 7805, I had no issues.
The 9.6V battery I'm planning to use is rated at 600mAh and I haven't tried it yet, so I'm not sure if it will work.
' {$STAMP BS2}'
{$PBASIC 2.5}
DEBUG "Program Running!"
counter VAR Word
FOR counter = 1 TO 600
PULSOUT 7, 850
PULSOUT 6, 850
PAUSE 20
NEXT
END
Hopefully I'm applying your instructions to my circuit correctly.
I think you have reason to be unsure of your battery pack. 600mAh is a bit on the low side for what you want to do.
erco posted a link to some very inexpensive LiMH. The batteries are actually closer to 900mAh in capacity than the claimed 3,000mAh but a set of these would probably work better than your planned 600mAh pack.
If you have the budget for it, you might want to consider LiPo batteries. LiPos have the best energy density of any of the currently sold battery chemistries. I've purchased many LiPo packs from HobbyKing (they have lots of inexpensive stuff). If/when you use LiPos, you'll need a special charger to charge them.
Older 7.2V RC batteries are cheap and plentiful, mainly NiCad. Larger size (sub-C) and slightly higher capacity than AAs if you have room for it. Those will need some voltage drop/regulation.