Hall effect sensor/Reading it/05. The first read
Reading it · 05 of 10

The first read

Three wires, no library and a sketch that prints what the pin says five times a second. With no magnet it prints HIGH; bring a magnet to the chip and it prints LOW, and the red LED on the block lights with it.

Three wires

Three wires
Your board
Wires
3
VCC to
3V3
SIGNAL to
GPIO 4
GND to GND, VCC to 3V3, SIGNAL to GPIO 4. With no magnet near, SIGNAL sits at VCC, so on a 3.3 V board VCC has to be 3V3, never 5V. GPIO 4 is a plain input the ESP32-S3 does not read at boot, so a magnet left beside the block cannot change how it starts.

GND to GND. VCC to your board's logic supply: 5V on an Uno, 3V3 on the three 3.3 V boards. SIGNAL to a digital pin. NC stays unconnected.

The input pin is the same in every sketch in this book, and the same as in the push button's: D2 on an Uno, GPIO 25 on an ESP32, GPIO 4 on an ESP32-S3, GP15 on a Pico. None of them is read by the chip at boot or tied up with its flash memory. That matters more for this block than for a button, because a magnet left beside it holds SIGNAL LOW through a reset, and on a pin the chip reads at boot that could change how it starts.

The sketch

Two calls do the work. pinMode(HALL_PIN, INPUT) makes the pin an input with nothing of its own switched on: the chip drives SIGNAL both ways, and the board has a pull-up anyway. digitalRead(HALL_PIN) returns HIGH or LOW, and on this block LOW is the magnet.

Written out, the test in the sketch is level == LOW. If you have seen this block's code elsewhere testing for HIGH, it was wrong.

What you should see

The serial monitor at 115200 prints a line five times a second. Bring a magnet to the small chip at the top left of the block, flat face first, and take it away again:

HIGH  no magnet
HIGH  no magnet
LOW   magnet
LOW   magnet
HIGH  no magnet

The red LED on the block lights for exactly as long as the lines say LOW. If nothing changes, bring the magnet closer: a small magnet has to come within about a centimetre, and how far a magnet reaches works out why.

The code

No library. pinMode makes the pin an input, and digitalRead returns HIGH or LOW. Change HALL_PIN to the pin you wired SIGNAL to.

hall_first_read.ino
/*
  Hall Effect Sensor - first read                        TK18 / /p/tk18

  Wiring. Count from the square pad on the TinkerBlock board, parts
  up, header at the bottom:

    GND    -> GND
    VCC    -> 5V on an Uno; 3V3 on an ESP32, ESP32-S3 or Pico
              (with no magnet, SIGNAL sits at whatever VCC is)
    NC     -> nothing   (unconnected on the board)
    SIGNAL -> D2 on an Uno, GPIO 25 on an ESP32, GPIO 4 on an
              ESP32-S3, GP15 on a Raspberry Pi Pico

  Arduino IDE
    Tools > Board                 your board, e.g. ESP32S3 Dev Module
    Tools > Port                  the one that appears when you plug in
    Tools > USB CDC On Boot       Enabled   (ESP32-S3 only)
    No library needed.
*/

// The GPIO number SIGNAL is wired to.
// Uno: 2. ESP32: 25. ESP32-S3: 4. Pico: 15.
const int HALL_PIN = 4;

void setup() {
  Serial.begin(115200);
  pinMode(HALL_PIN, INPUT);     // the chip drives SIGNAL both ways
}

void loop() {
  int level = digitalRead(HALL_PIN);

  if (level == LOW) {           // active low: LOW is a magnet
    Serial.println("LOW   magnet");
  } else {
    Serial.println("HIGH  no magnet");
  }
  delay(200);                   // five reads a second, to keep it readable
}

LOW means a magnet on this block: the chip is active low. INPUT is enough, because the chip drives SIGNAL both ways and the board has its own pull-up. The delay(200) only keeps the output readable.

When it does not work

It prints HIGH whatever I do with the magnet.

Look at the red LED. If it never lights, the chip is not seeing the magnet: hold it over the small chip at the top left, a flat face towards it, almost touching. If the LED lights and the monitor still says HIGH, SIGNAL is on a different pin from the one HALL_PIN names.

It prints LOW when there is no magnet.

If the red LED is lit, something magnetic really is close to the chip: move the block away from speakers, tools and phone cases. If the LED is dark, VCC is probably not connected, and SIGNAL is drifting.

It prints the opposite of what I expect.

Your expectation came from somewhere that had it backwards, possibly this block's old page. LOW is a magnet on this block. The front says ACTIVE LOW and the back says OUTPUTS LOW VOLTAGE WHEN MAGNETIC FIELD DETECTED.

The serial monitor stays empty on my ESP32-S3.

Set Tools > USB CDC On Boot to Enabled and upload again. Without it the S3's USB port does not bring up a serial port at boot, so the sketch runs with nowhere to print. The red LED lighting tells you the block itself is fine.

Where this goes next

Why the magnet has to come so close, and how much closer a small one has to come.

How far a magnet reaches

Edit this page — content/books/hall-effect-sensor/the-first-read.mdx

Community

Questions about this product

See what other owners have asked, and read their solutions.

Ask a question ↗

Hall Effect Sensor

Loading discussions…

Discuss this article

Ask about this page. The answer stays here, on the page it belongs to, for whoever hits the same wall next.

Browse Modules and blocks on the forum