Shop OBEX P1 Docs P2 Docs Learn Events
Basic Stamp 2 Fails to Follow Code — Parallax Forums

Basic Stamp 2 Fails to Follow Code

hiptechboyhiptechboy Posts: 9
edited 2012-05-29 09:14 in BASIC Stamp
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

Comments

  • PJAllenPJAllen Banned Posts: 5,065
    edited 2012-05-28 07:02
    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    
    counter VAR Word
    
    DEBUG "Program Running!", CR
    PAUSE 1500
    DEBUG CLS
    
    R_1:
    DEBUG "R_1", CR
     FOR counter = 1 TO 120 ' Loop for three seconds
      PULSOUT 7, 850 ' P13 servo counterclockwise
      PULSOUT 6, 850 ' P12 servo counterclockwise
      PAUSE 20
      NEXT
    
    R_2:
    DEBUG "R_2", CR
     FOR counter = 1 TO 124 ' Loop for three seconds
      PULSOUT 7, 650 ' P13 servo clockwise
      PULSOUT 6, 650 ' P12 servo clockwise
      PAUSE 20
      NEXT
    
    R_3:
    DEBUG "R_3", CR
     FOR counter = 1 TO 122 ' Loop for three seconds
      PULSOUT 7, 650 ' P13 servo clockwise
      PULSOUT 6, 850 ' P12 servo counterclockwise
      PAUSE 20
      NEXT
    
    R_4:
    DEBUG "R_4", CR
     FOR counter = 1 TO 122 ' Loop for three seconds
      PULSOUT 7, 850 ' P13 servo counterclockwise
      PULSOUT 6, 650 ' P12 servo clockwise
      PAUSE 20
      NEXT
    
    DEBUG "Re-cycling..."
    PAUSE 1500
    DEBUG CLS
    GOTO R_1
    
  • Mike GreenMike Green Posts: 23,101
    edited 2012-05-28 07:54
    Microcontrollers don't "fail to follow code". They simply don't do that. Either: 1) The code that you downloaded to your Stamp is different from what you posted and that difference is where the problem is. 2) There's a problem with your power supply and the supply voltage starts to drop after a while and eventually the Stamp resets which allows the supply voltage to rise for a little while as the servos stop drawing power for a short time. The 7805 is rated for about 1A and two servos can draw more than that. The 7805 can overheat too from the 3V x >1A current drawn and that will cause it to shut down. 3) Noise from the servo motors feeds back into the logic circuitry and eventually causes the Stamp to reset somewhat randomly.
  • hiptechboyhiptechboy Posts: 9
    edited 2012-05-28 08:20
    @PJ Allen

    I tried your code and all that seemed to happen were pulses being sent in 1.5-2s intervals.
  • Duane DegnDuane Degn Posts: 10,588
    edited 2012-05-28 08:29
    PJ's code is your code with a few debug statements to let you know which part of the code is being executed.

    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.
  • ercoerco Posts: 20,257
    edited 2012-05-28 08:33
    q1: Are you running everything off of a single 9V battery?

    q2: Does "program running" appear multiple times in your debug screen?

    q3: Did you try running it with one or both servos disconnected?
  • Duane DegnDuane Degn Posts: 10,588
    edited 2012-05-28 08:51
    erco wrote: »
    q1: Are you running everything off of a single 9V battery?

    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.
  • Mike GreenMike Green Posts: 23,101
    edited 2012-05-28 08:59
    Servos can draw on the order of 1A when they're starting up. Two servos means your power supply has to deliver in excess of 2A for brief periods of time. Most "wall warts" can't do that ... their output voltage will sag. The 7805 can only supply around 1A. It will shut itself down to protect itself at currents much over 1A. It will also shut itself down if it overheats. If your "wall wart" is really putting out 8V and there's a 1A load on the regulator, that's (8V - 5V) x 1A = 3W of heat that it has to dissipate. I assume you have no significant heatsink on the 7805. That means that it will overheat very quickly even with a 1A load and shutdown until it cools off. In any event, the Stamp will run for a while, then restart after a short pause, then repeat.
  • hiptechboyhiptechboy Posts: 9
    edited 2012-05-28 09:00
    1) The code is the same. 2) I can run both servos at the same time at full speed continuously for more than 5mins without a problem, so I doubt it's a voltage issue. I've also monitored the voltage into the stamp while they were running and it was ~6.3-6.7V. The current from the power supply is 1.3A @~9V, while the current draw from ONE servo connected to the 7805 is 70-75mA. The voltage regulator is warm, but not hot after 10min.

    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)?
  • hiptechboyhiptechboy Posts: 9
    edited 2012-05-28 09:16
    erco wrote: »
    q1: Are you running everything off of a single 9V battery?

    q2: Does "program running" appear multiple times in your debug screen?

    q3: Did you try running it with one or both servos disconnected?

    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.
  • ercoerco Posts: 20,257
    edited 2012-05-28 09:35
    Power/reset issue for sure. Per Mike & Duane, try seperate power supplies for Stamp & servos. Common ground connection.

    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.
  • hiptechboyhiptechboy Posts: 9
    edited 2012-05-28 09:41
    I tried your fail-safe solution with the "wall wart" as the power supply for the servos, and a 9V battery connected to the BS2's Vin port. Does this mean that, as Mike said, there was not enough current for both the BS2 and servos or...?

    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.
  • Mike GreenMike Green Posts: 23,101
    edited 2012-05-28 09:46
    You start with the assumption that the Stamp is doing its thing and something else is causing the problem. When microcontrollers fail, they don't behave anything at all like what you're observing. They just stop working and you can't download new programs to it. Sometimes I/O pins get burnt out, but that's permanent as well. It always helps to post a diagram of your setup. It's rarely enough to describe it since you tend to leave out details and the details are usually where the problem is. Because you have a voltage and noise sensitive device (the Stamp) trying to coexist with a power hungry and noisy device (a motor), you have a setup for problems. Get a pair of 4-cell AA battery holders and a package of 8 AA alkaline cells. Connect one to your servo's positive supply leads (red) and the common ground buss (black). Connect the other to the Stamp's Vin pin (red) and the Stamp's Vss pin (black). Very important ... connect all the black leads together as well (Stamp Vss and its battery's black lead to the servos' black lead and its battery's black lead.

    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.
  • hiptechboyhiptechboy Posts: 9
    edited 2012-05-28 11:55
    Ok, I'm starting to understand the issue. It wasn't an issue with the 7805's maximum current output because the two servos combined drew a maximum of 170mA combined, correct? This means that it was a noise problem, which is fixed by the capacitor acting as a filter, or using separate power supplies?

    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.
  • Mike GreenMike Green Posts: 23,101
    edited 2012-05-28 12:14
    If you want to use a 9-10V rechargable battery, you will need some kind of switching regulator to produce 6-7V for the servos and the Stamp's own regulator. You could use a 7806 or 7807, but a large portion of your energy from the battery will go up in heat dissipated by the regulator. Dimension Engineering makes a nice 3A adjustable regulator. You can also make your own, but it's not as simple as a linear regulator.

    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.
  • Duane DegnDuane Degn Posts: 10,588
    edited 2012-05-28 12:25
    hiptechboy wrote: »
    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.

    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).
  • Duane DegnDuane Degn Posts: 10,588
    edited 2012-05-28 12:31
    I hadn't seen Mike's last post when I made my previous post. It's always good when my answers match his (they should since a lot of what I know is from reading Mike's forum posts).

    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).
  • Mike GreenMike Green Posts: 23,101
    edited 2012-05-28 12:40
    The switching regulators have their own capacitors, but it's always better to add more. At a minimum, I'd put a 0.1uF capacitor across the input and the output of the regulator (to ground), maybe a 10uF 35V electrolytic across the input as well and a 100uF 10V across the output. All capacitors should be mounted as closely as possible to the regulator's pins. I'd put the regulator and its capacitors inside a little metal box that's connected to ground. You could use aluminum foil or copper foil around a little plastic box if you want. Scraps of printed circuit board also work, soldered around the edges.
  • hiptechboyhiptechboy Posts: 9
    edited 2012-05-28 22:23
    Duane Degn wrote: »

    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?

    A 9.6V RC battery would probably be good depending on its capacity (mAh).

    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
  • hiptechboyhiptechboy Posts: 9
    edited 2012-05-28 23:17
    For testing, I'll probably use separate power supplies (9V to stamp and AC adapter to servos). As for right now, anytime I'm not testing, I'll try the 9.6V RC car battery connected to a 7806 and if the current isn't enough then I'll use one of the capacitors you suggested (do they still qualify for the 9.6V battery?) between the output of the regulator and the servos/BS2 Vin.

    Hopefully I'm applying your instructions to my circuit correctly.
  • Duane DegnDuane Degn Posts: 10,588
    edited 2012-05-29 00:13
    hiptechboy wrote: »
    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.

    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.
  • hiptechboyhiptechboy Posts: 9
    edited 2012-05-29 07:57
    I'll check Fry's Electronics today and see if they have any batteries with higher capacities. The reason why I'm trying to use a single battery pack is because the base for the car will be pretty small, so I won't have that much space to put 8 AA cells on it. Does batteries do seem like a good deal though.
  • ercoerco Posts: 20,257
    edited 2012-05-29 09:14
    Individual AA NiMH cells are a pain to charge individually, but they have the advantage that you can adjust the number of cells to make 4.8 or 6V, which wouldn't require a regulator at all. You could probably power Stamp & servos directly off of 4 cells (4.8-5V) or run 5 cells (servos directly to 6V and Stamp through two series diodes to drop the voltage.

    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.
Sign In or Register to comment.