Linear Magnetic Hall Sensors
The Linear Magnetic Hall Sensors project demonstrates how to interface linear Hall sensors with an Arduino Nano to measure magnetic field strength. Linear Hall sensors are used in various applications such as position sensing, speed detection, and current sensing. This project provides a basic setup for reading analog values from a linear Hall sensor and displaying them on the Serial Monitor.
How It Works
A linear Hall sensor such as the AH49E — found on the KY-024 carrier — differs fundamentally from the digital switch used in the KY-003. Instead of a Schmitt trigger producing HIGH or LOW, it outputs a continuous voltage proportional to magnetic flux density, so it measures the field rather than merely detecting it.
With no field present the output rests at roughly half the supply voltage. A north pole drives it above that midpoint, a south pole below it. This ratiometric, bipolar behaviour means a single sensor reports both the strength and the polarity of the field.
The Arduino Nano samples with a 10-bit ADC, so analogRead() returns 0–1023 across 5 V. With the quiescent point near 512 counts, the usable swing in each direction is about 512 counts, though in practice a small magnet moves it far less.
The KY-024 also carries an LM393 comparator and a potentiometer, so it offers a digital output alongside the analog one — convenient when you want both a proportional reading and a threshold trip from the same part.
Components Needed
- Arduino Nano
- Linear Magnetic Hall Sensor module
- Arduino Nano
- USB cable for programming and power
- Arduino Nano
Wiring to the Arduino Nano
Connect AO to A0, optionally DO to D2, VCC to the 5 V rail and GND to ground. Because the output is ratiometric — it scales with supply voltage — power the sensor from the same rail that feeds the ADC reference, or the midpoint will not sit where you expect.
At 5 V the quiescent point sits near 2.5 V, giving a symmetric swing in both directions.
Always record the no-magnet baseline at startup rather than assuming exactly mid-scale. Device-to-device variation of a hundred counts or more is normal, and subtracting a measured baseline removes it.
| Module pin | Arduino Nano pin | Function |
|---|---|---|
| A0 / AO | A0 | Analog field strength, centred at mid-supply |
| D0 / DO | D2 | Comparator trip, threshold set by the pot |
| VCC / + | 5V | Supply |
| GND / − | GND | Common ground |
Build and Upload
Connect the Arduino Nano to your computer via USB.
Open the Arduino IDE and paste the provided code.
Upload the code to the Arduino Nano.
Once the code is uploaded, open the Serial Monitor.
Observe the Serial Monitor to view the linear Hall sensor values displayed in real-time.
Introduce magnetic objects near the sensor to observe changes in sensor readings.
Example Code
Measuring field strength and polarity against a baseline captured at boot. Upload it with the board set to Arduino Nano and open the Serial Monitor at 9600 baud.
const int HALL_PIN = A0;
const int DEADBAND = 15; // ignore small drift
int baseline = 0;
void setup() {
Serial.begin(9600);
delay(100);
long total = 0; // capture quiescent level, no magnet nearby
for (int i = 0; i < 64; i++) { total += analogRead(HALL_PIN); delay(2); }
baseline = total / 64;
Serial.print("Baseline: ");
Serial.println(baseline);
}
void loop() {
int raw = analogRead(HALL_PIN);
int delta = raw - baseline; // signed: polarity as well as strength
if (abs(delta) > DEADBAND) {
Serial.print(delta > 0 ? "NORTH" : "SOUTH");
Serial.print(" pole, strength ");
Serial.println(abs(delta));
}
delay(100);
}
Applications
A linear magnetic hall sensor turns up in a wide range of projects. These are the uses where it is the right choice rather than a compromise:
- Contactless position sensing, where a magnet on a moving part reports travel
- Current measurement, by sensing the field around a conductor
- Joystick and control-knob position without wiping contacts to wear out
- Distinguishing magnet polarity in sorting and orientation checks
- Proximity sensing with proportional output, so approach speed can be judged
Working with the Arduino Nano
The Arduino Nano is built around the ATmega328P and runs on 5 V logic with 2 KB of SRAM and 32 KB of program flash. These details change how this circuit is wired and what the sketch can do, so they are worth stating plainly before you build.
The Nano shares the Uno’s ATmega328P but adds A6 and A7, which are analog-input only and cannot be used as digital pins.
Its DIP footprint drops straight into a breadboard, which suits permanent sensor builds.
Older clones use the CH340 USB bridge and may need that driver plus the "ATmega328P (Old Bootloader)" processor option.
| Arduino Nano characteristic | Value | Why it matters here |
|---|---|---|
| Logic voltage | 5 V | Matches most hobby modules directly |
| ADC resolution | 10-bit (0–1023) | Sets how finely an analog reading can be resolved |
| Analog inputs | A0–A7 (eight channels, two more than the Uno) | Determines how many analog sensors can share the board |
| PWM outputs | D3, D5, D6, D9, D10 and D11 | Needed for brightness, speed and tone control |
| I²C pins | A4 (SDA) and A5 (SCL) | Fixed by hardware — wiring copied from another board may not match |
| Interrupt pins | D2 and D3 only | Required for counting fast or asynchronous events |
| Serial | a single hardware UART shared with USB | Monitor runs at 9600 baud by default |
Troubleshooting
Most problems with this module fall into a handful of categories. Work through these before suspecting the part itself:
- The reading sits at mid-scale and barely moves — the magnet is too weak or too far; linear Hall parts have a much shorter useful range than digital switches.
- The baseline drifts with temperature — AH49E output has a temperature coefficient. Re-capture the baseline periodically for long-running builds.
- Values change when the supply changes — this is expected from a ratiometric part. Use a stable regulated rail.
- Readings are noisy — average several samples, and keep the signal lead short and away from motor wiring.
- Code written for an ESP board gives odd analog values — the Arduino Nano uses a 10-bit ADC returning 0–1023; rescale any constant taken from a 12-bit example.
- An I²C sensor is not found after copying wiring from another Arduino — on the Arduino Nano I²C is on A4 (SDA) and A5 (SCL).
Taking It Further on the Arduino Nano
Once the basic reading works, where you go next depends very much on which board you are using. These are the directions that suit the Arduino Nano specifically:
The Nano’s DIP footprint makes it the natural choice once a breadboard prototype becomes a soldered build. Mount it on female headers rather than soldering it down, so the board can be recovered if the project is retired.
With A6 and A7 available in addition to A0–A5, the Nano can read two more analog sensors than an Uno. Remember that those two pins are analog-input only — they cannot be used with digitalWrite or as digital inputs.
For battery-powered builds, the Nano’s regulator and USB bridge dominate idle current. Powering the 5V pin directly from a regulated supply and removing the power LED substantially extends runtime.
Notes and Practical Limits
Treat the output as signed data relative to the baseline, never as an absolute number. The sketch above does this, and it is what makes polarity detection possible.
If your application only needs "magnet present or not", the digital KY-003 is cheaper, simpler and far more tolerant of distance. Reach for a linear sensor only when the amount matters.