Unique Servo Control Requirement

Hi,

Back to post #1, Rob made it clear that he is basically a modeller, wanting a "Simple" solution (similar to that shown in a YouTube video) and had resorted to asking "AI" to write a program. It would be interesting to know how that "conversation" might have proceeded because the AI hadn't even "realised" that a Servo moves much too fast for the desired effect, and needs to be slowed down somehow. Then the "usual" problem is that the Servo movement can be "jerky" or erratic, but here the problem reported by Rob is that his Servo "twitches" when it is NOT supposed to be moving. I must admit that my attention has been in trying to make the SERVO(POS) instruction operate "correctly", but this is actually a rather "unusual" application of Servos and thus may need a non-standard solution:

Normally, Servo operation is a "background" task whilst the program is doing "something else", but here the main task is simply to drive the Servo motors in a specific way for a specific (and quite short) time. Thus, as described in a few other threads (e.g. using "Twitch" as a forum search term ;) ), the instruction that we probably should be using is PULSOUT, which should be (almost) perfectly "clean", and simply stops when the program doesn't actively transmit it.

The program from post #7 hardly needs any changes; the only complication is that Rob decided/confirmed that a step rate of 30 ms is ideal, whilst the Servos update every 20 ms, so I needed to complicate the program slightly. However, adding the second Servo actually makes the timing simpler, and I've also added storage for the Last Position, to be stored over a power-down.

Code:
#picaxe 08m2
#no_data                       ; Avoid Overwriting the last stored position in EEPROM
; PICAXE 08M2 Momentary Switch Servo Control     AllyCat October 2025

; #DEFINE ACTIVELOW            ; Uncomment for Alternative pushbutton operation (button to Ground)
; Control/Variables :
  symbol PushButton = pinC.3              ; Press to toggle Servo to move to opposite Endstop
  symbol Servo_Pin = C.4                  ; Control signal to the servo 1
  symbol Servo_Pin2 = C.1                 ; Control signal to the servo 2
  symbol Servo_Pos = b1
  symbol Servo_Pos2 = b2
  symbol HRPos = w2                       ; High Resolution Position to permit step rate adjustment
; Constants depending on the Hardware configuration/requirements :
  symbol LIMIT_CCW = 105                  ; Endstop for Counter-Clockwise rotation (adjust as required)
  symbol LIMIT_CW = 195                   ; Endstop for Clockwise rotation (adjust as required)
  symbol TRIM_S2 = 0                      ; Trim Servo 2 position relative to Servo 1 (optional)
  symbol LOOP_TRIM = 1100                 ; Pause (* 10us) to extend pulse/loop Period to 20 ms
  symbol SWEEP_RATE = 33                  ; Steps per second (adjust as required)
  symbol LAST_POS = 0                     ; Address in EEPROM
; Calculate threshold which determines whether to move CW or CCW :
  symbol MIDDLE_I = LIMIT_CCW + LIMIT_CW  ; Sum the two Servo positions
  symbol MIDDLE = MIDDLE_I / 2            ; Average (Middle) Servo position
  symbol MIDDLEx2 = MIDDLE_I + TRIM_S2    ; To calculate Servo 2 position

#IFDEF ACTIVELOW                          ; Pushbutton connected to Ground (0v)
  symbol PRESSED = 0                      ; Logic value when button is pressed
  Pullup %001000                          ; Activate Pushbutton PullUp resistor
#ELSE                                     ; Pushbutton connected to Supply (Vdd)
  symbol PRESSED = 1                      ; Logic value when button is pressed  [Use only this line if button is connected to supply rail]
#ENDIF

init:
  Low Servo_Pin , Servo_Pin2
  Pause 1000                              ; Allow time for system to stabilise (probably unnecessary)
  Read LAST_POS , Servo_Pos
  If Servo_Pos < 50 AND Servo_Pos <> LIMIT_CCW then    ; Initialise starting position if necessary
     Write LAST_POS , LIMIT_CCW
  Endif
  Read LAST_POS , Servo_Pos
  Servo_Pos2 = MIDDLEx2 - Servo_Pos
  PULSOUT Servo_Pin , Servo_Pos           ; Starting position (assumed fully CCW)
  PULSOUT Servo_Pin2 , Servo_Pos2         ; Starting position (assumed fully CW)
  HRPos = Servo_Pos * 50                  ; To permit partial or multiple steps between Pulses
main:
  Do : Loop Until PushButton = PRESSED    ; Wait for button to be pressed
  If Servo_Pos <= MIDDLE then             ; Position should be at LIMIT_CCW, so can step immediately
     Do
        HRPos = HRPos + SWEEP_RATE        ; Move Clockwise                 [~1ms approx execution time]
        Servo_Pos = HRPos / 50            ; Incremented when appropriate     [~1ms]
        Servo_Pos2 = MIDDLEx2 - Servo_Pos ; Complementary position         [~1ms]  
        PULSOUT Servo_Pin , Servo_Pos     ; )
        PULSOUT Servo_Pin2 , Servo_Pos2   ; } Two complementary pulse widths [~4.5ms]
        Pauseus LOOP_TRIM                 ; Trim Total loop to ~20ms       [~11.5 ms]
     Loop Until Servo_Pos => LIMIT_CW     ; Close loop                     [~1ms]
  Else                                    ; Position should be at LIMIT_CW, so can step immediately
     Do
        HRPos = HRPos - SWEEP_RATE        ; Move Counter-Clockwise
        Servo_Pos = HRPos / 50            ; Decremented when appropriate
        Servo_Pos2 = MIDDLEx2 - Servo_Pos   
        PULSOUT Servo_Pin , Servo_Pos
        PULSOUT Servo_Pin2 , Servo_Pos2 
        Pauseus LOOP_TRIM
     Loop Until Servo_Pos <= LIMIT_CCW
  Endif
  Do : Loop Until PushButton <> PRESSED    ; Ensure button has been released
  Write Last_Pos , Servo_Pos
Goto main        ; Or a purist would close a DO : LOOP here

Cheers, Alan.
 
Last edited:
Rob, I suggest you go back to the program you were using in post #30, where it was working with no twitching.

I'm already using that code. It is the code Alan posted in post #7, with the modifications Alan posted in #25 and #29. It is the code in my last post. The servo kicks about 20 degrees to the left when I turn on the power. It will do a full 90 degree movement CCW with 4x off/on cycles.

Would it be easier to use stepper motors? I bought some at Amazon. They are pretty small. I also got 5X TMC2209 drivers. I also have 4x L289Ns
Hi,

Back to post #1, Rob made it clear that he is basically a modeller, wanting a "Simple" solution (similar to that shown in a YouTube video) and had resorted to asking "AI" to write a program. It would be interesting to know how that "conversation" might have proceeded because the AI hadn't even "realised" that a Servo moves much too fast for the desired effect, and needs to be slowed down somehow. Then the "usual" problem is that the Servo movement can be "jerky" or erratic, but here the problem reported by Rob is that his Servo "twitches" when it is NOT supposed to be moving. I must admit that my attention has been in trying to make the SERVO(POS) instruction operate "correctly", but this is actually a rather "unusual" application of Servos and thus may need a non-standard solution:

Normally, Servo operation is a "background" task whilst the program is doing "something else", but here the main task is simply to drive the Servo motors in a specific way for a specific (and quite short) time. Thus, as described in a few other threads (e.g. using "Twitch" as a forum search term ;) ), the instruction that we probably should be using is PULSOUT, which should be (almost) perfectly "clean", and simply stops when the program doesn't actively transmit it.

The program from post #7 hardly needs any changes; the only complication is that Rob decided/confirmed that a step rate of 30 ms is ideal, whilst the Servos update every 20 ms, so I needed to complicate the program slightly. However, adding the second Servo actually makes the timing simpler, and I've also added storage for the Last Position, to be stored over a power-down.

Code:
#picaxe 08m2
#no_data                       ; Avoid Overwriting the last stored position in EEPROM
; PICAXE 08M2 Momentary Switch Servo Control     AllyCat October 2025

; #DEFINE ACTIVELOW            ; Uncomment for Alternative pushbutton operation (button to Ground)
; Control/Variables :
  symbol PushButton = pinC.3              ; Press to toggle Servo to move to opposite Endstop
  symbol Servo_Pin = C.4                  ; Control signal to the servo 1
  symbol Servo_Pin2 = C.1                 ; Control signal to the servo 2
  symbol Servo_Pos = b1
  symbol Servo_Pos2 = b2
  symbol HRPos = w2                       ; High Resolution Position to permit step rate adjustment
; Constants depending on the Hardware configuration/requirements :
  symbol LIMIT_CCW = 105                  ; Endstop for Counter-Clockwise rotation (adjust as required)
  symbol LIMIT_CW = 195                   ; Endstop for Clockwise rotation (adjust as required)
  symbol TRIM_S2 = 0                      ; Trim Servo 2 position relative to Servo 1 (optional)
  symbol LOOP_TRIM = 1100                 ; Pause (* 10us) to extend pulse/loop Period to 20 ms
  symbol SWEEP_RATE = 33                  ; Steps per second (adjust as required)
  symbol LAST_POS = 0                     ; Address in EEPROM
; Calculate threshold which determines whether to move CW or CCW :
  symbol MIDDLE_I = LIMIT_CCW + LIMIT_CW  ; Sum the two Servo positions
  symbol MIDDLE = MIDDLE_I / 2            ; Average (Middle) Servo position
  symbol MIDDLEx2 = MIDDLE_I + TRIM_S2    ; To calculate Servo 2 position

#IFDEF ACTIVELOW                          ; Pushbutton connected to Ground (0v)
  symbol PRESSED = 0                      ; Logic value when button is pressed
  Pullup %001000                          ; Activate Pushbutton PullUp resistor
#ELSE                                     ; Pushbutton connected to Supply (Vdd)
  symbol PRESSED = 1                      ; Logic value when button is pressed  [Use only this line if button is connected to supply rail]
#ENDIF

init:
  Low Servo_Pin , Servo_Pin2
  Pause 1000                              ; Allow time for system to stabilise (probably unnecessary)
  Read LAST_POS , Servo_Pos
  If Servo_Pos < 50 AND Servo_Pos <> LIMIT_CCW then    ; Initialise starting position if necessary
     Write LAST_POS , LIMIT_CCW
  Endif
  Read LAST_POS , Servo_Pos
  Servo_Pos2 = MIDDLEx2 - Servo_Pos
  PULSOUT Servo_Pin , Servo_Pos           ; Starting position (assumed fully CCW)
  PULSOUT Servo_Pin2 , Servo_Pos2         ; Starting position (assumed fully CW)
  HRPos = Servo_Pos * 50                  ; To permit partial or multiple steps between Pulses
main:
  Do : Loop Until PushButton = PRESSED    ; Wait for button to be pressed
  If Servo_Pos <= MIDDLE then             ; Position should be at LIMIT_CCW, so can step immediately
     Do
        HRPos = HRPos + SWEEP_RATE        ; Move Clockwise                 [~1ms approx execution time]
        Servo_Pos = HRPos / 50            ; Incremented when appropriate     [~1ms]
        Servo_Pos2 = MIDDLEx2 - Servo_Pos ; Complementary position         [~1ms] 
        PULSOUT Servo_Pin , Servo_Pos     ; )
        PULSOUT Servo_Pin2 , Servo_Pos2   ; } Two complementary pulse widths [~4.5ms]
        Pauseus LOOP_TRIM                 ; Trim Total loop to ~20ms       [~11.5 ms]
     Loop Until Servo_Pos => LIMIT_CW     ; Close loop                     [~1ms]
  Else                                    ; Position should be at LIMIT_CW, so can step immediately
     Do
        HRPos = HRPos - SWEEP_RATE        ; Move Counter-Clockwise
        Servo_Pos = HRPos / 50            ; Decremented when appropriate
        Servo_Pos2 = MIDDLEx2 - Servo_Pos  
        PULSOUT Servo_Pin , Servo_Pos
        PULSOUT Servo_Pin2 , Servo_Pos2
        Pauseus LOOP_TRIM
     Loop Until Servo_Pos <= LIMIT_CCW
  Endif
  Do : Loop Until PushButton <> PRESSED    ; Ensure button has been released
  Write Last_Pos , Servo_Pos
Goto main        ; Or a purist would close a DO : LOOP here

Cheers, Alan.
Goodness Alan, I don't know what to say, except thank you, thank you, thank you. You're awesome man. I wouldn't have ever figured it out how to include that 2nd servo. it works great except for one little glitch when I turn on the power, both servos kick about 15-20 degrees to the left. I made a short video an posted it on Youtube
 
Hi,

As said before, the initial kick occurs before the PICaxe is, or can be, doing anything; Probably before the PICaxe has even set its pins as outputs. Which might be the cause, so it could be worth adding a pull-down resistor of perhaps 10k onto each Servo control pin (yellow wire), to ground. Otherwise, maybe tinker particularly with the setting of the current limit on the Bench PSU (perhaps it's set higher now than previously), or the value of the decoupling capacitor across the Servo(s).

No, it almost certainly wouldn't be "easier" to use stepper motors (and perhaps beyond the capability of an 08M2), but it somewhat depends if they have a suitable built-in gearbox (and the sophistication of the driver/amplifier chip). Did you follow my stepper link in post #12 ? Not particularly for my own suggested components, but Pongo made some interesting comments, although sadly some of the links are now broken.

Cheers, Alan.
 
maybe tinker particularly with the setting of the current limit on the Bench PSU (perhaps it's set higher now than previously), or the value of the decoupling capacitor across the Servo(s).
I was using the battery pack in that Youtube video.

I bought some other servos from amazon, and they don't do anything when the power is cycled off/on. They work perfectly. Here is the Datasheet for them. Unfortunately they are too large to use them to work the pylons in my Voyager model. All I have that will work to operate those pylons is the HS-40 servos or the small stepper motors. And yes, I did check out your thread. Apparently you were looking for something small to drive your small stepper motors. I am not concerned about the size of the driver. I will use a "project box" as the base for the model to mount on. All of the circuitry will be inside that box.

***EDIT***

I installed the 10K resistors, no change. I removed the 10uF caps and replaced them with 100uF caps, still, no change. 1000uF is as high as my cap-set goes. I have 100, 220, 330, 470, 680 and 1000uF,
 
Last edited:
Hi,

Looking back over the thread, it appears that the "starting kicks" appeared shortly after changing to a battery pack and/or investigating the flashing of the "current limit" LED on the PSU. So the "Servo-Kick" is probably due to the speed that the supply voltage is being applied to the Servos. Therefore, go back to using the PSU for the Servos, but preferably keep (for testing now) the PICaxe on the battery pack; we can "optimise" the PICaxe's supply arrangements later, if necessary. The PSU probably has a moderately large capacitor internally on its output terminals, but because the PSU has a "constant (limiting) current" output control, this may determine how quickly the supply voltage rises.

Many (mains and/or switching mode) "Power Supplies" have a "Soft (slow) Start" voltage output, which is perhaps what your small Servos require. Fortunately, those Servos need quite a low running current so we can probably find quite a "simple" solution, perhaps just a 1,000 uF capacitor fed through a resistor of a few tens of ohms. But I won't go any further until it's confirmed that this is the cause of the problem.

EDIT: Finally, it is "Common Practice" to put a PAUSE at the start of a program to allow the system (e.g. the Terminal Emulator for Debugging) to "stabilise", before executing the required functions (which is why the "AI" included one). But here, particularly if the supply voltage rises relatively slowly, it may be better to remove any initialisation PAUSEs and allow the PICaxe to take control of the Servos As Soon As Possible.
________

The first factor to consider with a Stepper is that the motors themselves usually move in "Steps" of only around 4 - 20 for each revolution of the shaft. So they nearly always need a gearbox or linkage with considerable "mechanical advantage" (speed reduction). Often this is in the form of a "Lead Screw" (originally in Lathes, now 3-D printers. etc.), but a few Stepper Motors do have a built-in gear-train. A very common/low-cost one is intended for the air flaps in Air Conditioning units. Another factor is that it will require some End-Stop detection (e.g. switches) or a linked potentiometer to "calibrate" the shaft position. Personally, I would only recommend a sophisticated "intelligent" driver chip/module (probably one for each Stepper Motor) as suggested by Pongo, certainly NOT L289Ns.

Cheers, Alan.
 
Last edited:
t works great except for one little glitch when I turn on the power, both servos kick about 15-20 degrees to the left. I made a short video an posted it on Youtube

Rob, I'm unable to duplicate the behaviour I see in the video in your post#42:
- Both servos kick to the left immediatelly the power is turned on.
- Nothing happens for about 1s
- Both servos move back to the position they were in before the power was turned on.
- The servos then wait for the button to be pressed.

I can't imagine how the 1s pause followed by the servos moving back to their original position could be caused by the power supply so I have a test to see if we can first identify whether the servos kick to the left as soon as you turn the power on is caused by the 08M2 chip or not.

Disconnect the servo signal wires to the 08M2 chip. i.e. leaving just power and ground connected.
Turn the power off and back on again, as you did in the video you just posted.
Do both servos kick to the left the first time you turn the power on?
Do the servos kick to the left the second and subsequent times you turn the power off and on.
 
Disconnect the servo signal wires to the 08M2 chip. i.e. leaving just power and ground connected.
Turn the power off and back on again, as you did in the video you just posted.
Do both servos kick to the left the first time you turn the power on?
Do the servos kick to the left the second and subsequent times you turn the power off and on.

Yes, and yes, but now they do not return to the staring point. I have the picaxe and the servos on separate power supplies (sharing the same ground). The picaxe on the battery pack and the servos on the BT-PSU. When they were on the same power supply, they would kick left, then return, as you saw in the video.


I asked the google AI about this "glitch", and here is what it said....

"Explanation:
When the PICAXE sets its output pin high, the gate of the corresponding MOSFET receives voltage. This turns the MOSFET on, completing the ground connection for that servo and allowing it to be powered. When the PICAXE sets the pin low, the MOSFET turns off and disconnects the servo from ground, effectively cutting its power. This approach provides clean power to the servos, isolates the PICAXE from noise, and prevents the initial "kick" by keeping them unpowered until your code explicitly turns them on"

I ordered some IRLZ44N N-Channel mosfets from amazon and they will be here tomorrow.

go back to using the PSU for the Servos, but preferably keep (for testing now) the PICaxe on the battery pack; we can "optimise" the PICaxe's supply arrangements later, if necessary.
Yep, way ahead of you :)

it is "Common Practice" to put a PAUSE at the start of a program to allow the system (e.g. the Terminal Emulator for Debugging) to "stabilise", before executing the required functions (which is why the "AI" included one). But here, particularly if the supply voltage rises relatively slowly, it may be better to remove any initialisation PAUSEs and allow the PICaxe to take control of the Servos As Soon As Possible.

What about using a logic level Mosfet to switch the servos on and off, giving the picaxe time to "boot up" and take control of them, as suggested by the AI? I mean, heck, we've tried everything else. By the way I removed the 100uF Caps and replaced them with 1000uF caps. No joy.

The first factor to consider with a Stepper is that the motors themselves usually move in "Steps" of only around 4 - 20 for each revolution of the shaft. So they nearly always need a gearbox or linkage with considerable "mechanical advantage" (speed reduction). Often this is in the form of a "Lead Screw" (originally in Lathes, now 3-D printers. etc.), but a few Stepper Motors do have a built-in gear-train. A very common/low-cost one is intended for the air flaps in Air Conditioning units. Another factor is that it will require some End-Stop detection (e.g. switches) or a linked potentiometer to "calibrate" the shaft position. Personally, I would only recommend a sophisticated "intelligent" driver chip/module (probably one for each Stepper Motor) as suggested by Pongo, certainly NOT L289Ns.

I also have the TMC2209 drivers, and can get whatever driver that you want to work with.
 
now they do not return to the staring point
Rob, this is the correct behaviour for this test.

We disconnected the servo signal wires from the 08M2 so the servos do not receive the "PULSOUT Servo_Pin , Servo_Pos" and "PULSOUT Servo_Pins , Servo_Pos2" commands which are what return the servos to their starting point.

This test result in your post #47 confirms that the "servos kick to the left" when you turn the power on is nothing to do with the 08M2 chip or the program that is running and there is not much point to spending any time making changes to the program to try and fix this particular issue.

The kick to the left has to be something to do with either the power supply and/or the servo so that's where we need to investigate.
 
From my RC days, most analogue servos twitch on power up.
You could try Digital servos, from memory they don't twitch on power up with no signal.
 
Rob, it occured to me for the first time to do a web search for "servo twitches when power is first turned on" and this is my summary of what I discovered:
- It is a pretty common problem. There are a lot of hits.
- There are many hits for this on the both Arduino and Model Radio Control forums. So it is an issue people strike trying to drive servos in general and not something specific to the PICAXE.
- it sounds like there is an element of luck involved in whether or not someone strikes this problem.

As neiltechspec has just posted, I found comments that it is something that can't be avoided:
- "Having used hobby servos in R/C models for decades I'm of the opinion that there is no way to totally eliminate servo 'twitching' at intial power-up, it's just the nature of basic servo design. Some will twitch more then others but it's totally normal to see them move a little (or a lot depending)."
- "The most common thing with servos is that they twitch when power is applied. Some are
much worse than others. The GWS servos I have are especially bad."
- "one thing that was interesting is that different brand servos twitch in different directions (ccw vs cw)."

One solution reported as working reliably is for the microcontroller to use a transistor to switch the power on to the servo _after_ the microcontroller has executed the command to start sending the servo the pulse signals.
- i..e. the same solution as given to you by Google AI.

I found two other solutions that you could try while you are waiting for the mosfets you ordered to be delivered.
- some people reported that a solution worked for them while others that it did not.

As before the 08M2 chip is disconnected for these tests so that if one works we can be 100% certain that the fix has nothing to do with what the PICAXE chip is doing.

Test #2:
Disconnect the servo signal wires to the 08M2 chip. i.e. leaving just power and ground connected.
Connect a 10k pullup resistor between one servo's signal line and the 5v line.
Turn the power off and back on again, as you did in the video you just posted.
Did the servo with the pullup resistor kick to the left the first time you turn the power on?
Did the servo with the pullup resistor kick to the left the second and subsequent times you turn the power off and on.

Test #3:
Disconnect the servo signal wires to the 08M2 chip. i.e. leaving just power and ground connected.
Connect a 10k pulldown resistor between one servo's signal line and the ground line.
Turn the power off and back on again, as you did in the video you just posted.
Did the servo with the pulldown resistor kick to the left the first time you turn the power on?
Did the servo with the pulldown resistor kick to the left the second and subsequent times you turn the power off and on.
 
Hi,
What about using a logic level Mosfet to switch the servos on and off, giving the picaxe time to "boot up" and take control of them, as suggested by the AI? I mean, heck, we've tried everything else. By the way I removed the 100uF Caps and replaced them with 1000uF caps. No joy.

Did you try using the PSU for the Servos and winding the Current Limit backwards at least until the LED started to flash again? That was the condition when you weren't reporting the initial kick. If so, we need to try to repeat that condition. Why didn't you report the kick until post #36, with "The servo is doing something different now" , didn't you notice it until then, or was the starting Kick not present?

Again, the "AI" was partly correct, but didn't tell the whole story and may have given wrong/bad advice again. :( Because your requirement is a very "unusual" application of a Servo, IMHO the AI is almost certain to fail (because it is combining the host of solutions for the "normal" problems and requirements).

If you (or a FET) disconnect the Earth rail to the Servo, the Control/Pulse pin will remain connected to the PICaxe's Output, so will be still pulled down to Earth. The input (impedance) of the Servo's Control pin is (AFAIK) not defined, but it is common for digital inputs to be "Protected" against Static Electricity by a diode connected to Earth (and sometimes another to the Supply Rail), a situation called "Phantom Powering". In that case, the Servo would try to draw its "Kick" current via its Control pin and the PICaxe's Output pin, which might even damage the PICaxe and/or the Servo. The "better" likelihood is that the Servo will have an internal physical resistance in series with its input which would limit the current safely, but to be sure you should introduce an external resistor (perhaps 1K) between the PICaxe and Servo control pin(s).

However, when the FET does (eventually) switch on, the Servo will still "see" a sudden voltage rise (across its supply rails) with the input at the "Earth" potential, which is the same as you have already tested (unsuccessfully you said), with a pull-down resistor on the control line. This is a fundamental problem with "Low-Side-Switching" of devices with Earth-related control signal inputs; "High-Side Switching" is far preferable. Also, what you (probably) need is for the Supply Rail to rise more slowly (whilst the input signal is "valid" relative to Earth), which requires a P-Channel FET, but this might still be quite difficult to control because the Gate Voltage will be relative to the (Servo's) Supply Rail, not Earth.

The solution that I would eventually have suggested (IF all else fails) is to use a PNP, medium power (bipolar) transistor in series with the Supply rail to the Servos, but it is "Simpler" to investigate the possibilities of using resistors and Capacitors first. Then, IMHO, a bipolar transistor may be better for this purpose because it is natively a current amplifier with a well-defined (and lower) input control voltage than a FET. In principle we can then control the switch-on voltage (and/or current) from the DAC (C.0) or more likely the PWM (C.2) Output of the PICaxe, to produce any desired rate of change.

Cheers, Alan.
 
Did the servo with the pullup resistor kick to the left the first time you turn the power on?

Yes, the CW servo kicks to the left, and continues kicking to the left through 5 off/on cycles, until it has turned a full 90 degrees CCW from the starting point. However, the CCW servo hardly moves at all. If I could get BOTH of them to barely move on power up, then I could live with that. But when they're operating (i.e. Following the picaxe's code) it is a thing of beauty, poetry in motion, Alan's code works perfectly.

Why didn't you report the kick until post #36, with "The servo is doing something different now" , didn't you notice it until then, or was the starting Kick not present?

I reported it as soon as I saw it. If you remember, at first, there was a "jitter" while the servo was at rest. I've forgotten how you guys solved that problem, but IIRC the kick started happening after the "jitter" was corrected. It could be that I didn't notice it right away.


The solution that I would eventually have suggested (IF all else fails) is to use a PNP, medium power (bipolar) transistor in series with the Supply rail to the Servos, but it is "Simpler" to investigate the possibilities of using resistors and Capacitors first. Then, IMHO, a bipolar transistor may be better for this purpose because it is natively a current amplifier with a well-defined (and lower) input control voltage than a FET. In principle we can then control the switch-on voltage (and/or current) from the DAC (C.0) or more likely the PWM (C.2) Output of the PICaxe, to produce any desired rate of change.

So, I gather, you don't think I should use the mosfets. You can't just say "use a pnp transistor in series with the supply rail" and leave it at that. Can you tell me how, specifically, to connect it. I already know the base should connect to the picaxe output via 1K resistor, but I need to know how to connect the emitter and the collector, as I've never used a pnp transistor, although I do have a few that came in a kit. PNP,2N2907, PNP,BC327, PNP,BC337, PNP,A1015, PNP,C1815. Name your poison

***EDIT***
After adding the 1K resistors to picaxe output, now both servos kick the same amount, the CCW servo will go as far as 90 degrees nd then stops, but the CW servo will go as far as 180 degrees then stops. Of course, when I press the button, they both jump back to the starting point, then they carry out their programmed 90 degree sweep.
:)
 
Last edited:
After adding the 1K resistors to picaxe output, now both servos kick the same amount, the CCW servo will go as far as 90 degrees nd then stops, but the CW servo will go as far as 180 degrees then stops. Of course, when I press the button, they both jump back to the starting point, then they carry out their programmed 90 degree sweep.

Rob, you've only given one result. Was this with the 1k resistor connectng the pixaxe output to 5V, to ground or did both act the same way?

You've also said that "when I press the button, they both jump back to the starting point". My two tests #2 & #3 were intended to be done with the servo lead disconnected from the PICAXE chip so that we will know for certain if either the pullup or pulldown resistor fixes the power-on twitch that you reported as always happening when the servo lead is disconnected from the PICAXE.
 
Last edited:
Hi,
[ Post #23 ] : If I turn the current knob up a little, the indicator light stops coming on. ...
[ Post #28 ] : When I first turn on the power, the servo will kick a little to the left (CCW). ... [The first observation of this]
A coincidence, or cause and effect ? Unknown, but it can (and needs to be) checked. ;)

Back in post #6 you explained that "I want you to help me with the best and easiest solution to get these servos operating the way I need them to". However, I can't do that until I know the precise behaviour of the components, and you haven't answered my first question in #51: "Did you try using* the PSU for the Servos and* winding the Current Limit backwards at least until the LED started to flash again?" (and what happened if you turned it even further back)? * EDIT: i.e. To be Specific : Switching the PSU Off and On again after reducing the Current Limit in fairly small increments, until something changes. Note that it is "safe" to turn Off the PSU, apply a "Short-Circuit" across the Output terminals and switch back On again. The current meter will indicate the limiting Current which can be adjusted and noted. Then remove the Short-Circuit to see the effect on the target system.

So, I gather, you don't think I should use the mosfets. You can't just say "use a pnp transistor in series with the supply rail" and leave it at that. Can you tell me how, specifically, to connect it. I already know the base should connect to the picaxe output via 1K resistor, but I need to know how to connect the emitter and the collector, as I've never used a pnp transistor, although I do have a few that came in a kit. PNP,2N2907, PNP,BC327, PNP,BC337, PNP,A1015, PNP,C1815. Name your poison

Indeed, I believe that a "Low Side Switch" may introduce more problems than it solves. Your "AI" quote seems to indicate that it was suggesting to use a Low-Side switch, so should have been asked: "BUT how can the Control Pulse function when there is no Earth Connection, and what about the (potential) issue of Phantom Powering via the control pin ?". A multimeter test suggests that my 9g Servo has about 10k in series with the control input, but the AI shouldn't assume that (because it's not in the specification). Also, IMHO its "solution" is fundamentally flawed, because it is only delaying the same "sudden" application of power, that causes the initial Kick.

A "High Side Switch" is potentially a useful diagnostics aid, and perhaps offers an ultimate solution, " IF all else fails ", but first I want to ensure that a "Simpler" solution doesn't exist: Did you try my test in post #45 : ".. a 1,000 uF capacitor (across the Servo's pins) fed through a resistor of a few tens of ohms." and what was the result? I could have written "20 ohms" but it's very unlikely you would have exactly that value and anything between 10 and 100 ohms may be "good enough" for a quick test. And if you happen to have only a few resistors nearer 4.7 ohms or 220 ohms, etc., then just connect a couple in series or parallel as appropriate (i.e. to make 9.4 or 110 ohms).

Similarly, I didn't quote an exact Transistor part number, because in my mind it's just a "General Purpose Medium Power PNP transistor", which will generally have different type numbers in Europe (typically starting BC...) and USA (2N....) , with the European numbers often in generic "families", so the exact part number can be "negotiable". For example BC548 is "Low Power NPN" (as are 546 to 549, generally with decreasing voltage and increasing gain), BC558 (etc.) are the PNP equivalents. For slightly higher current is the BC327 (PNP) and BC337 (NPN) which I might try for a test but is not really "up to the job". *

If pushed for a specific Medium Power PNP part number, it would be BD132 (from the BD131 - BD140 family, odd numbers NPN , even numbers PNP). That's because we used to manufacture them in our UK factory, but for my European colleagues it would be BD135 - BD140). I've been designing with these for more than 50 years, (so they're certainly tried and tested :) ) but I noticed just yesterday that Ali-Express will still sell you a pack of 10 x BD139 + 10 x BD140 for about 2 Dollars, and similarly BC54x/55x , BC327/337, etc.. Other "GP" numbers are 1N4148 for small-signal diodes and if I were designing a Low-Side-Switch (FET) for these servos, I would have started with a 2N7000 (that I find easier to remember than its European equivalent).

As for connecting a (PNP) as a High Side Switch, it would be Emitter to the Supply Rail, Collector to the Servo+ (and the + terminal of its 100 - 1000 uF decoupling/reservoir capacitor). The Base would be connected via a resistor of 200 ohms or somewhat higher (typically as used for a series current limiter to a signal LED), directly to the PWM output (C.2) of the PICaxe (permitting potential future development or testing). But with a multiple-supply-rail design it might be necessary to use an intermediate low-power NPN transistor and/or a resistor (of similar value) across the Base -Emitter of the PNP.

* ADDENDUM : By "Medium Power" I would generally consider a "rating" of over 1 Amp and perhaps 1 Watt dissipation, which will then work "well" at a few hundred milliAmps and milliWatts. Unfortunately, none of the parts you have listed appears to reach that rating, but the 2N2907 at 600 mA might be "good enough" for a trial. Ultimately, it may (need to) be in a slightly larger (through-hole leaded) package than the usual TO18/92.

Cheers, Alan.
 
Last edited:
the 2N2907 at 600 mA might be "good enough" for a trial

The spec for the Hitec HS-40 servo Rob is using is here
- The stall current is listed as 460mA

In post #21 Rob reported that
- "When the servo is moving, it's draw is around 40mA"
- "During the twitch, the current draw is from 100 to 130 mA"
- "The short indicator flashes in conjunction with the twitching, ... when the power supply is providing power to the picaxe and the servo."

That 40mA figure is, of course, without any load on the servo so the current with these servos when the servo has to move its pylon on the model will be a figure higher than 40mA, that we don't know yet, but never more than the stall current of about 460mA.
 
Hi,

In #6 Rob wrote: " If an extra component is needed to make a circuit operate properly and within the operating specs of each component's requirements to avoid potential problems, then that's the way I want to go." The HiTec Data Sheet doesn't specify if the Stall current is at 4.8 or 6 volts, but let's assume it's about 450 mA, limited by the internal electronics of the Servo (not the motor itself).

Nowadays, it can be quite difficult to find good (semiconductor) Data Sheets, but there is a reasonable one for the 2N2907 HERE. The behaviour of bipolar transistors when used as a switch can be quite problematic, but the top of Page 2 gives the switching characteristics quite close to 450 mA. Interpolating slightly, the base current might need to be 45 mA and the Vbe could be as high as 2.4 volts. Ignoring any voltage drop across the PICaxe output stage (noting that 45 mA is twice what the PICaxe is rated to deliver, and six times its highest specified output voltage (0.6v at 8 mA) ), the driving resistor might need to be as low as (5.0 - 2.4) / .045 = 58 ohms. Also, the 2N2907 Collector-Emitter saturation (switched On) voltage could be about 1.4 volts, dissipating 1.4 * 0.45 = 0.63 Watt, whilst the package is rated at 400 mW at 25 degrees C (unless a heat sink is fitted). Hence my assertion that it might be suitable for testing, but not for a final design:

2N2907switching.png

Cheers, Alan.
 
Sorry it's taken so long to reply, but I had a "situation" I had to deal with, but I'm back on it now.

Rob, you've only given one result. Was this with the 1k resistor connectng the pixaxe output to 5V, to ground or did both act the same way?

Sorry about that. The 1K resistor was not connected 5V OR 0V. It was simply a series resistor in the signal line. I thought that's what you wanted. With the 10K pullup resistor (what you asked for) from the signal line to the 5V rail, no change. The servo kicks cycling power off/on, and will keep kicking until it reaches about 90 degrees (5 off/on cycles). With the 10K pulldown resistor from the signal line to the ground rail, same result.

You've also said that "when I press the button, they both jump back to the starting point". My two tests #2 & #3 were intended to be done with the servo lead disconnected from the PICAXE
With the servos disconnected from the picaxe totally. Signal lines disconnected, running on a separate power supply (BT PSU). I've also disconnected the jumper that was allowing the picaxe and the servo's separate power supplies to share the same ground. The servos are TOTALLY isolated from the picaxe. No change. This behavior is obviously a problem with the servos. I don't think the picaxe has anything to do with it. I have also tried swapping the power supplies, so that the picaxe is running off the BT PSU, and the servos off the 4X (rechargeables) AA battery pack. No change. BTW, Flenser, I want you to know how enormously grateful I am for your help.

"Did you try using* the PSU for the Servos and* winding the Current Limit backwards at least until the LED started to flash ag

The "flashing" of the red indicator light was happening in association with the "twitching" that was happening while the servo was at rest. You stopped the twitching by some changes you made in the code. If I dial the PSU current knob back to when the red light comes on, the servos don't work at all. If I gradually dial it forward again just until the light goes off, the servos now work, and they still kick when cycling the power. However, having the PSU working right at the threshold, so to speak, the red light flashes quickly while the servos are motion, during their "normal" sweep. But the "kick" is still there.

* EDIT: Note that it is "safe" to turn Off the PSU, apply a "Short-Circuit" across the Output terminals and switch back On again. The current meter will indicate the limiting Current which can be adjusted and noted. Then remove the Short-Circuit to see the effect on the target system.

It did not have any effect whatsoever. The "kicking" still happens.

A "High Side Switch" is potentially a useful diagnostics aid, and perhaps offers an ultimate solution, " IF all else fails ", but first I want to ensure that a "Simpler" solution doesn't exist:

This has become anything but simple. :)

Did you try my test in post #45 : ".. a 1,000 uF capacitor (across the Servo's pins) fed through a resistor of a few tens of ohms." and what was the result? I could have written "20 ohms" but it's very unlikely you would have exactly that value and anything between 10 and 100 ohms may be "good enough" for a quick test. And if you happen to have only a few resistors nearer 4.7 ohms or 220 ohms, etc., then just connect a couple in series or parallel as appropriate (i.e. to make 9.4 or 110 ohms).

I really don't know what "fed through" means. If you want me to add something to the circuit, I will be more than happy to do so, but you need to tell me how you want it connected. When you say "across the servo supply line", I know what that means and I know how to connect it, but "fed through" doesn't tell me anything....SO, I connected a 1000uF CAP, with the Cap's + to the servo's 5V+ line, and the Caps negative to the servo negative through a 22ohm series resistor. In other words, I connect the CAP's GND to one side of the 22ohm resistor, and the other leg of the 22ohm resistor to the servo's GND line. There was no change. If I connected it wrong, just tell me what you want and I'll do it. After all, you guys have really bent over backwards to help me, and I am enormously grateful to both you.

Nowadays, it can be quite difficult to find good (semiconductor) Data Sheets, but there is a reasonable one for the 2N2907 HERE. The behaviour of bipolar transistors when used as a switch can be quite problematic, but the top of Page 2 gives the switching characteristics quite close to 450 mA. Interpolating slightly, the base current might need to be 45 mA and the Vbe could be as high as 2.4 volts. Ignoring any voltage drop across the PICaxe output stage (noting that 45 mA is twice what the PICaxe is rated to deliver, and six times its highest specified output voltage (0.6v at 8 mA) ), the driving resistor might need to be as low as (5.0 - 2.4) / .045 = 58 ohms. Also, the 2N2907 Collector-Emitter saturation (switched On) voltage could be about 1.4 volts, dissipating 1.4 * 0.45 = 0.63 Watt, whilst the package is rated at 400 mW at 25 degrees C (unless a heat sink is fitted). Hence my assertion that it might be suitable for testing, but not for a final design:

Now you're just showing off :)

* ADDENDUM : By "Medium Power" I would generally consider a "rating" of over 1 Amp and perhaps 1 Watt dissipation, which will then work "well" at a few hundred milliAmps and milliWatts. Unfortunately, none of the parts you have listed appears to reach that rating, but the 2N2907 at 600 mA might be "good enough" for a trial. Ultimately, it may (need to) be in a slightly larger (through-hole leaded) package than the usual TO18/92.

Alan, go HERE and find the transistor you want. If someone manufactures it, and it's not obsolete, chances are, Digi-Key has it. I already have a few things in my shopping cart there, I've been holding off on check out until I'm sure I don't need anything else, so as to avoid additional shipping costs. Find a suitable transistor or mosfet that blows your dress up and I will get a few of them. It'll only take them about 3 days to get here.


in the meantime......
As for connecting a (PNP) as a High Side Switch, it would be Emitter to the Supply Rail, Collector to the Servo+ (and the + terminal of its 100 - 1000 uF decoupling/reservoir capacitor). The Base would be connected via a resistor of 200 ohms or somewhat higher (typically as used for a series current limiter to a signal LED), directly to the PWM output (C.2) of the PICaxe (permitting potential future development or testing). But with a multiple-supply-rail design it might be necessary to use an intermediate low-power NPN transistor and/or a resistor (of similar value) across the Base -Emitter of the PNP.

I need specific values and instructions on connect it/them. If it's a resistor you want, then "across the PNP Base-Emitter" is all the information I need, except for the resistors value, which you're a little iffie on ("200 ohm or somewhat higher'). However, If I'm adding an NPN transistor, then "across the base-emitter of the PNP" isn't enough. An NPN transister has a base, collector and emmiter. What do you want across the pnp base-emitter, what NPN transistor do you want? Which pin of the npn transistor goes across the pnp's base-emitter? What do I do with the other two pins of the npn transistor? I want to get this right. Remember, you are the electrical engineer, not me. I am just a guy that likes to build models, and wants to add lighting effects (and now motion effects) to some of them. If it was a wrecked automobile, I would know exactly what to do, but this is a totally different animal. Pretend like I am in the "101 class" that you're teaching, and it's the first day of school.
 
Oh, btw, I have found and ordered some other nano/micro servos that are suitable for my needs (that are no wider than around 8 to 8.5mm)....

ebay: 3.7g Ultra Micro Digital Nano Servo "PES GH-S37D" (width 8.3mm).

Aliexpress: those will take a while to get here, but they are: 2.1g Micro Servo "DM-S0020 2g Mini Servo" 180 Degree 3.7V-5V Lightweight Servos Motor : (width 8.44mm)

Hobbyking.com: Turnigy TGY-0025 Digital Nano Servo (width 8.2mm)

Some of these servos are obviously digital. I'm not sure how much difference that makes. I'm sure the code will have modified slightly depend on the pulse rate of the servos, I guess...
 
Rob, a quick update to suggest that you hold off buying any more electronic components until after you test with the digital servo's you've ordered.

The original tests I did using the servos that I own it was to investigate the first jitter issue that you reported in your posts #8 & #10:
- "The "jitter" is happening while the servo is at rest (about every two seconds), at both the staring point, and the 90 degree point. When the servo is moving, it is as smooth as silk."
and in post #22
"The twitching does not appear to be random. it occurs at frequent and consistent intervals (about every two seconds)"
and I could not reproduce this issue on my 08M2 @4MHz with either of my servos.

Yesterday I did some investigation into the second jitter issue that you reported in your post #42:
- "it works great except for one little glitch when I turn on the power, both servos kick about 15-20 degrees to the left."
and I get the same behaviour from my standard size analog servo, except that it twitches CW, but no twitching at all from my micro servo.

My micro servo is described as a digital servo but it was so cheap that I didn't take that seriously. After neiltechspec's post I did a web search and it seems that could be a true digital servo.

I have not been able to get my micro digital servo to twitch at all in any of the testing I've done.
 
Hi,

By "Fed through..." I simply meant to connect the positive supply of the "target" (PICaxe or Servo as appropriate) via a resistor of approximately the value suggested * (+/-20% will make no difference, and probably more) to the power supply. The idea is to create (with the electrolytic capacitor) a "low pass filter" to smooth and slow down the voltage changes (a bit like an auto suspension).

Yes, the initial Kicks are certainly due to the Servo and may depend very considerably on their internal circuit design. So, as Flenser has said, you should certainly try the other servos you (will) have; it's very probable that any which employ "digital" techniques will not Kick when power is applied.

But if it is necessary to persevere with an Analogue Servo (perhaps by others reading this in years to come), a problem is that the Servo Data Sheet gives little information about how it behaves when the supply is not at a "stable 5 volts". The principle I've been assuming is that if the supply rail is increased slowly then the Servo should not Kick. So for now just one more test:

Run the PICaxe from the battery with the program that gives good normal operation (i.e. after the first few seconds of power-up). Connect the Servo to the PSU and "wind up" the supply voltage knob smoothly from zero to 5 volts over a period of a few seconds. Does the Servo Kick? If so then we have to hope that one of your new servos will be OK. Then try the PICaxe button to check that the Servo sweeps normally.

If the Servo doesn't kick, see if you can increase the PSU voltage more quickly. That might be difficult, depending on the PSU, because you don't want to overshoot about 5.5 volts. Does the PSU have separate "Mains" and Output (Load) switches? If so you might try leaving the Output switch ON and switch Off/On the mains switch (to perhaps give a "softer" start). Does the servo Kick with these latter tests (if they are possible)?

If the above tests suggest that a (more) slowly rising supply voltage can "tame" the Kick, then try connecting a 2N2907 as a High Side switch with approximately 220 ohms from base to PICaxe's C.2 and I will try to create a test program to make the Servo Supply Voltage rise more slowly. I did look through the Digikey offerings (about 20,000 of them) but with all the complications of packing types, marketplace sellers and Potential Tariffs, etc. (on top of the technical specifications of course), I couldn't find any that seemed a sufficiently worthwhile gamble, particularly in view of the last two posts above.

Finally, have you tested if any of the "AI" suggestions have actually worked ? ;)

* EDIT:
I really don't know what "fed through" means........, I connected a 1000uF CAP, with the Cap's + to the servo's 5V+ line, and the Caps negative to the servo negative through a 22ohm series resistor......... If I connected it wrong, just tell me what you want and I'll do it
No, the capacitor's "+" and "-" (0v) leads should be connected directly to the Servo's corresponding "+" and "-" (0v) connector terminals (plug/socket). Then the "20 ohm" resistor is connected between the PSU "+" terminal and the Servo/Capacitor's "+" connections. Basically in the same location as you would insert an Ammeter/Multimeter (or fuse) to detect (or protect) the current drawn by an auto's motor/lamp, etc..

Cheers, Alan.
 
Last edited:
Run the PICaxe from the battery with the program that gives good normal operation (i.e. after the first few seconds of power-up). Connect the Servo to the PSU and "wind up" the supply voltage knob smoothly from zero to 5 volts over a period of a few seconds. Does the Servo Kick?

No, they actually do something much worse, at about the 2.1V mark, they whirl around about 180 degrees and stay there until the PSU reaches 5.0V, at which time, I press the button, and they whirl back around to the starting point, then they carry out their normal 90 degree sweep.

* EDIT:

No, the capacitor's "+" and "-" (0v) leads should be connected directly to the Servo's corresponding "+" and "-" (0v) connector terminals (plug/socket). Then the "20 ohm" resistor is connected between the PSU "+" terminal and the Servo/Capacitor's "+" connections. Basically in the same location as you would insert an Ammeter/Multimeter (or fuse) to detect (or protect) the current drawn by an auto's motor/lamp, etc..

I changed the 22R resistor as per your instructions, but it made no difference. The kick is still there

Does the PSU have separate "Mains" and Output (Load) switches? If so you might try leaving the Output switch ON and switch Off/On the mains switch (to perhaps give a "softer" start). Does the servo Kick with these latter tests (if they are possible)?

The BT PSU has one knob for voltage (to the tenth of a volt), one knob for current (to the mA) and an on/off rocker switch. With 2 servos in the circuit, the current draw reaches 90 mA when they are in motion, and something is drawing 1 mA when nothing at all is happening. (only the servos are connected to the BT PSU)

If the above tests suggest that a (more) slowly rising supply voltage can "tame" the Kick, then try connecting a 2N2907 as a High Side switch with approximately 220 ohms from base to PICaxe's C.2 and I will try to create a test program to make the Servo Supply Voltage rise more slowly.

Don't worry about it, Alan. You have done enough, sir. I will report back when I've tried the other servos. Many thanks to you, and Flenser for all of your efforts.

P.S.
I also ordered some "KST X08 Plus V2.0 Micro Servos" (8.0mm width). KST was having a clearence sale, so I got them at half price. Out of all these other servos, some of them are bound to work. ###fingers crossed###
 
Hi,

Yes, it appears that we have to conclude that the original Servo always has (and always will) Kick when power is applied. A Servo has only three wires (an Earth and Two Inputs) so there is a finite number of signal combinations that we can apply: The pulse input can have only two basic states (ON and OFF), where the Pulse would have a width corresponding to the servo's position when the power was was turned Off. There are actually three possible states of "OFF", the most likely is a (logical) "Low" (i.e. 0 volts), but the input could be (logical) "High" (i.e. the supply rail) or "Floating" (i.e. disconnected). Then there are two (basic) ways that power can be applied, "Quickly" or "Slowly" (a "soft start"), but it seems that we have now tried all these possibilities. :(

Thus, changing to a different model/brand of Servo is the obvious (and perhaps only) solution, but if I were trying to "Make the best of a bad Job" then I think my procedure would be: Start the PICaxe first and wait until it is (continuously) generating the "correct" pulse (width), then apply power to the Servo quite "quickly". There might be some "optimum" rate to increase the voltage, but this would be specific to each particular Servo (brand) and is very much "Clutching at Straws". :

There are two further possible startup "variables"; the time (delay) within the 20 ms pulse period when power is applied (which implies a very fast risetime), or applying a slight "bias" to the width of the pulse (experimentally determined) to compensate for (ideally cancel out) the "expected" Kick. The power to the Servo would need to be applied under the control of the PICaxe (output control pin) to time the starting delay, and to then reduce/remove any pulse offset bias at an appropriate time, and rate of change. The problem with such an approach is that the Servo pulses are transmitted only once every 20 ms, but a Servo can move almost 20 degrees in that period (a typical specification indicates about 90 degrees in 0.11 second).

BTW the "cure" for the random twitches was using PULSOUT instead of SERVO(POS) instructions, which also automatically turns the Pulses OFF when the correct position has been reached. In principle no changes need be applied to the program to use a Digital Servo, but these are sometimes used with a 10 ms period, for a faster response. This shouldn't be necessary here, but could be achieved with the PULSOUT program, if desired (changing the LOOP_TRIM and SWEEP_RATE appropriately).

Cheers, Alan.
 
First a little background. I'm primarily a modeler, specifically, though not limited to, Star Trek ships, and because I wanted to light some of them...led me into hobby electronics and eventually Picaxe, mostly for the PWM aspect, as well as parallel-tasking without having to write dozens of lines of Arduino code, but I've never had the need to use a servo in my projects...until now. I recently watched a youtube video on how to motorize the 1/670 scale Voyager warp pylons using two HS-40 nano-servos. So, I googled to try to get info on how to go about using Picaxe to control them. What I need to do is have the servos turn only 40 degrees at the command of a momentary switch. I.E. at Case 1 turn the servos 40 degrees (pylons up), then Case 2 have the servos return to the starting point (pylons down). I ended up, if you can believe it, having a long conversation with the Google A.I. The darn thing wrote a Picaxe program for me, but I want to run it by you guys to make sure that AI is not leading me astray. Anyway, here is Google AI's code. Oh, and by the way, I haven't even put this in the Picaxe Editor to see if it would compile.

Code:
; PICAXE 08M2 Momentary Switch Servo Control
;
; --- Pin Assignments ---
; C.3 = Input for momentary switch
; C.2 = Output for HS-40 nano servo signal wire

; --- Variable Definitions ---
symbol switch_pin = C.3    ; Assign a friendly name to the switch pin
symbol servo_pin = C.2      ; Assign a friendly name to the servo pin
symbol servo_state = b0     ; Variable to hold the current servo state (0 = start, 1 = 40 deg)
symbol start_pos = 150      ; Initial servo position (adjust as needed for your specific servo)
symbol forty_degree_pos = 190 ; Position for 40 degrees (ADJUST THIS VALUE by experimenting)

; --- Main Program ---
init:
    ; Set the internal oscillator frequency to 16MHz for faster response
    setfreq m16
 
    ; Initialize the servo to the starting position
    servo servo_pin, start_pos
    pause 1000 ; Wait for the servo to settle
 
main:
    ; Check if the switch is pressed (pin goes high)
    if switch_pin = 1 then
        pause 50 ; Debounce delay to prevent multiple button press reads
    
        ; Wait for the button to be released before proceeding
        do while switch_pin = 1: loop
    
        ; Increment the state variable
        inc servo_state
    
        ; Handle the two servo states using SELECT CASE
        select case servo_state
            case 1
                ; First press: move servo to 40 degrees
                servopos servo_pin, forty_degree_pos
        
            case 2
                ; Second press: return servo to starting position
                servopos servo_pin, start_pos
            
                ; Reset the state for the next cycle
                servo_state = 0
            
        endselect
    endif
 
    goto main ; Loop back to check the button again
This year I've built 3x 009(N gauge track) shelf layouts all having points actuated by servos, controlled by AXE231 or 08M2. I tried using servo/servopos & got jittering servos. This was my crude fix - no jitters:

main:
if pinC.3=1 then through
if pinC.3=0 then out
goto main

through:
pulsout c.2, 170
pause 20
goto main

out:
pulsout c.2, 140
pause 20
goto main
 
Last edited:
Some of the servos I ordered came in today. It's these GH-S37D digital servos. that I got from an eBay vendor. Something weird happened. With servos powered by the BT PSU and the picaxe powered by the battery pack, when I switched off power of the battery pack, the servos would kick a little and would continue kicking when I kept cycling it, but they would do nothing when I cycled power with BT PSU (the power supply that's actually powering them), so I tried switching off power to BT PSU first (still connected to the servos), then turned off the battery pack powering the Picaxe, nothing would happen ie no kick. So I tried swapping the power supplies around, having the Picaxe powered by the BT {PSU) and the servos powered by the battery pack. It sill happens. So I decided to try powering the servos and the Picaxe with the same power supply. The kick still happens, but not the first time I cycle the switch off and back on, but usually the second time I turn the switch off, I get the kick, and then they'll kick again at subsequent off/on cycles, but always when switching off, never when switching on. The servos would always return to the starting point on their own after about 10 seconds or so, provided the power is on. Also, when powering the circuit with the BT PSU, I tried slowing reducing the voltage from 5.0V to 0.0V I NEVER get the kick. I even tried cycling the BT PSU on/off switch with it set to 0.0V, and still, nothing happens. So here is the obvious question, is there a way to gradually reduced the power from 5V+ to OFF (ie 0.0V) by adding some contraption to the circuit?

***EDIT***

One more thing, the servos do not respond to button presses after turning on the power for about 18 to 22 seconds, regardless of how the circuit is wired. Although at the times I get "the kick", and the servos return the their normal starting point, then at that time, the servos will respond to switch input.
 
Last edited:
One more thing, the servos do not respond to button presses after turning on the power for about 18 to 22 seconds, regardless of how the circuit is wired.
This sounds like we need to check which version of the program you are running.
The code in post #36 has 3 x pause 5000 commands in the init section that were put in to give a 15sec delay before the code would respond to a button press to check if the servo was twitching while it was stationary, so that could match the behaviour you describe.

There have been a couple of different versions of the code posted to this thread plus some modifications but you can't extract to code from the picaxe chip to get a copy of the code that is running so pls choose the code that you think is running, load it into the picaxe to check it shows this same 18 to 22 seconds delay, and then post it to this thread so that we can examine and test it.
 
Hi,

Yes, as mentioned by Flenser, the PAUSEs (5000) are intended only to help debugging, to identify what happens independently of the PICaxe's contol capability. But you don't need to download a different version of the program, just "comment out" the PAUSEs by putting a semicolon ( ; ) at the start of the line (which will probably change colour to show it's no longer a functional part of the program). It's probable that you don't even want the original PAUSE 1000 (as put in by the AI) because that is "Common Practice" (only) because many PICaxe users do their debugging via the "Terminal Emulator" (which takes some time to start up). But you're not using it (and I'm not recommending it), because its use is known to cause Servo glitches!

Similarly, the two separate power supplies are another "debugging aid" since ultimately you only want one ON/OFF switch! The two supplies may also be causing "confusion" because in practice you NEVER want to power the Servo when the PICaxe is NOT powered, so there is little point in testing that situation. Of course if the Servo is not powered then there should be no way for it to Kick!

Therefore, IMHO you should set up a complete system that you/we hope will work, and see if there are any "Real" problems. To this end, connect a 100uF capacitor directly across the Servo's connector (power) pins and connect them to the Power Supply (either the Bench Supply or the batteries, with a series switch). Then connect 10 uF (and ideally 100 nF in parallel) directly across Legs 1 - 8 of the 08M2 PICaxe, also linking the 0v directly to the Servo's 0v/GND. Now connect a resistor of around 100 ohms (or somewhat higher, as available) between the Servo's +volts and the PICaxe's +volts (so that they have effectively "separate" supplies). Then use one of the Programs which uses PULSOUT (not SERVO) commands, and with NO active PAUSE instructions.

Now try switching ON, then using the "Button" and switching OFF, with reasonable delays in-between and see if there are any "issues". If there are any problems, then to save time, try adding a 1,000 uF capacitor across either the PICaxe's supply pins OR the Servo's pins, one at a time, (i.e. in parallel with the 10/100 uF capacitors in the previous paragraph), and see if the extra 1,000 uF makes matters better, worse or the same.

To answer your final question; Yes it is possible to arrange for the supply voltage to the Servo to fall slowly, but we're looking for a KISS solution first ! In principle, the PICaxe could "wind down" the supply voltage to the Servo(s), but ONLY if it still has power for itself, so the "Turn Off" procedure may be much more complicated that simply opening a switch in the supply rail. This is the reason for testing with the 1000 uF capacitors in the previous paragraph, but another useful measurement could be the current drawn separately from their respective supply rails by the PICaxe and the Servo(s) when they are NOT moving (data sheets imply the PICaxe should be less than 1 mA, a Servo perhaps slightly above 10 mA).

Cheers, Alan.
 
Rob,

I'd like to try to isolate whether this problem where the servo kicks when the power is turned off is caused by the the GH-S37D digital servo.

Disconnect the signal wire from the picaxe to the servo and cycle the power to the servo several times.

It should not matter whether you use the power supply or batteries for this test but if you test with both we will know for certain whether there is some interaction between the supply and the servo when the power is turned off.

FYI: When I perform this test with my Emax ES 9051 micro digital servo I can never get the servo to kick when the power is cycled, even after discharging the stored charge in the servo.
  • No kick when the power is turned on and no kick when the power is turned off.
  • No kicks when powered from the USB 5V or the 4.8V battery pack.
 
Last edited:
I just got some other servos in today. They are the Turnigy TGY-0025 They don't kick or jitter. I cycled power on/off at least 10 times with both the battery pack and the BT PSU and they did not budge. They also respond to button input the very second I turned them on. Plus right now they're on sale. I want to thank you Flenzer and you Alan, from the bottom of my heart, for all you guys have done to help me.

I tried a couple of my Emax ES08MD, they don’t twitch on power cycles either.

Hi neiltechspec. I think it's probably just the servos, as these new ones I've got don't twitch or kick either., I didn't change anything in the code. This is the last iteration of the code that Alan so graciously wrote for me.

Code:
#picaxe 08m2
#no_data                       ; Avoid Overwriting the last stored position in EEPROM
; PICAXE 08M2 Momentary Switch Servo Control     AllyCat October 2025

; #DEFINE ACTIVELOW            ; Uncomment for Alternative pushbutton operation (button to Ground)
; Control/Variables :
  symbol PushButton = pinC.3              ; Press to toggle Servo to move to opposite Endstop
  symbol Servo_Pin = C.4                  ; Control signal to the servo 1
  symbol Servo_Pin2 = C.1                 ; Control signal to the servo 2
  symbol Servo_Pos = b1
  symbol Servo_Pos2 = b2
  symbol HRPos = w2                       ; High Resolution Position to permit step rate adjustment
; Constants depending on the Hardware configuration/requirements :
  symbol LIMIT_CCW = 105                  ; Endstop for Counter-Clockwise rotation (adjust as required)
  symbol LIMIT_CW = 195                   ; Endstop for Clockwise rotation (adjust as required)
  symbol TRIM_S2 = 0                      ; Trim Servo 2 position relative to Servo 1 (optional)
  symbol LOOP_TRIM = 1100                 ; Pause (* 10us) to extend pulse/loop Period to 20 ms
  symbol SWEEP_RATE = 33                  ; Steps per second (adjust as required)
  symbol LAST_POS = 0                     ; Address in EEPROM
; Calculate threshold which determines whether to move CW or CCW :
  symbol MIDDLE_I = LIMIT_CCW + LIMIT_CW  ; Sum the two Servo positions
  symbol MIDDLE = MIDDLE_I / 2            ; Average (Middle) Servo position
  symbol MIDDLEx2 = MIDDLE_I + TRIM_S2    ; To calculate Servo 2 position
  symbol PRESSED = 1



init:
  Low Servo_Pin , Servo_Pin2
  Pause 1000                              ; Allow time for system to stabilise (probably unnecessary)
  Read LAST_POS , Servo_Pos
  If Servo_Pos < 50 AND Servo_Pos <> LIMIT_CCW then    ; Initialise starting position if necessary
     Write LAST_POS , LIMIT_CCW
  Endif
  Read LAST_POS , Servo_Pos
  Servo_Pos2 = MIDDLEx2 - Servo_Pos
  PULSOUT Servo_Pin , Servo_Pos           ; Starting position (assumed fully CCW)
  PULSOUT Servo_Pin2 , Servo_Pos2         ; Starting position (assumed fully CW)
  HRPos = Servo_Pos * 50                  ; To permit partial or multiple steps between Pulses
main:
  Do : Loop Until PushButton = PRESSED    ; Wait for button to be pressed
  If Servo_Pos <= MIDDLE then             ; Position should be at LIMIT_CCW, so can step immediately
     Do
        HRPos = HRPos + SWEEP_RATE        ; Move Clockwise                 [~1ms approx execution time]
        Servo_Pos = HRPos / 50            ; Incremented when appropriate     [~1ms]
        Servo_Pos2 = MIDDLEx2 - Servo_Pos ; Complementary position         [~1ms]  
        PULSOUT Servo_Pin , Servo_Pos     ; )
        PULSOUT Servo_Pin2 , Servo_Pos2   ; } Two complementary pulse widths [~4.5ms]
        Pauseus LOOP_TRIM                 ; Trim Total loop to ~20ms       [~11.5 ms]
     Loop Until Servo_Pos => LIMIT_CW     ; Close loop                     [~1ms]
  Else                                    ; Position should be at LIMIT_CW, so can step immediately
     Do
        HRPos = HRPos - SWEEP_RATE        ; Move Counter-Clockwise
        Servo_Pos = HRPos / 50            ; Decremented when appropriate
        Servo_Pos2 = MIDDLEx2 - Servo_Pos   
        PULSOUT Servo_Pin , Servo_Pos
        PULSOUT Servo_Pin2 , Servo_Pos2 
        Pauseus LOOP_TRIM
     Loop Until Servo_Pos <= LIMIT_CCW
  Endif
  Do : Loop Until PushButton <> PRESSED    ; Ensure button has been released
  Write Last_Pos , Servo_Pos
Goto main        ; Or a purist would close a DO : LOOP here
 
Back
Top