A1301 Hall Effect Sensor

How am i going to program the arduino when led and sensor are plugged in? I want to let the LED switch OFF when magnet is near and switch ON when there are no magnet. i am using 1k Ohm resistor within the LED and the sensor. The sensor giving raw value of 514~516.

played with such long ago - Arduino Playground - Hall Effect -

leong92:
How am i going to program the arduino when led and sensor are plugged in? I want to let the LED switch OFF when magnet is near and switch ON when there are no magnet. i am using 1k Ohm resistor within the LED and the sensor. The sensor giving raw value of 514~516.

What does it go down or up to when you put the North pole of a magnet near the sensor face?
It should be a different value.
Something like this as an example if the value goes lower than 510:

  sensorVal = analogRead(sensorPin);
  if(sensorVal < 510){
    digitalWrite(ledPin, LOW);
  }
  else{
    digitalWrite(ledPin, HIGH);
  }

elac:

leong92:
How am i going to program the arduino when led and sensor are plugged in? I want to let the LED switch OFF when magnet is near and switch ON when there are no magnet. i am using 1k Ohm resistor within the LED and the sensor. The sensor giving raw value of 514~516.

What does it go down or up to when you put the North pole of a magnet near the sensor face?
It should be a different value.
Something like this as an example if the value goes lower than 510:

  sensorVal = analogRead(sensorPin);

if(sensorVal < 510){
    digitalWrite(ledPin, LOW);
  }
  else{
    digitalWrite(ledPin, HIGH);
  }

If south pole then the value more than the rawvalue whereas north pole the value less than rawvalue. The raw value is not so constant that's why i hard to set it. I have tried the above way, but the rawvalue floating around 514~516. Any alternative way?

sensorVal = analogRead(sensorPin);
  if(sensorVal < 500 || sensorVal > 530){  // if there is either a N or a S pole near the sensor
    digitalWrite(ledPin, LOW);
  }
  else{
    digitalWrite(ledPin, HIGH);
  }
  noMagVal = 515; // mean value with no magnet
  threshold = 10; // Find the best value with experiments.

  sensorVal = analogRead(sensorPin);
  if(abs(sensorVal - noMagVal) > threshold){
    digitalWrite(ledPin, LOW);
  }
  else{
    digitalWrite(ledPin, HIGH);
  }

dc42:

sensorVal = analogRead(sensorPin);

if(sensorVal < 500 || sensorVal > 530){  // if there is either a N or a S pole near the sensor
   digitalWrite(ledPin, LOW);
 }
 else{
   digitalWrite(ledPin, HIGH);
 }

Done with the ON/ OFF. Thanks! now i have another problem. When i attached a small fan (like USB fan) on a motor which one blade stick with a magnet. Once the motor turning and i put closer to the sensor sometimes the LED off and sometimes cant catch up the magnet. Is it delay problem? or speed of motor too fast? when i put delay lesser the LED look like blinking but not so obvious. If i turn manually it have the same problem also. Got any other suggestion?

What delay? Post all of your code.

Well most of the time the led is off, as it only turns on the fraction of a second the magnet passes by it would be difficult to see at high RPMs.
Try reading the values on the serial monitor when the magnet is spinning. Are the values swinging enough? How much of the rotation is the sensor in the magnetic field?

dc42:
What delay? Post all of your code.

int led=13;
void setup()
{
Serial.begin(9600);
pinMode(led,OUTPUT);
}

void DoMeasurement()
{
// measure magnetic field
int raw = analogRead(A0);

Serial.print("Raw reading: ");
Serial.println(raw);

if (raw < 513 || raw >517 )
{
digitalWrite(led, LOW);
}
else
{
digitalWrite(led, HIGH);
}
}

void loop()
{
delay(500);
DoMeasurement();
}

elac:
Well most of the time the led is off, as it only turns on the fraction of a second the magnet passes by it would be difficult to see at high RPMs.
Try reading the values on the serial monitor when the magnet is spinning. Are the values swinging enough? How much of the rotation is the sensor in the magnetic field?

The values are not swinging enough. What do you mean by " How much of the rotation is the sensor in the magnetic field? "

Yes, your delay(500) is affecting the result. You are only sampling the magnetic field every half a second, so you will miss most of the times the magnet goes past. Try removing the delay and the Serial.print commands.

Could you please fix the post of your sketch by using the code tags. :wink:

I was suggesting to test if the sensor was in the magnets field only when the blade with the magnet passes, or if the magnets field extended to the blades before and after the magnet or further.
Like so, place the sensor where it needs to be near the fan, manually rotate the fan slowly so the blade with the magnet passes, observe on the serial monitor when the sensor enters the magnets field and leaves the magnetic field.
This would let you know if the sensor is changing ONLY when the blade with the magnet passes or if your "stuck" in the magnets field for to much of the rotation.

As dc24 said remove the delay, and you can also simplify your sketch:

int led=13;
void setup()
{
  Serial.begin(9600);
  pinMode(led,OUTPUT);
}


void loop()
{
 // measure magnetic field
 int raw = analogRead(A0); 

 Serial.print("Raw reading: ");
 Serial.println(raw);

  if (raw < 513 || raw >517 )
  {
    digitalWrite(led, LOW);
  }
  else
  {
    digitalWrite(led, HIGH);
  }
}

After testing, remove or comment the serial commands, like dc24 also said.

robtillaart:
played with such long ago - Arduino Playground - Hall Effect -

Hi, thanks for your hall effect tutorial.

I am trying to make it work without success.

Using your code my Gaus reading is -982,-984 or -986 - always osciollating between these values no matter if a magnet is present or not.

When i comment the Gauss reading and uncomment the raw reading the raw reading is usually one but intermittently 2.

I have attached a photo so you know i'm not doing anything silly !

Maybe i should have a resistor ?

Thanks for any help you can give,

Gary.