Skip to main content

How to use a Hall Effect Sensor

What is ana LDR?Hall Effect Sensor?

AnThe LDRhall oreffect Light Dependent Resistorsensor is a componenttype of magnetic sensor which restricts how much power can flowbe throughused for detecting the strength and direction of a circuitmagnetic basedfield onproduced howfrom mucha permanent magnet or littlean lightelectromagnet hitswith its output varying in proportion to the sensitive part on the top.

Light Dependent Resistor.png

To use a Light Dependent Resistor, we have to use it in combination with a fixed value resistor, the combination of these two components acts a little like a kitchen mixer tap, we can vary the temperature (voltage) by adjusting the tap.

voltage-divider.png

The zig-zag lines indicate resistors, the voltage output we measure with the Arduino comes from this middle point between the two, as the valuestrength of the LDRmagnetic variesfield itbeing changesdetected.

the voltage between Ground (0V) and 5V (VCC).

image-1706547936079.png

Wiring

  1. (1)legleft to 5V (Power)
  2. (2)leg splitmiddle to GND
  3. right to PIN2 via 10K resistor
  4. (2)leghalleffectcircuit.png split to A0

ldrwiring_bb.png

Getting started

The following code uses analogRead(digitalRead() to get a integer between 0-1023(1/0) representing the voltage,detection whereof 0 is 0V and 1023 is 5V.

The code below uses the serial port to output the value every 50ms to the Serial Monitor.magnet.

#defineconst ldrPinint A0hallSensorPin = 2;  // Hall Effect sensor connected to digital pin 2
int hallSensorState;          // Variable to store the state of the sensor

void setup() {
  Serial.begin(9600);                // Start serial communication at 9600 )baud
  pinMode(hallSensorPin, INPUT);     pinMode(// ldrPin,Set the Hall Effect sensor pin as an INPUT );
}

void loop() {
  hallSensorState = digitalRead(hallSensorPin); // Read the state of the sensor
  Serial.println( analogRead( ldrPin ) )hallSensorState); 

  delay( 50 )100); 
}