The first read
Three wires, no library, and a sketch that prints each time the block changes sides. VCC goes to your board's logic voltage, SIGNAL to the pin the Push Button and Reed Switch books use: GPIO 4 on an ESP32-S3, GPIO 25 on an ESP32, D2 on an Uno, GP15 on a Pico. pinMode is INPUT, because the pull-down is on the board.
Three wires
GND to GND. VCC to your board's logic voltage: 3V3 on an ESP32, an ESP32-S3 or a Pico, 5V on an Uno. SIGNAL to the pin this book uses throughout. NC stays unconnected.
The pins are the Push Button and Reed Switch books' pins, so one wiring serves all of them. None of them is a strapping pin, which matters on this block: lying with its left edge down at power-up, it holds the pin HIGH while the board starts, and the other way it holds it LOW through 10 kΩ. On an ESP32-S3 GPIO 0 that LOW beats the dev board's BOOT pull-up, to about 1.65 V, and the chip may start in its download mode instead of running your sketch.
The sketch
pinMode(TILT_PIN, INPUT) and nothing else: the pull-down is on the board.
In loop(), digitalRead gives HIGH while the switch is closed and LOW
while it is open. The sketch keeps the last reading in last and prints
only when the new one differs, so the monitor shows one line per change
instead of a hundred a second.
What you should see
At 115200 the serial monitor prints one line at the start: whichever side
the block was last tipped to. Tip the left edge down: the red LED lights and
it prints left edge down: switch closed. Tip the right edge down: the LED
goes out and it prints right edge down: switch open. Lay it flat and it
prints nothing, because nothing changed.
The sketch compiles for an ESP32-S3 and an Uno. Here is the block with the owner's own sketches, which light an XL LED block instead of printing:
The code
No library. digitalRead returns HIGH while the left edge is down and the balls hold the switch closed, LOW while the right edge is down; the sketch prints only when that changes. Change TILT_PIN to the pin you wired SIGNAL to.
/*
Tilt Sensor - first read TK62 / /p/tk62
Wiring. Count from the square pad on the TinkerBlock board, parts
up, header at the bottom:
GND -> GND
VCC -> 3V3 on an ESP32, ESP32-S3 or Pico; 5V on an Uno.
Your board's logic voltage: a closed switch puts
VCC straight onto the pin.
NC -> nothing (unconnected on the board)
SIGNAL -> GPIO 4 on an ESP32-S3, GPIO 25 on an ESP32,
D2 on an Uno, GP15 on a Raspberry Pi Pico
The can's leads are at the board's left edge. Left edge down:
switch closed, HIGH. Right edge down: open, LOW. Near level it
keeps whatever it last read.
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 pin SIGNAL is wired to.
// Uno: 2. ESP32: 25. ESP32-S3: 4. Pico: 15.
const int TILT_PIN = 4;
int last = -1; // nothing read yet
void setup() {
Serial.begin(115200);
// INPUT, not INPUT_PULLUP: the board has its own 10k pull-down.
pinMode(TILT_PIN, INPUT);
}
void loop() {
int level = digitalRead(TILT_PIN);
if (level != last) {
if (level == HIGH) {
Serial.println("left edge down: switch closed");
} else {
Serial.println("right edge down: switch open");
}
last = level;
}
delay(10);
}pinMode is INPUT: the board's own 10 kΩ pull-down holds SIGNAL LOW, and INPUT_PULLUP would fight it. The sketch compiles for an ESP32-S3 and an Uno.
View on GitHub · blocks/tk62-tilt-sensor/arduino/tilt_first_read/tilt_first_read.ino @ v1.5The same read in MicroPython: Pin.IN with no pull, and a line printed each time the value changes. Change TILT_PIN to the GPIO you wired SIGNAL to.
"""
Tilt Sensor - first read, MicroPython TK62 / /p/tk62
Wiring. Count from the square pad on the TinkerBlock board, parts
up, header at the bottom:
GND -> GND
VCC -> 3V3 (your board's logic voltage; never 5V beside
a 3.3 V board)
NC -> nothing (unconnected on the board)
SIGNAL -> GPIO 4 on an ESP32-S3, GPIO 25 on an ESP32,
GP15 on a Raspberry Pi Pico
The can's leads are at the board's left edge. Left edge down:
switch closed, 1. Right edge down: open, 0. Near level it keeps
whatever it last read.
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 SIGNAL is wired to.
# ESP32: 25. ESP32-S3: 4. Pico: 15.
TILT_PIN = 4
# Pin.IN with no pull: the board has its own 10k pull-down.
tilt = Pin(TILT_PIN, Pin.IN)
last = None
while True:
level = tilt.value()
if level != last:
if level:
print("left edge down: switch closed")
else:
print("right edge down: switch open")
last = level
time.sleep_ms(10)Pin.IN with no pull argument, because the pull-down is on the board. On a Pico, TILT_PIN is 15 and VCC goes to 3V3, never VBUS.
View on GitHub · blocks/tk62-tilt-sensor/micropython/tilt_first_read.py @ v1.5When it does not work
Then the switch, VCC and GND are fine and the level is not reaching the pin you read. Check SIGNAL is on the pin TILT_PIN names, and that it is the right-hand pin, not NC beside it.
The switch never closed, or the block has no VCC or GND. Tip the left edge down, the one the can's leads are at, well past level. Then count from the square pad: GND, VCC, NC, SIGNAL.
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 balls bounce as they land, and any that lift off for more than about a millisecond reach the pin. For a message that does no harm; for counting, filter it, as the counting build does.
What a knock does to SIGNAL, what the capacitor hides, and what the sketch has to.
A rattle is not a tilt →Edit this page — content/books/tilt-sensor/the-first-read.mdx
Questions about this product
See what other owners have asked, and read their solutions.
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.