Lab: a wave switch
Wave a hand over the TK57 reflective sensor and the XL LED toggles. A hand is a drop below the background the sketch learns at start-up, and three things make one wave one toggle: a line to cross, a second line to come back over, and 600 ms of not looking after each switch.
This lab uses the TK57 reflective sensor and the TK01 XL LED, and leans on analogRead and millis().
What goes where
| Module | Socket | Pin | Move |
|---|---|---|---|
| TK57 reflective sensor | A0/A1 | A1 | Unplug the TK20 light sensor and plug the TK57 in its place |
| TK01 XL LED | D2/D3 | D3 | Stays where it is |
A module is on the second pin printed beside its socket, as the shield explains. The other modules can stay in their sockets; this sketch touches only A1 and D3.
A hand is a drop
The TK57's infrared emitter is lit whenever the block has power, and its phototransistor pulls SIGNAL down from 5 V as much as the light coming back lets it. With nothing in front, little comes back and the reading sits near the top of the range. A hand reflects the emitter's light into the receiver, and the reading falls. More light back reads lower: the reverse of the light sensor this module replaces.
Room light is infrared too, and it lowers the reading in the same way. So the sketch does not use a fixed number. For one second at start-up it averages the readings with nothing in front, and from then on a hand is a drop of 100 counts under that background, about half a volt.
The sensor is made for short range. Vishay's datasheet gives 0.2 to 15 mm against a reflecting card, with the strongest reading at 2.5 mm. A hand registers a little farther out, a centimetre or two depending on the hand and the room. Print the readings and see what yours gives.
One wave, one toggle
Run the wave with One line. The trace is drawn to show the shape, not measured. As the edge of the hand arrives, part of the receiver's view is hand and part is room, and the reading wobbles across the line. Each crossing toggles the lamp, and so does each finger of a spread hand.
Two lines fixes the edges. The sketch counts a hand on crossing 100 under the background and lets go of it only when the reading is back within 50. That gap is hysteresis, and a wobble at an edge is far smaller than 50. It does not fix the fingers: the gap between two fingers lets the reading rise all the way back, and each finger arrives as a new hand.
Two lines and a lockout fixes that. For 600 ms after each toggle the sketch notices hands but does not act on them. A wave is over well inside that, so one wave is one toggle.
The sketch
reading() averages four analogRead calls, which takes the edge off the
noise without slowing anything a hand can do. loop() returns at once
unless 10 ms have passed since the last reading, so there is no delay()
in it.
The heart is two tests on drop = background - r. Crossing ENTER_DROP
sets handThere, and toggles the lamp only if LOCKOUT_MS has passed
since lastToggle. Coming back within LEAVE_DROP clears handThere. A
hand that arrives during the lockout still sets the flag, so it must leave
before the next hand counts.
Make it yours
- Count the waves in one second instead of toggling: one wave on, two off. Start a window on the first hand and act when it closes.
- The TK01 is on D3, a PWM pin. Fade it up and down with
analogWriteinstead of switching it. - Lower
ENTER_DROPto reach farther. Below about 30 the room's own flicker starts to cross the line.
The code
Learns the background for a second, then reads the TK57 every 10 ms as the mean of four readings. A drop of 100 under the background is a hand and toggles the TK01; the hand has gone once the reading is back within 50; after each toggle the sketch ignores hands for 600 ms. No delay() in loop().
/*
A wave switch TinkerBlock UNO kit, /manuals/tkbu
Wave a hand over the TK57 reflective sensor and the TK01 XL LED
toggles. More infrared back from the hand reads LOWER, so a hand is
a drop below the background the sketch learns at start-up. It
counts a hand once the reading drops 100 below the background and
lets go only once it is back within 50: two lines, so the wobble
at the edge of a hand cannot toggle twice. After each toggle it
stops looking for 600 ms, so the gaps between spread fingers do
not toggle it again.
Wiring, on the TinkerBlock UNO shield:
TK57 reflective sensor in the A0/A1 socket, so it is on A1
(it takes the TK20 light sensor's place)
TK01 XL LED in the D2/D3 socket, so it is on D3
A module is on the second pin printed beside its socket.
Arduino IDE
Tools > Board: Arduino Uno
Tools > Port: the CH340 port
No library needed. Serial Monitor at 115200.
Keep your hands away for the first second while it learns.
*/
const int SENSOR_PIN = A1; // TK57 SIGNAL: lower = more light back
const int LED_PIN = 3; // TK01 SIGNAL: HIGH lights it
const int ENTER_DROP = 100; // counts under the background: a hand
const int LEAVE_DROP = 50; // back within this: the hand has gone
const unsigned long LOCKOUT_MS = 600; // blind after each toggle
const unsigned long EVERY_MS = 10; // one reading this often
const unsigned long LEARN_MS = 1000; // learn the background
int background;
bool handThere = false;
bool lampOn = false;
unsigned long lastRead = 0;
unsigned long lastToggle = 0;
// The mean of four readings, which takes the edge off the noise.
int reading() {
long sum = 0;
for (int i = 0; i < 4; i++) sum += analogRead(SENSOR_PIN);
return sum / 4;
}
void setup() {
Serial.begin(115200);
pinMode(LED_PIN, OUTPUT);
digitalWrite(LED_PIN, LOW);
Serial.println(F("Learning the background. Hands away..."));
long sum = 0;
int n = 0;
unsigned long start = millis();
while (millis() - start < LEARN_MS) {
sum += reading();
n++;
delay(EVERY_MS);
}
background = sum / n;
Serial.print(F("Background "));
Serial.print(background);
Serial.print(F(", a hand under "));
Serial.println(background - ENTER_DROP);
if (background - ENTER_DROP < 0) {
Serial.println(F("Too much reflected already: is something "
"over the sensor?"));
}
}
void loop() {
unsigned long now = millis();
if (now - lastRead < EVERY_MS) return;
lastRead = now;
int r = reading();
int drop = background - r; // how far below the background
if (!handThere && drop >= ENTER_DROP) {
handThere = true;
// A hand, unless it is the same wave still passing.
if (now - lastToggle >= LOCKOUT_MS) {
lampOn = !lampOn;
digitalWrite(LED_PIN, lampOn ? HIGH : LOW);
lastToggle = now;
Serial.print(F("Hand at "));
Serial.print(r);
Serial.println(lampOn ? F(": on") : F(": off"));
}
} else if (handThere && drop <= LEAVE_DROP) {
handThere = false; // gone: ready for the next one
}
}Keep your hands away from the sensor while it learns: whatever is in front of it then is its idea of nothing. It prints the background and the line a hand has to cross, so you can see what your room gives.
View on GitHub · kits/tkbu/wave-switch/Wave_Switch/Wave_Switch.ino @ v1.14When it does not work
Spread fingers each cross both lines, and a wave that takes longer than the lockout lets the last ones through. Raise LOCKOUT_MS from 600 to 800, or wave with the fingers together.
The TCRT5000 is made for millimetres, not centimetres: its datasheet gives 0.2 to 15 mm against a reflecting card. A hand farther out returns little light. Lower ENTER_DROP to 60 and LEAVE_DROP to 30 to reach farther, at the cost of more false toggles.
The room changed after it learned: a lamp switched on, the sun moved, or something was over the sensor during the first second. Infrared from the room lowers the reading as a hand does. Press reset with nothing in front of the sensor so it learns again.
Something reflective sits in front of the sensor, or the module is not in the A0/A1 socket and A1 is reading nothing. With nothing within a few centimetres the reading should be near the top of the range, since SIGNAL sits near 5 V when no light comes back.
The microphone, a five-second alarm, and why it must not listen while it rings.
Lab: a sound alarm →Edit this page — content/books/tkbu/wave-switch.mdx
Questions about this product
See what other owners have asked, and read their solutions.
TinkerBlock UNO R3 Starter Kit, 12 Modules
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.