Hall sensor
by Juan Guillermo (Coco) Gomez

A hall sensor is a magnetic field sensor, it can detect when a magnet is nearby. This example turns on a light (LED) connected to a digital pin when a magnet is near the Hall sensor

Created 3 November 2005
Revised 4 April 2007
 

   
// Hall sensor 
// by Juan Guillermo (Coco) Gomez 

int ledPin = 48;  // diagnostic LED on the Wiring I/O board (pin 48) 
int pinHall = 0;  // Pin for the Hall sensor 
int pinLed = 1;   // Pin for the led that turns on if the magnetic field is near 
 
 
void setup() 
{ 
  pinMode(ledPin, OUTPUT); // sets the digital pin as output 
  pinMode(pinLed, OUTPUT); // sets the digital pin as output 
  pinMode(pinHall, INPUT); // sets the digital pin as input 
  digitalWrite(ledPin, HIGH); //turn on the Wiring board diagnostic LED 
} 
 
void loop() 
{ 
  if (digitalRead(pinHall) == HIGH)  // If a magnet is near the Hall sensor 
  { 
    digitalWrite(pinLed, HIGH);	     // turns ON the LED 
  } 
  else { 
    digitalWrite(pinLed, LOW);       // if not turns OFF the LED 
  } 
} 
   
 
           
           
    Circuit schematics: the LED is connected to Wiring digital pin 1.