BLDC Motor Rare Problem

Hello, I have a rare problem on a project that uses A2212 motors and 30 A ESC, I have done some test separately and after so many test I have identified the problem and I am able to reproduce it. The following is the description of how I can reproduce it, is by doing some steps physically and by using a specific code.

Physically:
(PDE: the motors and Arduino Nano use different power supply, I use lipo for motors and USB for Arduino Nano.)
Connect the motors
Connect Arduino (all goes well, I hear some beeps, maybe not all but it seems that all works and I am able to control the motors)
Disconnect Arduino
Connect Arduino

And now the motors spin at max speed even though I have not specified that I want them to do it.

This is the Software that makes the problem present with the previous steps:
(This is in setup)

MotorESC.attach(9, 1000, 2000);
while (millis() < 3000);

If I don't specify "writeMicroseconds(1000);" after the .attach method, the motors will spin at max speed all the time that the while is blocking (in this case 3000), after that the code will move to loop where I have writeMicroseconds(1000) and then they stop.

(I have as well tried delay(3000) looking for any difference but no difference)

In brief, by following these steps the motors won't stop until they receive "MotorESC.writeMicroseconds(1000)".

Now, my question is if is this normal?, the problem I have got in the main project is similar to this but I didn't follow any steps, I was controlling the motors and then randomly started to spin very fast and I could not make it stop, I had to unplug the power supply

I can put the writeMicroseconds(1000); right after the .attach, but I am wondering if this will make the motors to not be good armed at set up and I am as well concerned if then I can get this anomalous behavior when I am already controlling the motors.

Any help or information, experience... is very appreciated

Never connect or disconnect anything when power is on.
Please post schematics and describe what the steady state problem is.

1 Like

I would expect that code to cause you problems, especially in the start up function.

However, we need to see a complete code that shows the problem that we can compile ourselves and investigate. Even if your full code is too long or has too much other stuff in it then write a short version that doesn't do much except show this problem.

It is also vital that you post a schematic as well. Embedded code only makes sense in the context of the hardware attached to the processor.

We only know what you tell us so help us to help you, by doing these things.

Hello,

I maybe I skipped some context that could help this topic is about the results of inspecting a problem on another project, here I have asked about the problem PWM A2212 Issues - #11 by venaber but I don't get it fixed, so I have done the test and in this topic I am asking about the results of the testing of the problem, here are schematic and software:

This is the schematic (the nrf24l01 is wired but I don't use it):
image

But I think that the wiring is not the problem, in my honest opinion I think the problem is certainly with software and the steps previously described, and I assume that this behavior is not normal, but please do you know a code that works for arming the motors? so I can test if the problem is the software or hardware...?

This is the code I have done and I have been using for testing (is different from the one on the problem topic link):

#include <RF24.h> 
#include <Servo.h>

Servo ESC1, ESC2, ESC3, ESC4;

#define signal1 3
#define signal2 6
#define signal3 9
#define signal4 10

RF24 radio(7, 8); // CE, CSN

const byte address[6] = "12345"; 


void setup() {
  Serial.begin(115200); 

  radio.begin()
  radio.openReadingPipe(0, address);
  radio.setPALevel(RF24_PA_MIN); // En max no va, consuma demasiado y la nano no se lo puede dar
  radio.startListening(); 

// is this necessary?
  pinMode(signal1, OUTPUT);
  pinMode(signal2, OUTPUT);
  pinMode(signal3, OUTPUT);
  pinMode(signal4, OUTPUT);

  ESC1.attach(signal1, 1000, 2000);
  ESC2.attach(signal2, 1000, 2000);
  ESC3.attach(signal3, 1000, 2000);
  ESC4.attach(signal4, 1000, 2000);
// right after this (I have checked 5 times and I can reproduce) the motors starts to spin at max speed

  while(millis() < 3000); 
}


void loop() {
  if (radio.available()) { // this is never true because nrf24l01 connection is never available because now I am doing the test so I on intention skips to the else
    {off topic code}
  } else { // here the motors stop because the code enters on else and so they are told to be at 0 speed
    ESC1.writeMicroseconds(1000);
    ESC2.writeMicroseconds(1000);
    ESC3.writeMicroseconds(1000);
    ESC4.writeMicroseconds(1000);
  }
}

Thanks for clarifying your project for me. I have run your code and find that it works exactly as you described.

The trick in to getting it to work is to do a "writeMicroseconds(1000);" before you attach the servos.

Yes it is. A servo attached without specifying any position defaults to a 1.5 mS pulse. On a servo this corresponds to an angle of 0˚. When you are using servo pulses to drive an ESC this corresponds to motors being on.

No it is not. It can be removed as the servo library sets the pin to an output when you attach it.

so much thanks :grinning:, now it does not max speed.

But two further question, I had tested with this:
#define DEFAULT_PULSE_WIDTH 1000

That redefines a value related to attach (idk if redefines the 1500) but in my case it didn't work, but should it work by just changing that? and then is not necessary to set all ESC to "writeMicroseconds(1000)"?

And the other question is if I should really care about the beeps at the arming sequence, while I was doing the test some times I heard the fully beep arming, another times not at all, but the results were the same, so should I really care?

Yes but changing it where? You can't just write that in your sketch and hope that it works because it will not. You have to change the #define DEFAULT_PULSE_WIDTH in the Servo.h file of the Servo library. When you do that it should work, but that means that your code will only work on this modified version of the library.

What are these beeps?
Where are they coming from?
Is it from the motors or something else?

This is the first time you have mentioned such a thing.

but I can not redefine them on my sketch right after including the library?

no no, is not a problem it self, I use a2212 motors and they do some beeps for telling different things, for example for calibrating the ESC the motors will do a sequence of beeps indicating the calibration steps process, when arming some other sequence... and I don't hear the complete sequence when arming, but I don't think it is a problem at all

Correct, you can't change this from your sketch. What gave you the idea you could? As you have seen it doesn't work when you try.

Neither do I.

I use platformIO (similar to Arduino IDE but is an extension on VSCode) and when I uploaded the code I got on the terminal that it was being redefined, but maybe I had not been able to interpret and maybe it was defining a new one and redefining on my own sketch.... I don't really know.

But never mind, now the motors don't speed at max speed, so much thanks :slightly_smiling_face:.

I think you misunderstood what was going on. Once this #define is in the header file then the whole library needs to be recompiled in order for that new value to be used in the code.

You could however do this:-

  #define DEFAULT_PULSE_WIDTH 1000
    ESC1.writeMicroseconds(DEFAULT_PULSE_WIDTH);
    ESC2.writeMicroseconds(DEFAULT_PULSE_WIDTH);
    ESC3.writeMicroseconds(DEFAULT_PULSE_WIDTH);
    ESC4.writeMicroseconds(DEFAULT_PULSE_WIDTH);

  ESC1.attach(signal1, 1000, 2000);

But that is just a cosmetic change.

yes, it was exactly that, so helpful

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