PWM pins 5 and 6 not working as expected.

I'm working on a project ( a quadcopter). the brushless DC motors are driven by an Electronic Speed controller which drives the motors at varying speeds depending upon the PWM values inputted to the ESC. I'm varying the pwm by means of a visual studio application that i designed.

So, basically, PC-->arduino dumilanalov-->RF transmitter --- RF receiver-->arduino dumilanalov-->ESC-->brushless DC motor

The motor starts when i gradually increase the pwm. When i tried using pwm pins 3 and 11, the motor worked perfectly, but pins 5 and 6 didn't seem to work. So i tried connecting an LED to it. But the LED responded correctly. VirtualWire(the library i used for the wireless communication) takes over Arduino Timer1, and this will affect the PWM capabilities of the digital pins 9 and 10.

In the arduino reference page, it is given

Notes and Known Issues
The PWM outputs generated on pins 5 and 6 will have higher-than-expected duty cycles. This is because of interactions with the millis() and delay() functions, which share the same internal timer used to generate those PWM outputs. This will be noticed mostly on low duty-cycle settings (e.g 0 - 10) and may result in a value of 0 not fully turning off the output on pins 5 and 6.

Here is code on the receiver arduino,

#include <VirtualWire.h>


const int pin=5;

void setup() //reception-12, motor-3
{
  Serial.begin(9600);//debugging purposes
  pinMode(13,OUTPUT);
  pinMode(pin,OUTPUT);
   pinMode(2,OUTPUT);
  vw_set_rx_pin(12);
  vw_set_ptt_inverted(true);
  vw_setup(2000);
  vw_rx_start(); //start the PLL running
}

void loop()
{
  transmission();
}

void transmission()
{
    uint8_t buf[VW_MAX_MESSAGE_LEN],pwm;
    uint8_t buflen = VW_MAX_MESSAGE_LEN;
    char msg[10],start[2]="s";
    int i=0;
   //
   
    if (vw_get_message(buf, &buflen))
    {
      
      digitalWrite(13,HIGH);
      //pwm=atoi(buf);
      for(i=0;i<buflen;i++)         
      msg[i]=buf[i];      
      msg[i]='\0';
      
      if(strcmp(msg,start)==0) // checking for an input from PC for automatic start
      starter();
      
      pwm=atoi(msg);
      Serial.println(pwm);
      analogWrite(pin,pwm);

      digitalWrite(13,LOW);
    }
}
void starter()
{
  digitalWrite(2,HIGH);
     analogWrite(pin,0);
      delay(200);
      analogWrite(pin,50);
      delay(1000);
      analogWrite(pin,80);
      delay(1000);
      analogWrite(pin,100);
      delay(2000);
      analogWrite(pin,150);
      delay(1000);
      digitalWrite(2,LOW);
}

R/C ESC controllers emulate and utilize the standard servo commands. Why are you using analogWrite commands to try and control ESCs? Where did you find an example showing that method could work? You should be using the servo library to control your ESCs.

Lefty

Why are you using analogWrite commands to try and control ESCs?

I didn't just try, it worked like i told, in pwm pins 3 and 11. It doesn't work only on pins 5 and 6. I didn't want to add another library to code and increase its size. Could you tell me what difference there is between the two?

Thanks

grimreaper:

Why are you using analogWrite commands to try and control ESCs?

I didn't just try, it worked like i told, in pwm pins 3 and 11. It doesn't work only on pins 5 and 6. I didn't want to add another library to code and increase its size. Could you tell me what difference there is between the two?

Thanks

analogWrite() pwm output uses a 500 Hz switching frequency, a servo is expecting a 40 Hz switching frequency.
Servo library is the correct way to interface with R/C type ESC controllers.

Lefty

Thanks, retrolefty. :slight_smile:

@retrolefty, thanks to your advice, my testing worked out great. But i had to choose ServoTimer2 library as the servo library and the virtualwire library( for wireless transmission) use timer 1. Heres a video to what i was doing. :slight_smile:

Very impressive start to your project, especially using that inexpensive RF link between the two arduinos. That is not always an easy thing to get working properly.

Lefty

Thank you. And yes, there were troubles with reception, but with adding longer wires to the antenna pin or, sometimes by touching the tip of the wires, the reception works like magic. :slight_smile: