Pages: [1]   Go Down
Author Topic: A1301 Hall Effect Sensor  (Read 1226 times)
0 Members and 1 Guest are viewing this topic.
Kuala Lumpur
Offline Offline
Newbie
*
Karma: 0
Posts: 17
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 216
Posts: 13664
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

played with such long ago - http://playground.arduino.cc/Code/HallEffect -
Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Connecticut, USA
Offline Offline
Full Member
***
Karma: 8
Posts: 198
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
Code:
  sensorVal = analogRead(sensorPin);
  if(sensorVal < 510){
    digitalWrite(ledPin, LOW);
  }
  else{
    digitalWrite(ledPin, HIGH);
  }
« Last Edit: October 31, 2013, 02:58:13 pm by elac » Logged

It's all about the skills

Kuala Lumpur
Offline Offline
Newbie
*
Karma: 0
Posts: 17
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
Code:
  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?
Logged

United Kingdom
Offline Offline
Tesla Member
***
Karma: 224
Posts: 6613
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
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);
  }
Logged

Formal verification of safety-critical software, software development, and electronic design and prototyping. See http://www.eschertech.com. Please do not ask for unpaid help via PM, use the forum.

Tokyo, Japan
Offline Offline
Jr. Member
**
Karma: 0
Posts: 91
Trackballer
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
  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);
  }
Logged

Kuala Lumpur
Offline Offline
Newbie
*
Karma: 0
Posts: 17
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
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?
Logged

United Kingdom
Offline Offline
Tesla Member
***
Karma: 224
Posts: 6613
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

What delay? Post all of your code.
Logged

Formal verification of safety-critical software, software development, and electronic design and prototyping. See http://www.eschertech.com. Please do not ask for unpaid help via PM, use the forum.

Connecticut, USA
Offline Offline
Full Member
***
Karma: 8
Posts: 198
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?

Logged

It's all about the skills

Kuala Lumpur
Offline Offline
Newbie
*
Karma: 0
Posts: 17
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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();           
}
Logged

Kuala Lumpur
Offline Offline
Newbie
*
Karma: 0
Posts: 17
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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? "
Logged

United Kingdom
Offline Offline
Tesla Member
***
Karma: 224
Posts: 6613
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Formal verification of safety-critical software, software development, and electronic design and prototyping. See http://www.eschertech.com. Please do not ask for unpaid help via PM, use the forum.

Connecticut, USA
Offline Offline
Full Member
***
Karma: 8
Posts: 198
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Could you please fix the post of your sketch by using the code tags.  smiley-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:
Code:
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.
Logged

It's all about the skills

Offline Offline
Newbie
*
Karma: 0
Posts: 1
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

played with such long ago - http://playground.arduino.cc/Code/HallEffect -


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.


* hall effect circuit.JPG (993.33 KB, 2048x1536 - viewed 16 times.)
Logged

Pages: [1]   Go Up
Jump to: