Magnetic and Position Sensors
Magnetic and position sensors are used to detect magnetic fields, the presence or absence of magnets, the position of an object, rotation, angle, movement, or mechanical limits.
They are widely used in:
- Robotics
- Motors
- Automatic doors
- Security systems
- Industrial automation
- Encoders
- Smart locks
- Vehicle systems
- Drones
- CNC machines
- Printers
- IoT devices
Basic concept:
Magnetic Field / Mechanical Position ↓ Sensor ↓ Electrical Signal ↓ ESP32 / Arduino ↓ Processing / Logic ↓ Motor / Display / Alarm
1. What Are Magnetic Sensors?
A magnetic sensor detects the presence, strength, direction, or change of a magnetic field.
For example, place a magnet near a Hall-effect sensor:
Magnet 🧲 ↓ Magnetic Field ↓ Hall Sensor ↓ Output
The controller can then determine that the magnet is nearby.
2. What Are Position Sensors?
A position sensor determines where an object is located or how far/which direction it has moved.
Examples:
- Linear position
- Rotary position
- Angular position
- End position
- Relative movement
For example:
Motor shaft ↓ Encoder ↓ Rotation information ↓ ESP32
3. Hall-Effect Sensor
One of the most important magnetic sensors is the Hall-effect sensor.
It works using the Hall effect.
When a magnetic field is applied to a suitable semiconductor while current flows through it, a voltage can develop across the material.
Simplified:
Current → ┌─────────────┐ │ Hall Sensor │ ← Magnetic Field └─────────────┘ ↓ Hall Voltage
The electronics inside the sensor convert this effect into a useful output.
4. Digital Hall Sensor
A digital Hall sensor generally behaves like a switch.
Example:
No Magnet → LOW Magnet → HIGH
However, the exact output polarity depends on the sensor/module.
A common module is based on the A3144 Hall-effect sensor.
Typical applications:
- Magnet detection
- Door position
- Motor rotation
- Wheel speed
- Limit detection
5. Hall Sensor with ESP32
Basic connection:
Hall Sensor ESP32 VCC ──────────── 3.3V* GND ──────────── GND OUT ──────────── GPIO 27
* Check the exact sensor/module specifications before powering it.
Example:
#define HALL_PIN 27 void setup() { Serial.begin(115200); pinMode(HALL_PIN, INPUT); } void loop() { int state = digitalRead(HALL_PIN); if (state == LOW) { Serial.println("Magnet Detected"); } else { Serial.println("No Magnet"); } delay(200); }
The logic may be reversed depending on the particular Hall sensor.
6. Hall Sensor for Motor Rotation
A magnet can be attached to a rotating wheel.
🧲 \ \ Rotating wheel ┌─────────┐ │ │ └─────────┘ ↓ Hall Sensor
Every time the magnet passes the sensor:
Magnet passes ↓ Hall pulse ↓ ESP32 ↓ Count pulses
If one magnet produces one pulse per revolution:
RPM = Revolutions per minute
For example, if 10 revolutions occur in 1 second:
10 × 60 = 600 RPM
7. Reed Switch
A reed switch is a magnetically operated mechanical switch.
Inside a typical reed switch are two metal contacts inside a sealed glass tube.
Glass Tube ┌─────────────────────┐ │ Contact Contact│ │ \ / │ └─────────────────────┘
When a magnet approaches:
Magnet 🧲 ↓ Contacts change state
Depending on the type, the contacts may open or close when the magnet is nearby.
8. Reed Switch Example
A very common application is a door sensor.
Door Frame Door ┌────────┐ ┌──────┐ │ Reed │ 🧲 │Magnet│ │ Switch │ │ │ └────────┘ └──────┘
When the door closes:
Magnet near reed switch ↓ Switch changes state ↓ ESP32 ↓ Door = CLOSED
When the door opens:
Magnet moves away ↓ Switch changes state ↓ Door = OPEN
9. Reed Switch vs Hall Sensor
| Feature | Reed Switch | Hall Sensor |
|---|---|---|
| Technology | Mechanical magnetic contact | Semiconductor |
| Moving parts | Yes | No |
| Wear | Possible | Very low |
| Speed | Limited compared with electronic sensing | High |
| Power | Can operate as a passive switch | Requires power for typical IC versions |
| Common use | Door/window sensor | Speed/rotation/magnetic detection |
10. Analog Hall Sensors
Not all Hall sensors are simply ON/OFF.
An analog Hall sensor can provide an output related to magnetic-field strength.
Concept:
Weak Magnetic Field ↓ Small Output Change Strong Magnetic Field ↓ Larger Output Change
This allows applications such as:
- Magnetic field measurement
- Current sensing
- Position measurement
- Motor control
11. Magnetic Field Sensor
Magnetic sensors can detect:
- Presence of a magnet
- Magnetic-field strength
- Magnetic-field direction
- Rotation
- Position
More advanced magnetic sensors can measure multiple axes.
For example:
X-axis → Magnetic field Y-axis → Magnetic field Z-axis → Magnetic field
These are useful for electronic compasses and orientation systems.
12. Magnetometer
A magnetometer measures magnetic fields.
A common example is the HMC5883L, while newer sensor modules such as the QMC5883L are also commonly encountered in hobby projects.
A 3-axis magnetometer can measure:
X → Magnetic field Y → Magnetic field Z → Magnetic field
This can be used to estimate heading.
13. Electronic Compass
A magnetometer can act as an electronic compass.
Concept:
Earth's Magnetic Field ↓ Magnetometer ↓ ESP32 ↓ Direction
For example:
North ↑ | | 🚗
The controller can estimate the direction the device is facing.
However, nearby metal, motors, magnets, and electrical currents can distort the magnetic field.
14. Potentiometer as a Position Sensor
A potentiometer is a variable resistor.
It has three main terminals:
Potentiometer ┌─────────┐ VCC ─┤ ├─ GND │ ↑ │ │ Wiper │ └────┬────┘ │ OUT
The wiper position changes the output voltage.
Therefore:
Knob Position ↓ Resistance Ratio ↓ Output Voltage ↓ ADC ↓ Digital Value
This allows the controller to estimate the knob's position.
15. Potentiometer with ESP32
Example:
#define POT_PIN 34 void setup() { Serial.begin(115200); } void loop() { int value = analogRead(POT_PIN); Serial.println(value); delay(200); }
The exact ADC behavior and usable resolution depend on the ESP32 variant and configuration.
16. Potentiometer Applications
A potentiometer can be used as:
- Volume control
- Motor speed control
- Servo angle input
- Menu control
- Brightness control
- Position input
Example:
Potentiometer ↓ ESP32 ↓ Servo
Rotate the potentiometer:
0° → Servo 0° 50% → Servo ~90° 100% → Servo ~180°
The exact mapping depends on software and servo limits.
17. Rotary Encoder
A rotary encoder detects rotational movement.
Unlike a simple potentiometer, an encoder can provide information about:
- Direction
- Relative rotation
- Number of steps
A common incremental encoder has:
A B
signals.
Sometimes it also has:
SW
for a push-button.
18. How a Rotary Encoder Works
When the shaft rotates:
Rotation ↓ Encoder ↓ A and B pulses ↓ ESP32 ↓ Direction + Position
The A and B signals are offset in phase.
Simplified:
A: ──██──██──██── B: ────██──██──██
The relationship between A and B tells the controller which direction the shaft is rotating.
19. Rotary Encoder Applications
Used in:
- CNC machines
- 3D printers
- Robot wheels
- Motor control
- Digital menus
- Volume knobs
- Industrial machines
Example:
Rotate clockwise ↓ Encoder ↓ Increase value Rotate counter-clockwise ↓ Encoder ↓ Decrease value
20. Incremental vs Absolute Encoder
Incremental Encoder
Reports changes in position.
Movement → Pulses
The controller usually needs to maintain a count.
Absolute Encoder
Reports an actual position value.
Shaft Position ↓ Encoder ↓ Absolute Position
Even after power loss, an absolute encoder can provide its position when read, depending on its technology and mechanical setup.
21. Limit Switch
A limit switch detects whether a mechanical object has reached a particular position.
Example:
Moving Mechanism ↓ ↓ ───────┐ │ [Limit Switch]
When the mechanism reaches the switch:
Mechanical contact ↓ Switch activated ↓ ESP32 ↓ STOP MOTOR
This is extremely important in:
- CNC machines
- 3D printers
- Linear actuators
- Robotic arms
- Automatic doors
22. Limit Switch for Motor Safety
Suppose a motor moves a platform.
Motor → Platform →→→→→ [LIMIT]
Without a limit switch:
Motor keeps running ↓ Mechanical collision ↓ Possible damage
With a limit switch:
Platform reaches limit ↓ Switch activated ↓ ESP32 ↓ Motor STOP
23. Linear Position Sensors
Some systems need to know linear position rather than rotation.
Examples include:
- Linear potentiometer
- Linear encoder
- LVDT
- Magnetic linear sensor
Example:
Moving Object ↓ ─────────────●───────────── ↑ Position Sensor
The sensor tells the controller where the object is along the axis.
24. Angle Sensors
An angle sensor measures angular position.
For example:
Arm / / ● ↑ Rotation
Applications:
- Robotic arms
- Steering systems
- Servo mechanisms
- Joysticks
- Industrial machines
Technologies include:
- Potentiometers
- Magnetic angle sensors
- Encoders
- Optical encoders
25. Magnetic Position Detection
A magnet and Hall sensor can work together as a position detector.
Moving Part 🧲 ↓ Hall Sensor ↓ ESP32
Multiple Hall sensors can provide multiple positions:
Position 1 → Hall Sensor 1 Position 2 → Hall Sensor 2 Position 3 → Hall Sensor 3
This can be useful for detecting the position of mechanical components.
26. Wheel Speed Measurement
A magnet can be attached to a wheel.
🧲 ┌───────┐ ┌──┤ Wheel ├──┐ │ └───────┘ │ │ │ └──────↓──────┘ Hall Sensor
Every rotation produces a pulse.
If wheel circumference is known:
Speed = Distance / Time
For one pulse per revolution:
Distance per pulse = Wheel Circumference
Then the ESP32 can calculate speed.
27. Smart Door Security
Magnetic sensors are excellent for door/window monitoring.
Door │ ├── Magnet │ └── Reed/Hall Sensor ↓ ESP32 ↓ Security
Example logic:
Door CLOSED ↓ Normal Door OPEN ↓ Sensor triggered ↓ ESP32 ↓ Alarm / Notification
28. Magnetic vs Position Sensors
These categories overlap.
| Sensor | Main Purpose |
|---|---|
| Hall sensor | Magnetic field / magnet detection |
| Reed switch | Magnetic ON/OFF switching |
| Magnetometer | Magnetic-field measurement |
| Potentiometer | Position/angle |
| Rotary encoder | Rotation/direction |
| Limit switch | End-position detection |
| Linear encoder | Linear position |
| Magnetic encoder | Rotation/position |
29. Practical ESP32 Project
Smart Door Position Monitor
Components:
- ESP32
- Reed switch
- Magnet
- Buzzer
- LED
Connection concept:
Reed Switch ↓ ESP32 GPIO ↓ ┌────┴─────┐ ↓ ↓ LED Buzzer
Logic:
Door Closed ↓ LED OFF Buzzer OFF Door Open ↓ LED ON Buzzer ON
30. Motor Position Control
A more advanced system can combine:
Motor ↓ Encoder ↓ ESP32 ↓ Motor Driver ↓ Motor
This creates a feedback system:
Desired Position ↓ Controller ↓ Motor Driver ↓ Motor ↓ Encoder ↓ Actual Position ↓ Feedback └────────→ Controller
This is the basic idea behind closed-loop control.
31. Open Loop vs Closed Loop
Open Loop
Controller ↓ Motor
The controller doesn't directly measure the actual position.
Closed Loop
Controller ↓ Motor ↓ Encoder ↓ Feedback └────→ Controller
Closed-loop systems can correct errors based on feedback.
32. Common Problems
Hall Sensor
- Wrong magnet orientation
- Insufficient magnetic field
- Incorrect output logic
- Electrical noise
Reed Switch
- Mechanical wear
- Contact bounce
- Incorrect magnet distance
Encoder
- Missed pulses at high speed
- Incorrect A/B interpretation
- Electrical noise
- Software counting errors
Potentiometer
- Mechanical wear
- Noisy ADC readings
- Poor connections
Limit Switch
- Contact bounce
- Incorrect switch placement
- Mechanical damage
33. Sensor Selection Guide
Choose Hall sensor when you need:
Magnet detection or rotational sensing.
Choose reed switch when you need:
Simple magnetic ON/OFF detection.
Choose magnetometer when you need:
Magnetic-field measurement or compass functionality.
Choose potentiometer when you need:
Simple manual position/angle input.
Choose rotary encoder when you need:
Rotation, direction, and relative position.
Choose limit switch when you need:
Mechanical end-position detection.
Choose linear encoder when you need:
Precise linear position measurement.
Key Points to Remember
Hall Sensor → Detects magnetic fields Reed Switch → Magnetically operated switch Magnetometer → Measures magnetic field Potentiometer → Measures position/angle through voltage Rotary Encoder → Measures rotation and direction Limit Switch → Detects mechanical end position Linear Encoder → Measures linear position Magnetic Encoder → Measures rotational position using magnetic sensing
The overall concept is:
MAGNET / MOVEMENT / POSITION ↓ ┌───────────┼───────────┐ ↓ ↓ ↓ Hall Reed Encoder ↓ ↓ ↓ Magnetic ON/OFF Position Field State /Rotation └───────────┼───────────┘ ↓ ESP32 ↓ Decision / Control ↓ Motor / Alarm / Display