# Using a 556 to control RC servo speed

#### MikeK

Joined Apr 1, 2016
34
The breadboard deal looks good, the little power supply is handy, and I tend to use a lot of breadboards, leave circuits on them for reference. I get my sound cards from the same guy, no avoiding the \$5 each cost, wish I could. Looks like I'm going to need that 14 pin PICAXE, where's the best deals on them? If I make one, I usually make extras for a friend in Australia and I have members on http://www.modeltrainforum.com/search.php?searchid=1123905 that seem interested.

#### dannyf

Joined Sep 13, 2015
2,197
I would like to smooth this out.
It may not even be possible. The servo has a range of input signal, in terms of frequency + duty cycle. How the servo behaves when those parameters are pushed outside of its tolerance is unknown and likely what you are observing.

Having said that, you are essentially building a pwm generator with variable frequency + duty cycle. It can be done easily with 555 timers as you did, with logic gates, or with a mcu. For something this simple, I don't think the mcu route offers much more than any other solutions, given the investments (of time and money) involved.

So I would suggest that you investigate the cause of the motor's mis-bahavior first before trying to find a solution for it.

#### bertz

Joined Nov 11, 2013
327
Looks like I'm going to need that 14 pin PICAXE, where's the best deals on them?
There are no good deals on PICAXE chips. Best to buy the directly from RevEd in the UK or from Sparkfun in the states.

#### bertz

Joined Nov 11, 2013
327
Good Morning Mike

Attached is the program listing for the master (14M2) processor. In my first iteration of this program there was one problem. The distance between the embedded phototransistors could not exceed the length of the full train less the length of one car. In other words, at least one sensor must be covered at all times while the train is in the crossing zone. The easy fix is to use a third sensor positioned midway between the other two sensors, such that at least one sensor is covered as long as the train is in the crossing zone. From a software perspective we added another input and modified the conditional statement on lines 31, 38 and 46. In an earlier post you indicated that you wanted to initiate the action from an embedded sensor located approximately 3 ft. from the crossing gate, but that the train length would not necessarily be that long. This solution covers all contingencies from a train length standpoint. Note the statement in Line 35. This tells the coprocessor to activate the crossing gate(s).

Meanwhile, I expect that you have already downloaded the IDE from the Picaxe website. Make sure it’s Ver. 6.0.8.8. Open a new program, then copy and paste this listing into the new program. Then use the simulator to learn how the program behaves. You can use the “Break” feature that allows you to step through the program one line at a time. This is a good way for you to get familiar with the commands and use of symbols in the program structure. The code is really very simple. Just a bunch of if…then and do…loop statements. The manual explains these in greater detail. Program listing for the 08M2 co-processor to follow.
Code:
'****************CrossingGateVer3.0.bas*****************
'Version 2.0
'AJZ/April 23, 2016
'When a train approaches a crossing gate it triggers a sensor
'which simultaneously lowers the crossing gate and starts a
'red flashing signal
'using digital inputs

'===== Inputs =====
symbol trip1 = pinC.0    'initiates crossing gate sequence
symbol trip2 = pinC.1    'sensor before the crossing
symbol trip3 = pinC.2    'sensor after the crossing
'trip1 = 0 when a train is present
'trip1 = 1 when sensor is uncovered.
'same for trip2 and trip3
'Distance between trip2 and trip3 must be
'less than one train length.
'===== Outputs =====
symbol red1 = B.1            'one crossing gate LED
symbol red2 = B.2            'the other crossing LED
symbol gate = B.3            'output to the slave processor
'===Directives===
#com 3                        'specify serial port
#picaxe 14M2                'specify processor

dirsB = %11111111
dirsC = %00000000

main:
do
if trip1 = 0 and trip2 = 1 and trip3 = 1 then      'train detected
exit
end if
loop          'loops while waiting for train
high gate        'activates slave processor

flash:
do while trip1 = 1 or trip2 = 1 or trip3 = 1
high red1        'flash red lights
low red2
pause 100
low red1
high red2
pause 100
low red2
if trip3 = 1 and trip2 = 1 and trip1 = 1 then    'no train
exit
end if
loop
low gate
goto main

#### bertz

Joined Nov 11, 2013
327
And here is the code for the co-processor. Right now all it does is operate the gate crossing servo. Later we will modify it to also play your sound cards.
Code:
'****************CrossingGate2.bas*****************
'Version 1.0
'AJZ/April 21, 2016
'this code is used to drive a servo operated crossing gate
'based on a digital input signal plus any auxiliary devices

'===== Inputs =====
symbol master = pinC.3        'control signal from master processor

'===== Outputs =====
symbol gate_servo = C.2        'PWM out to servo

'===Variables===
symbol pos0 = b0            'increments servo position
'===Directives===
#com 3                        'specify serial port
#picaxe 08M2                'specify processor
servo gate_Servo, 150            'make sure gate is in raised position
pause 500
main:
do
if master = 1 then        'train detected
exit
end if
loop while master = 0
gosub activate
activate:
for pos0 = 150 to 80 step -1         'lower the gate
servopos gate_Servo, pos0
pause 5                    'sets the lower speed
next pos0
do
if master = 0 then            'train clears the sensor
exit
end if
loop while master = 1
pause 500                        'pause to let the last car clear
for pos0 = 80 to 150             'raise the gate
servopos gate_Servo,pos0
pause 5                    'sets the gate speed
next pos0
goto main

#### bertz

Joined Nov 11, 2013
327
Here is the revised schematic:

#### bertz

Joined Nov 11, 2013
327
And here is the revised breadboard layout - now go and build it. While you have it on the breadboard you will be able to:
2. Reverse direction of servo (if necessary)
3. Adjust end points of servo travel
4. Adjust the rate of the LED flashers
5. Set the time lag from flasher start to gate close
All these adjustments are done in the software.
Have fun!
Get it working the way you want and then we can talk about sound effects.

#### MikeK

Joined Apr 1, 2016
34
Thanks. It will take me some time to digest all this, it's quite new to me. Hopefully while this is going on, something may come up to allow just two sensors, one to start the sequence, one to end it. The sticky point is the horn sequence, which can take up to 20 seconds. That sets the first sensor so far up the track. I'll look for a shorter version. The horn sequence is two 'long', one short, and then one long, I think. If the 'long's can be shortened up a bit, that may make enough difference to help this situation. The situation is if a modeler is just running a little switcher or such, (they can be only 4 inches long) sensors are going to be uncovered while the loco is approaching the crossing. Another solution is to forget the horn, DCC setups have the horn controlled by the operator, no need for recordings. Keep it simple, 'eh? Then the first sensor only needs to be up the track far enough to allow time for the gates to go down, maybe a foot or so. This way, we can use what you already have worked up. I don't want this project to get complicated.

#### bertz

Joined Nov 11, 2013
327
Thanks. It will take me some time to digest all this, it's quite new to me. Hopefully while this is going on, something may come up to allow just two sensors, one to start the sequence, one to end it. The sticky point is the horn sequence, which can take up to 20 seconds. That sets the first sensor so far up the track. I'll look for a shorter version. The horn sequence is two 'long', one short, and then one long, I think. If the 'long's can be shortened up a bit, that may make enough difference to help this situation. The situation is if a modeler is just running a little switcher or such, (they can be only 4 inches long) sensors are going to be uncovered while the loco is approaching the crossing. Another solution is to forget the horn, DCC setups have the horn controlled by the operator, no need for recordings. Keep it simple, 'eh? Then the first sensor only needs to be up the track far enough to allow time for the gates to go down, maybe a foot or so. This way, we can use what you already have worked up. I don't want this project to get complicated.
Give me a day or two to work on this. I think I may be able to come up with something to solve your problem. I wanted to keep the programming constructs simple so that you would be comfortable hacking into them. Hardware wise things get simpler because we eliminate a phototransistor. We'll see if we can't come up with a different construct in the software that will work for you.

#### bertz

Joined Nov 11, 2013
327
Mike, you're killing me! Here is the software solution per your request. It utilizes the SRLATCH function built into the chip. Now you can move the first sensor as far away from the crossing gate as you please (but 20 seconds? seriously?). Also the program is independent of train length; can be a whole string of cars or a single switcher locomotive. Schematic and breadboard layout to follow.

I built a little robot a year or so ago using the 200 sec. USB MP3 with 5 buttons. You should be able to get by using the single button version from the same vendor. I'll need to go back and see how I programmed the Picaxe to play sounds on demand. I think all you need is a brief pulse output since we will be replacing the pushbutton with a Picaxe pin.

Code:
'****************CrossingGateVer3.1.bas*****************
'Version 3.1
'AJZ/April 24, 2016
'When a train approaches a crossing gate it triggers a sensor
'which simultaneously lowers the crossing gate and starts a
'red flashing signal using digital inputs
;SRQ / B.2 connected to C.3.
;green LED connected to SRQ to indicate latch occured

'===== Inputs =====
symbol trip1 = pinC.0    'initiates crossing gate sequence
symbol trip2 = pinC.1    'sensor after the crossing
symbol detect = pinC.3  'the input only leg
'trip1 = 0 when a train is present
'trip1 = 1 when sensor is uncovered.
'same for trip2

'===== Outputs =====
symbol red1 = B.4            'one crossing gate LED
symbol red2 = B.5            'the other crossing LED
symbol SRQ = B.2            'latch output
symbol gate = B.3            'output to the slave processor

'===Directives===
#com 3                        'specify serial port
#picaxe 14M2                'specify processor

dirsB = %11111111            'all outputs
dirsC = %00000000            'all inputs

init:
low SRQ
srlatch %10001000, %10000000 'configure latch
pause 100
main:
do
if trip1 = 0 and trip2 = 1 and detect = 0 then
'train detected
exit
end if
loop          'loops while waiting for train
srset            'sets the latch
high gate        'activates slave processor

flash:
do while detect = 1
'the following sequence flashes the LEDs
high red1 :    low red2 :     pause 500
low red1 :     high red2 : pause 500
if trip2 = 0 then        'trip2 covered
exit
end if
loop

finish:
do while trip2 = 0
high red1 :    low red2 :     pause 500
low red1 :     high red2 : pause 500
if trip2 = 1 then        'trip2 uncovered
exit
end if
loop
srreset        'reset the latch
low gate        'tells co-processor the train has passed
goto main

#### bertz

Joined Nov 11, 2013
327
Here is the revised schematic and breadboard layout. The green LED is optional, but is handy for debugging. It tells you when the latch is made.

#### MikeK

Joined Apr 1, 2016
34
Thanks so much for all your effort, it's really appreciated. I'm glad you could find that latch, it's what was needed. I know the other crossing programs must use it, too. I imagine copyright rules keep folks from copying the ones already out there.
I've been busy working outside, getting yard work done before it gets too hot.
I found good prices at Robot Mesh, waiting for a 14m2 project board to get back in stock. I presume I need one to program the 14m2. I'll get to studying the programs and schematics. What program do you use to draw the schematics? I like to use graph paper to draw my schematics, I mark the lines to match my proto-boards, make a front and back view. It helps when I'm soldering up a board.

#### bertz

Joined Nov 11, 2013
327
I found good prices at Robot Mesh, waiting for a 14m2 project board to get back in stock. I presume I need one to program the 14m2
Not necessarily, it depends whether or not you want to retain programming capability on the final project board. I usually program my chips on the breadboard during the debugging and tweaking process, then plug them into the socket on the project board. You can make your own breadboard programmer. All you need is a 3.5mm Audio Jack, a breakout board, some male headers and a 10k and 22k resistor.

What program do you use to draw the schematics?
I use a free download from ExpressPCB. It allows you to draw the schematic and then do the PCB layout. They want to sell you printed circuit boards so they don't give you the Gerber files, but I etch my own boards. However their software is very easy to use and it suits my purposes nicely.

Have you downloaded the IDE from RevEd yet? If not I strongly urge you to do so and then start playing with the simulator. Good luck!

#### MikeK

Joined Apr 1, 2016
34
Not necessarily, it depends whether or not you want to retain programming capability on the final project board. I usually program my chips on the breadboard during the debugging and tweaking process, then plug them into the socket on the project board. You can make your own breadboard programmer. All you need is a 3.5mm Audio Jack, a breakout board, some male headers and a 10k and 22k resistor.

I use a free download from ExpressPCB. It allows you to draw the schematic and then do the PCB layout. They want to sell you printed circuit boards so they don't give you the Gerber files, but I etch my own boards. However their software is very easy to use and it suits my purposes nicely.

Have you downloaded the IDE from RevEd yet? If not I strongly urge you to do so and then start playing with the simulator. Good luck!
I have downloaded the Picaxe Editor, copied and pasted your commands into it and ran the simulation. It was cute to see the program stop at the loop and wait for the sensor to see a train. Also I downloaded the Express PCB, it's something I have been wanting for a long time. I will probably check out their board making estimates, building a board is way too much bother for me, as I make mistakes. If I can build one to check out a circuit the old way (proto board, soldering wires) and then have them make a PC board, that sounds good to me. I presume the board they send would have holes for the components? I can't imagine they provide boards with components installed. I can solder components just fine, It's getting all those connections right that challenges me.
The 14m2 and necessary supplies should be here soon, I'll put it all together for tests and get back to you.

#### bertz

Joined Nov 11, 2013
327
I have downloaded the Picaxe Editor, copied and pasted your commands into it and ran the simulation. It was cute to see the program stop at the loop and wait for the sensor to see a train. Also I downloaded the Express PCB, it's something I have been wanting for a long time. I will probably check out their board making estimates, building a board is way too much bother for me, as I make mistakes. If I can build one to check out a circuit the old way (proto board, soldering wires) and then have them make a PC board, that sounds good to me. I presume the board they send would have holes for the components? I can't imagine they provide boards with components installed. I can solder components just fine, It's getting all those connections right that challenges me.
The 14m2 and necessary supplies should be here soon, I'll put it all together for tests and get back to you.
See attached notes to step through the whole simulation.

#### Attachments

• 298.6 KB Views: 1

#### MikeK

Joined Apr 1, 2016
34
Assembling a breadboard. The 08 gets programmed on its proto board with the jumper to the right, leaving the 'out' pin exposed?_______ Then it could be inserted into the breadboard. _______The 14m gets programmed on the breadboard with the 'serin' lead from the audio jack breakout to the #2 pin on the 14m (22k and 10k resistors in place)_______ (The 'serout' is connected to pin 13_____ during programming?_______
The audio jack breakout connects to the breadboard with the long headers?_______ (The audio cord goes to the computer).
I presume an audio jack could be soldered directly to a proto board like I use for my circuits. The pins are just a tad short to use in a breadboard.
I've been reading the manual, about half-way through. Took a quick look at the forum, looks good. Deep subject, but I expected that.

#### bertz

Joined Nov 11, 2013
327
Assembling a breadboard. The 08 gets programmed on its proto board with the jumper to the right, leaving the 'out' pin exposed?_______ Then it could be inserted into the breadboard. _______The 14m gets programmed on the breadboard with the 'serin' lead from the audio jack breakout to the #2 pin on the 14m (22k and 10k resistors in place)_______ (The 'serout' is connected to pin 13_____ during programming?_______
The audio jack breakout connects to the breadboard with the long headers?_______ (The audio cord goes to the computer).
I presume an audio jack could be soldered directly to a proto board like I use for my circuits. The pins are just a tad short to use in a breadboard.
I've been reading the manual, about half-way through. Took a quick look at the forum, looks good. Deep subject, but I expected that.

Now for the 14M2, yes you can download the program while it is on the breadboard. (You can also program the 08M2 on the breadboard as long as your jumper wires can reach the pins). You can't use a 3.5 mm audio jack directly on the breadboard because as you have pointed out, the pins are too short and stubby. You need a breakout board similar to the one I recommended from Sparkfun; or you can make your own using perfboard, an audio jack and the two (22k & 10k) resistors. If you are using a breakout board for the audio jack, the long pins on the headers will fit nicely into the holes on the breadboard.

I am still learning to program in C++ and as an exercise I wrote a program to do the same thing (lights, crossing gate, sound) using an Arduino. It's not tested yet, but there are similarities and also significant differences. But that's a topic for another Oprah show, for now we'll stay focused on getting you up and running with Picaxe.

#### MikeK

Joined Apr 1, 2016
34
Going well, thanks to you. I now have both pics programmed, the led flashed a couple of times, showing the latch as you planned. I'll attach the rest of the hardware on the breadboard and start testing tomorrow. Really glad to see this progress. I presume the pics will not erase themselves, hopefully.

#### MikeK

Joined Apr 1, 2016
34
More progress today. I mounted the components on my breadboard, fired it up. The servo moves 45 degrees when the PTs are covered/uncovered. Hopefully full motion and slow travel is available. I had to reverse the flashing leds to get them to work, put the negative legs to the pic. They flash nicely when the first PT is covered, stop flashing when the second PT is covered/uncovered (just right) but the led from pin 8 stays on, a minor glitch. The PTs work nicely. I'll test the setup at the track to see if it is noise resistant next.

#### bertz

Joined Nov 11, 2013
327
I had to reverse the flashing leds to get them to work, put the negative legs to the pic. They flash nicely when the first PT is covered, stop flashing when the second PT is covered/uncovered (just right) but the led from pin 8 stays on, a minor glitch.
Insert the following line of code after lines 49 and 58 in the 14M2 program and then download again:
low red1 : low red2
That should take care of the glitch. Can you explain why?

The servo moves 45 degrees when the PTs are covered/uncovered. Hopefully full motion and slow travel is available.
The positioning signal for a typical servo is a pulse between 0.75 and 2.25 milliseconds (ms) long. With a 0.75 ms pulse the servo moves to one end of its range, and with a 2.25 ms pulse the servo moves to the other. Therefore with a 1.5 ms pulse, the servo will move to the center position.

With that knowledge in hand, let's have a look at the code for the 08M2 co-processor. As you study the code you will see that we have set the limits of servo travel between 150 and 80. Recall that the full range of motion is between 225 and 75 (Picaxe doesn't do floating point math) so what you are seeing accurately reflects what the program is telling the servo to do.

Now go ahead and experiment. Change the values in lines 18, 28, and 38 from 80 and 150 to 75 and 225. Then watch what happens to the range of motion. Pretty neat, huh? You can precisely set the limits of travel by adjusting these values.

Next we address the issue of servo speed. Take a look at lines 30 and 40 where you will see a pause command followed by a value in milliseconds. If you want the servo to go faster, decrease this pause value. If you want to slow the servo down, increase this value. Go ahead and experiment, it's the best way to learn. Try decreasing the value to 1 and see what happens. Then go ahead and set the value to give you the effect you're looking for. Of course you must do a new download every time you make a program change.

I'm interested to see what impact locomotive operation has on the new app, if any.

Last edited: