Reading RC receiver (PPM) and converting to PWM

hi all,
Have been looking for and finding some samples of RC receiver reading sketches. I created this based on what I found:

//Reads PPM signals from 6 channels of an RC reciever, translates the values to 
//PWM and prints the values to serial port. 
//Works with Spectrum DX7 (haven't tested anything else)

//Create variables for 6 channels
int RXCH[6]; 
volatile int RXSG[6];
int RXOK[6];
int PWMSG[6];

void setup() {
  
  //Start communication to serial port
  Serial.begin(115200);  
  
  //Assign PPM input pins. The receiver output pins are conected as below to non-PWM Digital connectors:
  RXCH[1] = 4;  //Throttle
  RXCH[2] = 6;  //Aile / Yaw
  RXCH[3] = 5;  //Elev. / Pitch
  RXCH[4] = 2;  //Rudd. / Roll
  RXCH[5] = 7;  //Gear
  RXCH[6] = 8;  //Aux / Flt Mode
  
  for (int i = 1; i < 7; i++){
    pinMode(RXCH[i], INPUT);
  }
   
}

void loop() {
  
// Read RX values 
  for (int i = 1; i < 7; i++){                                        //for each of the 6 channels: 
  RXSG[i] = pulseIn(RXCH[i], HIGH, 20000);                            //read the receiver signal
  if (RXSG[i] == 0) {RXSG[i] = RXOK[i];} else {RXOK[i] = RXSG[i];}    //if the signal is good then use it, else use the previous signal
  PWMSG[i] = map(RXSG[i], 1000, 2000, 0, 511);                        //substitute the high values to a value between 0 and 511
  constrain (PWMSG[i], 0, 511);                                       //make sure that the value stays within the disired boundries

// Print RX values
  Serial.print(" ||   Ch: ");
  Serial.print(i);
  Serial.print(" / PWMSG: ");
  Serial.print(PWMSG[i]);
  //Serial.print(" / RXSG: ");  
  //Serial.print(RXSG[i]);
  delay(5);  
 }
 Serial.println();
}

I thought that it might be usefull to someone hence this post. I have it running on a 2009.
Hope you’ll have fun with t!
:slight_smile:

Hi eswink, I like your code :) - have you yet considered to read the sum signal instead of every single channel? You would have to use only one port instead of 6 - Im trying to get it working, but Im experiencing some problems as I already posted here: http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1290705249/

Greets Damian

hey Grey,

I haven't considdered that actually because I have sufficient ports availablde for my need.

On top of I don't have a sum signal output on my receiver so would need to mess up my hardware to locate it and make it available;

Otherwise I would indeed see the benefits of your suggestion.

Perhaps this thread might help you: http://diydrones.com/profiles/blogs/705844:BlogPost:38393

Greetz, Eric

Does it really work? Arrays are declared with their size but acessed using number between 0 and size-1, so your RXCH[6] is in fact out of bounds, you should use 0 to 5 and not 1 to 6.

Hi Senso, I have read the same but... it does work! Try it... In any case I would have caught the bug while testing.....

I have read the same but... it does work

Right up until you change something unrelated and..pow!

Fix it - you'll feel better for it.

Right. Here it is:

//Reads PPM signals from 6 channels of a Spectrum DX7 RC reciever, translates the values to 
//PWM and prints the values to serial port. 
//Works with Spectrum DX7 (haven't tested anything else but should work with any PPM output

//Create variables for 6 channels
int RXCH[6]; 
volatile int RXSG[6];
int RXOK[6];
int PWMSG[6];

void setup() {
  
  //Start communication to serial port
  Serial.begin(115200);  
  
  //Assign PPM input pins. The receiver output pins are conected as below to non-PWM Digital connectors:
  RXCH[0] = 4;  //Throttle
  RXCH[1] = 6;  //Aile / Yaw
  RXCH[2] = 5;  //Elev. / Pitch
  RXCH[3] = 2;  //Rudd. / Roll
  RXCH[4] = 7;  //Gear
  RXCH[5] = 8;  //Aux / Flt Mode
  
  for (int i = 0; i < 6; i++){
    pinMode(RXCH[i], INPUT);
  }
}

void loop() {
  
// Read RX values 
  for (int i = 0; i < 6; i++){                                        //for each of the 6 channels: 
  RXSG[i] = pulseIn(RXCH[i], HIGH, 20000);                            //read the receiver signal
  if (RXSG[i] == 0) {RXSG[i] = RXOK[i];} else {RXOK[i] = RXSG[i];}    //if the signal is good then use it, else use the previous signal
  PWMSG[i] = map(RXSG[i], 1000, 2000, 0, 511);                        //substitute the high values to a value between 0 and 511
  constrain (PWMSG[i], 0, 511);                                       //make sure that the value stays within the disired boundries
  
 // Print RX values
  Serial.print(" ||   Ch: ");
  Serial.print(i+1);
  Serial.print(" / PWMSG: ");
  Serial.print(PWMSG[i]);
  //Serial.print(" / RXSG: ");  
  //Serial.print(RXSG[i]);
  delay(10);  
 }
 Serial.println();
}

Grtz!