Code A1301/A1302 Hall Effect Sensor

Last Modified: November 13, 2013, at 12:42 PM
By: robtillaart

Navigation


History

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


Description

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. :)

Happy tinkering,
Rob
(and Ralph)


Example

/*
 *    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

void setup() 
{
  Serial.begin(9600);
}

void DoMeasurement()
{
// 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: ");
//  Serial.println(raw);

  long compensated = raw - NOFIELD;                 // adjust relative to no applied field 
  long gauss = compensated * TOMILLIGAUSS / 1000;   // adjust scale to Gauss

  Serial.print(gauss);
  Serial.print(" Gauss ");

  if (gauss > 0)     Serial.println("(South pole)");
  else if(gauss < 0) Serial.println("(North pole)");
  else               Serial.println();
}

void loop() 
{
    delay(1000);
    DoMeasurement();
}

minimized Float variation (for weaker fields)

#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
// For A1301: 2.5mV = 1Gauss, and 1024 analog steps = 5V, so 1 step = 1953mG
// For A1302: 1.3mV = 1Gauss, and 1024 analog steps = 5V, so 1 step = 3756mG

#define TOMILLIGAUSS 1.953125
// #define TOMILLIGAUSS 3.756010  

void setup() 
{
  Serial.begin(115200);
}

void loop() 
{
  float gauss = (analogRead(0) - NOFIELD) * TOMILLIGAUSS;

  Serial.print(gauss, 2);
  Serial.print(" Gauss ");

  if (gauss > 0)     Serial.println("(South pole)");
  else if(gauss < 0) Serial.println("(North pole)");
  else               Serial.println();

  delay(1000);
}


FAQ

Sensitivity

The A1301 has a sensitivity of 2.5mVolt per Gauss.
The A1302 is less sensitive, outputting 1.3mVolt / Gauss.

Datasheet

A1301 / A1302 http://www.allegromicro.com/en/Products/Part_Numbers/1301/1301.pdf

Tesla vs Gauss 1 Tesla = 10000 Gauss


 
Author:  Rob Tillaart
Contact: rob dot tillaart at gmail dot com

Share