Hall Effect Sensor Demo
This demo shows how to use a Hall effect sensor with an Arduino Uno.
Hall effect sensors detect the presence of a magnetic field. Two different types of Hall effect sensors are used in the demo, as shown in the following photo:
On the right of the above photo is a "bare" sensor. Left to right it's pins are: 5V, GND and the signal pin. The signal pin must pulled high to 5V via a suitable resistor. 10kΩ is recommended for pull up resistors when using an Uno, although the 5.1kΩ used in this demo is suitable and works just fine. The left hand component has a Hall effect sensor contained in a module. This module includes a pull up resistor for the signal pin. It also has a LED that illuminates when a magnetic field is detected. The pinout for the module, left to right is GND, 5V and the signal pin.
Both types of sensor keep the signal pin pulled high in the absence of a magnetic field. When a field is detected then the signal pin is set low.
Some versions of the Hall effect sensor module have the order of the pins reversed. Check the markings on the module to see which is the correct order.
The following diagram shows both types of sensor connected to an Arduino Uno. Two LEDs are used to indicate when their adjacent sensors detect a magnetic field. The LEDs have their cathodes connected to ground via 470Ω resistors. The bare sensor has its signal pin pulled up to 5V via a 5.1Ω resistor. No pull-up resistor is required for use with the sensor module.
The Uno is connected as follows:
Arudino pin | Attached to |
---|---|
5V | 5V pins of both sensors and the bare sensor's pullup 5.1kΩ resistor. |
GND | Ground pins of both sensors and the 470Ω resistors connected to the cathodes of each LED. |
8 | Signal pin of the bare sensor. |
9 | Signal pin of the sensor module. |
10 | Anode of the LED associated with the bare sensor. |
11 | Anode of the LED associated with the sensor module. |
The Uno digital pin connections used above are arbitrary: any digital pins can be used providing the demo code is changed accordingly.
The demo source code is available from the cahamo/demo-projects
repository on GitHub. The relevant code will be found in the hall-effect-sensor--uno
directory. All the C++ code is in hall-effect-sensor--uno/src/main.cpp
.
This demo is a PlatformIO project for use with Visual Studio Code. It can be modified for use with the Arduino IDE simply by copying the code from hall-effect-sensor--uno/src/main.cpp
into a new, blank Arduino API project. No Arduino libraries are required.
Once the code is compiled and uploaded to the Uno bring a suitably oriented† magnet close to each sensor in turn and observe that the associated LED will illuminate. The code also writes the state of each sensor to the Uno's serial port. In order to keep the serial output readable, each sensor is checked only 3 times per second. Therefore there may be a lag between the sensor module's built-in LED changing state and the time associated LED on the breadboard follows suit.
The next image shows the demo circuit as built on a breadboard, with power supplied via the Uno's USB connector. The left hand image shows the circuit with no magnet present: no LEDs are illuminated. The right hand image shows a magnet close to both sensors. Notice that the LEDs are lit, showing that both sensors have detected the magnetic field†. The small LED on the sensor module is also lit.
† Hall effect sensors usually only detect a magnet when its poles are oriented in a certain way relative to the sensor. When the magnet shown in the photo was turned around it was not detected.
Finally, here is a video of the demo in action: