Using a Hall effect sensor with Arduino

A Hall effect sensor is a solid state magnetic switch and can be used to switch a circuit in the presence of a magnetic field. A low cost Keyes KY-003, unit is used here as there seems to be a number of these Keyes sensor boards at low cost on eBay. This board has a single digital output that goes LOW when a magnetic field is detected. Note, each side of the sensor reacts to a different magnetic pole, so if nothing seems to happen use the other pole of the magnet.

/home/wpcom/public_html/wp-content/blogs.dir/10d/72092500/files/2014/12/img_1556.jpg

The following sketch turns on the onboard LED when a magnet is brought near to the sensor.


/***************************************************

Hall Effect magnetic switch, a magnet is used to turn on the onboard Led

Hall Effect board is from Keyes. KY-003

Connection:
Pin 1 connect to Arduino Gnd
Pin 2 connect to Arduino 5 volts
Pin 3 marked S connect to Arduino pin 2

***************************************************/

# define hallPin 2
# define ledPin 13

void setup() {
Serial.begin(9600);
pinMode(hallPin, INPUT);
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, HIGH);
}

void loop() {
if (! digitalRead(hallPin)) {
Serial.println("Magnetic field detected");
digitalWrite(ledPin, LOW); // turn on LED
}
else{
digitalWrite(ledPin, HIGH);
}
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s