Flysky and Relay

I am trying to get my Flysky RC radio to control a relay and I wrote a code but nothing seems to happen at the output . I was wondering if someone can take a look at the code let me what I am doing wrong

Flysky with fs-ia6b-

Arduino uno -
Relay -

include "Arduino.h"

//Test with Channel 1 
//Test with switch SWB 
//we declare the inputs to them we connect our RC from 1 channel and SWB 
int ch1=3;
int ch6=5;


//we declare outputs we connect LEDs to them
int sd1 = 2;
int sd6 = 4;

//variables for recording the signal
int sig1;
int sig6;


//variables for PWM control
int forward;
int plow;

void setup()
{
 
 //assign pins as inputs
    pinMode(ch1, INPUT);
    pinMode(ch6, INPUT);
   
//assign pins as outputs
    pinMode(sd1,OUTPUT);// Cha1 Left :1005 Right 1981
    pinMode(sd6,OUTPUT);// OFF 1981 ON 994
    
  
   //just in case, set the speed to the Serial port so that you can see what is happening in the program
  Serial.begin(9600);
}

void loop()
{
 
 //we record the readings of the receiver
  sig1 = pulseIn(ch1, HIGH);
   sig6 = pulseIn(ch6, HIGH);

   if(sig1>1900)
   {
    digitalWrite(forward,HIGH);
   }
   
   if(sig1<1000)
   {
    digitalWrite(forward,LOW);
   }
   // These are the code for the if statement for the plow.
   if(sig6>1900)
   {
    digitalWrite(plow,HIGH);
   }
   
    if(sig6<1000)
   {
    digitalWrite(plow,LOW);
   }
     //calculate the PWM value
     //forward = (sig1 - 990)/4.2;
    // plow = (sig6 - 990)/4.2;
   
//et the PWM ports to the value received from the receiver
     digitalWrite(sd1,forward);
     digitalWrite(sd6,plow);
     
    
     //outputting data to the port monitor
        
        //but it is not necessary, I used it when writing
          
           //programs
  Serial.println();
  
    Serial.print("1");
    Serial.print(": ");
   // Serial.print(forward);
    Serial.print("    ");
    
    Serial.print("2");
    Serial.print(": ");
   // Serial.print(plow);
    Serial.print("    ");
    
    Serial.print("3");
    Serial.print(": ");
    //Serial.print(shim3);
    //Serial.print("    ");

     Serial.print("4");
    Serial.print(": ");
    //Serial.print(shim4);
    //Serial.print("    ");
  
  //delay(20);
}

@spinorose, your topic has been moved to a more suitable location on the forum. Installation and Troubleshooting is not for problems with your project :wink: See About the Installation & Troubleshooting category.

Please edit your post, select all code and click the </> button to apply code tags and next save your post. It makes it easier to read, easier to copy and prevents the forum software from incorrect interpretation of the code.

What do you get when you print sig1 or sig6 ?

I did your edit thing

1 Like

The typical range of a servo pulse is "about 1000 microseconds" to "about 2000 microseconds". maybe you should put your switch points further from the typical limits. I would try >18000 for HIGH and <1200 for LOW.

how do I do that in code ?
Because all I am doing at this point is writing a "DigitalWrite High or Low "to pins 2 & 4 based on the Pulse width that comes in to Pins 3 & 5 . I sounds fairly simple in my head my I must be going something wrong , any help is deeply appreciated

Really? You can't figure out how to change 1900 and 1000 to 1800 and 1200?

Sorry I misunderstood your directions - off cause I can do that ,I thought you were something to do with the output . I will give that a shot and see if it makes any difference - Thanks

I'm sorry for not being clear. Hope it works!

The other suggestions to print out the pulse lengths are good. If you get 0's (timeout) you are not seeing the input pulses. If the values are below 500 or above 3000 then something is very weird. If the values don't change when you move the controls, you may have the wrong channel. The values should be 'about 1000' at one end of the control travel and 'about 2000' at the other end.


my scope showing my input channels

You’ve been asked to simply print out the numbers coming from the pulse measurement, sig1 and sig6.

Do that, please. Or did I miss it, no caffeine yet…

a7

From the scope picture it looks like the signal level from the radio is 3.3V and the pulses are 1500 microseconds wide ('control in the middle of the 1000 to 2000 range'). You should be getting 1500 microseconds displayed for both pulseIn() values.

How long is it between lines appearing on Serial Monitor? The default timeout for pulseIn() is one second (1 million microseconds) so it should be about two seconds between lines with no input signals.

Do you have the Arduino Ground connected to the Radio Receiver Ground?

I added print to my code and here is it

Input ch1: 1488    Input ch6: 1483    1: 0    2: 0    
Input ch1: 1483    Input ch6: 1489    1: 0    2: 0    
Input ch1: 1489    Input ch6: 1456    1: 0    2: 0    
Input ch1: 1488    Input ch6: 1211    1: 0    2: 0    
Input ch1: 1483    Input ch6: 1051    1: 0    2: 0    
Input ch1: 1489    Input ch6: 1052    1: 0    2: 0    
Input ch1: 1483    Input ch6: 1059    1: 0    2: 0    
Input ch1: 1489    Input ch6: 1172    1: 0    2: 0    
Input ch1: 1489    Input ch6: 1472    1: 0    2: 0    
Input ch1: 1483    Input ch6: 1790    1: 0    2: 0    
Input ch1: 1488    Input ch6: 1964    1: 0    2: 0    
Input ch1: 1483    Input ch6: 1965    1: 0    2: 0    
Input ch1: 1489    Input ch6: 1965    1: 0    2: 0    
Input ch1: 1488    Input ch6: 1821    1: 0    2: 0    
Input ch1: 1483    Input ch6: 1570    1: 0    2: 0    
Input ch1: 1977    Input ch6: 1354    1: 0    2: 0    
Input ch1: 1977    Input ch6: 1182    1: 0    2: 0    
Input ch1: 1977    Input ch6: 1056    1: 0    2: 0    
Input ch1: 1977    Input ch6: 1053    1: 0    2: 0    
Input ch1: 1977    Input ch6: 1053    1: 0    2: 0    
Input ch1: 1977    Input ch6: 1075    1: 0    2: 0    
Input ch1: 1483    Input ch6: 1184    1: 0    2: 0    
Input ch1: 1489    Input ch6: 1482    1: 0    2: 0    
Input ch1: 989    Input ch6: 1815    1: 0    2: 0    
Input ch1: 989    Input ch6: 1963    1: 0    2: 0    
Input ch1: 989    Input ch6: 1965    1: 0    2: 0    
Input ch1: 989    Input ch6: 1965    1: 0    2: 0    
Input ch1: 989    Input ch6: 1959    1: 0    2: 0    
Input ch1: 989    Input ch6: 1799    1: 0    2: 0    
Input ch1: 989    Input ch6: 1558    1: 0    2: 0    
Input ch1: 1977    Input ch6: 1353    1: 0    2: 0    
Input ch1: 1977    Input ch6: 1136    1: 0    2: 0    
Input ch1: 1977    Input ch6: 1053    1: 0    2: 0    
Input ch1: 1977    Input ch6: 1054    1: 0    2: 0    
Input ch1: 1977    Input ch6: 1076    1: 0    2: 0    
Input ch1: 1977    Input ch6: 1262    1: 0    2: 0    
Input ch1: 1483    Input ch6: 1501    1: 0    2: 0    
Input ch1: 989    Input ch6: 1782    1: 0    2: 0    
Input ch1: 989    Input ch6: 1963    1: 0    2: 0    
Input ch1: 988    Input ch6: 1964    1: 0    2: 0

Yes everything is properly grounded off the the ground on the Arduino

@spinorose OK THX.

Now post the code as it is now. I see there's been some changes and adjustments and you added the printing, so.

Post what isn't working now if your best common sense can't let you see why it isn't working.

a7

#include "Arduino.h"

//Test with Channel 1 
//Test with switch SWB 
//we declare the inputs to them we connect our RC from 1 channel and SWB 
int ch1=3;
int ch6=5;


//we declare outputs we connect LEDs to them
int sd1 = 7;
int sd6 = 8;

//variables for recording the signal
int sig1;
int sig6;


//variables for PWM control
int forward;
int plow;

void setup()
{
 
 //assign pins as inputs
    pinMode(ch1, INPUT);
    pinMode(ch6, INPUT);
   
//assign pins as outputs
    pinMode(sd1,OUTPUT);// Cha1 Left :1005 Right 1981
    pinMode(sd6,OUTPUT);// OFF 1981 ON 994
    
  
   //just in case, set the speed to the Serial port so that you can see what is happening in the program
  Serial.begin(9600);
}

void loop()
{
 
 //we record the readings of the receiver
  sig1 = pulseIn(ch1, HIGH);
   sig6 = pulseIn(ch6, HIGH);

   if(sig1>1900)
   {
    digitalWrite(forward,HIGH);
   }
   
   //if(sig1<1000)
   //{
   // digitalWrite(forward,LOW);
   //}
   // These are the code for the if statement for the plow.
   if(sig6>1900)
   {
    digitalWrite(plow,HIGH);
   }
   
   // if(sig6<1000)
   //{
    //digitalWrite(plow,LOW);
  // }
     
   
//et the PWM ports to the value received from the receiver
     digitalWrite(sd1,forward);
     digitalWrite(sd6,plow);
     
    
     //outputting data to the port monitor
        
        //but it is not necessary, I used it when writing
          
           //programs
  Serial.println();
  
    
    Serial.print("Input ch1");
    Serial.print(": ");
   Serial.print(sig1);
    Serial.print("    ");

 Serial.print("Input ch6");
    Serial.print(": ");
   Serial.print(sig6);
    Serial.print("    ");

Serial.print("1");
    Serial.print(": ");
   Serial.print(forward);
    Serial.print("    ");
    
    Serial.print("2");
    Serial.print(": ");
   Serial.print(plow);
    Serial.print("    ");

    
    
    delay(200);
}

nothing happens on the out Pins 7&8 . completely dead.

forward and plow are variables you never set. They are initialised to 0. You are misusing them, it is no surprise you get "nothing".

You need to learn more about variables, digitalRead and digitalWrite.

Meanwhile try this loop(), adjust the constants using what you learned by printing the values that your pulseIn calls showed you when you printed them.

I assume you have LEDs that are working on your output pins. You could write a small bit of code to test that... just sayin'.

void loop()
{
  sig1 = pulseIn(ch1, HIGH);
  sig6 = pulseIn(ch6, HIGH);

   if(sig1 > 1900)
   {
     digitalWrite(sd1, HIGH);
   }
   
   if(sig1 < 1000)
   {
     digitalWrite(sd1, LOW);
   }

    if(sig6 > 1900)
   {
     digitalWrite(sd6, HIGH);
   }
   
   if(sig6 < 1000)
   {
     digitalWrite(sd6, LOW);
   }
       
   delay(200);
}

HTH

a7

It looks like what you were trying to do is:

#include "Arduino.h"

//Test with Channel 1
//Test with switch SWB
//we declare the inputs to them we connect our RC from 1 channel and SWB
const byte ch1Pin = 3;
const byte ch6Pin = 5;


//we declare outputs we connect LEDs to them
const byte sd1Pin = 7;
const byte sd6Pin = 8;

//variables for PWM control
int forward = LOW;
int plow = LOW;

void setup()
{
  //assign pins as inputs
  pinMode(ch1Pin, INPUT);
  pinMode(ch6Pin, INPUT);

  //assign pins as outputs
  pinMode(sd1Pin, OUTPUT); // Cha1 Left :1005 Right 1981
  pinMode(sd6Pin, OUTPUT); // OFF 1981 ON 994


  //just in case, set the speed to the Serial port so that you can see what is happening in the program
  Serial.begin(9600);
}

void loop()
{
  //we record the readings of the receiver
  unsigned long sig1 = pulseIn(ch1Pin, HIGH, 30000);
  unsigned long sig6 = pulseIn(ch6Pin, HIGH, 30000);

  if (sig1 > 1800)
    forward = HIGH;
  else if (sig1 < 1200)
    forward = LOW;

  if (sig6 > 1800)
    plow = HIGH;
  else if (sig6 < 1200)
    plow = LOW;

  //et the PWM ports to the value received from the receiver
  digitalWrite(sd1Pin, forward);
  digitalWrite(sd6Pin, plow);


  //outputting data to the port monitor
  //but it is not necessary, I used it when writing
  //programs
  Serial.println();


  Serial.print("Input ch1");
  Serial.print(": ");
  Serial.print(sig1);
  Serial.print("    ");

  Serial.print("Input ch6");
  Serial.print(": ");
  Serial.print(sig6);
  Serial.print("    ");

  Serial.print("1");
  Serial.print(": ");
  Serial.print(forward);
  Serial.print("    ");

  Serial.print("2");
  Serial.print(": ");
  Serial.print(plow);
  Serial.print("    ");

  delay(200);
}