Hardware
Software
Knowledge
Project description
Component description
Arduino Uno - another type of arduino board that is used commonly by specialist and hobbyist because of its robust design. It is equipped with a microcontroller board that is based on ATMega328P with 14 advanced I/O pins (6 are PWM outputs), 6 analog inputs, 16 Mhz quartz crystal, a power jack, a USB port, an ICSP header and a reset button.Reference
Hall effect sensor - This is a special sensor that works on the hall effect principle. The principle states that, when a conductor/semiconductor with current flowing in one direction is placed perpendicular to a magnetic field, there would be a generated voltage at right angles to the current path. This change in voltage can be used by the arduino to detect if there is a magnet or not.Reference.
There are two main types of hall effect sensor: The latching and the non-latching hall effect sensor. The latching hall effect sensor(44E) gives out a HIGH output whenever the north pole of the magnet is placed nearby and it will not stop sending HIGH output even if the magnet is remove, only the south pole of the magnet can stop it from giving an output of HIGH. The other type of the sensor is the non-latching hall effect sensor (US5881 or US1881), where it sends a HIGH output whenever the north pole of the magnet is placed close to it and after removing the magnet, it will automatically send a LOW output without the need for the south pole of the magnet. Reference
This are the pin outs of the hall effect sensor: VCC, GND, D0(digital output). It is hooked with a 10 kilo ohm resistor to pull the output of the hall sensor to 5 volts.
Using a Hall effect sensor
Step 1: Gather the parts
Step 2: Construct the circuit
Connect the source
Connect the sensor
-Connect the 10 kilo ohms resistor to the ground and vcc pin of the hall effect sensor. This is to pull the output of the hall sensor to 5 volts.
Connect the LED
-Connect the Anode(+) of the LED to a 220 ohm resistor that is connected to pin 4 of the arduino uno board. This resistor will protect the LED from over supply of current.
-Connect the Cathode(-) of the LED to the common ground.
Step 3: Programming
const byte hallsensorPin = 3; //sets the hall sensor @pin 3
const byte ledPin = 4; //sets the LED @pin 4
volatile byte state = LOW; //sets the state to 0
int value = 0; //defines the value variable
void setup() {
pinMode(hallsensorPin, INPUT_PULLUP); //sets the hall sensor as the input
pinMode(ledPin, OUTPUT); //sets the LED as the output
attachInterrupt(digitalPinToInterrupt(hallsensorPin), toggle, CHANGE); //Initialize the interrupt pin @pin 3
}
void loop() {
digitalWrite(ledPin, state); //sets the LED to change depending on the value of the state variable
}
void toggle() {
state = !state; //changes the value of the state from 0 to 1 or 1-0
value++; //increments the value
}
What this code does is it first sets the pins of the sensor and the LED, then it defines the 'state' and 'value' variables to be LOW at the start. In the setup command, it sets the hall sensor to be the input and the LED to be the output. In the
attachInterrupt(digitalPinToInterrupt(hallsensorPin), toggle, CHANGE);, it uses the interrupt pin of the arduino uno board (pins 2 & 3). When the hallsensorpin is interrupted by the magnetic field, it will trigger the toggle functionvoid toggle()which will then change the output from 0 - 1 or from 1 - 0. In the loop command, the LED is set to change whenever the 'state' changes its value from HIGH to LOW.
Step 4: Testing
Check out my other arduino tutorials below: