Micropython Lesson 14: Magnetic Sensor
Micropython Lesson 14

Magnetic Sensor

Use hall effect sensor to detect magnetic fields

Components Required

hall effect sensor


Magnetic sensors are able to detect magnetic fields and process this information. The outcome on the position, angle and strength (Hall Effect) or the direction (Magneto Resistive) of an applied magnetic field can be converted into specific output signals.

Learning outcomes:

  • Using Hall Effect sensor and detect magnetic fields.
  • Applications of Hall Effect Sensor


There are actually, two different types of Hall sensors one is a Digital Hall sensor and the other is an Analog Hall sensor. The digital Hall sensor can only detect if a magnet is present or not (0 or 1). But an analog hall sensor’s output varies based on the magnetic field around the magnet that is, it can detect how strong or how far the magnet is. This project will aim only at the digital Hall sensors, for they are the most commonly used ones.

In a Hall Effect sensor, a thin strip of metal has a current applied along with it. In the presence of a magnetic field, the electrons in the metal strip are deflected toward one edge, producing a voltage gradient across the short side of the strip (perpendicular to the feed current).

Hall Effect Sensor (Magnetic Sensor)
Hall Effect Sensor (Magnetic Sensor)


Connect the magnetic sensor to the Magicbit. For this demonstration, we connect the magnetic sensor to the D32 pin of the Magicbit. After connecting the magnetic sensor to the Magicbit connect it to your pc and upload the code below.



from machine import Pin
import time

hall_sens = Pin(32, Pin.IN)

while True:


This Magnetic sensor gives digital outputs. Therefore, you can open the serial monitor and see the outputs. ‘1’ for occurred a magnetic field near to the sensor ‘0’ for there is no any considerable magnetic field nearby the sensor

Related Posts
Leave a Reply