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
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 magnetThe 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 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.
The same read in MicroPython, for an ESP32, an ESP32-S3 or a Pico. Pin.IN makes the pin an input and value() returns 1 or 0.
"""
Hall Effect Sensor - first read, MicroPython TK18 / /p/tk18
Wiring. Count from the square pad on the TinkerBlock board, parts
up, header at the bottom:
GND -> GND
VCC -> 3V3 (never 5V: with no magnet, SIGNAL sits at VCC)
NC -> nothing (unconnected on the board)
SIGNAL -> GPIO 25 on an ESP32, GPIO 4 on an ESP32-S3,
GP15 on a Raspberry Pi Pico
Thonny
Run > Configure interpreter MicroPython (ESP32) or
MicroPython (Raspberry Pi Pico)
Save it to the board as main.py to run it on every power-up.
Nothing to install: machine and time are built in.
"""
from machine import Pin
import time
# The GPIO number SIGNAL is wired to. ESP32: 25. ESP32-S3: 4. Pico: 15.
HALL_PIN = 4
hall = Pin(HALL_PIN, Pin.IN) # the chip drives SIGNAL both ways
while True:
if hall.value() == 0: # active low: 0 is a magnet
print("LOW magnet")
else:
print("HIGH no magnet")
time.sleep_ms(200) # five reads a secondThere is no Uno here: an Uno cannot run MicroPython. value() is 0 when a magnet is near, because the chip is active low. No pull argument is needed. Stop it with Ctrl-C in Thonny's shell.
When it does not work
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.
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.
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.
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.
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
Questions about this product
See what other owners have asked, and read their solutions.
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.