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.

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:

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.

Code:

[code lang=“cpp“]
// constants
const int hallPin = 8; // the number of the hallsensor pin

// 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);
}
[/code]

Video:

Leave a comment

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind markiert *