Hi all, very sorry if this has been answered elsewhere, but I've spent several hours searching this site and the web in general and have not found a great answer.


I'd like to read the PWM servo signals coming out of my receiver (Spektrum Ar600) with an Arduino UNO board and mirror them back out to the servo (so I can eventually add some control in the future). Creating PWM signals with the Arduino appears straight forward, but reading them has not been so obvious.

I've found instructions on doing this with pulseIn but it appears from what I've read that that's a slow method and limited to only 2-3 channels.  I've also seen a lot of discussion regarding using interupts to do this but haven't found any good explanation or code on how I can implement.  Any links to help/example code would be greatly appreciated!


Views: 8818

Reply to This

Replies to This Discussion

The simplest example we have in in our Blimpduino code. Check the RC tab:


Thanks Chris..that helped.  I can't say I fully understand the code and how to parse out what I need, but that code and some more searching on interrupts led me to write the following sketch that appears to be reading the servo position. Please let me know if you notice I'm doing anything blatantly wrong here.  I have the signal line from the rx connected to digital pin 2 of the uno. 


#include <avr/interrupt.h>
#include <avr/io.h>

unsigned int clock1=0;//Clock to count the pulse length
int ch1_position=0;  //This variable stores the raw position of channel 1
byte ch1_state_flag=0;//Something used to messure the pulse length...

void setup(){
  attachInterrupt(0, rxISR, CHANGE);
   /*Activating Timer2, we use it to count only useconds, is the same timer used to pulse the servo, can't be used at the same time*/
  TCCR1B = 0x00 |(1 CS11)|(1WGM12) ; // using prescaler 8 =), page 134
  OCR1A  = 10; //prescaler 8/8mhz= 1 us resolution, so the interrupt will be execute every 10us...                      
  TIMSK1 |=(1 OCIE1A); //See page 136, Enabling timer interrupt 

void loop(){

ISR(TIMER1_COMPA_vect)//This is a timer interrupt, executed every 10us
  clock1++;//Just a counter that increments every 10us...

void rxISR()
  /****This part is for channel 1****/
  if((ch1_state_flag==0)) //if read_ch1 goes high, and if the flag is equal to 0
    clock1=0; //restart the clock
    ch1_state_flag=1;//and change the flag to avoid undesired restart of the clock1..
  else //if((ch1_state_flag==1))//check if the pin is low, and also check if the flag was activated...
    ch1_position=clock1; //pass the value of clock1 to ch1_position...
    ch1_state_flag=0; //Reset the flag.... and that's all...


I am implementing reading PWM on arduino mega by interupt by the signal is not perfect, i am wondering if this "noise" some from software signal or hardware? (see attached image)

Do you have the same noise?

In the image nomovement i don't move the radio, in the image MinMaxMovement i move the radio to min and max.

Due to this error i have more error in my PID :-(



Reply to Discussion


© 2020   Created by Chris Anderson.   Powered by

Badges  |  Report an Issue  |  Terms of Service