Hall effect sensor not counting?

Hello, i am trying to count the number of rotations a wheel makes, I'm using a hall effect sensor and a magnet on the wheel, i want an led to light up when the magnet passes the hell sensor 5 times, but it lights up on the first no matter what number i set the fancied count to, any input on what I'm doing wrong would be greatly appreciated

btw i have a 10k resistor from pin 1 to 3 of the hall

onst int hallPin = 12; // the number of the hall effect sensor pin
const int ledPin = 13; // the number of the LED pin
int count = 0;

int hallState = 0; // variable for reading the hall sensor status
void setup() {
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize the hall effect sensor pin as an input:
pinMode(hallPin, INPUT);
}
void loop(){
hallState = digitalRead(hallPin);
if (hallState == LOW)
{
count++;
if (count >= 5) //fancied passes
{
digitalWrite(ledPin, HIGH);
}
}

}

Unless DigitalRead() does something I am not aware of (I am used to PIC processors), you need for it to go high again before waiting for the next count. Typically, looking for it to go low then increment the count will count low many times before it goes high again (very similar problem to switch bounce/debounce).

I suspect that your loop runs so fast that the loop reads the sensor many times on each pass of the magnet. You could fix this two ways:

  1. use a time delay after each reading that increments the counter, provided it is short enough not to block the next legitimate transition from high to low.
  2. use an interrupt, high to low, to read the sensor. That would be the most bullet proof way, provided the sensor cleanly enters and exits the sensing of the magnet.

Thank you so much, i will take both comments into consideration and give you an update when then processes have been executed.

Hi,

After

count++;
digitalWrite(ledPin, LOW);

I assume you are using the onboard LED as your pin13 LED.

Can you please post a copy of your sketch, using code tags?
They are made with the </> icon in the reply Menu.
See section 7 http://forum.arduino.cc/index.php/topic,148850.0.html

Also with a DMM, check to see what voltage you have on the input pin, with a magnet in postion,note you will get a different response with each pole, no magnet will probably give you half the supply voltage.

Thanks... Tom.... :slight_smile:

Hi,
Again, what is the part number of the hall effect, a link to data sheet would help.

Tom... :slight_smile:

hi, i believe this is the data sheet

Hello, i have found a way to make it work by using some code from diy hacking

ill show the mod that i did

/*
Arduino Hall Effect Sensor Project
by Arvind Sanjeev
Please check out  http://diyhacking.com for the tutorial of this project.
DIY Hacking
*/


 volatile byte half_revolutions;
 unsigned int rpm;
 unsigned long timeold;

 void setup()
 {
   Serial.begin(115200);
   attachInterrupt(0, magnet_detect, RISING);//Initialize the intterrupt pin (Arduino digital pin 2)
   pinMode(12, OUTPUT);
   half_revolutions = 0;
   rpm = 0;
   timeold = 0;
 }
 void loop()//Measure RPM
 {
   if (half_revolutions >= 20) { 
     rpm = 30*1000/(millis() - timeold)*half_revolutions;
     timeold = millis();
     half_revolutions = 0;
     //Serial.println(rpm,DEC);
   }
 }
 void magnet_detect()//This function is called whenever a magnet/interrupt is detected by the arduino
 {
   half_revolutions++;
   Serial.println("detect");
   if (half_revolutions >= 5) //this is the mod that i made, add an if statement, the number it equals is 
//the number of rotations, if you wanted it to blink at 500 rotations you would set it to 500, easy
   {
     digitalWrite(12, HIGH); //this says what you want the led to do 

   }
 }