hall effect sensor with arduino circuit and code

hall effect sensor Module interfacing with arduino This module is used to detect the presence of magnetic. Whenever, it will be placed in the region of magnetic field then it will detect it and will give us the output. The module consists of a hall sensor which detects the presence of magnetic field.This module can be used in many projects like making a door alarm in which whenever the door will close, the magnetic will come near to the module, so it will detect the magnet. It can also be used in making speedometer for car or a bike. If a magnet is attached to the wheel and we have place this module in the body, then we can know the time taken to complete a cycle and by doing further calculation, we can calculate the speed.

How hall effect sensor works?




The analog hall sensor module contains a hall sensor. The hall sensor works on the principle of Hall Effect, which states the whenever a magnetic field is applied in perpendicular direction to the flow of the current, then a potential difference is induced. We can use this voltage to detect that whether the magnetic is placed near the sensor or not.

hall effect sensor Pin out 

The pin out of the hall sensor module from left to right is as followshall effect sensor pin out Ground: The first pin from the left is the ground pin and it will be connected to the ground of the Arduino.

+5V: The middle pin is the 5V pin and it will be connected to the +5V of the Arduino.

S: The right pin is the output pin which will give us the output whenever the magnet will be placed near it. This will be connected to the digital pin of the Arduino.




If you want to power the module from external power, then you can power it by giving 3.3 to 5V.

hall effect sensor interfacing with arduino

hall effect sensor interfacing with arduino

The connections for the hall sensor module are very easier. First connect the +5 v and ground pin of the sensor to the +5v and ground pin of the Arduino. Then connect the digital pin of the sensor to the pin 2 of the Arduino.  We will use the pin 13 of the Arduino as the output pin which has a built in led attached to it on the Arduino board. So, whenever the magnet will be placed near the sensor, the module will detect the magnet and will turn on the led which is at the pin 13 of the Arduino. Through this way we can use this module in many other projects like making a door alarm and making a speedometer.

hall effect sensor with arduino code

 This code is for the analog hall sensor module.constants won’t change. They’re used here to set pin numbers :

const int hallPin = 2 ;     // initializing a pin for the sensor output

const int ledPin =  13 ;     // initializing a pin for the led. Arduino has built in led attached to pin 13

// variables will change :

int hallState = 0 ;          // initializing a variable for storing the status of the hall sensor.

void setup ( ) {

  pinMode ( ledPin , OUTPUT ) ;      // This will initialize the LED pin as an output pin :

  pinMode ( hallPin , INPUT ) ;                        // This will initialize the hall effect sensor pin as an input pin to the Arduino :

 Serial.begin( 9600 ) ;

Serial.println ( “ microcontrollerslab.com ” ) ;

Serial.println ( “ Testing the analog hall sensor module : ” ) ;

}

void loop ( ) {

  hallState = digitalRead ( hallPin ) ;                           // reading from the sensor and storing the state of the hall effect sensor :

  if ( hallState == LOW ) {                                                // Checking whether the state of the module is high or low

    Serial.println ( “ The state of the analog hall module is high ” ) ;

    digitalWrite ( ledPin , HIGH ) ;                                 // turn on the LED if he state of the module is high

  } 

  else {

    digitalWrite ( ledPin , LOW ) ;                                      // otherwise turn off the LED :

Serial.println ( “ The state of the analog hall module is low ” ) ;

  }

}

Working of Code

In the code, we have declared the pin 12 for sensor output and pin 13 for the led. The Arduino has a led attached at the pin 13. Then we checked the state of the sensor by using the digitalread command and applied the condition that if the state of the analog sensor module is high, then turn high the led and show on the serial monitor that the state of the analog sensor is high and of the state of the module is low, then turn low the led and show on the serial monitor that the state of the analog hall module is low.

Output of the hall effect sensor will be like that

microcontrollerslab.com

Testing the analog hall sensor module :

The state of the analog hall module is high

Add Comment

Subscribe to our blog to get updates in your email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 901 other subscribers