Code example for A1301 / A1302 Hall Effect Sensor Author: Rob Tillaart Contact: rob dot tillaart at gmail dot com
0.1 2010-07-23: Initial page
0.2 2010-08-21: added some info
0.3 2012-06-10: Fixed incorrect calculation, tidied up code, added further info
A Hall effect device produces a voltage when placed in a magnetic field. Here is some simple code to use an A1301 or A1302 Hall effect sensor, which give output proportional to field strength. (Other Hall effect sensors may be simpler and just switch on or off when the field exceeds a certain value).
If there is no magnetic field applied the sensor outputs approximately half of the supply voltage, typically 2.5V, assuming the supply is 5V. The output drops towards 0V or rises towards 5V, according to magnetic polarity, at a rate of 2.5mv / Gauss for the A1301, or 1.3mV / Gauss for the A1302.
These devices come in LH and UA packages; beware that the pinouts depend on the package (see sample code for details). No other components are needed for the example.
The device has a specific orientation; the magnet should be on the logo side of the device. (It works quite well on the other side too but the logo side is probably more accurate).
Note that these sensors are relatively inaccurate (their sensitivity to magnetic field can vary within 20% or so), so there is probably not much point in using a precise 5V voltage reference.
On the other hand, you probably do expect a reading of zero when no magnet is near the device. So, before trying to measure any fields, you need to find the voltage output by the device when no magnet is near it, as this voltage varies slightly from device to device. This can be done by uncommenting the lines in the code which print the raw reading. For the particular sensor I used, this raw value was 505.
The strength of the field around a magnet depends on the distance in a complicated way according to the type of magnet - see http://en.wikipedia.org/wiki/Magnet to get started. :)
No class for this sensor yet :)
* FILE: MM01
* AUTHOR: Rob van den Tillaart; modified Ralph Martin
* DATE: 2012 06 10
* ORIGINAL URL: http://playground.arduino.cc/Code/HallEffect
* PURPOSE: use an A1301 or A1302 as magnetometer
* Pin Layout LH Package
* 1 VCC 5V
* 2 signal connected to Analog 0
* 3 GND
* Pin Layout UA Package
* 1 VCC 5V
* 2 GND
* 3 signal connected to Analog 0
#define NOFIELD 505L // Analog output with no applied field, calibrate this
// Uncomment one of the lines below according to device in use A1301 or A1302
// This is used to convert the analog voltage reading to milliGauss
#define TOMILLIGAUSS 1953L // For A1301: 2.5mV = 1Gauss, and 1024 analog steps = 5V, so 1 step = 1953mG
// #define TOMILLIGAUSS 3756L // For A1302: 1.3mV = 1Gauss, and 1024 analog steps = 5V, so 1 step = 3756mG
// measure magnetic field
int raw = analogRead(0); // Range : 0..1024
// Uncomment this to get a raw reading for calibration of no-field point
// Serial.print("Raw reading: ");
long compensated = raw - NOFIELD; // adjust relative to no applied field
long gauss = compensated * TOMILLIGAUSS / 1000; // adjust scale to Gauss
Serial.print(" Gauss ");
if (gauss > 0) Serial.println("(South pole)");
else if(gauss < 0) Serial.println("(North pole)");
The A1301 has a sensitivity of 2.5mVolt per Gauss.
The A1302 is less sensitive, outputting 1.3mVolt / Gauss.
Tesla vs Gauss
1 Tesla = 10000 Gauss
|Last Modified:||June 10, 2012, at 03:15 PM|