I have a wheel with magnets on it, passing on X2 hall effect sensors that I use to measure RPM.
I now want to know the direction of rotation but haven't got a clue on how to approach in the programming.
My idea is to place hall sensor A right next to hall sensor B.
If magnets first passes over A and immediately after passes over B then direction would be"right".
If magnets first passed over B and immediately after passes over A then direction would be "left".
Any idea how to put it in code?
Thanks a lot!
Search Wikipedia or the forum for "rotary encoder". There exist several solutions, depending on needs and available resources.
Rotary encoders use a quadrature signal where A pulses and B pulses overlap by 50% on both sides. The description seems to indicate that the pulses from the two hall effect sensors do not overlap.
If the distance between magnets is large compared to the distance between sensors it should be possible to compare the time between A and B signals to the time between sequential A signals. If the AB time is much shorter than the AA time, the order is AB_AB. If the AB time is close to the AA time, the order is BA_BA.
Then it should be not so hard to make both signals overlap. An exact 50/50 overlap is not required.
Your design will depend on how far you allow the wheel to turn before you determine the speed or the direction. Only one magnet means the wheel must make a complete turn. Two magnet, placed opposite, means a half turn, and so on.
That's what I thought at first. Then I tried to design software to do it.
Let's assume the magnets are far apart and the sensors are relatively near each other. How would you convert two separate pulses to a quadrature signal that can simultaneously detect motion and direction?
That is a quadrature signal. Take an edge of one or both signals for the clock and check the other signal for fwd/back move. This will give single or double resolution, with also the other edges quad resolution.
Not when the pulses don't overlap. On both A edges the B input will be LOW. On both B edges the A input will be LOW.
I can see how it might work if the pulses overlapped. The "rising" edge of A would have B LOW in one direction and B HIGH in the other. The "falling" edge would have the opposite.
Perhaps the OP has the option to have the sensors close enough together that the pulses overlap.
Maybe this also helps to get you started.
Here is an example system with a rotating disk and two sensors A and B placed at the 12 o'clock and 3 o'clock position respectively. The disk has a single magnet (red) mounted on it.
The disk is rotating clockwise at 1 revolution per second (1000ms).
It starts with the magnet midway between sensor A and sensor B and the system clock is at 2875ms (to make the example figures easy to work with)
Acurr is the latest ms (millis()) reading from sensor A
Aprev is the previous ms reading from sensor A
and the same for sensor B
Once a full revolution has occurred, you should be able to use a formula, at the time a sensor has just been triggered.
If sensor A has just been triggered:
if ( (Acurr - Bcurr) < (Acurr - Aprev)/2 ) then counter-clockwise else clockwise
If sensor B has just been triggered:
if ( (Bcurr - Acurr) < (Bcurr - Bprev)/2 ) then clockwise else counter-clockwise
This will not work if the sensors are evenly spaced.
Are the magnets equidistant from each other on the periphery of the disc? That is, one at 0° and one at 180°?
@dougp the magnets haven't been placed yet. I can place how many I want, and wherever necessary.
Fair enough. But have you decided how long you can wait to know the speed and direction?
@6v6gt looks like it will work thank you very much.
Could you suggest how to write the code for that? (sorry, I'm a noob ;))
@Paul_KD7HB It is for a test on a concrete truck. Usually RPM goes between 2 and 15 (yes, very slow...).
I think 1 full rotation is max time to wait.
Do you have to build hardware for this project or is it simply a paper exercise where you have submit a diagram of the chosen design and some plausible looking code to support it ?
Anyway, you have to have some code to read the sensors (you could poll them in the loop() or you could trigger interrupts), you capture the time in milliseconds that the sensors are triggered, you have to maintain the latest and previous timestamp from the sensors and you have to apply the appropriate formula, against that data, to derive the direction of rotation and the speed of rotation if that is also required.
Start simple. Read a sensor and, if triggered, print out the time stamp ( millis() ) .
You should make an attempt at coding this and then ask questions when you get stuck. Be prepared to explain your hardware design.
When you give us your attempt at code, can you also please give us a diagram of the magnet(s) and sensor(s) positions on the circle.
I believe with two targets and a minimum of two sensors placed as below you could get the quadrature feature and get two sensing events per revolution if the sensors are placed such that the sequence S1 ON, S2 ON, S1 OFF, S2 OFF is observed.
Also, you could relax the sensor position tolerances by using inductive proximity switches* with wider sheet metal targets - providing the bowl can be modified to take the targets. Maybe the bowl already has some feature (bolt head, cover plate, etc.) that could be sensed by an inductive prox.
A final thought: since we're talking concrete truck here - rough treatment and exposure to weather - I would not use a toy/hobby grade sensor. Spend the money and get some industrial quality components.
This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.