Hall Effect Sensor Data

Hi,

I am new to Arduino and starting a project where I've decided to use a Hall Effect Sensor and magnet attached to a bicycle as a switch to control a speedometer in Unity, but I'm having issues with the data it is outputting into the serial port to Arduino. The device outputs 1023 when the magnet is far away, and 0 when it is in close proximity as it should, but this is not consistent. Sometimes it does not change at all from 1023 to 0 when the magnet is close, and other times it just completely inverts the values (1023 with close magnet, 0 when far). Any idea what I might be doing wrong?

Please provide links to the hardware you're using and post a wiring diagram of your setup.

Hardware:
Hall Effect Sensor: $0.95 - Digital Hall Effect Sensor: Magnetism Sensor - Tinkersphere
Breadboard: Half Sized Premium Breadboard - 400 Tie Points : ID 64 : $4.95 : Adafruit Industries, Unique & fun DIY electronics and kits
Arduino Uno

Setup Diagram!

That is a digital Hall effect sensor, so you should be reading it with a digital pin.

Did you read the note in the link, stating that two magnets are required?

Output toggles when a north followed by south pole of a magnet are nearby. Note: Requires both a north and south pole to function. If mounting on a wheel, you will mount 2 magnets on the wheel diametrically opposite from each other so that the north and south pole both pass the sensor and toggle the input high and low.

Most pushbike speedos use a magnet and reed switch . There are lots of cheap speedos on eBay - you could buy one and just use the sensor parts, which would be easy to use and fit

You don't need two magnets. You need to have both magnetic poles passing the sensor! This can be done with a single magnet, but also with two magnets where one presents the north, the other the south pole.
This can explain why you see the sensor "reverses" it's output at times or see it not react at all: you're probably holding the magnet the other way around (which apparently matters for this sensor):

Output toggles when a north followed by south pole of a magnet are nearby. Note: Requires both a north and south pole to function.

So if you move the magnet north first along the sensor, it'll react. When you move it south first it does not react the first time, but will reverse it's reaction the second time: the first half pass, the south pole, is ignored - the north pole in the second half is registered, then when the south arrives of the second pass it reacts.

Furthermore, this sensor toggles upon passing of a magnet and then stays in that position. So it's not HIGH when close LOW when far away: it's HIGH after one pass (north then south), LOW after the second pass, HIGH after the third pass, etc.

I used two magnets and the data the output was no different. I think wvmarle is right, the sensor likely has been reversing because I was most likely holding the magnet the other way around.

Though I'm only in the prototyping phase of the project so if any has other suggestions for achieving that in Arduino that would I'm open to hearing them. Right now the idea is to use a bike with a stationary trainer attached. A magnet would be on the back wheel and when it passes close by the sensor it would track that as a full rotation to determine speed.

Is this even the correct type of sensor for your project?
I was a bit surprised to read the description, I've before worked with sensors that just detect the presence of a magnetic field. Hold a magnet close, no matter north or south pole, it switches on. Remove the magnet, it switches off. Works fine for detecting rotation as well, as long as you poll fast enough (or use interrupts).

I'm just using it to track the rotation, so this sensor should be able to do the job. I'm just prototyping, so I'm willing to try out other methods to see what would work best. Would a reed switch work better?

Major difference: a reed switches on when there's a magnet present, off when not. So you have to poll it constantly, or if your movement is very fast you have to use interrupts to not miss ticks.
This sensor changes state upon a magnet passing, so you have to look for state change (high to low, or low to high) to detect a passage. Makes your code slightly more complex but gives you a full rotation to read the sensor, not just the short time of it passing.
So whether it's better for your project - hard to say.
Your sensor is billed for detecting rotation, and it will be very easy to read (no fast polling needed - though you still need that if you want to measure the speed as you need to know when exactly the change took place). Having your magnet attached to the wheel in a fixed orientation will make it work predictably.
Do start using a digital pin and digitalRead() instead of analogRead(). You'll simply get a 1 or 0, HIGH or LOW signal, which is exactly what the sensor puts out. A digitalRead() is much faster than an analogRead().

Have you got a (pull up) resistor in your hall sensor circuit ? search these forums for this aswell :slight_smile:

Beoir:
Have you got a (pull up) resistor in your hall sensor circuit ? search these forums for this aswell :slight_smile:

The sensor as linked to in #2 doesn't need a pull-up or pull-down resistor.

A reed switch indeed does need one, but you can just as well use the internal pull-up of the Arduino. That's what they are for :slight_smile:

You don't need both poles of the magnet to trip this sensor, just the field lines to reverse, which
means having the sensor side-on to the magnet's end.

You can get different varieties of hall-switch, some need both polarities of field, some don't, some
are more sensitive than others, some have loads of hysteresis and some have less (they all have
hysteresis).

Or you can go for a true analog sensor (hall sensors are inherently noisy, so be aware of that)