This experiment aims to measure the magnetic field's strength and detect its presence using a Hall effect sensor and a Raspberry Pi Pico. A Hall effect sensor outputs a voltage proportional to the magnetic field strength, which allows us to detect the magnitude and polarity of the magnetic field. Additionally, the sensor's digital output indicates whether a magnetic field is present or not. The Pico reads both analog and digital outputs, providing detailed insights into the magnetic field.
Scope:
This experiment covers:
Measuring the magnetic field strength using the analog output of the Hall effect sensor.
Detecting the presence of a magnetic field using the sensor's digital output.
Implementing the solution in both Embedded C and MicroPython.
Providing insights into the working of magnetic sensors in real-world applications like proximity sensing, motor control, and more.
Prerequisite:
3.1. Hardware:
Pico microcontroller
Linear Hall effect sensor module (with analog and digital output).
Breadboard
Jumper wires
USB cable (to connect the computer)
3 .2. Software:
Thonny Software(For micro python)
MicroPython firmware installed on Raspberry Pi Pico
ubuntu Terminal(For embedded C)
CMake,Pico SDK,GNU Arm Embedded Toolchain,WSL.
Connection Details:
Analog Output (AO) from the Hall effect sensor → Pico ADC pin (e.g., GP26)
Digital Output (DO) from the Hall effect sensor → Pico GPIO pin (e.g., GP15)
VCC of the Hall effect sensor → 3.3V on Pico
GND of the Hall effect sensor → GND on Pico
UART Serial Console: Use a micro-USB cable to connect the Pico to the PC, then open PuTTY and select the correct COM port and baud rate (115200).

Working Principle:
Hall Sensor Module
The Hall Sensor module is a magnetic non-contact sensor that produces an electrical signal proportional to the applied magnetic field. It can measure both north and south polarity of a magnetic field and the relative strength of the field. It’s used for detecting magnetic fields, acting like a magnet detector that can identify nearby magnets. This sensor is useful in various projects, such as developing door alarm systems or measuring the speed of rotating objects.
Principle
The working principle of the Hall Sensor module is based on the Hall effect , discovered by Edwin Hall. Here’s how it works in simple terms: when electricity flows through a conductor (like a wire), and there’s a magnetic field around it, the magnetic field pushes the moving electrons in the conductor to one side. This creates a voltage difference across the conductor - this is the Hall Effect.
In the Hall Sensor module, when a magnet comes close, the magnetic field affects the electrons in the semiconductor material inside the sensor. This changes the voltage across the sensor, which the sensor detects. The Arduino can read this voltage change and understand whether there’s a magnet nearby and how strong its magnetic field is.
The Hall Sensor module is equipped with a 49E Linear Hall-Effect Sensor, capable of measuring both the north and south polarity of a magnetic field as well as the relative strength of the field. The output pin provides an analog representation indicating the presence and strength of a magnetic field, along with its polarity (north or south). When no magnetic field is present, the 49E outputs a voltage around half of the source voltage. If the south pole of a magnet is placed near the labeled side of the 49E (the side with text etched on it), then the output voltage will linearly increase towards the source voltage in proportion to the strength of the applied magnetic field. Conversely, if you place a north pole near this side, then there will be a linear decrease in output voltage relative to the strength of that magnetic field.
For instance, when powering 49E with 5V and no magnetic field present, its output will be approximately 2.5V. In this scenario, placing a strong magnet’s south pole near it would cause an increase in output voltage up to around 4.2V; while placing its north pole nearby would result in dropping down to about 0.86V from source based on their respective strengths.
Application code:
MicroPython:
from machine import ADC, Pin
from time import sleep
# Setup analog pin (GP26)
analog_pin = ADC(26)
# Setup digital pin (GP15)
digital_pin = Pin(15, Pin.IN)
while True:
# Read analog value (magnetic field strength)
analog_value = analog_pin.read_u16()
voltage = (analog_value / 65535) * 3.3 # Convert ADC value to voltage
print("Magnetic field strength (analog): {:.2f} V".format(voltage))
# Read digital value (magnetic presence)
if digital_pin.value() == 1:
print("Magnetic field detected (digital): Present")
else:
print("Magnetic field detected (digital): Not Present")
sleep(1) # Delay of 1 second
Embedded C:
#include "pico/stdlib.h"
#include "hardware/adc.h"
#include <stdio.h>
#define ANALOG_PIN 26 // GP26 (ADC0)
#define DIGITAL_PIN 15 // GPIO15
int main() {
stdio_init_all();
// Initialize digital GPIO pin for magnetic presence detection
gpio_init(DIGITAL_PIN);
gpio_set_dir(DIGITAL_PIN, GPIO_IN);
// Initialize ADC
adc_init();
adc_gpio_init(ANALOG_PIN); // Connect ADC pin
adc_select_input(0); // Select ADC0 (GP26)
while (1) {
// Read analog value (magnetic field strength)
uint16_t analog_value = adc_read();
float voltage = (analog_value * 3.3) / (1 << 12); // Convert ADC to voltage (assuming 3.3V reference and 12-bit ADC)
printf("Magnetic field strength (analog): %.2f V\n", voltage);
// Read digital value (magnetic presence)
bool magnetic_presence = gpio_get(DIGITAL_PIN);
if (magnetic_presence) {
printf("Magnetic field detected (digital): Present\n");
} else {
printf("Magnetic field detected (digital): Not Present\n");
}
sleep_ms(1000); // Delay of 1 second
}
return 0;
}
Hardware Image:

Output:
Analog Output: Displays the magnetic field strength in voltage (e.g., 2.45V) to the console.
Digital Output: Indicates whether the magnetic field is present ("Magnetic field detected: Present") or absent ("Magnetic field detected: Not Present").
Video Demonstration:
Appendix:
Common CMakeLists:
cmake_minimum_required(VERSION 3.12)
# Pull in SDK (must be before project)
include(pico_sdk_import.cmake)
project(pico_experiments C CXX ASM)
set(CMAKE_C_STANDARD 11)
set(CMAKE_CXX_STANDARD 17)
if (PICO_SDK_VERSION_STRING VERSION_LESS "1.3.0")
message(FATAL_ERROR "Raspberry Pi Pico SDK version 1.3.0 (or later) required. Your version is ${PICO_SDK_VERSION_STRING}")
endif()
# Initialize the SDK
pico_sdk_init()
add_compile_options(-Wall
-Wno-format # int != int32_t as far as the compiler is concerned because gcc has int32_t as long int
-Wno-unused-function # we have some for the docs that arent called
)
if (CMAKE_C_COMPILER_ID STREQUAL "GNU")
add_compile_options(-Wno-maybe-uninitialized)
endif()
# Hardware-specific examples in subdirectories:
add_subdirectory(expt_10_LCD)
CMakeLists:
cmake_minimum_required(VERSION 3.13)
# Define the project
project(magnetic_field_sensor C CXX ASM)
# Include the SDK
include(pico_sdk_import.cmake)
# Initialize the SDK
pico_sdk_init()
# Add executable
add_executable(magnetic_field_sensor main.c)
# Link Pico SDK libraries
target_link_libraries(magnetic_field_sensor pico_stdlib hardware_adc)
# Enable USB output
pico_enable_stdio_usb(magnetic_field_sensor 1)
pico_enable_stdio_uart(magnetic_field_sensor 0)
# Create binary and hex output
pico_add_extra_outputs(magnetic_field_sensor)
References:
| # | Topic | Date & time | Action |
|---|
0% Completed (0/1)