Hall Sensor Set Up

Board: Arduino Micro
Hall Sensor: U18007

Hi all,
I'm trying to build a large encoder using two hall sensors in series. I've managed to program the hall-sensors using interrupts from a previous project using the following code. But the output points at an issue with the way I set up the Hall Sensor:

const byte ledPin = 13;
const byte interruptPinA = 2;
volatile byte state = LOW; //LOW

void setup() {
  pinMode(ledPin, OUTPUT);
  Serial.begin(9600);
  pinMode(interruptPinA, INPUT_PULLUP); //pullup
  attachInterrupt(digitalPinToInterrupt(interruptPinA), blink, CHANGE);
}

void loop() {
  digitalWrite(ledPin, state);
  Serial.println(digitalRead(interruptPinA));
  delay(1);
  
}

void blink() {
  state = !state;
}

The problem I am having is: the Monitor starts printing 0s - until I approach the magnet to the sensor, after which the LED turns on and it starts printing 1s - then when I remove the magnet, the state of the sensor does not change back (keeps printing 1s), nor the LED off. How can I change the code so that it does? I tried using the RISING, FALLING, etc arguments in the attachInterrupt() with no avail. I have no resistors in the circuit.

Monitor:

0
0
0
1 // after magnet is near the sensor
1
1
1
1 // magnet is moved away
1
1
1
1
0 // magnet is near again but this time with the opposite pole near the sensor
0
0
0

The only way I can get it to change back to 0s and shut off the LED is nearing the magnet with the opposite pole that I used to trigger it.

I also tried changing it to a regular input instead of (INPUT_PULLUP) in case its not the common type of Hall Sensor - that didn't work either.

Any help is much appreciated!

Just figured out the Hall Sensor is of the latching type - therefore it stays on. Getting another one that is non-latching since I can't figure out how to make the encoder work with the oppositely charged magnets in series.

See the StateChangeDetection example.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.