4-direction tilt sensor/How it is wired/03. No line goes to ground
How it is wired · 03 of 11

No line goes to ground

Set all four pins to INPUT_PULLUP and they read HIGH whichever way the board leans, because the roller joins two lines to each other and neither of them to ground. So the sketch pulls one line LOW itself and reads which neighbour follows it down. Two lines held LOW in turn, four reads, and every pair is found.

A button, and this

A button on a TinkerBlock board has one side on VCC or GND and the other on your pin, with a resistor holding the pin at the opposite level. Press it and the pin changes. Your board only has to read.

This switch has no side on anything. Look at the net list and the four lines reach the switch, their own LED, and the header, and that is all. When the roller joins A to B, it joins two lines that are both wherever your board left them, and they stay there. Set all four pins to INPUT_PULLUP and all four read HIGH, tilted or not. The block's old page said a pin reading LOW showed its contact was closed; no pin can read LOW until something on your side makes it.

Pull one LOW, see who follows

Pull one LOW, see who follows
The roller is on
Outputs
A, then C
Inputs read
B and D, twice
Driven HIGH
never
Every line is an input with its pull-up, so every line reads HIGH whichever way the board leans. Reading the four pins now tells you nothing. Run the scan.

The fix is to make one line the ground for a moment. Hold A LOW as an output, leave the others as inputs with their pull-ups, and read B and D. If the roller is joining A and B, B follows A down and reads LOW. If it is joining D and A, D does. Then let A go, hold C LOW, and read B and D again: B means B–C, D means C–D.

Four pairs, and each shows up in exactly one of the four reads. A and C are never read, and never need to be: A and C are never joined, so holding one LOW tells you nothing about the other. The whole scan takes a few tens of microseconds.

A keypad works the same way. Pressing a key joins one row wire to one column wire and neither to ground, and whatever scans it holds one row LOW at a time and reads which column follows. Sixteen keys, eight wires is that grid sixteen keys wide. This one is two by two.

LOW, never HIGH

Every released line is an input with its pull-up switched on. It is never an output driven HIGH. The roller can join any two neighbours at any moment, and if one were driven HIGH while the other was held LOW, two pins would be fighting each other through a piece of metal with nothing to limit the current.

A LOW and a pull-up can meet safely: the pull-up is tens of kilohms, so the current it can push into a LOW pin is a fraction of a milliamp. That is why the sketches in this book only ever write LOW, and release a line by making it an input again.

What your board has to offer

Four pins that can each be an output and an input with a pull-up. That rules out the classic ESP32's pins 34 to 39, which are inputs only. The pins every sketch here uses are D2 to D5 on an Uno, GPIO 25, 26, 27 and 32 on an ESP32, GPIO 4 to 7 on an ESP32-S3, and GP10 to GP13 on a Pico.

When it does not work

All four pins read HIGH all the time.

That is what they should read if the sketch only reads them. The switch never connects a line to GND, so a pull-up input sees nothing change. The sketch has to hold one line LOW and read the others, as the scan here does.

Can I use plain INPUT instead of INPUT_PULLUP?

No. Nothing on the board pulls a line up. The LED and resistor from VCC are not a usable pull-up, because an LED barely conducts until nearly 2 V is across it, so an input line with no pull-up sits at some undefined voltage and reads whatever it likes. Use INPUT_PULLUP, or Pin.PULL_UP in MicroPython.

Why not set the line OUTPUT HIGH when it is not being held LOW?

Because the roller may join it to the line that is being held LOW, and then one pin drives HIGH into another pin driving LOW with nothing between them. Released lines are always inputs with their pull-up. The only thing the sketch ever drives is LOW.

Does this work on a classic ESP32's pins 34 to 39?

No. Those pins are input-only and have no internal pull-up, and this scan needs every line to be able to go LOW as an output and to be pulled up as an input. Use the pins this book names, or any four ordinary GPIOs.

Where this goes next

Why the four LEDs stay dark until the sketch holds a line LOW, and what they cost when it does.

The lights wait for you

Edit this page — content/books/four-direction-tilt-sensor/no-line-goes-to-ground.mdx

Community

Questions about this product

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

Ask a question ↗

4-Direction Tilt 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