Today I’ll show you how you can connect a hall effect sensor to an Arduino and use it with an interrupt.
Tools and materials used in the video (Affiliate links):
Arduino Uno: http://s.click.aliexpress.com/e/biNyW0zK
Hall effect sensors: http://s.click.aliexpress.com/e/bRNtliPQ
Assorted Resistors: http://s.click.aliexpress.com/e/bb3P6qJW
Step 1: What Is a Hall Effect Sensor?
A Hall effect sensor is a device that is used to measure the magnitude of a magnetic field. Its output voltage is directly proportional to the magnetic field strength through it.
Hall effect sensors are used for proximity sensing, positioning, speed detection, and current sensing applications.
The one I’ll be working with today is labeled as 3144 which is a hall effect switch primarily used for high temperature and automotive applications. Its output is high by default and goes low once in presence of a magnetic field.
The sensor has 3 pins, VCC, ground and output. You can identify them in that order if you hold the sensor with the labels toward you. VCC is on the left, and output is on the right side. To prevent any voltage drift, a 10k resistor is being used between VCC and the output in a pull-up configuration.
Step 2: What Is an Interrupt?
To connect the sensor on the Arduino, we will use a simple, yet very powerful feature called Interrupt. An Interrupt job is to make sure that the processor responds quickly to important events. When a certain signal is detected, an Interrupt (as the name suggests) interrupts whatever the processor is doing, and executes some code designed to react to whatever external stimulus is being fed to the Arduino. Once that code has wrapped up, the processor goes back to whatever it was originally doing as if nothing happened!
What's awesome about this is that it structures your system to react quickly and efficiently to important events that aren't easy to anticipate in software. Best of all, it frees up your processor for doing other stuff while it's waiting on an event to show up.
The Arduino Uno has two pins that we can use as Interrupts, pin 2 and 3. The function that we use to register the pin as an interrupt is called attachInterrupt where as a first parameter we send in the pin to be used, second parameter is the name of the function that we want to call once an interrupt is detected and as a third parameter we send in the mode at which we want the interrupt to work. There is a link in the video description to the full reference for this function.
Step 3: Connections and Code
In our example, we connect the hall effects sensor to pin 2 on the Arduino. At the beginning of the sketch, we define the variables for the pin number of the built in LED, the interrupt pin as well as a byte variable that we will used to modify through the interrupt. It is crucial that we mark this one as volatile so the compiler can know that it is being modified outside of the main program flow through the interrupt.
In the setup function, we first specify the modes on the pins used and then we attach the interrupt as previously explained. One other function that we use here is digitalPinToInterrupt which as the name implies, translates the pin number to the interrupt number.
In the main method, we just write the state variable on the LED pin and add a very small delay so the processor can have time to work properly.
Where we attached the interrupt, we specified blink as the second parameter and this is the function name to be called. Inside we just invert the state value.
The third parameter of the attachIntertupt function is the mode at which it operates. When we have it as CHANGE, the blink function will be executed each time the interrupt state changes so, it will be once called once we get the magnet close to the sensor and triggered again once we remove it. This way, the LED is on while we hold the magnet close to the sensor.
If we now change the mode to RISING, the blink function will only be triggered once a rising edge of the signal is seen on the interrupt pin. Now every time we bring the magnet close to the sensor, the LED either turns off or turns on so we basically made a magnetic switch.
The final mode that we gonna try is LOW. With it, when the magnet is close, the blink function will be constantly triggered and the LED will flicker, having its state inverted all the time. When we remove the magnet, it’s really unpredictable how the state will end up as this is dependent on the timing. However, this mode is really useful if we need to know for how long a button was pressed as we can use timing functions to determine that.
Step 4: Further Actions
Interrupts are a simple way to make your system more responsive to time sensitive tasks. They also have the added benefit of freeing up your main `loop()` to focus on some primary task in the system. (I find that this tends to make my code a little more organized when I use them - it's easier to see what the main chunk of code was designed for, while the interrupts handle periodic events.) The example shown here is just about the most basic case for using an interrupt - you can use them for reading an I2C device, sending or receiving wireless data, or even starting or stopping a motor.
If you have an interesting use of an interrupt or a hall effects sensor then be sure to let me know in the comments, like and share this Instructable, and don’t forget to subscribe to my YouTube channel for more awesome tutorials and projects in the future.
Cheers and thanks for watching!