Site icon AranaCorp

Using a Hall effect sensor with Arduino

The KY-024 Hall effect linear magnetic sensor reacts in the presence of a magnetic field. It is equipped with a potentiometer to adjust the sensitivity of the sensor and provides two analog and digital outputs.

Material

Principle of operation

The KY-024 module consists of a linear Hall effect sensor SS49E and a double differential comparator LM393, a potentiometer BOCHEN 3296). The comparator coupled with the potentiometer allows to compare the sensor value with a threshold value in order to use the sensor as an all-or-nothing sensor. Two leds are present on the module. The led1 indicates that the sensor is supplied with voltage and the led2 indicates that a magnetic field is detected.

Scheme

The hall effect sensor has an analog output and a digital output. The analog output returns an image of the measurement and the digital output returns a high or low state depending on the threshold given by the potentiometer. You can use one or the other according to your application. The module can be powered by the 5V output of the microcontroller.

Code

To test the sensor, we will read the digital output and display the analog output. So we use the functions analogRead and digitalRead.

//initiation des pins
int ledPin = 13; //pin pour la LED
int digitalPin = 2; //pin pour le capteur
int analogPin = A0; //pin pour le capteur
int digitalVal;
int analogVal;


void setup()
{
  Serial.begin(9600); // vitesse de transmission
  pinMode(ledPin, OUTPUT); //la pin est en sortie
  pinMode(digitalPin, INPUT); //la pin est en entrée
}

void loop()
{
  digitalVal = digitalRead(digitalPin);
  if (digitalVal == HIGH) //condition "si" : la valeur numérique est au niveau haut
  {
    digitalWrite(ledPin, HIGH); //alors la led s'allume
  }
  else
  {
    digitalWrite(ledPin, LOW); // sinon la led s'éteint ou reste éteinte
  }
  analogVal = analogRead(analogPin);
  Serial.println(analogVal); //afficher la valeur analogique
  delay(100); //délai 100ms
}

To pass the analog value to a physical value, you can use the map() function

//transforme la valeur de 0 à 1023 vers 0 à 5V
int volt=map(analogVal, 0, 1023, 0, 5);

//transforme la valeur lue en Gauss
int gss = map(volt, 1, 4, -1000, 1000);

Result

If you move a magnet towards and away from the sensor, you should see the LED turn on and off and the sensor value change.

Applications

Sources

Exit mobile version