Magnetic Field Visualizer Using Hall Effect Sensor OLED & Arduino

In this project, we will build a Magnetic Field Visualizer using an analog Hall effect sensor, SSD1306 OLED display, and Arduino UNO. This simple yet educational project graphically shows the magnetic field strength and polarity on an OLED screen. As you bring a magnet near the sensor, a bar graph fills left or right depending on the magnetic field direction (North or South), making it an excellent project to understand how magnetic fields interact with Hall sensors.

PCBWay offers high-quality PCB prototyping and assembly at an affordable price, starting at just $5 for 5 PCBs. With fast turnaround, great customer support, and easy online ordering, it’s a top choice for hobbyists and professionals alike.

Hardware Components

You’ll need the following hardware components to get started:

ComponentQuantityDescription
Arduino UNO1Main microcontroller board
Analog Hall Effect Sensor1AH49E or equivalent analog sensor
SSD1306 OLED Display (I2C)1128×64 pixels OLED module
Breadboard1For easy prototyping
Jumper Wires~10Male-to-male wires
USB Cable1For uploading code and powering Arduino
Small Magnet1To test the sensor

Schematic

Make connections according to the circuit diagram given below.

Wiring / Connections

Hall EffectOLED SSD1306Arduino UNO
Ground (-)GNDGND
VCC (+)VCC3V3
Signal (S)A0
SDAA5
SCLA4

Arduino Code

#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1

#define HALL_SENSOR_PIN A0  // Hall sensor analog output connected to A0

Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

int baseline = 0;

void setup() {
  Serial.begin(9600);

  // Initialize I2C and OLED display
  Wire.begin();
  if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
    Serial.println("SSD1306 initialization failed!");
    while (true);
  }

  pinMode(HALL_SENSOR_PIN, INPUT);

  // Calibrate sensor baseline
  display.clearDisplay();
  display.setTextColor(SSD1306_WHITE);
  display.setCursor(0, 10);
  display.setTextSize(1);
  display.println("Calibrating...");
  display.display();

  long total = 0;
  for (int i = 0; i < 100; i++) {
    total += analogRead(HALL_SENSOR_PIN);
    delay(10);
  }
  baseline = total / 100;

  display.clearDisplay();
  display.setCursor(0, 10);
  display.println("Baseline set:");
  display.print("Base: ");
  display.println(baseline);
  display.display();
  delay(2000);
}

void loop() {
  int reading = analogRead(HALL_SENSOR_PIN);
  int delta = reading - baseline;

  display.clearDisplay();
  display.setTextColor(SSD1306_WHITE);

  // Bar display layout
  int barY = 20;
  int barHeight = 20;
  int barWidth = 124;
  int centerX = SCREEN_WIDTH / 2;

  // Draw bar outline
  display.drawRect(centerX - barWidth / 2, barY, barWidth, barHeight, SSD1306_WHITE);

  // Calculate and constrain fill width
  int fillAmount = map(abs(delta), 0, 500, 0, barWidth / 2);
  fillAmount = constrain(fillAmount, 0, barWidth / 2);

  // Fill bar left or right based on polarity
  if (delta > 0) {
    display.fillRect(centerX, barY + 1, fillAmount, barHeight - 2, SSD1306_WHITE);  // North
  } else if (delta < 0) {
    display.fillRect(centerX - fillAmount, barY + 1, fillAmount, barHeight - 2, SSD1306_WHITE);  // South
  }

  // Display text labels
  display.setTextSize(1);
  display.setCursor(centerX - barWidth / 2, barY + barHeight + 5);
  display.print("S (-)");

  display.setCursor(centerX + barWidth / 2 - 30, barY + barHeight + 5);
  display.print("N (+)");

  display.setCursor(centerX - 10, barY - 10);
  display.print("NEUTRAL");

  // Show numeric strength
  display.setCursor(0, 0);
  display.print("Field:");
  display.setCursor(40, 0);
  display.print(abs(delta));

  display.display();
  delay(100);
}

Conclusion

This project is a fun and educational way to visualize magnetic fields using a Hall effect sensor and OLED display. It’s beginner-friendly and gives you insight into how analog sensors work with microcontrollers. It can be extended further into a digital compass, proximity magnetic switch, or rotational sensing system for motors.

Whether you’re a student, hobbyist, or maker, this is a great addition to your collection of Arduino-based sensor projects.

If you liked this project, follow more on circuit-diy.com for tutorials, projects, and practical circuits for makers like you!