Encoder Library Qestion

Not open for further replies.


Hello i have a question about encoder library i mounted dual bipolar hall effect switch (allegro A1230) on top of motor shaft housing
Motor shaft has 4 magnets mounted N S N S about every 90 degrees. However output is not exactly grey code (not shifted by 90 degrees ).
So my question is: can the library still reliably count rotations or it will not be able to do so. I tried to check and it looks like it is working fine but just want to make sure.
Here is output from logic analyzer
Last edited:
So my question is: can the library still reliably count rotations or it will not be able to do so.

I'd give this a solid "maybe" for working properly. You've got 2 sensors and they seem to be spaced closer than the distance between the N-S poles. So, maybe...
I did some testing it does seam to work fine but i just count by looking at it so i might be wrong by few rotation or 4-8 pulses(motor connected to reducer 50:1 so 3.75-7.5 degrees on the output gear) , do you know in which case it can be inaccurate? Is there a way to change something in library?
Sensors are spaced 1mm apart it is recommended that magnet poles are 2mm in width (according to datasheet). The magnets themselves are 2mm but they are spaced much further away .
Last edited:
do you know in which case it can be inaccurate?

Well, the ideal is 90 degree quadrature. Looks like you've got much less than 90 degrees phase shift, but still enough for the library to work.

If the time between edges get too small, the library might not detect it. But that's probably not an issue. Probably...
Ok i have done the test at absolute maximum motor speed (it was slightly over-volt and with no load connected to the shaft) so in real life i expect rotations to be at least 30% slower. At this rate library looks functional so at slower speeds it should have no problem.
Also i run the test on good old arduino, while the actual project will have teensy lc which is faster then arduino.
Other then that i have no other options the motors original disk encoder got demagnetized and there is absolutely no space to put external rotary encoder so the best thing i could do was to put 4 magnets on motor shaft and mount hall sensor on the case above.
Last edited:
Hmm maybe i spoke too soon here i was testing the motor using arduino + teensy. Arduino reads hall sensors and reports to serial/lcd with 15 ms delay, while teensy goes trough 6 steps of pwm:50 150 180 200 150 50 whith delay of 650 ms between. And it all worked ok until i noticed this glitch in serial monitor. It jumps up and down few values and then jumps 32 values!!! glitch.png

Motor rotates in same direction! only speed changes.
/* Encoder Library - Basic Example
 * http://www.pjrc.com/teensy/td_libs_Encoder.html
 * This example code is in the public domain.
#include <Wire.h>
#include <LiquidCrystal_I2C.h>

#include <Encoder.h>
LiquidCrystal_I2C lcd(0x27,20,4); 

// Change these two numbers to the pins connected to your encoder.
//   Best Performance: both pins have interrupt capability
//   Good Performance: only the first pin has interrupt capability
//   Low Performance:  neither pin has interrupt capability
Encoder myEnc(2, 3);
//   avoid using pins with LEDs attached

void setup() {
  Serial.println("Basic Encoder Test:");
  digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
  lcd.init();                      // initialize the lcd 

long oldPosition  = -999;

void loop() {
  long newPosition = myEnc.read();
  if (newPosition != oldPosition) {
    oldPosition = newPosition;
    lcd.setCursor(0, 1);
    lcd.print("    ");
Last edited:
Ok looks like motor has huge EMF. My logic analyzer looses connection to usb when motor turns on. i was running it at 14 KHz pwm frequency. I have a diode across motor terminals
its rated at 50 ns recovery time is it fast enough?
I got logic a. connected somehow and that's how it looked like. glitch.jpgglitch.jpg
Last edited:
Wrap each motor lead through a ferrite a few times. Put a power filter on the input to the motor power supply. Shield your signal cables and ground the shield at one end.
OK i have a question. Why ground shield only at 1 end i ? Also i have 2 independent systems (they powered from different sorces and not connected in any way) should i connect grounds of thous 2 systems?
Also I added 0.1uf cap across mosfet gate and ground and it fixed almost all of the EMF noise (i can still see some blimps on 2Mhz scans). I don't think its enough to drive mosfet in to linear region maybe i got a bad connection somewhere?
Ground shields at one end to avoid ground loops. Are your two systems intentionally isolated including the grounds? If designed that way then I suppose that may work but otherwise they would commonly share a ground. No pun intended.
Not open for further replies.