How to properly arm 4 or more ESC and send PWM to motor in arduino Pro mini

Hi,
Just get in hands with arduino and decided to build myself a quadcopter using arduino for education purpose and self learning. Of cause nothing is smooth initially. So here is the problem I can’t solve it.

Initially I had wrtten a sketch to run and test with one ESC and motor. Everything goes well and I started to re-write the sketch that controlling 4 ESCs and motors using servo library. I uploaded to pro mini and mega2650 as well, but it seem that there is some problem when I start to attach more than one ESC. When I use the servo library to attach an ESC to run the motor, everything go smooth. So I decided to attach 4 ESC and motors for my quadcopter. When I uploaded the sketch and run, ESCs can’t arm properly, motor didn’t spin and ESC keep beeping…and beeping…!

A brief intro of my setup:
I am using pro mini for my quadcopter,

  • 4 x HW30A ESC
  • 4 x A2122, 13T, 1000kV brushless motor
  • 7.2v 3800maH NiMH battery.
  • 1 x potentiometer as collective to run 4 motors
  • 1 x mini joystick as forward and Aft, left and right control.
    Everything start as simple as possible, I won’t implement PID at the moment, what I want is to spin the 4 motors smooth from my controller. My idea is to be able to control it wireless using nRF24L01 module to control my quadcopter. Current I still haven’t implement the wireless control yet, just want to solve the motor issue.

Hope any guru here can help me see what is going wrong. Below is my sketch:

#include <Servo.h>
#include <Serial.h>
#define minSpeed 1500
#define maxSpeed 2000

Servo esc1;
Servo esc2;
Servo esc3;
Servo esc4;

//attached pins for ESC signal 
int MOTOR_1 = 9; //CW
int MOTOR_2 = 10; //CCW
int MOTOR_3 = 11; //CW
int MOTOR_4 = 12; //CCW

//attach pin to analog for control stick
int potCycPitch = A0; //pot for cyclic Fwd & Aft
int potCycLR = A1; //pot for Left & right
int potCollective = A2; //pot for collective
int potYaw = A3; //pot for yaw left & right

int motor1 = 0;
int motor2 = 0;
int motor3 = 0;
int motor4 = 0;
int newSpeed = 0;
int previousSpeed = 0;

int initESC(){
  esc1.attach(MOTOR_1);
  esc2.attach(MOTOR_2);
  esc3.attach(MOTOR_3);
  esc4.attach(MOTOR_4);
  
  esc1.write(90);
  esc2.write(90);
  esc3.write(90);
  esc4.write(90);
  delay(3000);
  return 1;
}

void setup(){
  
  Serial.begin(9600);
  int escOk = initESC();
  delay(1000);
  if(escOk == 1){
    Serial.println("ESC Armed and Initialize Done");
  } else {
    Serial.println("System not ready");
  }
  
}

void loop(){

  int collective = analogRead(potCollective);
  int newSpeed = map(collective, 0, 1023, minSpeed, maxSpeed);
  motor1 = newSpeed;
  motor2 = newSpeed;
  motor3 = newSpeed;
  motor4 = newSpeed;
  
  int pitch = analogRead(potCycPitch);
  pitch = constrain(pitch, 0, 1023);

  int LR = analogRead(potCycLR);
  LR = constrain(LR, 0, 1023);
  
  int yaw = analogRead(potYaw);
  yaw = constrain(yaw, 0, 1023);
  
  if(pitch >= 0 || pitch <= 517){
    int pitchFwd = constrain(pitch, 0, 517);
    int addSpd = map(pitchFwd, 517, 0, 0, 100);
    
    //increase rpm of the motor 3,4 greater than motor 1,2
    if(motor1 == maxSpeed && motor2 == maxSpeed && motor3 == maxSpeed && motor4 == maxSpeed)
    {
      motor1 = newSpeed - addSpd;
      motor2 = newSpeed - addSpd;
      motor3 = newSpeed;
      motor4 = newSpeed;
    } else {
      motor1 = newSpeed;
      motor2 = newSpeed;
      motor3 = newSpeed + addSpd;
      motor4 = newSpeed + addSpd;
    }
  }
  
  if(pitch >= 519 || pitch <= 1023){
    int pitchAft = constrain(pitch, 519, 1023);
    int addSpd = map(pitchAft, 519, 1023, 0, 100);
    
    //increase rpm of the motor 1,2 greater than motor 3,4
    if(motor1 == maxSpeed && motor2 == maxSpeed && motor3 == maxSpeed && motor4 == maxSpeed)
    {
      motor1 = newSpeed;
      motor2 = newSpeed;
      motor3 = newSpeed - addSpd;
      motor4 = newSpeed - addSpd;
    } else {
      motor1 = newSpeed + addSpd;
      motor2 = newSpeed + addSpd;
      motor3 = newSpeed;
      motor4 = newSpeed;
    }
  }
  
  if(LR >= 0 || LR <= 516){
    int Left = constrain(LR, 0, 516);
    int addSpd = map(Left, 516, 0, 0, 100);
    //increase motor 2,4 rpm greater than motor 1,3
    if(motor1 == maxSpeed && motor2 == maxSpeed && motor3 == maxSpeed && motor4 == maxSpeed)
    {
      motor1 = newSpeed - addSpd;
      motor2 = newSpeed;
      motor3 = newSpeed - addSpd;
      motor4 = newSpeed;
      
    } else {
      motor1 = newSpeed;
      motor2 = newSpeed + addSpd;
      motor3 = newSpeed;
      motor4 = newSpeed + addSpd;
    }
  }
  
  if(LR >= 517 || LR <= 1023){
    int Right = constrain(LR, 517, 1023);
    int addSpd = map(Right, 517, 1023, 0, 100);
    //increase motor 1,3 rpm greater than motor 2,4
    if(motor1 == maxSpeed && motor2 == maxSpeed && motor3 == maxSpeed && motor4 == maxSpeed)
    {
      motor1 = newSpeed;
      motor2 = newSpeed - addSpd;
      motor3 = newSpeed;
      motor4 = newSpeed - addSpd;
      
    } else {
      motor1 = newSpeed + addSpd;
      motor2 = newSpeed;
      motor3 = newSpeed + addSpd;
      motor4 = newSpeed;
    }
  }
  
  if(yaw >= 0 || yaw <= 516){
    int yawL = constrain(yaw, 0, 516);
    int addSpd = map(yawL, 516, 0, 0, 100);
    //decrease motor 1,4 and maintain motor 2,3 rpm
    motor1 = newSpeed - addSpd;
    motor2 = newSpeed;
    motor3 = newSpeed;
    motor4 = newSpeed - addSpd;

  }
  
  if(yaw >= 517 || yaw <= 1023){
    int yawR = constrain(yaw, 0, 516);
    int addSpd = map(yawR, 516, 0, 0, 100);
    //decrease motor 2,3 and maintain motor 1,4 rpm
    motor1 = newSpeed;
    motor2 = newSpeed - addSpd;
    motor3 = newSpeed - addSpd;
    motor4 = newSpeed;
  } 

  esc1.writeMicroseconds(motor1);
  esc1.writeMicroseconds(motor2);
  esc1.writeMicroseconds(motor3);
  esc1.writeMicroseconds(motor4);
  delay(500);
}

Why not arm them one at a time?

void initESC()
{
  for (int i = 0; i < 4; i++)
  {  
    esc [i].attach(motorPin [i]);
    esc [i].write(90);
    delay(750);
  }
}

Why aren’t you using serial debug to tell you what’s going on?

NiMH battery for 4 motors? Unlikly to work, try 20C LiPo pack instead...

Thank guys, will give it a try and post the outcome!