# Rotary Encoder Skip Steps When It connected with microbrushless dc motor

Joined Sep 28, 2021
264
Dear all,
Referring to this video, I have bought the same photoelectric rotary encoder.

In the picture below, I have attached a microbrushless DC motor to the rotary encoder to control using PID in Arduino. When I tried to write a simple program to run both encoders together with a fixed PWM to be fed into the microbrushless DC motor and without PID, the encoder keeps skipping steps.

I am certain that the encoder cannot handle small high frequency vibrations from the microbrushless DC motor because when I disconnect the DC motor and tried to rotate it by my hand, it works perfectly fine. I have also give it a recommended voltage of 5-12V DC using a lithium ion battery.

Is this photoelectric rotary encoder not able to handle small high frequency vibrations? Is there any other encoder recommendation that is not so susceptible to vibration noise?

A lot of highly efficient code is posted at playground.arduino.cc/Main/RotaryEncoders , and all of them I tried to run using arduino but still the encoder keep skipping steps when undergo mini vibrations from the mini microbrushless motor. If anyone has this encoder at home (mine is 600 steps/rev) , you can try to attach a low power microbrushless motor there and the encoder readings will jump up and down and will skip steps. Not sure what can I do to resolve it

Mine one after a 10seconds, can accumulate a 200 steps skipped from a 600 steps/rev encoder, very disapointing after 2 days fixing this

Last edited by a moderator:

#### Jerry-Hat-Trick

Joined Aug 31, 2022
207
Are you able to check the proportion of steps skipped when going at different speeds? I suspect that the quadrature encoder is doing its job but the Arduino may be missing steps because you are asking it to do other things - like control the PWM to the motor? Maybe you could have another Arduino running in parallel also counting the steps at the same time. Let both count the steps and turn on an LED when they have counted up to a big number to see if the one that is doing nothing else gets there first! And which Arduino are you using? I think the UNO has a clock frequency of 16MHz whilst the DUO has 48MHz and there are versions (and other MCUs) which have much higher clock rates. The standard Seeeduino XIAO has 48Mz, is cheaper and I've done 70k A/D conversions per second compared to less than 9k with a UNO.

#### KeithWalker

Joined Jul 10, 2017
2,611
This sounds like a power supply problem to me. Can you supply a diagram of how you have things wired together? Are you using seperate supply wiring for each device right back to the power source? Do you have enough capacitive supply smoothing to maintain the voltage during current surges? Are you using twisted or shielded wiring between the encoder and microprocessor?

Joined Sep 28, 2021
264
This sounds like a power supply problem to me. Can you supply a diagram of how you have things wired together? Are you using seperate supply wiring for each device right back to the power source? Do you have enough capacitive supply smoothing to maintain the voltage during current surges? Are you using twisted or shielded wiring between the encoder and microprocessor?

hi, I have updated all my connections in my post, I have tried the code that is from the video and other efficient code from playground.arduino.cc/Main/RotaryEncoders . This time I try to REALLY filter out the problem, I disconnect the entire motor and just left a very light straw attached to encoder shaft. However, when I use my hand to slightly rotate the stick violently a bit, the reading skips a lot. I am really not sure what to do anymore as I have been on this problem 24/7 for 3 consective days. I really really really feel like giving up.

And please, if anyone has this encoder at home, attach a straw like me in the picture and slightly rotate the straw violently, you will see the readings very weird and skip steps. Can anyone just try this if you all have this photoelectric encoder at home?

You guys should just do this, and you will know what I have experienced

Seriously, I feel like giving upppppppppppppppp. The encoder is 5V to 12V recommended voltage and I applied a good low impendence 8V using lithium ion already. I really tried so hard on this and yet failedd.

#### KeithWalker

Joined Jul 10, 2017
2,611
Although you have supplied a diagram and a number of photos, I still have no idea of how you have things connected together. The diagram shows only an arduino and the encoder. The photos show the arduino being powered by the USB connection and a two cell battery for the motor, but no information on the motor wiring. Can you make a wiring diagram showing how everything is connected together?
I would like to try to help, but there is just not enough information for me to make any valid suggestions.

Joined Sep 28, 2021
264
Although you have supplied a diagram and a number of photos, I still have no idea of how you have things connected together. The diagram shows only an arduino and the encoder. The photos show the arduino being powered by the USB connection and a two cell battery for the motor, but no information on the motor wiring. Can you make a wiring diagram showing how everything is connected together?
I would like to try to help, but there is just not enough information for me to make any valid suggestions.
There is no motor. It is just an encoder powered by the lithium ion battery and connected to arduino. That's it. If you have one of these encoders in this video here, please just try it and you will be really disappointed to see the bad readings. I am not sure what to do anymore. The encoder has 4 wires and I have shown in the schematic how everything is connected.

Please Please Just Try if you have encoder,only by experience you will know what I meant

Joined Sep 28, 2021
264
Although you have supplied a diagram and a number of photos, I still have no idea of how you have things connected together. The diagram shows only an arduino and the encoder. The photos show the arduino being powered by the USB connection and a two cell battery for the motor, but no information on the motor wiring. Can you make a wiring diagram showing how everything is connected together?
I would like to try to help, but there is just not enough information for me to make any valid suggestions.
May I ask if you have this encoder at home first, photoelectric rotary encoder, it is very common in martket? I am on the verge of really giving up and throwing this encoder straight to the bin. Surprisingly, I thought this thing will work and yet spent 3 days fixing it without any results. I have to ask God Why. Yes, it works when there is no vibration. When it has slight vibration, the reading becomes really really bad

#### Sensacell

Joined Jun 19, 2012
3,096
I am personally unfamiliar with the encoder libraries for the Arduino, but I can say that I have considerable experience with encoders and PIC chips.

In general, a software approach has distinct and painful performance limitations.
Any polled concept is almost hopeless unless the MCU is doing almost nothing else besides servicing the encoder routine.
The best is an interrupt driven encoder routine, but even here, there are performance limitations.
The interrupt code has an unavoidable latency, at high angular speeds and high encoder counts, the software will always miss counts- nothing you can do.

A better idea is to use hardware- a dedicated encoder counter chip, or an MCU chip with a dedicated encoder peripheral, like the Microchip QEI - which is what I always end up doing.

Determine the maximum encoder frequency your application needs - remember you need to process both edges of the signal on two channels, it all adds up to a rather high frequency. Take the reciprocal of the frequency and you see there is no time for any code to run.

#### MrChips

Joined Oct 2, 2009
27,674
I can sense the frustration and wanting to bin the encoder.
But have you given any thoughts that the problem perhaps is in the software?
Have you investigated how you can fix the software?

Joined Sep 28, 2021
264
I can sense the frustration and wanting to bin the encoder.
But have you given any thoughts that the problem perhaps is in the software?
Have you investigated how you can fix the software?
Hi MrChips, ermm i dont think so, these are the best encoder codes out there at playground.arduino.cc/Main/RotaryEncoders and others have none of this issue, perhaps I need to get another hardware system, maybe arduino nano to do that because arduino uno can't handle. I am not sure what to do now to be honest

Joined Sep 28, 2021
264
Microchip QEI
Hi about the Microchip QEI, can you send me an amazon link to it, I want to see how it looks and can it be used together with Arduino Uno ?

Thank you, much appreciated

#### Ya’akov

Joined Jan 27, 2019
6,855
Which library are you using? Are you using interrupts?

#### ericgibbs

Joined Jan 29, 2010
16,761
A better idea is to use hardware- a dedicated encoder counter chip, or an MCU chip with a dedicated encoder peripheral, like the Microchip QEI - which is what I always end up doing.
I agree with @Sensacell, I have found that a dedicated up/down quadrature counter is required with an encoder.
I use the PIC 18F2431 as an example of QEI operation.
Attached is PDF to show what internal hardware is involved in the quadrature capture.
E
BTW:
Could you define more precisely what you mean by:
undergo mini vibrations from the mini microbrushless motor

#### Attachments

• 218.4 KB Views: 13

#### MrChips

Joined Oct 2, 2009
27,674

#### djsfantasi

Joined Apr 11, 2010
8,652
perhaps I need to get another hardware system, maybe arduino nano to do that because arduino uno can't handle.
A Nano won’t be any better than an Uno.

Which library are you using? Are you using interrupts?
The Arduino Digital Playground hasn’t been updated in 4 years. The links to the libraries that article uses may be to obsolete code.

Plus, there are several examples of code contained therein. Which code are you using?

There is code using interrupts. And there is code that debounces the encoder output. And more… The specific code you are using may be why you’re experiencing difficulties.

The fact that you’re using external pull-up resistors may be an indication that the article may not contain “the best codes”.

I also think this may be a software problem particularly because you’ve tried simplifying the hardware and the problem remains unchanged.

May I suggest posting the actual code you’ve loaded onto the Uno? Post it using code tags. Start with [ CODE] and end with [ /CODE] (extra space added to show tags. Remove them when posting code)

Joined Sep 28, 2021
264
MODERATOR NOTE: Please use CODE tags when posting code. You can use the </> Code option from the menu, above. (tags added)

I have a MPU 6050 connections and also Arduino code taken from a website here which includes a low-pass filter activated for this MPU 6050. It seems like this MPU 6050 works perfectly well when it has low frequency vibration such as vibrating with my hand. However, the angle readings are jumping all over the place **(can be 90 degrees difference-A lot)** when I turn on the microbrushless motor (high frequency small vibrations) . Any ideas on how to solve it ? . Note: The motor is not even connected to Arduino and its just powered directly with battery.

**If anyone has this gyroscope, can you try this experiment using a MPU 6050 and a dc microbrushless motor with Arduino to test whether you all get the same result?**

MPU6050 Arduino Code:
Arduino code is below
#include<Wire.h>

int16_t AcX,AcY,AcZ,Tmp,GyX,GyY,GyZ;
int minVal=265;
int maxVal=402;
double x;
double y;
double z;
void setup(){
Wire.begin();
Wire.write(0x6B);
Wire.write(0);
Wire.endTransmission(true);
Serial.begin(9600);
}
void loop(){
Wire.write(0x3B);
Wire.endTransmission(false);
int xAng = map(AcX,minVal,maxVal,-90,90);
int yAng = map(AcY,minVal,maxVal,-90,90);
int zAng = map(AcZ,minVal,maxVal,-90,90);
x= RAD_TO_DEG * (atan2(-yAng, -zAng)+PI);
y= RAD_TO_DEG * (atan2(-xAng, -zAng)+PI);
z= RAD_TO_DEG * (atan2(-yAng, -xAng)+PI);
Serial.print("AngleX= ");
Serial.println(x);
delay(400);
}

Last edited by a moderator:

#### Ya’akov

Joined Jan 27, 2019
6,855
The first thing I would do is clean up your breadboarding. In particular, shorten the motor wires as much as practical and lightly twist them together up to where the battery connection has to be made. Get a battery holder, and a switch to control the power more consistently.

In general, wires that are too long—or too short—can be the source of a lot of mysterious problems, particularly in the presence of vibration. I can’t tell how the sensor is connected on its end but if it is with Dupont wires, high speed vibration is not your friend. Make the connection more certain, solder leads to the board if necessary. The other end is OK with the header pins so long as you have some strain relief between the breadboard and the sensor. Taping it down is OK.

You will save yourself a lot of time and heartache is you spend the time making your breadboarding neat and orderly and mechanically sound. Mysterious problems about when you use the chaos approach.

The second thing is placing a .1μF (non-polarized, i.e.: ceramic, tantalum, etc.) capacitor across the motor leads as close to the motor as possible, preferably right on the terminals.

Joined Sep 28, 2021
264
The first thing I would do is clean up your breadboarding. In particular, shorten the motor wires as much as practical and lightly twist them together up to where the battery connection has to be made. Get a battery holder, and a switch to control the power more consistently.

In general, wires that are too long—or too short—can be the source of a lot of mysterious problems, particularly in the presence of vibration. I can’t tell how the sensor is connected on its end but if it is with Dupont wires, high speed vibration is not your friend. Make the connection more certain, solder leads to the board if necessary. The other end is OK with the header pins so long as you have some strain relief between the breadboard and the sensor. Taping it down is OK.

You will save yourself a lot of time and heartache is you spend the time making your breadboarding neat and orderly and mechanically sound. Mysterious problems about when you use the chaos approach.

The second thing is placing a .1μF (non-polarized, i.e.: ceramic, tantalum, etc.) capacitor across the motor leads as close to the motor as possible, preferably right on the terminals.
Hi. Thanks for your reply. I will definitely try it out including your 0.1uF capacitor suggestion. Do you know any other gyroscope sensor besides this common MPU 6050? I have tried searching for BNO055 but sadly it's not available on sale in my country

#### MisterBill2

Joined Jan 23, 2018
13,722
One other thing to try is running the motor at a much slower speed for testing. If the system counts correctly at the much slower speed then you can assume that it is a response time issue.

The encoder errors from vibration usually appear as excess counts, not missing counts.

Joined Sep 28, 2021
264
The first thing I would do is clean up your breadboarding. In particular, shorten the motor wires as much as practical and lightly twist them together up to where the battery connection has to be made. Get a battery holder, and a switch to control the power more consistently.

In general, wires that are too long—or too short—can be the source of a lot of mysterious problems, particularly in the presence of vibration. I can’t tell how the sensor is connected on its end but if it is with Dupont wires, high speed vibration is not your friend. Make the connection more certain, solder leads to the board if necessary. The other end is OK with the header pins so long as you have some strain relief between the breadboard and the sensor. Taping it down is OK.

You will save yourself a lot of time and heartache is you spend the time making your breadboarding neat and orderly and mechanically sound. Mysterious problems about when you use the chaos approach.

The second thing is placing a .1μF (non-polarized, i.e.: ceramic, tantalum, etc.) capacitor across the motor leads as close to the motor as possible, preferably right on the terminals.
Well, I have tried all your method, but not working. If you have this MPU 6050 at home, can you power it up and put something to vibrate it (Maybe a motor), and see whether it has this super-weird readings (+-90 degrees even sometimes).

Or can you share your MPU 6050 arduino code so that I can run it and test whether it is the Arduino Code issue ? I really need some help on this haha, I am surprised that no one experience this before?

Thank you for reading