Built-in project · Arduino Uno R3

Hall sensor on pin 3 lights the LED on 13, on an Arduino Uno

An A3144 Hall sensor on pin 3: bring the south pole of a magnet close and its output goes LOW, and the LED on pin 13 lights. The whole build, an Arduino Uno and 1 more part, runs here in your browser on the firmware below; open it in the editor to change the wiring or the code and run it again.

Beginner About 10 minutes to build and run. Runs in your browser. Free, and no account needed.

The parts retail for about $30. On Mokxi they're free.

The circuit runs here once the page has loaded.

What you'll learn

  • Hall effect sensors and magnets
  • Internal pull-up with INPUT_PULLUP
  • Active-low signals
  • Printing only when something changes
  • Difficulty: Beginner
  • Time: About 10 minutes
  • Board: Arduino Uno R3
  • Parts: 3

How it works

The A3144 Hall sensor detects a magnet. Its output only pulls low, so it cannot make a HIGH by itself. The sketch turns on the pin's built-in pull-up, which holds pin 3 HIGH until a magnet's south pole comes close. Then the output goes LOW.

The sketch lights the LED on pin 13 while the magnet is near. It also prints "magnet" or "no magnet", but only when the state changes, so the monitor does not fill up. Drag the magnet closer in the simulator to see it.

Parts list

3 parts, plus the jumper wires. They retail for about $30; on Mokxi they're free.

Wiring table

Every connection, pin by pin, read from the circuit itself. Each row joins the pin on the left to the pin on the right, by a jumper wire or a breadboard strip.

Part and pinConnects to
Hall sensor, A3144 pin GNDArduino Uno R3 pin GND
Hall sensor, A3144 pin OUTArduino Uno R3 pin 3
Hall sensor, A3144 pin VCCArduino Uno R3 pin 5V

The code

The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.

sketch.ino · Arduino Uno R3
// Hall sensor on pin 3 lights the LED on 13.
//
//   A3144 OUT -> pin 3 (internal pull-up)   VCC -> 5V   GND -> GND
//
// The sensor's output only pulls low, so the pin's own pull-up gives it its HIGH. The kit's module has that resistor on its board.
//
// Drag the magnet closer.

const int SENSOR = 3;
const int LED_PIN = 13;
int last = -1;

void setup() {
  pinMode(SENSOR, INPUT_PULLUP);
  pinMode(LED_PIN, OUTPUT);
  Serial.begin(9600);
  Serial.println("Mokxi Uno: Hall sensor on pin 3, the LED on pin 13");
}

void loop() {
  int now = digitalRead(SENSOR) == LOW ? 1 : 0;
  digitalWrite(LED_PIN, now ? HIGH : LOW);
  if (now != last) {
    last = now;
    Serial.println(now ? "magnet" : "no magnet");
  }
  delay(10);
}

Code walkthrough

The key functions of the sketch, and what each one does.

setup sketch.ino

Sets SENSOR (pin 3) to INPUT_PULLUP so the pin reads HIGH when the sensor lets go of it. Makes pin 13 an output for the LED, and starts serial at 9600 baud.

void setup() {
  pinMode(SENSOR, INPUT_PULLUP);
  pinMode(LED_PIN, OUTPUT);
  Serial.begin(9600);
  Serial.println("Mokxi Uno: Hall sensor on pin 3, the LED on pin 13");
}

loop sketch.ino

Reads pin 3. LOW means a magnet, so now becomes 1. It copies that to the LED. If now differs from last, it saves the new state and prints "magnet" or "no magnet". A 10 ms delay ends each pass.

void loop() {
  int now = digitalRead(SENSOR) == LOW ? 1 : 0;
  digitalWrite(LED_PIN, now ? HIGH : LOW);
  if (now != last) {
    last = now;
    Serial.println(now ? "magnet" : "no magnet");
  }
  delay(10);
}

Troubleshooting

The usual mistakes with this circuit, and the fix for each.

  • Nothing happens with the magnet This project
    The A3144 reacts to one pole. Try the other side of the magnet.
  • The LED is on when no magnet is near This project
    Check that OUT goes to pin 3 and the sensor has 5V and GND.
  • OUT never changes, or floats. Hall sensor
    The A3144 has an open-collector output. Add a pull-up of about 10 k ohms from OUT to VCC. VCC may be 4.5 V to 24 V. The output goes LOW near a magnet.
  • The sensor ignores the magnet. Hall sensor
    Bring the south pole to the marked face of the sensor. A north pole does nothing. If it still fails, flip the magnet or the sensor.
  • Upload fails or nothing runs after wiring pins 0 and 1 to a module. Arduino Uno R3
    Pins 0 and 1 are the USB serial port. Move the module to other pins, or unplug it while uploading. Also check the right board is selected.
  • Pins misbehave or the board resets when you power motors or servos from it. Arduino Uno R3
    Each I/O pin gives about 20 mA, 40 mA max. Power motors and servos from their own supply and join its ground to the Uno's GND.