An Arduino gaussmeter with a 49E Hall effect sensor
Beginner
Or see every lesson.
- 455parts on the bench
- 43boards running now
- 1.00xreal time, on every board
A Hall effect sensor feels a magnetic field. Hold a magnet near one and its output changes. They are how a bike computer counts wheel turns, how a laptop knows its lid is shut, and how a brushless motor knows where its rotor is. There are two kinds: switches, such as the A3144, that only say "magnet or no magnet", and linear sensors, such as the 49E, that say how strong the field is and which pole it is.
The circuit above is an Uno with a 49E linear sensor on A0 and a 16x2 I2C LCD. Drag the magnet toward the sensor and the screen shows the field in gauss and which pole is facing it. Click the magnet to turn it around. It runs in your browser on the sketch below.
What you need
- An Arduino Uno
- A 49E (SS49E) linear Hall effect sensor
- A small magnet
- A 16x2 LCD with an I2C backpack
- A half-size breadboard and jumper wires
Wiring
| Part and pin | Goes to | Note |
|---|---|---|
| 49E VCC | 5 V | Printed face toward you: VCC is the left leg |
| 49E GND | GND | The middle leg |
| 49E OUT | A0 | The right leg; an analog reading |
| LCD SDA and SCL | A4 and A5 | Backpack address 0x27 |
| LCD VCC and GND | 5 V and GND |
Half the supply, plus or minus the field
With no magnet near, the 49E puts out half its supply, about 2.5 V on 5 V, so A0 reads about 512. A south pole at the printed face pushes the output up, and a north pole pulls it down, by about 1.4 millivolts for every gauss. The output stops about 0.95 V from either rail, so the sensor reads up to roughly 1100 gauss each way.
One step of the Uno’s ADC is 4.89 millivolts, so each step is about 3.5 gauss. That is the meter’s resolution, which is plenty for telling magnets apart.
The sketch, step by step
A real 49E’s zero can sit anywhere from 2.25 to 2.75 V, so the sketch does not assume 512. At power-on it averages 64 readings with no magnet near and calls that zero; in the circuit above it prints "zero at count 512". Then every quarter second it averages eight readings, subtracts the zero and divides by 1.4 mV a gauss.
Bring the magnet to 10 mm from the face and the serial monitor prints "field 230 G": a south pole. Flip it and move it to 15 mm and it prints "field -90 G": a north pole, weaker because it is further away. Push it to 3 mm and the output hits the rail, so the line ends in "(over)" and the LCD says "over range".
void loop() {
long raw4 = readQuarters(8);
int raw = (int)(raw4 / 4);
bool over = raw <= LOW_END || raw >= HIGH_END;
long gauss = (raw4 - zero4) * UV_PER_COUNT / 4 / UV_PER_GAUSS;
long size = gauss < 0 ? -gauss : gauss;
if (size > peak) peak = size;
// ...the LCD shows size, NORTH or SOUTH, and the peak
Serial.print("field ");
Serial.print(gauss);
Serial.println(over ? " G (over)" : " G");
delay(250);
}Linear or switch: which one you want
Use a linear sensor like the 49E to measure a field, sense a position, or read a joystick or a throttle without contacts. Use a switch like the A3144 to count things going past: wheel turns for a speedometer or RPM, a door opening, a motor’s position. The switch has a digital output, so you read it with digitalRead() or an interrupt, and it needs a pull-up resistor on its output.
Both are on the Mokxi bench. The A3144 answers only one pole, the south, which surprises people the first time.
Try it in the editor
Turn it into a door sensor: light an LED when the field is above 100 gauss. Or into a contactless knob: map the field from -300 to 300 gauss onto an LED’s brightness, and slide the magnet to dim it.
Common mistakes
Reading a linear sensor with digitalRead(). Its output sits at half the supply, which a digital pin reads as whatever it likes. Use analogRead().
Assuming the zero is exactly 512. Zero it at power-on with no magnet near, as the sketch does.
Holding a strong magnet right against it. A neodymium magnet pins the output at a rail from several millimeters away. The useful range of a 49E is a centimeter or two.
The legs the wrong way around. With the printed face toward you, the legs are VCC, GND, OUT from left to right.
Questions
How do I connect a Hall effect sensor to an Arduino?
For a linear 49E: VCC to 5 V, GND to GND and OUT to an analog pin, read with analogRead(). For a switch such as the A3144: VCC, GND and OUT to a digital pin with a pull-up, read with digitalRead().
How do I measure RPM with a Hall effect sensor?
Use a switch-type sensor such as the A3144, put a magnet on the wheel, and count the pulses on an interrupt pin. Pulses in one second times 60 is the RPM for one magnet.
Why does my Hall sensor read about 512 with no magnet?
That is right for a linear sensor: its output rests at half the supply. The field moves it up for a south pole and down for a north pole.
What does Mokxi not model on the 49E?
The zero and the sensitivity drifting with temperature, the spread between parts, noise, and fields that are not straight along the sensor’s axis.
Keep going
Build this for real
Open the editor, change a value and watch the number move with it. Nothing to install, and no account needed.