Control DC motor on/off with Hall Effect Sensor counts

I'm trying to control the number of rotations of a 24V DC motor using the two latching Hall Sensors. I'm using interrupts to count and getting far too many counts (1.4x +). Is my code faulty or my approach?

/* Motor (not brushless) with two latching Hall Sensors (A3282)
Sensors are triggered twice for each motor shaft rotation.
One sensor cycle is about 14.1ms (2 cycles / rotation) or
appx. 2128 RPM for the motor.
Signal stays low for about 3.9ms, high for 10.2ms
Should record about 71 half rotations per second.
I’m getting far greater than that.

volatile long int halfRot; //half rotation
volatile int flagA; //flag for sensor A
volatile int flagB; //flag for sensor B
int oldRot=0; //prior rotational 1/2 count

void setup()
attachInterrupt(0, hall_A, FALLING);
attachInterrupt(1, hall_B, FALLING);
digitalWrite(2, HIGH);
digitalWrite(3, HIGH);

halfRot = 0;
flagA = 0;
flagB = 0;

void loop() {
if(halfRot != oldRot) {

void hall_A() {
if(flagB) {

void hall_B() {
if(flagA) {

What is the magnet setup that the hall sensors are detecting?

The motor is commercially produced. The magnet setup is unknown.
However, O-Scope traces provide the following:
10.2ms from sensor high to sensor low.
3.8ms from sensor low to sensor high.
Again - these are latching sensors.

I don't understand why your code is attempting to do quadrature decoding - are
the sensors meant to be quadrature? The M/S ratio is wrong if so.

Do you have photo from the oscilloscope showing A and B waveforms?

Waveform doesn't look quite like quadrature (had to google it). What is m/s ratio?

Scope trace attached. Upper trace is A, lower trace B.

I will use relays to chose motor polarity and will therefore know motor direction. I need to be able to run the motor a specified number of revolutions at specified times. The motor output shaft is geared down 236:1 - then meshed with a slew bearing.

This motor drives a solar tracker for a solar array. I must have an interrupt that counts the Hall sensors reliably to be able to track the solar azimuth. With 2 counts per motor revolution and a 236:1 gear reduction to the output shaft; 10 revolutions of the output shaft should provide 2 x 236 x 10 = 4720 counts. I'm getting about 40% too many counts or around 6600 counts. I've examined my code and the O-scope trace and cannot understand where the extra counts are coming from. Any insight would be very much appreciated. My goal is to be tracking the sun by Xmas day - assuming there is a sun; haven't seen much of it lately. Thanks.

Hi, have you tried slowing the motor down and seeing if the accuracy improves?
Slow down I mean by a factor of 5 or 10 so you can see what is happening.
It looks like you are tracking the sun with an algorithm, rather than track the sun using light sensors.
I understand you may need that speed and gear reduction to get torque, but for problem solving reduce the speed and see what your counts are.
If you are not quadrature encoding why two sensors on the shaft, only one on the motor shaft speed should be enough.
Also a circuit diagram would be nice and a picture of your project, the diagram can even be a hand drawn photographed effort.
Hope it helps.. Tom..... :slight_smile:

Tracking the sun with light sensors is unreliable - especially when overcast.

A powered slew bearing is being used to rotate the tracker. The motor came with the slew bearing and has the two latching hall sensors from the factory. My results when reading only one sensor are worse. My coding requires that I see both sensors in sequence before recording a count. Frustrating is that today I got more phantom counts than yesterday (15.x to 1.9x vs 1.4x yesterday).
I would send a picture but my phone (camera) has failed on me - battery died and won't wake up. Overnight charge should resolve that. Attached is my circuit to attempt to read and count the sensors. My fallback is to fabricate an optical encoder but everything I read about hall sensors suggest they should be superior to any other method.
Do the scope traces provide any clues?

the circuit

Hi, just looking at your circuit, are you putting 12V pulses into the arduino, 5V is what the inputs are designed for, unless micro is 3.3V supply.
So change the supply for the sensors to 5V.

Tracking the sun with light sensors is unreliable - especially when overcast.

I could get on a soapbox about that, when you are tracking the sun, you are pointing in the direction of maximum energy.
When it is cloudy the sensors will still find the direction of maximum energy.
When you track to an algorithm if you get localised cloud over the sun, and maximum is now in clear bit of sky, the panels will not be pointing in that direction.
But a sensor system will ALWAYS find direction of maximum energy, the accurate clock it uses is free and always right. my soapbox now..

By the way you haven't shown or said if the neg or gnd of the supply for the sensor is connected to the gnd of the arduino.

Tom..... :slight_smile:

There are 3 power supplies in this setup.
24V power supply for the motor
12V adjustable bench power supply for the Hall Sensor
5V (USB) for the arduino

I have also tried a 9V battery putting out about 8V for the Hall.

The bench PS has connections for +, -, gnd. I have not connected gnd to anything since I didn't really know where to connect it.

Interesting what you say about sun tracking with photo sensors. From what I have read and from anecdotal info, is that the sensor gets confused with overcast skies and keeps searching. Could it be that there is an optimal arrangement of the sensor unknown to these people?

I never did care for how noisy my scope trace looked. However, I recognize that a real-world square wave isn't going to be perfect.

Then again - there is another thing that bothered me from the beginning but dismissed as being paranoid. The 50 foot motor wire harness that will be installed at the solar tracker sat coiled up on my desk next to the motor - with included sensors.

Tonight I fabricated a 2 foot wire harness and tried again. The scope trace is much cleaner - although sensor A still has a curious dropout. using both sensors, my count is within 1/2% of the expected count. Using sensor B alone is not quite as good; 1.5% - 2.5% phantom counts.

Hi, your latest trace shows that you are now putting 9V pulses on the digital input.
Supply the hall effect with 5V supply and you will then have 5V pulses which will make the arduino inputs safe from damage.

The noise is worrying, what do you get on your cro if you just look at the hall device power supply, not the output, just the supply and ground?

Tom..... :slight_smile:

The power supply voltage looks quite stable. The operating range of the sensor is 5-24V with nominal of 12V. This is why I'm operating at 12V.
I'm unsure where you see 9V. I see 5.4V Take a look at this measured trace.

HI, no problems, its lower than in reply 11, note the noise has decreased, now can you give us a trace of the DC power supply that goes to the sensors please.
I'm not worried about the voltage level applied to the sensors, I'm concerned that your are putting more than the 5V rated input into the arduino..................
The gnd on the lab supply you don't need to worry about, however have you got the negative of the lab supply connected to the negative or gnd as marked on the arduino.
I assume all your scope traces are using lab neg as reference.

Tom... :slight_smile:

A little off topic but ....

I have experimented a bit with slew drives, I think stepper motors would be a better (simpler) solution than the dc motor/sensor route

This is the power being provided to the Hall Sensors. The bench PS is set at 12V and the trace seems to show 10V. In any case, the signal coming from the Hall seems to reach 5.4V max.
Since, in this test setup, I am only using the arduino to run a program and receive a signal from the Hall Sensors, I have connected nothing to the arduino gnd. The motor is provided 24V with a switching power supply and the sensors are powered by the bench PS.
Being paranoid, based on what I've read, I have been loathe to use the probe ground clip. I did connect it briefly to the 24V PS ground and saw no difference in the signal and therefore saw no need to use it. I have since done the same with the bench PS - no change in the signal.

Stepper motors - That bridge was crossed many weeks ago when the motorized slew bearing was purchased. I'm trying to work with what I have. Also - we are swinging nearly a ton of mass.
The linear actuators have not yet been purchased as we are concentrating on rotation at the moment. When that is working, we will want to tilt the array as well. We have calculated the need for appx 48" of travel as we will want to be able to flatten the array in high wind. An open question now is electric or hydraulic - and of course electric would allow a choice of hall effect or stepper motors. I have also considered a MEMS accelerometer from which elevation can be calculated.

Tom -

A note on my earlier success in cleaning up signal noise by removing the coil of wire on my desk. I uncoiled this cable and laid it out on the floor to confirm that the signal would still be okay. The noise returned. The coil was not he problem - it is the power lines jacketed with the signal lines.

I then strung out 36 feet of CAT3 telephone UTP and used one pair for the two signals. The signal was improved again although not as clean as my 2 foot wire harness. I suppose CAT5 would be better but I am now considering using two coax lines for signal - haven't tried it yet; multiple priorities with holidays beginning to interfere.

  • Gil

Hi, Gil, okay on the noise, must be coming from the motor current in the associated wiring.
Have you tried a .01uF capacitor across each of the two signal wires to gnd to possibly filter out the noise without causing any distortion to the waveform.
The gnd on the lab supply you don't need to worry about, however have you got the negative of the lab supply connected to the negative or gnd as marked on the arduino.

Tom..... :slight_smile:

by the way Merry Christmas..