Skip to main content

Interfacing Hall Effect Sensor with Arduino

Using Hall Effect Sensor with Arduino UnoUsing Hall Effect Sensor with Arduino Uno
Sensors have always been a vital component in any Project. These are the ones which convert the real real-time environmental data into digital/variable data so that it can be processed by electronics. There are many different types of sensors available in the market and you can select one based on your requirements. In this project we will learn how to use a Hall sensor a.k.a Hall effect sensor with Arduino. This sensor is capable of detecting a magnet and also the pole of the magnet.
Why detect a magnet?, You may ask. Well there are a lot of applications which practically use Hall Effect sensor and we might have never noticed them. One common application of this sensor is to measure speed in bicycles or any rotating machines. This sensor is also used in BLDC motors to sense the position of Rotor Magnets and trigger the Stator coils accordingly. The applications are endless, so let’s learn how to Interface Hall effect sensor Arduino to add up another tool in our arsenal. Here are some projects with Hall sensor:

In this tutorial we will use interrupts function of Arduino to detect the magnet near Hall sensor and glow a LED. Most of the time Hall sensor will be used only with Interrupts because of their applications in which high reading and executing speed is required, hence let us also use interrupts in our tutorial.

Materials Required:

  1. Hall Effect Sensor (any digital verison)
  2. Arduino (Any version)
  3. 10k ohm and 1K ohm Resistor
  4. LED
  5. Connecting Wires

Hall Effect Sensors:

Before we dive into the connections there are few important things that you should know about Hall Effect sensors. There are actually, two different types of Hall sensors one is Digital Hall sensor and the other is Analog Hall sensor. The digital Hall sensor can only detect if a magnet is present or not (0 or 1) but an analog hall sensor’s output varies based on the magnetic field around the magnet that is it can detect how strong or how far the magnet is. In this project will aim only at the digital Hall sensors for they are the most commonly used ones.

As the name suggests the Hall Effect sensor works with the principle of “Hall effect”. According to this law “when a conductor or semiconductor with current flowing in one direction was introduced perpendicular to a magnetic field a voltage could be measured at right angles to the current path”. Using this technique, the hall sensor will be able to detect the presence of magnet around it. Enough of theory let’s get into hardware.

Circuit Diagram and Explanation:

The complete circuit diagram for interfacing Hall sensor with Arduino can be found below.
 circuit diagram of interfacing hall effect sensor with arduino
As you can see, the hall effect sensor arduino circuit diagram is pretty simple. But, the place where we commonly make mistakes is at figuring out the pin numbers of hall sensors. Place the readings facing you and the first pin on your left is the Vcc and then Signal and Ground respectively.
We are going to use Interrupts as told earlier, hence the output pin of Hall sensor is connected to the Pin 2 of the Arduino. The Pin is connected to a LED which will be turned ON when a magnet is detected. I have simply made the connections on a breadboard and it looked somewhat like this below once completed.
interfacing hall effect sensor with arduino

Hall Effect Sensor Arduino Code:

The complete Arduino code is just few lines and it can be found at the bottom of this page which can be directly uploaded to your Arduino Board. If you want to know how the program works read further.
We have one input, which is the sensor and one output which is a LED. The sensor has to be connected as an interrupt input. So inside our setup function, we initialize these pins and also make the Pin 2 to work as an interrupt. Here pin 2 is called Hall_sensor and pin 3 is called LED.
void setup() {
  pinMode(LED, OUTPUT); //LED is a output pin
  pinMode(Hall_sensor, INPUT_PULLUP); //Hall sensor is input pin
  attachInterrupt(digitalPinToInterrupt(Hall_sensor), toggle, CHANGE); //Pin two is interrupt pin which will call toggle function
}

When there is a interrupt detected, the toggle function will be called as mentioned in the above line. There are many interrupt parameters like ToggleChange, Rise, Fall etc. but in this tutorial we are detecting the change of output from Hall sensor.

Now inside the toggle function, we use a variable called “state” which will just change its state to 0 if already 1 and to 1 if already zero. This way we can make the LED turn ON or Turn OFF.
void toggle() {
  state = !state;
}

Finally inside our loop function, we just have to control the LED. The variable state will be altered each time a magnet is detected hence we use it to determine if the LED should stay on or off.
void loop() {
  digitalWrite(LED, state);
}

Arduino Hall Effect Sensor Working:

Once you are ready with your Hardware and Code, just upload the Code to the Arduino. I have used a 9V battery to power the whole set-up you can use any preferable power source. Now bring the magnet close to the sensor and your LED will glow and if you take it away it will turn off.

Note: Hall sensor is Pole sensitive, meaning one side of the sensor can either detect only North Pole or only South Pole and not both. So if you bring a south pole close to the north sensing surface your LED will not glow.
working of interfacing hall effect sensor with arduino
What actually happens inside is, when we bring the magnet close to sensor the sensor changes its state. This change is sensed by the interrupt pin which will call the toggle function inside which we change the variable “state” from 0 to 1. Hence the LED will turn on. Now, when we move the magnet away from the sensor, again the output of sensor will change. This change is again noticed by our interrupt statement and hence the variable “state” will be changed from 1 to 0. Thus the LED if Turned off. The same repeats every time you bring a magnet close to the sensor.  
The complete working video of the project can be found below. Hope you understood the project and enjoyed building something new. If otherwise kindly use the comment section below or the forums for help.

video

Code: 
const byte ledPin = 13;
const byte interruptPin = 2;
volatile byte state = LOW;
int val=0;
void setup() {
pinMode(ledPin, OUTPUT);
pinMode(interruptPin, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(interruptPin), test, CHANGE);
Serial.begin(9600);
}
void loop() {
digitalWrite(ledPin, state);
Serial.println(val/2);
}
void test() {
state = !state;
val++;
}

Comments

Popular posts from this blog

Automatic Call answering Machine using Arduino and GSM Module

Automatic Call answering Machine using Arduino and GSM module In today’s modern world we all depend on mobile phones as our primary means of wireless communication. But, we all have faced situations during which we might not be able to answer to our calls, these calls might be an important personal call or a life changing business call and you could have just missed that opportunity since you were not able to answer that call at that particular time. This project aims to solve this problem by creating an  Automatic Call answering Machine by using Arduino and GSM module . Next time when you are changing to a new phone number or out for a long pilgrimage trip or enjoying a well deserved vacation just use this machine to record your voice stating the reason for absence and all your calls will be automatically answered by this machine and your recorded voice will be played to them. This can also be used for your business numbers to answer to your customer’s calls duri...

AC Voltmeter using Arduino

AC Voltmeter using Arduino In this project, we are going to make an  AC Voltage Measuring Device using Arduino,  which will measure the voltage of Alternating Current Supply at our home.  We are going to print that voltage on serial monitor of Arduino IDE as well as show on the multimeter. Making a Digital Voltmeter is a lot easy than making an analog one because in case of analog voltmeter you must have good knowledge of physical parameters like torque, friction losses etc. whereas in case of digital voltmeter you can just use a LCD or LED matrix or even your laptop (as in this case) to print the voltage values for you. Here are some  Digital Voltmeter Projects : Simple Digital Voltmeter Circuit with PCB using ICL7107 LM3914 Voltmeter Circuit 0-25V Digital Voltmeter using AVR Microcontroller Required Components: One 12-0-12 transformer    1N4007 diode    1uf capacitor    Resistors 10k; 4.7k.    ...

mission: impossible 7

One of the most captivating parts of 2018's Main goal: Incomprehensible — Aftermath, the last Mission: Unimaginable film, was the carefree disposition with which it treated plot article. Its initial scenes floated through a parade of names and associations and inspirations that essentially tried you to comprehend what was happening. This was, truly, freeing. Try not to stress over why anything is going on, essayist chief Christopher McQuarrie appeared to be telling us. Simply partake in the display. This is the film wherein Tom Journey hangs off a helicopter. Mission: Unthinkable — Dead Retribution Section One, as proposed by that unfavorable "section one," appears to mind a smidgen more about its plot, and seeing why is not hard. This time, Journey's Ethan Chase isn't battling skeptical psychological militants or vaporous global surveillance networks yet an almighty man-made reasoning known as "the Substance" that has moment admittance to all internet b...