Arduino with hall sensor

In this article I will show you, how easy it is to sense a magnetic field with an Arduino. At first I will describe the electronic circuit, then I present the small code necessary for this design.

Electronic circuit:

All we need is a hall sensor, two capacitors, one resistor and some wires and of course an Arduino and a magnet.

An Arduino Due microcontroller connected to a small component via red, green, and blue wires on a grid paper.

I chose the MLX90248ESE [datasheet] from Melexis. Since it senses an omnipolar magnetic field, we don’t need to care about, where the north and where the south pole on our magnet is.

Let’s take a look on the schematic that can be found in the datasheet:

Circuit diagram of an MLX90248 sensor connected to a 2.5-3.5V power source with a 10nF capacitor and output components.

Source: Melexis

In order not to need an additional prototyping shield for soldering the two capacitors and resistor, I soldered them directly on the sensor itself. It’s kind of tricky, but if you have a certain soldering experience you will get it, otherwise use a prototyping shild instead. Please mind, that you should use the 3.3V supply instead of the 5V for powering the sensor.

Two close-up photos showing a sensor connected to various electronic components like resistors and capacitors.

Code:

// constants

// variables will change: int hallState = 0; // variable for reading the pushbutton status

void setup() { Serial.begin(9600); // initialize the pushbutton pin as an input: pinMode(hallPin, INPUT); }

void loop(){ // read the state of the pushbutton value: hallState = digitalRead(hallPin);

if (hallState == HIGH) { Serial.println("no mag"); } else { Serial.println("mag detected"); }

delay(200); }

Video: