Interface Picaxe 18X with an RC receiver to control relay.

NM87

New Member
Hi there,

I am new to this forum and to picaxe in general.
My hobby is building radio controlled machines/devices similiar in scale to that of those which were used in Robot Wars or Battlebots.

In the past I have used the servo and lever micro-switch setup, in order to control, turn on and off a relay.

I recently purchased a factory made interface which is a more reliable method of getting an RC receiver to turn a relay on and off cleanly.
But it gets a little expensive when you need about six of them.

http://www.robotmarketplace.com/products/0-TD-RCE200D.html

From what I gather it receives the signal from the the receiver, when a certain pulse is seen it triggers the program to activate the solid state relay.


So what I am wondering is can this be achieved with a Picaxe 18X.
Is it possible to write a program, so that when an input pin receives a certain pulse from the receiver (transmitter stick at about 60% forward) it will trigger the program to put an output pin high. Once the transmitted stick is returned to neutral position the the program tells the output pin to go low.


Any info on this would be greatly appreciated.
Thanks for your help in advance.
Regards NM
 

Rickharris

Senior Member
Yes the picaxe can read the output of a radio control receiver and process it's outputs accordingly.

there are a number of posts on radio control and Picaxes if you search.

BB is your man for Robot wars :)
 

NM87

New Member
Do you know of any links/posts that describe how to go about doing this..?
I have found some posts but its not what I am looking for.
Regards NM
 

Wrenow

Senior Member
NM, there are other solutions that are cheaper. The Vex Motor Controller, for USD$10, should be able to do the job (3 relays per channel), if you simply or the output with diodes (it is a bidirectional speed contoller). Look on the Vex Robotics site under "Logic" (Yeah, I know, not logical).

You might want to know that the Team Delta Electronic Switches you are looking at are pretty touchy on their supply voltage and need a protection diode for relays and solenoids ( we use them a lot in RC Model Warship Combat).

Though a bit more expensive, you would only need 3 of these: http://www3.towerhobbies.com/cgi-bin/wti0001p?&I=LXCJD9&P=7
to run 6 things off of 3 channels (though, if the Vex ESC would work for you , you might could cobble something even cheaper.....).

But, a lot depends on exactly what you want to control and how you want to control it.

Cheers,

Wreno
 

goom

Senior Member
Any Picaxe is ideally suited to this purpose. It is simply a question of measuring the pulse width from the RC receiver in an endless loop, and taking some action depending on the value.
In the simplest application you could simply turn on a Picaxe output if the pulse was greater than, say, 175, and off if less than 170 (a bit of hysteresis is probably a good thing).
Depending on what you are switching on/off you could then use the output to drive a transistor, MOSFET, solid-state relay or mechanical relay (via a transistor).
A single -18X could be used for multiple switches by reading different channels on different pins. You could also switch multiple outputs from a single input channel depending on different thresholds.
You could get fancier by doing such things as checking for valid pulses and taking some action on signal loss, or measuring multiple consecutive pulses to help eliminate anomalous interference spikes.
Give us some more details as to exactly what you want to do, and what you want to turn on/off. I may have some suitable code and/or PCB layout.
 

BeanieBots

Moderator
goom has pretty well covered it.

Any PICAXE will do.
You need the pulsin command.
Connect an RC signal to an input.
A value of 75 is stick full one way.
A value of 225 is stick fully the other way.

You will probably want to include detection of missing and/or out of range pulses to prevent chattering during interference/loss of signal.
Hysterisis is also a good idea.
 

NM87

New Member
Thanks for the help!

What I am tring to achieve for the moment is to get an rc receiver to basacially tell the picaxe to trigger a relay/output pin on and off.

I have no trouble with the relay side of things, I plan to have an output on the picaxe driving the relay through a darlington transistor.

Its just, how do I go about writng the code to get the picaxe to do this, how do I tell it to begin the program when the stick is at say 225, the manuals I have dont go into great detail on this.

Regards NM
 

goom

Senior Member
Here is some code to get you started. It is for 2 switches (could be extended to 4 or more), and has the option for latched or non-latched operation.

Code:
'Radio control 2 channel switch using PICAXE-08 or 08M
'Either channel can operate in latched or unlatched operation.
'For latched operation tie pin3 (leg4) and/or pin4 (leg3) to +5V for channel A and B respectively.
'OutputA (pin0) will then go high the first time the receiver pulse exceeds the threshold, and low the next time.
'OutputB (pin1) will then go high the first time the receiver pulse goes below the threshold, and low the next time.
'For unlatched operation,OutputA is high when the receiver pulse exceeds the threshold and vice-versa.
'For unlatched operation,OutputB is high when the receiver pulse goes below the threshold and vice-versa.
'Change the values of ThresholdA and ThresholdB as desired to change the switching points.   
'When program first runs it assumes the transmitter "stick" is in the neutral position and calibrates accordingly.
'PICAXE-08 connections are:
' Leg 1 +5V
' Leg 2 Only connected when downloading program
' Leg 3 Channel B latching option
' Leg 4 Channel A latching option
' Leg 5 Pulse input from radio receiver
' Leg 6 Channel B output
' Leg 7 Channel A output (must disconnect the programming cable when running program)
' Leg 8 Ground
symbol OutA=0      'Name channel A output pin (leg7)
symbol OutB=1      'Name channel B output pin (leg6)
symbol LatchedA=pin3     'Name channel A latch option pin (leg4)
symbol LatchedB=pin4     'Name channel B latch option pin (leg3)
symbol Pulse_Width=w0     'Name measured input pulse width
symbol Prev_Pulse_Width=w1    'Name previous measured input pulse width
symbol ThresholdA=w2     'Name channel A switching threshold
symbol ThresholdB=w3     'Name channel B switching threshold
symbol Offset=w4      'Name the neutral offset
symbol Pulse_In=2      'Name pulse input pin (leg5)
'Initialise
   ThresholdA=180      'Set channel A threshold to 1.80 ms
   ThresholdB=120      'Set channel B threshold to 1.20 ms
   Prev_Pulse_width=0     'Set previous input pulse width 0 initially
Restart:
   low OutA       'Set channel A output low initially
   low OutB       'Set channel B output low initially
Pause 500       'Wait for 1/2 second initially
   pulsin Pulse_In,1,Pulse_Width   'Measure and store input pulse
   if Pulse_Width<50 or Pulse_Width>250 then Restart 'Go back if no valid pulse detected
   Offset=Pulse_Width-150    'Record the neutral position offset
Start:
   pulsin Pulse_In,1,Pulse_Width   'Measure and store input pulse
   if Pulse_Width<50 or Pulse_Width>250 then Restart 'Go back if no valid pulse detected
   Pulse_Width=Pulse_Width-Offset   'Adjust for the neutral position offset
        'Channel A
   if LatchedA=1 then LatchA    'Check whether channel A is latched operation
         'Unlatched operation
   if Pulse_Width>ThresholdA then Out_OnA       'Check whether pulse is longer than threshold
   low OutA          'Turn off output if pulse<threshold
   goto ChannelB         'Jump to check channel B
Out_OnA:
   high OutA          'Turn on output if pulse>threshold
   goto ChannelB         'Jump to check channel B
LatchA:        'Latched Operation
   if Prev_Pulse_Width<ThresholdA then TholdA    'Check whether previous pulse was above threshold
   goto ChannelB         'Jump to check channel B if previous pulse above threshold
TholdA:           'Previous pulse below threshold
   If Pulse_Width<ThresholdA then ChannelB   'Check whether pulse is longer than threshold
   toggle OutA        'Toggle output if pulse is longer than threshold
ChannelB:       'Channel B
   if LatchedB=1 then LatchB    'Check whether channel B is latched operation
         'Unlatched operation
   if Pulse_Width<ThresholdB then Out_OnB      'Check whether pulse is longer than threshold
   low OutB          'Turn off output if pulse<threshold
   goto Reset_PPW         'Jump to reset previous pulse width
Out_OnB:
   high OutB          'Turn on output if pulse>threshold
   goto Reset_PPW         'Jump to reset previous pulse width
LatchB:        'Latched Operation
   if Prev_Pulse_Width>ThresholdB then TholdB    'Check whether previous pulse was above threshold
   goto Reset_PPW         'Jump to reset previous pulse if previous pulse above threshold
TholdB:           'Previous pulse below threshold
   If Pulse_Width>ThresholdB then Reset_PPW   'Check whether pulse is longer than threshold
   toggle OutB        'Toggle output if pulse is longer than threshold
Reset_PPW:
   Prev_Pulse_Width=Pulse_Width   'Reset previous pulse width to current pulse width
goto Start       'Go back to measure input pulse again
 

NM87

New Member
Goom,
Thanks for the code.
I'll just have to figure it out now.
I see the code has been written for the 08/08M series,
since mine is an 18X, do you think I will encounter any problems, will I have to make some adjustments.
Anyways,
Thanks for taking the time to help.
Regards NM
 

goom

Senior Member
Just change the symbol pin asignments to the appropriate pins for your -18X, and you should be good to go. Probably worth running on the simulator first.
 

GAP

Senior Member
Here is some code to get you started. It is for 2 switches (could be extended to 4 or more), and has the option for latched or non-latched operation.

Code:
'Radio control 2 channel switch using PICAXE-08 or 08M
'Either channel can operate in latched or unlatched operation.
'For latched operation tie pin3 (leg4) and/or pin4 (leg3) to +5V for channel A and B respectively.
'OutputA (pin0) will then go high the first time the receiver pulse exceeds the threshold, and low the next time.
'OutputB (pin1) will then go high the first time the receiver pulse goes below the threshold, and low the next time.
'For unlatched operation,OutputA is high when the receiver pulse exceeds the threshold and vice-versa.
'For unlatched operation,OutputB is high when the receiver pulse goes below the threshold and vice-versa.
'Change the values of ThresholdA and ThresholdB as desired to change the switching points.   
'When program first runs it assumes the transmitter "stick" is in the neutral position and calibrates accordingly.
'PICAXE-08 connections are:
' Leg 1 +5V
' Leg 2 Only connected when downloading program
' Leg 3 Channel B latching option
' Leg 4 Channel A latching option
' Leg 5 Pulse input from radio receiver
' Leg 6 Channel B output
' Leg 7 Channel A output (must disconnect the programming cable when running program)
' Leg 8 Ground
symbol OutA=0      'Name channel A output pin (leg7)
symbol OutB=1      'Name channel B output pin (leg6)
symbol LatchedA=pin3     'Name channel A latch option pin (leg4)
symbol LatchedB=pin4     'Name channel B latch option pin (leg3)
symbol Pulse_Width=w0     'Name measured input pulse width
symbol Prev_Pulse_Width=w1    'Name previous measured input pulse width
symbol ThresholdA=w2     'Name channel A switching threshold
symbol ThresholdB=w3     'Name channel B switching threshold
symbol Offset=w4      'Name the neutral offset
symbol Pulse_In=2      'Name pulse input pin (leg5)
'Initialise
   ThresholdA=180      'Set channel A threshold to 1.80 ms
   ThresholdB=120      'Set channel B threshold to 1.20 ms
   Prev_Pulse_width=0     'Set previous input pulse width 0 initially
Restart:
   low OutA       'Set channel A output low initially
   low OutB       'Set channel B output low initially
Pause 500       'Wait for 1/2 second initially
   pulsin Pulse_In,1,Pulse_Width   'Measure and store input pulse
   if Pulse_Width<50 or Pulse_Width>250 then Restart 'Go back if no valid pulse detected
   Offset=Pulse_Width-150    'Record the neutral position offset
Start:
   pulsin Pulse_In,1,Pulse_Width   'Measure and store input pulse
   if Pulse_Width<50 or Pulse_Width>250 then Restart 'Go back if no valid pulse detected
   Pulse_Width=Pulse_Width-Offset   'Adjust for the neutral position offset
        'Channel A
   if LatchedA=1 then LatchA    'Check whether channel A is latched operation
         'Unlatched operation
   if Pulse_Width>ThresholdA then Out_OnA       'Check whether pulse is longer than threshold
   low OutA          'Turn off output if pulse<threshold
   goto ChannelB         'Jump to check channel B
Out_OnA:
   high OutA          'Turn on output if pulse>threshold
   goto ChannelB         'Jump to check channel B
LatchA:        'Latched Operation
   if Prev_Pulse_Width<ThresholdA then TholdA    'Check whether previous pulse was above threshold
   goto ChannelB         'Jump to check channel B if previous pulse above threshold
TholdA:           'Previous pulse below threshold
   If Pulse_Width<ThresholdA then ChannelB   'Check whether pulse is longer than threshold
   toggle OutA        'Toggle output if pulse is longer than threshold
ChannelB:       'Channel B
   if LatchedB=1 then LatchB    'Check whether channel B is latched operation
         'Unlatched operation
   if Pulse_Width<ThresholdB then Out_OnB      'Check whether pulse is longer than threshold
   low OutB          'Turn off output if pulse<threshold
   goto Reset_PPW         'Jump to reset previous pulse width
Out_OnB:
   high OutB          'Turn on output if pulse>threshold
   goto Reset_PPW         'Jump to reset previous pulse width
LatchB:        'Latched Operation
   if Prev_Pulse_Width>ThresholdB then TholdB    'Check whether previous pulse was above threshold
   goto Reset_PPW         'Jump to reset previous pulse if previous pulse above threshold
TholdB:           'Previous pulse below threshold
   If Pulse_Width>ThresholdB then Reset_PPW   'Check whether pulse is longer than threshold
   toggle OutB        'Toggle output if pulse is longer than threshold
Reset_PPW:
   Prev_Pulse_Width=Pulse_Width   'Reset previous pulse width to current pulse width
goto Start       'Go back to measure input pulse again
goom
I have asked a similar question in the active forum just recently and I was directed to search for programs that do what this one does, which is how I found it.
I just want to switch a relay so that it stays in one position (eg NC) till it receives a command to switch to the other position (eg NO). Latching option?.
If I just remove the non latching option from this program then I should be able to achieve this yes?
GAP
 

goom

Senior Member
Yes, for OutputA, removing the latching option will output a high when the RC joystick is above the threshold, and output a low when below the threshold. The reverse is true for OutputB
 

GAP

Senior Member
goom
I have asked a similar question in the active forum just recently and I was directed to search for programs that do what this one does, which is how I found it.
I just want to switch a relay so that it stays in one position (eg NC) till it receives a command to switch to the other position (eg NO). Latching option?.
If I just remove the non latching option from this program then I should be able to achieve this yes?
GAP
goom

I am dissecting this program and removing the latching option and one output (B) to get it to run only one relay.
When I run the program in the simulator I get a syntax error at this point.

Restart:
low OutA 'Set channel A output low initially
low OutB 'Set channel B output low initially

If I remove the "restart" command then I get an error at this line because there is no restart.
pulsin Pulse_In,1,Pulse_Width 'Measure and store input pulse
if Pulse_Width<50 or Pulse_Width>250 then Restart 'Go back if no valid pulse detected
Offset=Pulse_Width-150 'Record the neutral position offset

I have looked at the manuals and restart is a valid command so now I am confused.
Any help please?
 

westaust55

Moderator
RESTART is a valid command for M2 parts only to allow to stopped task to resume/recommence.
This is not what you want to do. For an M2 part the Programming Editor would be looking for a number after the RESTART to know which task number to resume.

If using an M2 part you will need to change to label and calling point (i.e. The name after the THEN) to something else.
The restart action in gooms code is intended to clear the output so the relay is turned off.

Try changing "Restart" in the code to "release" in all cases/instances.

You will still need one of the LOW OutA/B commands after the Release label otherwise the relay will not be released (drop out).
 

GAP

Senior Member
RESTART is a valid command for M2 parts only to allow to stopped task to resume/recommence.
This is not what you want to do. For an M2 part the Programming Editor would be looking for a number after the RESTART to know which task number to resume.

If using an M2 part you will need to change to label and calling point (i.e. The name after the THEN) to something else.
The restart action in gooms code is intended to clear the output so the relay is turned off.

Try changing "Restart" in the code to "release" in all cases/instances.

You will still need one of the LOW OutA/B commands after the Release label otherwise the relay will not be released (drop out).
Thanks for the prompt reply.
I now have another question about editor 6, I want to run the program in simulator but when looking at the simulate tab all the controls eg run, pause,stop etg are all greyed out.
Is there something that I have to do to get this to run?
I have resorted to going back to editor 5 to run a simulation.
 

GAP

Senior Member
Thanks for the prompt reply.
I now have another question about editor 6, I want to run the program in simulator but when looking at the simulate tab all the controls eg run, pause,stop etg are all greyed out.
Is there something that I have to do to get this to run?
I have resorted to going back to editor 5 to run a simulation.
Solved my simulation problem I was saving the file as txt not bas

I changed restart to release and I took a ? out of line 81 and it now works .

Another question is how I change the inputs on the simulator so that I can see if they are doing the right thing with respect to the outputs.
Also is there a way to get the pins to be of a colour when they change from 0 - 1 like in Editor 5.5?
Again any help appreciated.
 

westaust55

Moderator
Another question is:
in PE6 how I change the inputs on the simulator so that I can see if they are doing the right thing with respect to the outputs.
Also is there a way to get the pins to be of a colour when they change from 0 - 1 like in Editor 5.5?
Again any help appreciated.
When running the simulator, (when the digital tab at the bottom of the simulation window is selected so you see the PICAXE chip pinout) clicking on the pin in the PE6 simulation window will toggle the input low/off (=0) and on/high (=1).
By default a pin:
(a) defined as an input when low (=0)is light grey and when high (=1) is yellow.
(b) defined as an output either by default, by HIGH or LOW commands or by a DIRS<x> command when low is dark grey and when high is green.
 
Top