Brushless motor does not spin continuously when an encoder is plugged in

Hello all.
I'm a relatively new Arduino user, and my experience with electronics is questionable at best. Currently, I am trying to use an Arduino to control the speed of a brushless DC motor with the help of an encoder to build a test stand. Currently, I'm just trying to read the speed of the encoder when the motor is run at a certain power level. Unfortunately, once I get above 15% motor power with the encoder plugged in, the motor will accelerate to what looks like maximum power, suddenly stop, then beep in the A tone for the motor. Resetting the target power to 0% then changing the target power repeats the previous action. The Serial, which I'm using to monitor things, also appears to hang up when this happens.
This only happens when the encoder is plugged in to the 5V and ground on the Arduino. The motor spins up and down from 0% - 100% fine when the encoder is not plugged in.
Any idea what could be causing this and how I can try and fix it? Thanks

Hardware I'm using:
ELEGOO UNO R3
Eflite Park 480 1020Kv Brushless motor - https://www.horizonhobby.com/product/park-480-brushless-outrunner-motor-1020kv-3.5mm-bullet/EFLM1505.html#
RC Electric Parts 30A ESC - https://www.rcelectricparts.com/30a-rc-brushless-motor-electric-speed-controller-esc-3a-ubec-with-xt60--35mm-bullet-plugs.html
ESC User Guide - CLASSIC ESC User Guide - RCElectricParts.com
Encoder datasheet - https://www.cuidevices.com/product/resource/amt10.pdf

Code:

#include <Encoder.h>
#include <Servo.h>

//Encoder Constants
#define PPR 512  //pulses per revolution
#define dirPin 2
#define stepPin 3
Encoder myEnc(dirPin, stepPin);

//ESC setup
Servo esc;
#define escPin 5

//system constants
#define DT 10  //ms

int encPos = 0;

int target = 0;

//system constants
#define DT 50  //ms

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  Serial.println("Hello");
  esc.attach(escPin, 1000, 2000);
  driveMotor(0);
  delay(1500);
  while (!(Serial.available())) {
    Serial.println("waiting");
    delay(500);
  }
}

//Serial information

bool check = true;

void loop() {
  // put your main code here, to run repeatedly:
  driveMotor(target);
  Serial.print(target);
  Serial.print(" ");
  Serial.print(encoderVel());
  Serial.print(" ");
  Serial.println(myEnc.read());
  delay(DT);
  if (Serial.available()) {
    int val = Serial.parseInt();
    if (check && val) {
      target = val;
      check = false;
    } else if (!val && !check) {
      check = true;
    } else {
      target = 0;
    }
  }
}

void driveMotor(float spd) {
  spd = int(max(min(100, spd), 0));
  esc.write(map(spd, 0, 100, 0, 180));
}

float encoderVel() {
  int cPos = myEnc.read();
  float aSpeed = float(cPos - encPos) / PPR / DT * 1000;
  encPos = cPos;
  return aSpeed;
}

System Schematic (it is very scuffed):

Project Image:

Video of problem:

Sorry if this isn't the right category, I've never posted in a forum.

A good reason to post photos of your entire project.

Sorry about that. Edited the post to include it

Hi, @brightknight
Welcome to the forum.

Have you tried JUST your encoder on its own with the example code from the library?

Although its a bit trivial, the two pins from the encoder are not direction an step, they are output A and B.

Encoder myEnc(dirPin, stepPin);

The encoder is called a quadrature encoder.

This link shows how they work and signal speed and direction
https://dronebotworkshop.com/rotary-encoders-arduino/

The datasheet of your encoder also explains.

What speed is the problem occurring at (RPM)?
What speed is your encoder capable of?
What is the encoders output frequency at the problem RPM?
Can you change the PPR of your encoder?

What does the ESC manual say

then beep in the A tone for the motor

mean.

Thanks.. Tom.. :smiley: :+1: :coffee: :australia:

Hi,

How do you calculate speed, what is the time base?
How do you keep a constant time base?

Google;

arduino encoder calculating velocity

Tom.. :smiley: :+1: :coffee: :australia:

Yeah, those encoder pin names were basically just a holdover from my last project that used an encoder. I have tested the encoder just on its own, and looking at it right now it looks like I didn't set the PPR correctly, so I need to fix that. Otherwise, it looks like the encoder is working fine.

The problem is occurring at 15% motor power. I unfortunately can't get the RPM since the serial monitor seems to pause then the motor stops, giving me no encoder reading, and I have no idea if 15% of maximum theoretical motor RPM would be a good estimate.

At 2048 PPR, the encoder is capable of 7500 RPM, and at the intended PPR of 512 it's capable of 15000 RPM. The encoder does have changeable PPR.

Based on the ESC manual, I think that beeping in the A tone means the ESC somehow entered throttle range calibration, or it's receiving strange controller signals from the Arduino, but I may be completely wrong on that.

I have a delay of DT ms in the loop, and that function is called once per loop. Kinda a lazy way to do it, and honeslty may be part of the problem. I don't guarantee that the time interval is constant, I kinda just assume it's roughly constant with that delay.

It looks like my 4 switch in the encoder doesn't work properly, so I can't actually set the encoder to a PPR capable of 15000 RPM. Would spinning the encoder too fast for it's rated velocity cause the encoder to wig out the Arduino?

After recording the motor spinning, my best estimate for the RPM that the problem is happening at is 1200 RPM. This corresponds to a pulse rate of about 15.2 kHz for the output frequency, assuming the frequency is just RPM * PPR. I also recorded a video of the problem which is now in the main post.

Alright, I think I found my solution. From what I could tell, the encoder was pulsing too fast for the Arduino, which I think caused the the PWM signal controlling the motor to stutter or something, causing an invalid signal to be sent to the ESC. This is what caused the motor to play the tones it did; the ESC received an invalid controller signal. I decreased the PPR of the encoder to 192, and it seems to be working fine. Thank you Tom for asking what the encoder frequency was at the RPM the motor was running at, as that was what allowed me to figure this out.

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