PulseIn: Reading RC signal for switching IR led on

I'm trying to turn my IR leds on with a switch on my 40Mhz transmitter.
This is the code I currently have:

int transistorIR = 4; // TIP121 transistor pin
int RCsignalCh5 = 2; // D2 pin connected to channel 5 on rc receiver
int ch5; // hold the value

void setup(){
  pinMode(transistorIR, OUTPUT);
  pinMode(RCsignalCh5, INPUT);
}



void loop(){

  ch5 = pulseIn(2, HIGH, 25000); 
  if(ch5>1000){
    digitalWrite(transistorIR, HIGH);
  } 
  if(ch5<1000){
    digitalWrite(transistorIR, LOW);
  } 
}

Now when I run the code and test it, the IR leds are already on(very dim) without turning the switch.
And when I turn the switch, the IR leds become brighter, but go on and off very fast.
What could be the problem?
The leds even go on when I set the value of 1000 to 10 like this:

  if(ch5>10){
    digitalWrite(transistorIR, HIGH);

Hi,

Your RC Transmitter is very unlikey to send signals that are exactly from 1000 to 2000 for any channel including the toggle switch channels.

Its better to calibrate or test a wider range, I generally use > 1700 and < 1300 for my toggle channels.

The RC Transmitter range is between 1000us and 2000us -

Heres a bit about calibration and what you can expect from an RC Transmitter/Receiver in the real world that than theory -

What are you using the IR LEDs for ?

Duane B

rcarduino.blogspot.com

I just tested the ppm values by serial printing them, and when the switch is on, it gives values around 1988 and when the switch is off it gives around 915.
So I changed the code like this:

if(ch5>1900){
digitalWrite(transistorIR, HIGH);
} 
if(ch5<1900){
digitalWrite(transistorIR, LOW);
}

And still the same problem...

I'll take a look at your blog :slight_smile:

The IR leds are for night vision for my UGV.

Here are the results of the test:

Channel 5:915
Channel 5:917
Channel 5:923
Channel 5:915
Channel 5:1359
Channel 5:1968
Channel 5:1986
Channel 5:1986
Channel 5:1986
Channel 5:1986
Channel 5:1988
Channel 5:1986
Channel 5:1986
Channel 5:1988
Channel 5:1129
Channel 5:923
Channel 5:919
Channel 5:919
Channel 5:919
Channel 5:923
Channel 5:917
Channel 5:918
Channel 5:918
Channel 5:917
Channel 5:925
Channel 5:918
Channel 5:918
Channel 5:917
Channel 5:918
Channel 5:917
Channel 5:918
Channel 5:918
Channel 5:918
Channel 5:918
Channel 5:917
Channel 5:918
Channel 5:917
Channel 5:918
Channel 5:917
Channel 5:917
Channel 5:918
Channel 5:923
Channel 5:917
Channel 5:918
Channel 5:918
Channel 5:917
Channel 5:918
Channel 5:918
Channel 5:917
Channel 5:917
Channel 5:917
Channel 5:918
Channel 5:918
Channel 5:918
Channel 5:917
Channel 5:917
Channel 5:918
Channel 5:918
Channel 5:923
Channel 5:917
Channel 5:918
Channel 5:918
Channel 5:918
Channel 5:923
Channel 5:918
Channel 5:918
Channel 5:918
Channel 5:918
Channel 5:923
Channel 5:918
Channel 5:918
Channel 5:918
Channel 5:918
Channel 5:918
Channel 5:917
Channel 5:918
Channel 5:917
Channel 5:917
Channel 5:917
Channel 5:918
Channel 5:917
Channel 5:917
Channel 5:918
Channel 5:917
Channel 5:918
Channel 5:923
Channel 5:917
Channel 5:1467
Channel 5:1495
Channel 5:1960
Channel 5:1990
Channel 5:1985
Channel 5:1986
Channel 5:1988
Channel 5:1988
Channel 5:1988
Channel 5:1988
Channel 5:1988
Channel 5:1988
Channel 5:1989
Channel 5:1988
Channel 5:1747
Channel 5:1514
Channel 5:935
Channel 5:919
Channel 5:915
Channel 5:915
Channel 5:922
Channel 5:917
Channel 5:917
Channel 5:917
Channel 5:917
Channel 5:922
Channel 5:917
Channel 5:917
Channel 5:917
Channel 5:917
Channel 5:917
Channel 5:917
Channel 5:915
Channel 5:917

Hi,
Using pulseIn is blocking your code which will prevent you from doing anything more complex in the future. You might as well switch to interrupts now, the links I posted will show you how.

Duane B

[Duane B](http://Duane B)

I'll probably use a seperate Arduino for complex tasks in the future.
I'm already looking at your blog at this time, but it seems to be difficult. But it's nice :slight_smile:

So, there is no solution for my problem?

The symptoms suggest that the results from pulseIn are fluctuating so that your sketch sees the signal cycling rapidly above and below your threshold. Your test results don't support that, but you haven't shown the code used for your test and I guess it's running very slowly and only sampling the RC pulses occasionally.

It would be useful to see what your pulseIn is actually returning. For example, you could count the number of readings above and below the threshold and print these on the serial port once per second. This will show whether the pulse times are consistent, and you can compare the total count with the RC system frame rate (assuming you know / can find that) to see whether you have a significant number of missed/spurious pulses.

Here is the test code:

int ch5; // Here's where we'll keep our channel values


void setup() {

  pinMode(2, INPUT); // Set our input pins as such


  Serial.begin(9600); // Pour a bowl of Serial

}

void loop() {

  
 ch5 = pulseIn(2, HIGH, 25000); // Read the pulse width of 



  Serial.print("Channel 5:"); // Print the value of 
  Serial.println(ch5);        // each channel


  delay(100); // I put this here just to make the terminal 
              // window happier
}

I got this code from sparkfun.

For example, you could count the number of readings above and below the threshold and print these on the serial port once per second. This will show whether the pulse times are consistent, and you can compare the total count with the RC system frame rate (assuming you know / can find that) to see whether you have a significant number of missed/spurious pulses.

What is the RC system frame rate? I do not really understand this.

Already thanks for helping everyone.

The codes works perfect with my other Arduino, without the IR leds flashing.
So it's a problem on my arduino on my robot(it's an onboard arduino on a motor controller "Wild Thumper")

These are the PPM values on the working arduino:

Switch OFF:
Lowest is 905
Highest is 932

Switch ON:
Lowest is 2039
Highest is 2049

Is it possible that the values change on other arduino, because that's why the leds are flashing.

This is the code that worked on my Arduino Uno, but didn't work on my onboard Arduino on the motor controller:

int transistorIR = 2; // transistor pin
int RCsignalCh5 = 4; // pin connected to channel 5 on rc receiver
int ch5; // hold the value

void setup(){
  pinMode(transistorIR, OUTPUT);
  pinMode(RCsignalCh5, INPUT);
}



void loop(){

  ch5 = pulseIn(4, HIGH, 25000); 
  if(ch5>2039){
    digitalWrite(transistorIR, HIGH);
  } 
  if(ch5<932){
    digitalWrite(transistorIR, LOW);
  } 
}

I can't use the serial monitor on the Wild Thumper arduino because the motors start turning then(strange?).

Strange, now it works...
So i'm sure it has something to do with the PPM value, because sometimes the switch reacts much slower, because the PPM value don't reach the treshold always. I need to play with the values.

Or, maybe it needs to much memory because of the PulseIn?

TiboJ:
What is the RC system frame rate? I do not really understand this.

That is how often the RC receiver sends a pulse to the servo.

PeterH:

TiboJ:
What is the RC system frame rate? I do not really understand this.

That is how often the RC receiver sends a pulse to the servo.

Okay thanks :slight_smile:

I just tested again, and if my transmitter antenna gets to close to the receiver antenna, the switch doesnt work. But that's no problem at all.