Hall Effect Sensor

SS451A

I needed a Hall Effect Sensor to count revolutions of a windmill, so I first went to sparkfun. I grabbed the first one I saw, the Melexis 1881. Unfortunately, when I went to use the sensor I discovered what many other people have already noticed- that this is a latched sensor. Instead of changing values when a magnetic field is present, it changes values depending on the magnetic pole applied to the sensor. This means that a north pole will switch it one way and the only way to switch it back is with a magnet’s south pole.

I might have been able to still use this sensor. As JPFlash suggested on sparkfun, I could have turned on and off the power for the sensor to emulate non-latched behavior. I didn’t want to do this because I didn’t want to have the needed minimum 10ms delay between readings. I also could have put two magnets on my windmill- one with the north pole outward and one with the south pole outward- which would have actually been an elegant solution, but I only got that idea from someone else after I ordered a replacement part.

I bought a couple of the Honeywell SS451A sensors from Mouser. When I got the parts, I looked at the datasheet but I was confused as to how to wire up the parts. After looking around for an embarassing amount of time, I finally realized that the pins are marked on the datasheet in a microscopic font.

Hall_Effect

 

Once I finally realized the right way to connect the sensor, and then connected it to 5V and GND and pin2 it was easy to count the number of times the magnet passed the sensor.

 

/**
 * Switch the status LED on when a magnet is nearby
 * also, increase a counter
 * but only increase the counter when we have a rising edge
 */
 
#define PIN_SENSOR_OUTPUT      2
#define PIN_STATUS             13
 
unsigned int last_printed_count;
unsigned int count;
unsigned int last_sensor_reading;
 
void setup()
{
  pinMode(PIN_SENSOR_OUTPUT, INPUT);
  digitalWrite(PIN_SENSOR_OUTPUT, HIGH);  
 
  Serial.begin(9600);
  Serial.println("Welcome");
 
  // just starting so last printed count at time zero
  last_printed_count = 0;
  // just starting, preset count to zero
  count = 0;
 
  pinMode(PIN_STATUS, OUTPUT);
}
 
void loop()
{
  int hall_effect_sensor_reading;
 
  // 1 = no magnet present
  // 0 = magnet detected
  hall_effect_sensor_reading = digitalRead(PIN_SENSOR_OUTPUT);
 
  // TURN OFF or ON the LED
  if( hall_effect_sensor_reading == 1 ) {
    digitalWrite(PIN_STATUS, 0);
  } else {
    // magnet detected!
    // turn on LED
    digitalWrite(PIN_STATUS, 1);
  }
 
  // only increase the count if..
  // the magnet is here now AND the magnet wasn't here before
  if ((hall_effect_sensor_reading == 0) && (last_sensor_reading == 1)){
    count++;  
  }
  // advance our reading. we just processed this reading so it should now become the "last sensor reading"
  last_sensor_reading = hall_effect_sensor_reading;
 
  // we only want to print once a second
  // look at the current time (millis())
  // if it is greater than the last time we printed + 1000 (one second later than the last time)
  // then print a new message!
  if( millis() > last_printed_count + 1000) {
    Serial.println(count);    
 
    // we just printed the count, save the current time for our next check
   last_printed_count = millis(); 
  }
}

Also read...

Comments are closed.