How to detect magnetic field | Hall effect Sensor RPI

Hello Tech Folks, I’m back again with another interesting article on Raspberry Pi, Today we’re going to discuss hooking up the How to detect magnetic field. This project is very simple to make and very interesting, also the components used in this tutorial are easily available in the market but the only thing which can be proved costly is Raspberry Pi Board itself(Pi 3B+ using here).

If you’re a beginner, then must visit my Raspberry Pi article series for an introduction to Raspberry Pi Board and Python from scratch.

HTML Image as link

What is Linear Hall-Effect Sensor:

The hall-Effect sensor can be the easiest method to detect Magnetic fields in a region. There are many module ranging from the cheapest to the high-priced version of the Hall effect sensor. The version I’m using here is very basic and can be easily found in any online or offline store.

The module has great sensitivity for weak as well as strong magnets. this hall effect sensor we are using. and we will describe how to detect the magnetic field. Also, its sensitivity can be adjusted by tuning the 10k pot available on the module onboard. It gives both types of output, Analog and digital.

To enhance the function of the sensor and to make the analog value available at the output it is paired with an LM393 Op-amp which does a perfect job in both analog and digital signals. The onboard pot(potentiometer) described above actually is connected to a hall effect transistor-type component in series with GND to give a combined signal to the op-amp.

Material Required for detect magnetic field

Hall Effect Sensor RPI
  • Hall-Effect Sensor
  • Raspberry Pi (with keyboard, display, and mouse)
  • Breadboard
  • Jumper wires
  • A magnet for testing
S.NoComponent RequiredQuantityBuy Link
1.Raspberry pi 3B+1Amazon
2.Raspberry pi Cable1Amazon
3USB Mouse & Keyboard1Amazon
4.Raspberry pi Combo1Amazon
5.Jumper Wire40Amazon
7.Power supply1Amazon
9.220 Ohm Resistor1Amazon
10.Hall Effect Sensor1Amazon

Circuit Diagram

hall effect sensor with rpi circuit

Hall Effect Sensor D0 Pin to Board pin number 11 & LED to pin number 13 of Raspberry Pi according to BOARD numbering

Raspberry PI

Hall Effect Sensor

GPIO 11 Pin



+5 V, VCC



Raspberry PI


220-ohm Resistor

GPIO 13 Pin

Anode Terminal ( + )




Terminal 1


Cathode Terminal ( – )

Terminal 2

Hall effect Python Code


From the beginning, if we observe the code then we’ll see that n the first two ones we have imported two important modules in the program RPI & time module which is useful and import for defining pins and time for calculation of time(sleep).

Then from the next line, we define pin mode which can be altered from BOARD or BCM, the difference between the two is clearly defined in my previous articles. For more clarification, visit my series of Raspberry Pi mentioned above.

Then in the while loop, we give the main code which performs all conditions.


 import RPi.GPIO as gp  
 from time import sleep  
 while True:  
     if gp.input(11)==1:  
     elif gp.input(11)==0:  
   except KeyboardInterrupt:  

With this, we have completed our project Hall Effect Sensor RPI. I hope you enjoyed it and for any queries comment down below.

Leave a Comment