# Servo control using arduino via rc receiver

**URL:** <https://forum.arduino.cc/t/servo-control-using-arduino-via-rc-receiver/976208>\
**Category:** Programming\
**Created:** [April 2, 2022, 12:01am UTC](https://forum.arduino.cc/t/servo-control-using-arduino-via-rc-receiver/976208 "2022-04-02T00:01:29Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![nice\_servo](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/nice_servo/32/248390_2.png) [@nice\_servo](https://forum.arduino.cc/u/nice_servo)\
**Post date:** [April 2, 2022, 12:01am UTC](https://forum.arduino.cc/t/servo-control-using-arduino-via-rc-receiver/976208/1 "2022-04-02T00:01:29Z")

</div>

Hi,

I tried to control servo using an arduino via rc receiver, well it is moving well just like this tutorial taught....  
[https://www.instructables.com/Control-Servos-with-Arduino-and-RC-receiver/](https://www.instructables.com/Control-Servos-with-Arduino-and-RC-receiver/)

I tried to change it a little bit so that every time i move the gimbal towards same direction the servo goes +10 each time, but it only goes +10 once and doesnt respond thereafter...what can i change?

```cpp

// ***------ RC Servo control -----***
// ***---- K. Michalsky / 2015 ----***

//Arduino Pins
// Pin7 to channel2 of receiver
// Pin9 to Signal of Servo1

//Include Servo library.The Servo library supports up to 12 motors on most Arduino boards and 48 on the Arduino Mega. 
#include <Servo.h>

//Create servo objects to control the servo motors
Servo servoMotor1;

//Define variables for the position of the servos
int posServo1 = 0;

//Define a variable for the channel of the Receiver with which you want to control the servos, in this case channel2 (ailerons channel from my receiver)
int channel2;

//Attach each servo variable to a pin
void setup() { 
  servoMotor1.attach(9); //Servo1 attached to pin 9

    //Define the input Pin for the Receiver
  pinMode(7, INPUT);
  Serial.begin(9600); //Serial comunication for later monitoring of channel signal value
}

void loop() {
  //Read the pulse from Pin7 either HIGH or LOW
  channel2 = pulseIn(7, HIGH);

  //Monitor channel value
  Serial.print("Channel 2:");
  Serial.println(channel2);

  //Define a range of the channel value to center the servo when the stick of the transmiter is centered. This range can variate depending on which servos and Transmiter you are using. Servos are centered at 90 degrees
  if((channel2 >= 1450) && (channel2 <= 1550)){
    //posServo1 = 0;
    servoMotor1.write(posServo1);
  }
  //If the channel value is bigger than the highest value of the range do following
  else if(channel2>1550){
    for(int i = 0; posServo1 < 10; posServo1 ++){
      servoMotor1.write(posServo1);
      i = posServo1;
      delay(1);
              }        
          }
                  
                 
  delay(100); //Time for Monitoring values
}

```

---

<div class="post-metadata">

**Author:** ![J-M-L](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/j-m-l/32/766245_2.png) [@J-M-L](https://forum.arduino.cc/u/J-M-L)\
**Post date:** [April 2, 2022, 12:38am UTC](https://forum.arduino.cc/t/servo-control-using-arduino-via-rc-receiver/976208/2 "2022-04-02T00:38:27Z")

</div>

> [@nice\_servo](#):
>
> ```arduino
> for(int i = 0; posServo1 < 10; posServo1 ++){
> servoMotor1.write(posServo1);
> i = posServo1; 
> delay(1);
> }        
> 
> ```

What do you expect there? (Mismatch in loop index and position testing)

---

<div class="post-metadata">

**Author:** ![johnwasser](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/johnwasser/32/214270_2.png) [@johnwasser](https://forum.arduino.cc/u/johnwasser)\
**Post date:** [April 2, 2022, 1:51am UTC](https://forum.arduino.cc/t/servo-control-using-arduino-via-rc-receiver/976208/3 "2022-04-02T01:51:03Z")

</div>

I think you over-complicated things. If you want to move 10 degrees:

```cpp
  if(channel2 > 1550)
  {
    posServo1 = constrain(posServo1 + 10, 0, 180);
    servoMotor1.write(posServo1);
    delay(10);
  }

```

[/quote]

---

<div class="post-metadata">

**Author:** ![nice\_servo](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/nice_servo/32/248390_2.png) [@nice\_servo](https://forum.arduino.cc/u/nice_servo)\
**Post date:** [April 2, 2022, 11:13pm UTC](https://forum.arduino.cc/t/servo-control-using-arduino-via-rc-receiver/976208/4 "2022-04-02T23:13:43Z")

</div>

Thank it works, i also tried it on AC servo PUL and DIR and it works, but i cannot make it go faster it rotates very slowly, what can i do?

```cpp

// ***------ RC Servo control -----***
// ***---- K. Michalsky / 2015 ----***

//Arduino Pins
// Pin7 to channel2 of receiver
// Pin9 to Signal of Servo1

//Include Servo library.The Servo library supports up to 12 motors on most Arduino boards and 48 on the Arduino Mega. 
#include <Servo.h>

#define dirPin 2
#define stepPin 3

//Create servo objects to control the servo motors
//Servo servoMotor1;

//Define variables for the position of the servos
//int posServo1 = 0;

//Define a variable for the channel of the Receiver with which you want to control the servos, in this case channel2 (ailerons channel from my receiver)
int channel2;

int mSpeed = 0;

//Attach each servo variable to a pin
void setup() { 

  pinMode(stepPin, OUTPUT);
  pinMode(dirPin, OUTPUT);
  // Set the spinning direction CW/CCW:
  digitalWrite(dirPin, HIGH);
  
  //servoMotor1.attach(9); //Servo1 attached to pin 9

    //Define the input Pin for the Receiver
  pinMode(7, INPUT);
  Serial.begin(9600); //Serial comunication for later monitoring of channel signal value
}

void loop() {
  //Read the pulse from Pin7 either HIGH or LOW
  channel2 = pulseIn(7, HIGH);

  //Monitor channel value
  Serial.print("Channel 2:");
  Serial.println(channel2);

  //Define a range of the channel value to center the servo when the stick of the transmiter is centered. This range can variate depending on which servos and Transmiter you are using. Servos are centered at 90 degrees
  //if((channel2 >= 1450) && (channel2 <= 1550)){
    //posServo1 = 0;
    //servoMotor1.write(posServo1);
  //}
  //If the channel value is bigger than the highest value of the range do following
  if(channel2 > 1550){
    digitalWrite(dirPin, HIGH);
    digitalWrite(stepPin, HIGH);
    delayMicroseconds(1);
    digitalWrite(stepPin, LOW);
    delayMicroseconds(1);
  }   
  if(channel2 < 1450){
    digitalWrite(dirPin, LOW);
    digitalWrite(stepPin, HIGH);
    delayMicroseconds(1);
    digitalWrite(stepPin, LOW);
    delayMicroseconds(1);
    
  }

        
          }                  
                 
   
  //Time for Monitoring values

```

---

<div class="post-metadata">

**Author:** ![nice\_servo](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/nice_servo/32/248390_2.png) [@nice\_servo](https://forum.arduino.cc/u/nice_servo)\
**Post date:** [April 3, 2022, 1:18pm UTC](https://forum.arduino.cc/t/servo-control-using-arduino-via-rc-receiver/976208/5 "2022-04-03T13:18:08Z")

</div>

```cpp
[type or paste code here](https://forum.arduino.cc/t/ac-servo-rc-radio-speed-fix/976720)

```

now here pls

---

<div class="post-metadata">

**Author:** ![johnwasser](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/johnwasser/32/214270_2.png) [@johnwasser](https://forum.arduino.cc/u/johnwasser)\
**Post date:** [April 3, 2022, 1:34pm UTC](https://forum.arduino.cc/t/servo-control-using-arduino-via-rc-receiver/976208/6 "2022-04-03T13:34:52Z")

</div>

> [@nice\_servo](#):
>
> i cannot make it go faster it rotates very slowly, what can i do?

You are moving one step for each pulse from the radio (pulseIn()). There are usually about 50 pulses per second so 50 steps per second.

To go any faster you will have to send multiple step pulses for each pulse from the radio.

---

<div class="post-metadata">

**Author:** ![nice\_servo](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/nice_servo/32/248390_2.png) [@nice\_servo](https://forum.arduino.cc/u/nice_servo)\
**Post date:** [April 3, 2022, 1:38pm UTC](https://forum.arduino.cc/t/servo-control-using-arduino-via-rc-receiver/976208/7 "2022-04-03T13:38:05Z")

</div>

is it possible to multiply pulses using arduino code 🙂

---

<div class="post-metadata">

**Author:** ![nice\_servo](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/nice_servo/32/248390_2.png) [@nice\_servo](https://forum.arduino.cc/u/nice_servo)\
**Post date:** [April 3, 2022, 1:40pm UTC](https://forum.arduino.cc/t/servo-control-using-arduino-via-rc-receiver/976208/8 "2022-04-03T13:40:10Z")

</div>

thanks, if i do this it works much faster but is there a way to make it a short cut

```cpp
  if(channel2 > 1550){
    digitalWrite(dirPin, HIGH);
    digitalWrite(stepPin, HIGH);
    delayMicroseconds(1);
    digitalWrite(stepPin, LOW);
    delayMicroseconds(1);
    digitalWrite(stepPin, HIGH);
    delayMicroseconds(1);
    digitalWrite(stepPin, LOW);
    delayMicroseconds(1);
    digitalWrite(stepPin, HIGH);
    delayMicroseconds(1);
    digitalWrite(stepPin, LOW);
    delayMicroseconds(1);
    digitalWrite(stepPin, HIGH);
    delayMicroseconds(1);
    digitalWrite(stepPin, LOW);
    delayMicroseconds(1);
    digitalWrite(stepPin, HIGH);
    delayMicroseconds(1);
    digitalWrite(stepPin, LOW);
    delayMicroseconds(1);
    digitalWrite(stepPin, HIGH);
    delayMicroseconds(1);
    digitalWrite(stepPin, LOW);
    delayMicroseconds(1);
    digitalWrite(stepPin, HIGH);
    delayMicroseconds(1);
    digitalWrite(stepPin, LOW);
    delayMicroseconds(1);
    digitalWrite(stepPin, HIGH);
    delayMicroseconds(1);
    digitalWrite(stepPin, LOW);
    delayMicroseconds(1);
  }

```

---

<div class="post-metadata">

**Author:** ![johnwasser](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/johnwasser/32/214270_2.png) [@johnwasser](https://forum.arduino.cc/u/johnwasser)\
**Post date:** [April 3, 2022, 1:51pm UTC](https://forum.arduino.cc/t/servo-control-using-arduino-via-rc-receiver/976208/9 "2022-04-03T13:51:02Z")

</div>

> [@nice\_servo](#):
>
> is there a way to make it a short cut

Yes. Function, or loop, or both.

Function:

```arduino
void StepPulse()
{
    digitalWrite(stepPin, HIGH);
    delayMicroseconds(1);
    digitalWrite(stepPin, LOW);
    delayMicroseconds(1);
}

  if(channel2 > 1550){
    digitalWrite(dirPin, HIGH);
    StepPulse();
    StepPulse();
    StepPulse();
    StepPulse();
    StepPulse();
    StepPulse();
    StepPulse();
    StepPulse();
  }

```

Or loop:

```arduino
  if(channel2 > 1550){
    digitalWrite(dirPin, HIGH);
    for (int i=0; i< 8; i++) {
      digitalWrite(stepPin, HIGH);
      delayMicroseconds(1);
      digitalWrite(stepPin, LOW);
      delayMicroseconds(1);
    }
  }

```

Or both:

```arduino
  if(channel2 > 1550) {
    digitalWrite(dirPin, HIGH);
    for (int i=0; i< 8; i++) {
      StepPulse();
    }
  }

```

---

<div class="post-metadata">

**Author:** ![nice\_servo](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/nice_servo/32/248390_2.png) [@nice\_servo](https://forum.arduino.cc/u/nice_servo)\
**Post date:** [April 3, 2022, 1:59pm UTC](https://forum.arduino.cc/t/servo-control-using-arduino-via-rc-receiver/976208/10 "2022-04-03T13:59:29Z")

</div>

o thank you 😊

---

<div class="post-metadata">

**Author:** ![nice\_servo](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/nice_servo/32/248390_2.png) [@nice\_servo](https://forum.arduino.cc/u/nice_servo)\
**Post date:** [April 4, 2022, 2:43am UTC](https://forum.arduino.cc/t/servo-control-using-arduino-via-rc-receiver/976208/11 "2022-04-04T02:43:00Z")

</div>

i tried to add channel3, when the left (other) gimbal is above 1550 it increases variable speed and vice versa, but i didnt get it right, what do you think?

```cpp

// ***------ RC Servo control -----***
// ***---- K. Michalsky / 2015 ----***

//Arduino Pins
// Pin7 to channel2 of receiver
// Pin9 to Signal of Servo1

//Include Servo library.The Servo library supports up to 12 motors on most Arduino boards and 48 on the Arduino Mega. 
//#include <Servo.h>

#define dirPin 2
#define stepPin 3

//Create servo objects to control the servo motors
//Servo servoMotor1;

//Define variables for the position of the servos
//int posServo1 = 0;

//Define a variable for the channel of the Receiver with which you want to control the servos, in this case channel2 (ailerons channel from my receiver)
int channel2;
int channel3;

int mSpeed = 0;

//Attach each servo variable to a pin

void StepPulse()
{
    digitalWrite(stepPin, HIGH);
    delayMicroseconds(1);
    digitalWrite(stepPin, LOW);
    delayMicroseconds(1);
}

void StepPulse2()
{
    digitalWrite(stepPin, HIGH);
    delayMicroseconds(1);
    digitalWrite(stepPin, LOW);
    delayMicroseconds(1);
}

void setup() { 

  pinMode(stepPin, OUTPUT);
  pinMode(dirPin, OUTPUT);
  // Set the spinning direction CW/CCW:
  //digitalWrite(dirPin, HIGH);
  
  //servoMotor1.attach(9); //Servo1 attached to pin 9

    //Define the input Pin for the Receiver
  pinMode(7, INPUT);
  pinMode(8, INPUT);
  Serial.begin(9600); //Serial comunication for later monitoring of channel signal value
  
}

void loop() {
  //Read the pulse from Pin7 either HIGH or LOW
  channel2 = pulseIn(7, HIGH);
  channel3 = pulseIn(8, HIGH);

  //Monitor channel value
  Serial.print("Channel 2:");
  Serial.println(channel2);

  //Define a range of the channel value to center the servo when the stick of the transmiter is centered. This range can variate depending on which servos and Transmiter you are using. Servos are centered at 90 degrees
  //if((channel2 >= 1450) && (channel2 <= 1550)){
    //posServo1 = 0;
    //servoMotor1.write(posServo1);
  //}
  //If the channel value is bigger than the highest value of the range do following
if(channel3 > 1550) {
      for (int s=50; s < ((mSpeed * 2) + 1); s++) {
      
  }
}

if(channel3 < 1450) {
      for (int s=50; s < ((mSpeed * 2) + 1); s--) {
      
  }
}

if(channel2 > 1550) {
    digitalWrite(dirPin, HIGH);
    for (int i=0; i < mSpeed; i++) {
      StepPulse();
  }
}

if(channel2 < 1450) {
    digitalWrite(dirPin, LOW);
    for (int i=0; i < mSpeed; i++) {
      StepPulse2();
  }
}
        
}    
              
                 
   
  //Time for Monitoring values

```

---

<div class="post-metadata">

**Author:** ![johnwasser](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/johnwasser/32/214270_2.png) [@johnwasser](https://forum.arduino.cc/u/johnwasser)\
**Post date:** [April 4, 2022, 5:03pm UTC](https://forum.arduino.cc/t/servo-control-using-arduino-via-rc-receiver/976208/12 "2022-04-04T17:03:36Z")

</div>

When you add another pulseIn() you add another 20 millisecond delay. Everything will go at half speed.

I recommend you find a library to read the RC inputs using interrupts instead of pulseIn().

---

<div class="post-metadata">

**Author:** ![nice\_servo](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/nice_servo/32/248390_2.png) [@nice\_servo](https://forum.arduino.cc/u/nice_servo)\
**Post date:** [April 4, 2022, 9:15pm UTC](https://forum.arduino.cc/t/servo-control-using-arduino-via-rc-receiver/976208/13 "2022-04-04T21:15:23Z")

</div>

i dont mind the extra delay, only problem is the variable speed for channel3 is not functioning.

---

<div class="post-metadata">

**Author:** ![johnwasser](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/johnwasser/32/214270_2.png) [@johnwasser](https://forum.arduino.cc/u/johnwasser)\
**Post date:** [April 5, 2022, 12:53am UTC](https://forum.arduino.cc/t/servo-control-using-arduino-via-rc-receiver/976208/14 "2022-04-05T00:53:36Z")

</div>

> [@nice\_servo](#):
>
> ```arduino
> void StepPulse2()
> {
> digitalWrite(stepPin, HIGH);
> delayMicroseconds(1);
> digitalWrite(stepPin, LOW);
> delayMicroseconds(1);
> }
> 
> ```

You can't use the same 'stepPin' for two different stepper drivers. Are you trying to have two RC channels control the same stepper?!?

---

<div class="post-metadata">

**Author:** ![nice\_servo](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/nice_servo/32/248390_2.png) [@nice\_servo](https://forum.arduino.cc/u/nice_servo)\
**Post date:** [April 5, 2022, 2:14am UTC](https://forum.arduino.cc/t/servo-control-using-arduino-via-rc-receiver/976208/15 "2022-04-05T02:14:03Z")

</div>

only using one motor, i fixed it stepPulse..... channel2 is the one that moves the motor which works great. channel3 changes variable speed, this part isnt working.

---

<div class="post-metadata">

**Author:** ![johnwasser](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/johnwasser/32/214270_2.png) [@johnwasser](https://forum.arduino.cc/u/johnwasser)\
**Post date:** [April 5, 2022, 12:39pm UTC](https://forum.arduino.cc/t/servo-control-using-arduino-via-rc-receiver/976208/16 "2022-04-05T12:39:36Z")

</div>

These empty 'for' loops don't change mSpeed at all.

> [@nice\_servo](#):
>
> ```arduino
> if(channel3 > 1550) {
> for (int s=50; s < ((mSpeed * 2) + 1); s++) {
>       
> }
> }
> 
> if(channel3 < 1450) {
> for (int s=50; s < ((mSpeed * 2) + 1); s--) {
>       
> }
> }
> 
> ```

---

<div class="post-metadata">

**Author:** ![nice\_servo](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/nice_servo/32/248390_2.png) [@nice\_servo](https://forum.arduino.cc/u/nice_servo)\
**Post date:** [April 5, 2022, 11:52pm UTC](https://forum.arduino.cc/t/servo-control-using-arduino-via-rc-receiver/976208/18 "2022-04-05T23:52:20Z")

</div>

yay this worked 🙂

```cpp
if(channel3 > 1550 && channel3 < 1700) {
        mSpeed = 10;
              
  }
  
if(channel3 > 1701 && channel3 < 1850) {
        mSpeed = 100;
        //delay(5);
  }

if(channel3 > 1850 && channel3 < 2000) {
        mSpeed = 300;
        //delay(5);
  }

if(channel2 > 1550) {
    digitalWrite(dirPin, HIGH);
    for (int i=0; i< mSpeed; i++) {
      StepPulse();
    }

```

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [October 2, 2022, 11:52pm UTC](https://forum.arduino.cc/t/servo-control-using-arduino-via-rc-receiver/976208/19 "2022-10-02T23:52:46Z")

</div>

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.
