Linear Hall effect sensor module with Arduino | How it works
Hello, welcome back. In this tutorial, we will learn what is the linear magnetic hall sensor and how to connect it to the Arduino. This sensor module is mainly designed using hall effect sensors. These are activated using a magnetic field. Also, these sensors are mainly used for distance measurement, anti-lock braking systems, proximity sensors, and switches. This hall effect sensor includes a rule called the hall effect.
What is the hall effect?
When power is applied to a material, the current flows through it in a straight line. That is, the electrons travel in a straight line. Then, when a magnet is placed near that material, the electrons that traveled along a straight line travel in a curved path again. This results in a potential difference or voltage between the permanent magnet and the electric current flow. This is mainly called the hall effect.
There are two types of Hall effect sensors.
1. Digital output sensors.
2. Analog output sensors.
The ky-024 linear magnetic hall sensor module includes a hall effect sensor called 49E. Also, we can get digital and analog outputs using this sensor module. The potentiometer here can change the sensitivity of the output. The digital output can be used as a switch near a magnet, and the analog output can be used to measure the polarity and relative strength of a magnetic field.
The PIN structure of this sensor
OK, let’s learn how to connect this sensor with Arduino. The required components are as follows.
- Arduino Nano board x 1 — Our Store / Amazon
- Magnetic hall sensor x 1 — Our Store / Amazon
- LED x 2 — Our Store / Amazon
- 180-ohm resistor x 2 — Our Store / Amazon
- Breadboard x 1 — Our Store / Amazon
- Jumper wires — Our Store / Amazon
Disclosure: These Amazon links are Affiliate links. As an Amazon Associate, I earn from qualifying purchases.
Step 1
Firstly, identify these components.
Arduino Nano board
Magnetic hall sensor
LED bulb
180-ohm Resistor
Breadboard
Jumper wires
Step 2
Secondly, connect these components. To do this, use the circuit diagram below.
Step 3
Thirdly, let’s create the program for this project. It is as follows.
- The complete program of this project – Download
#define SensorAO A0
#define SensorDO 4
#define LED1 2
#define LED2 3
void setup() {
Serial.begin(9600);
pinMode(SensorDO, INPUT);
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
}
void loop() {
int valueAnalog = analogRead(SensorAO);
bool valueDigital = digitalRead(SensorDO);
Serial.print("Analog value :");
Serial.print(valueAnalog );
Serial.print("\t");
Serial.print("Digital value :");
Serial.println(valueDigital);
if (valueDigital == 1) {
digitalWrite(LED1, HIGH);
digitalWrite(LED2, LOW);
} else if (valueDigital == 0) {
digitalWrite(LED2, HIGH);
digitalWrite(LED1, LOW);
} else {
digitalWrite(LED1, LOW);
digitalWrite(LED2, LOW);
}
}
Code explanation
Firstly, sensor pins and LED pins are defined.
#define SensorAO A0
#define SensorDO 4
#define LED1 2
#define LED2 3
In the setup function, these pins are set as output and input pins. Also, the serial monitor is beginning.
void setup() {
Serial.begin(9600);
pinMode(SensorDO, INPUT);
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
}
In the loop function, the analog and digital sensor values are read and entered into integers and Boolean variables.
int valueAnalog = analogRead(SensorAO);
bool valueDigital = digitalRead(SensorDO);
After, these values are printed on the serial monitor.
Serial.print("Analog value :");
Serial.print(valueAnalog );
Serial.print("\t");
Serial.print("Digital value :");
Serial.println(valueDigital);
Next, the digital value is checked using the IF condition. If the value is 1, LED1 is activated. If the value is 0, LED2 is activated. Otherwise, LED1 and LED2 will be inactive.
if (valueDigital == 1) {
digitalWrite(LED1, HIGH);
digitalWrite(LED2, LOW);
} else if (valueDigital == 0) {
digitalWrite(LED2, HIGH);
digitalWrite(LED1, LOW);
} else {
digitalWrite(LED1, LOW);
digitalWrite(LED2, LOW);
}
Step 4
Lastly, select the board and port. After, upload this code to the Arduino board.
OK, enjoy this tutorial. The full video guide is given below. So, we will meet in the next tutorial. Have a good day.
Linear hall effect sensor module with Arduino | How it works