Arduino Hall Effect Sensor: A Beginner's Guide to Using an Arduino with a Hall Effect Sensor
Introduction
An Arduino hall effect sensor detects the strength and polarity of a magnetic field. In this tutorial, we will use a
hall effect sensor to control an LED using an Arduino
Uno.
Hall effect sensors are ideal for detecting objects like gears,
screws, or other components for sensing movement e.g. rpm
measurement, position, or proximity. For this to work you typically
embed a small magnet somewhere on the piece to be measured.
The type of hall effect sensor used here (49e) is a linear output
device meaning it outputs an analog voltage proportional to the detected
magnetic field.
In this project, the output of the sensor controls the brightness of
LED, which varies in brightness depending on the magnetic field
strength.
This project shows you operation and usage of a hall effect sensor with an
Arduino, reading the analog output, and mapping it to LED brightness.
What is a 49e Hall Effect Sensor
The 49e hall effect sensor is a basic hall effect sensor module
commonly used with Arduino. Inside the 49e, there is a thin piece of
conductive material like gallium arsenide. When a magnetic field is
passed through this material, it alters the placement of electrons. This
change in electron movement can be measured as a voltage difference.
So the 49e output a voltage 0~5V directly proportional to the magnetic field it experiences.
Unexpected operation of the 49e
The output of the sensor does vary from 0V to 5V in the presence of a magnetic field. So you probably expect a zero reading for no magnetic field.
Warning: The 49e output is not zero Volts for No magnetic field.
However, what is not clear from the datasheet is that the output is offset to the midrail for zero magnetic output i.e. 2.5V
This is done so that the sensor can determine the polarity of the magnetic field i.e. North or South polarity fields.
You can see this effect in the first example program below. Hold a
magnet in front of the sensor and then hold it behind the sensor to see
the serial output value changing.
Results from the 1st Example program:
This is the type of result you will get (hold the magnet very close
to the 49e sensor when doing this to get max. and min. values):
Note that the voltage is estimated assuming a 5V accurate supply (its not that accurate); but for this purpose is close enough.
What you will notice is that:
the mid voltage is 2.54V
the lowest voltage is 0.84V
the highest voltage is 4.27V
Is this right? answer - yes the datasheet indicates that this is the correct output. Here's a graph of the output:
Graph of 49e hall effect Voltage vs Gauss detected
So the measured results from the Example1 program match this
characteristic. Now you can create a program that will operate correctly
to drive the LED. See Example 2
Required Components
Arduino Uno
49e Hall Effect Sensor
Breadboard
Jumper wires
Magnet
LED
1k Ohm resistor
Arduino Hall Effect Sensor: Circuit Layout
The circuit connects the 49e hall effect sensor to an Arduino. Vcc of
the sensor connects to the 5V pin on the Arduino. GND connects to
ground. The 49e's output connects to analog pin A0. As magnetic field
strength changes, A0 reads the varying output voltage. This value drives
the brightness of an LED via PWM on pin 5.
Diagram using Fritzing
Diagram using Fritzing
Arduino Hall Effect Sensor: Example Sketch Ex1
Here's an example sketch to get you started with the magnetic sensor
49e. Note this program allows you to see the zero field output voltage, maximum and minimum ADC values detected.
This is a first stab at detecting the magnet and you'll notice that
it is not very good at dimming the LED This is because it makes an
assumption about the 49e sensor i.e. that it goes from 0V to 5V when a
magnetic field is detected. As noted above the 49e sits at the mid point
and goes above and below 2.5V, in the presence of a magnetic field.
You can copy and paste the code below into the Arduino IDE (in a new
sketch) replacing everything that is in the new sketch window (See "Uploading the code" below).
constfloat ADCmV=5.0/1024; // Estimated as 5V is not accurate
constint hallSensor = A0;
constint pwmLED = 5;
voidsetup(){
Serial.begin(9600);
pinMode(pwmLED, OUTPUT);
}
voidloop(){
int reading = analogRead(hallSensor);
int brightness = map(reading, 0, 1023, 0, 255);
analogWrite(pwmLED, brightness);
delay(1000);
Serial.print("Reading: ");
Serial.print(reading);
Serial.print("\tVolts: ");
Serial.print(reading * ADCmV );
Serial.print("\tBright: ");
Serial.println(brightness);
}
Arduino Hall Effect Sensor: Ex1 Code Explanation
The code is for testing and viewing Arduino hall effect sensor output
Const float ADCmV=5.0/1024 defines a constant for the analog to
digital converter voltage reference which is estimated at 5V divided by
1024 possible readings
Const int hallSensor = A0 defines the analog pin A0 to read the hall sensor voltage
Const int pwmLED = 5 defines the pin 5 to control the brightness of the LED using PWM
Setup initializes the serial port at 9600 baud and sets pin 5 to OUTPUT for PWM
Loop continuously runs the following:
Reads the analog value from the hall sensor pin into an int variable called reading
Maps the reading from 0-1023 range to 0-255 for brightness level
Writes the brightness level to the PWM pin to control LED brightness
Prints the reading, voltage calculated from reading, and brightness level to the serial monitor
Adds a 1 second delay before repeating
It
converts the raw analog reading to a voltage and maps it to a
brightness level to control the LED based on the hall sensor voltage
reading
Arduino Hall Effect Sensor: Uploading the Code
There are a few steps to uploading the code using the Arduino IDE:
Connect the Arduino Uno to the PC with a USB cable.
Select the Arduino Uno hardware.
Open a new sketch.
Paste the code above into the new page (overwrite everything).
Press the upload button (right arrow at top).
You can find a more detailed tutorial on the Arduino IDE page.
Arduino Hall Effect Sensor: Example Sketch Ex2
This code gives a far better response to the magnetic field when the
LED is dimmed as the ADC readings are automatically detected for
midpoint, maximum and minimum values.
To get the code working correctly, calibrate the sensor each
time. Slowly bring the magnet in front of the sensor to touching the
sensor, then do the same for the back side of the sensor.
Now when the magnet approaches from either side, the LED will slowly
get brighter. Note that the sensing distance is far better than the
previous code operation
constint hallSensor = A0;
constint pwmLED = 5;
int midValue = 0; // Auto cal 1st 500ms
int minValue = 0; // Auto cal.
int maxValue = 0; // Auto cal.
constfloat ADCmV=5.0/1024; // Estimated as 5V is not accurate.
voidsetup(){
Serial.begin(9600);
pinMode(pwmLED, OUTPUT);
// Gather readings over 500ms and calibrate the mid point.
Initially, over 500ms, 5 readings are taken to get the midpoint ADC
value - assuming that no magnet is nearby. After that maximum and
minimum readings are used to get the limits of the magnetic field which
is then mapped onto a PWM LED. This gives a greater range of dimming
than the 1st Example because it does not assume incorrect ADC values.
It's using an analog hall effect sensor connected to analog pin A0 to read the magnetic field strength.
It's controlling the brightness of an LED connected to PWM pin 5 based on the hall sensor reading.
The variables midValue, minValue, maxValue are used to calibrate the sensor readings.
In setup():
It initializes serial communication at 9600 baud
Sets pin 5 as output for the LED
Takes 5 analog readings over 500ms from the hall sensor pin
Averages these readings to determine the mid/center value with no magnet present
Initializes minValue and maxValue to this mid value
In loop():
Takes a new reading from the hall sensor
Updates minValue if reading is lower
Updates maxValue if reading is higher
Maps the reading to a PWM brightness value:
Above midValue maps to top half of brightness range
Below midValue maps to bottom half
Writes brightness value to LED pin
Prints debug values over serial
It's continuously calibrating the sensor's min/max readings and
controlling LED brightness based on the hall sensor reading, mapped
between its calibrated range.
The end result is the LED brightness responds to the strength of the magnetic field detected by the hall sensor.
Testing the Circuit
Sweep a magnet near the 49e sensor. The LED should change brightness
proportionally to the magnetic field detected. This demonstrates how to
interface a linear hall sensor for proportional sensing applications.
Conclusions
In this tutorial you learned how to use a hall effect sensor to
detect magnetic fields and control an Arduino output. Hall effect
sensors are very useful for proximity, position, and motion sensing.
This is a basic example but demonstrates the core concept of using a
hall effect sensor with an Arduino. There are many ways to expand on
this project, such as integrating the sensor into a larger electronic
project for automatic control or feedback.
It is interesting to note that using a more complex program allows the magnet to be detected more easily at greater distance.
Further reading: For a more complex Arduino Hall Effect
sensor (measures magnetic field in 3 directions x, y, z - for
orientation detection) check out the HMC5883L chip.
Written by John Main who has a degree in Electronic Engineering.
Note: Parts of this page were written using claude-instant
as a research assistant.
How to get accurate DHT22 digital humidity sensor readings with an Arduino. Did you know it also measures temperature as Well? Find out why, in this page...
A PIR sensor lets your Arduino sense movement without contact. This tutorial covers PIR sensor basics, connecting one to an Arduino board and coding a motion detector.
Arduino Hall Effect Sensor: Add magnetic sensing superpowers to your Arduino projects with an easy-to-use hall effect sensor. With full code and layout...
Comments
Have your say about what you just read! Leave me a comment in the box below.
Don’t see the comments box? Log in to your Facebook account, give Facebook consent, then return to this page and refresh it.