A Hall sensor can detect magnetic fields and is therefore very suitable in the handicraft area to detect movements. You can often find videos on the Internet in which a Hall sensor KY-024 serves as a tachometer with an Arduino.
This might be interesting for you: We built a Hall sensor movement detection wheel
We have already started small Arduino projects. Next we want to build a prayer wheel that will light up when it is rotated. For this we will need a Hall sensor.
This post shows a simple example to explain how to connect a Hall sensor to an Arduino and program it so that an LED starts to light up as soon as the Hall sensor KY024 detects a magnetic field.
List of components
- Arudino Mega
- Jumper cable
- 220 Ohm resistor
- LED
- Magnets
- Hall sensor KY-024
How does a Hall sensor work or what is the Hall effect?
When a current-carrying electrical conductor is in a magnetic field, an electrical field builds up. This is perpendicular to the current direction and to the magnetic field and compensates for the Lorentz force which acts on the electrons.
A Hall sensor also provides a signal when the magnetic field in which it is located is constant. This is the advantage compared to a simple coil, which can only determine the derivation of the magnetic field over time.
The Hall sensor KY-024
The linear magnetic Hall sensor KY-024 can detect magnetic fields and therefore reacts as soon as you approach it with a magnet. It has a potentiometer with which the sensitivity of the sensor can be set. The KY-024 Hall sensor offers both an analog and a digital output.
The digital output acts as a switch that turns on and off when a magnet is nearby. The analog output, on the other hand, can measure the polarity and relative strength of the magnetic field.
KY-024 Arduino Wiring
It is a simple structure that only serves to illustrate the functioning of the KY-024 Hall sensor. Please pay attention to the forward direction of the LED, the kinked end is connected to an Arduino pin and the straight end to GND. Correctly the LED needs a 220 Ohm ballast resistor. However, this can also be neglected for the short duration of use.
Otherwise, connect the GND of the KY-024 to the GND of the Arduino analogous to the Fritzing sketch. Connect the analog output A0 of the KY-024 to A0 of the Arduino Mega. Connect the + of the KY-024 to 5V of the Arduino and connect the digital output D0 of the KY-024 to pin 9 of the Arduino. That was the complete wiring.
Hall sensor KY-024 Arduino Code
int LED = 53 ; // LED int digitalPin = 9; // Hall magnetic sensor input 1 (high) or 0 (low) int analogPin = A0; // analog Pin also available, but not necessary int digitalInputValue ; // digital readings int analogInputValue; // analog readings void setup () { pinMode (LED, OUTPUT); pinMode (digitalPin, INPUT); pinMode(analogPin, INPUT); //not necessary, but it is interesting to see the analog values Serial.begin(9600); } void loop () { digitalInputValue = digitalRead(digitalPin) ; if (digitalInputValue == HIGH) // When magnet is present, digitalInputValue gets 1 (HIGH) and turns LED on { digitalWrite (LED, HIGH); } else { digitalWrite (LED, LOW); } //Here you can see the analog values of the sensor analogInputValue = analogRead(analogPin); Serial.println(analogInputValue); // print analog value delay(100); }
We first define the assignment of the LED (here pin 53), the digital (pin 9) and the analog pin (pin A0) and two variables to read the digital and analog sensor data. The digital pin is sufficient for our purpose of lighting the LED.
The analog pin can actually be neglected. However, since it is useful for this tutorial to show the analog values, the analog pin was nevertheless taken into account in the code.
The respective pin mode is defined in the setup and the digital sensor value of the KY 024 is read in the loop function and, depending on the value, the LED receives the command to light or not. The digital sensor value is either 0 or 1. If you want to, you can use the penultimate 2 lines to display the associated analog values.
Hi there! This is my first visit to your blog! We are a group of volunteers and starting a new project in a community in the same niche. Your blog provided us beneficial information to work on. You have done a wonderful job!
Thanks for sharing this information!
I’m thoroughly enjoying your blog!