TinkerBlock UNO kit/Hands, sound and beams/20. Lab: an invisible beam alarm
Hands, sound and beams · 20 of 21

Lab: an invisible beam alarm

The TK63 infrared transmitter shines steadily across a gap at the TK64 receiver, and a hand through the beam sounds the TK36. The transmitter sits on an I2C socket, so its pin is A5 with the shield's 4.7 kΩ pull-up on it: harmless for a steady beam, and the reason the beam is lit before the sketch starts.

This lab uses the TK63 infrared transmitter, the TK64 infrared receiver and the TK36 active buzzer. It puts a module on the I2C pins without using I2C.

What goes where

ModuleSocketPinMove
TK63 IR transmitteran I2C socketA5 (SCL)Plug it into any of the four; the book uses the first
TK64 IR receiverA2/A3A3Unplug the TK12 thermistor and plug the TK64 in its place
TK36 active buzzerD10/D11D11Stays where it is

The eight digital and analogue sockets each carry a module already, so the transmitter goes on an I2C socket. Its fourth pin is SCL, which on the UNO is A5, and A5 is an ordinary pin when nothing uses I2C: pinMode(A5, OUTPUT) and digitalWrite(A5, HIGH) work as on D3. The socket's third pin is SDA, A4, and lands on the TK63's NC pin, which is connected to nothing.

The pull-up on A5

The shield fits a 4.7 kΩ resistor from SCL to 5 V, because I2C needs one. The TK63's SIGNAL is 1 kΩ into a transistor's base, with nothing on the board holding that base low. So the pull-up is wired straight into the transmitter's input.

A5, the pull-up and the beam
A5 not driven
base needs
0.14 mA
base gets
0.75 mA
IR LED
24.3 mA
red LED
2.9 mA
Before setup() runs, during a reset and while the bootloader waits, A5 is an input, and the shield's 4.7 kΩ alone feeds the base 0.75 mA, 6 times what it needs. The beam is on before the sketch is. SCL sits near 1.5 V, which no I2C device reads as HIGH.

With A5 HIGH, which is what the sketch does, both ends of the pull-up are at 5 V and no current flows in it. The pin gives the base 4.3 mA, as it would on any socket, and the TK63 runs its infrared LED at about 24 mA through its own 150 Ω, from VCC, not from the pin. For a steady beam the pull-up changes nothing.

With A5 LOW the beam is off, and the pin sinks the pull-up's 1.06 mA as well, a small fraction of what a pin can take.

Before setup() is the case that differs from the other sockets. During a reset, while the bootloader waits after one, and until the sketch sets pinMode, A5 is an input. The pull-up alone then feeds the base about 0.75 mA, several times what it needs, and the beam is on. Here that is harmless, even useful. It costs two things elsewhere: a sketch can turn this beam off only by driving A5 LOW, never by letting go of it; and while the TK63 is plugged in, SCL cannot rise above about 1.5 V, so no I2C module on the other three sockets can work.

The beam

The TK63's LED shines at 940 nm, the TK64's peak, and its beam is narrow: the datasheet gives half the intensity 10° either side of straight on. Both domes stand straight up off their boards, so stand the blocks on edge, domes facing, a few centimetres apart to start.

The TK64 turns infrared into a voltage across 10 kΩ: more infrared reads higher, and in the dark it reads about 0. At start-up the sketch averages 20 readings with the beam clear. A reading under half of that is a break; the beam counts as clear again only above three quarters. Two lines, as in the wave switch, so a hand at the edge of the beam cannot make the buzzer chatter.

The sketch

setup() makes A5 an output and drives it HIGH before it learns, then waits 10 ms for the receiver to settle; it needs about a tenth of a millisecond.

loop() reads A3 every 5 ms. broken flips on crossing a line and prints the reading, and lastBroken is stamped on every read while the beam is broken. The buzzer sounds while broken is true or for HOLD_MS after the last broken read. A hand that passes in 50 ms still gives two seconds of sound.

What a steady beam cannot do

The receiver adds up all the infrared in view, the room's included, and the clear reading was learnt with the room in it. When a lamp goes off, the reading falls, and that can look like a break. The TK64's own book cures that by reading twice, beam off and beam on, and keeping the difference.

Make it yours

  • Do the TK64 book's rounds here: digitalWrite(A5, LOW), wait 2 ms, read; digitalWrite(A5, HIGH), wait 2 ms, read; keep on minus off. The pull-up does not stop A5 turning the beam off.
  • Count the breaks and print how long each lasted, for a doorway counter.
  • Add the TK01 on D3 as a silent alarm that stays lit until reset.

The code

Beam_Alarm.ino

Drives A5 HIGH to light the beam, then learns the clear beam as the mean of 20 readings of the TK64. Every 5 ms it reads A3: under half the clear beam is a break, and only over three quarters is the beam clear again. The TK36 sounds while the beam is broken and for two seconds after.

/*
  An invisible beam alarm             TinkerBlock UNO kit, /manuals/tkbu

  The TK63 infrared transmitter shines steadily at the TK64 infrared
  receiver. Break the beam and the TK36 active buzzer sounds, and it
  keeps sounding for two seconds after the beam comes back, so a
  hand that passes quickly is still heard.

  At start-up the sketch learns the clear beam: the mean of 20
  readings. The beam is broken under half of that, and clear again
  only over three quarters of it: two lines, so a reading that
  hovers near one cannot chatter.

  Wiring, on the TinkerBlock UNO shield:
    TK63 IR transmitter  in an I2C socket, so it is on SCL, which
                         is A5 (the shield's 4.7 kOhm I2C pull-up
                         is on that line too; it does no harm here)
    TK64 IR receiver     in the A2/A3 socket, so it is on A3
                         (it takes the TK12 thermistor's place)
    TK36 active buzzer   in the D10/D11 socket, so it is on D11
  A module is on the second pin printed beside its socket.
  Stand the two blocks on edge, domes facing, a few cm apart.
  Plug no I2C module in beside the TK63: while A5 is not driven,
  its transistor's base holds SCL near 1.5 V, under a HIGH.

  Arduino IDE
    Tools > Board: Arduino Uno
    Tools > Port:  the CH340 port
    No library needed. Serial Monitor at 115200.
    Keep the beam clear for the first moment while it learns.
*/

const int TX_PIN = A5;       // TK63 SIGNAL: HIGH lights the beam
const int RX_PIN = A3;       // TK64 SIGNAL: more infrared, higher
const int BUZZER_PIN = 11;   // TK36 SIGNAL: HIGH sounds it

const float BREAK_FRAC = 0.5;   // under half the clear beam: broken
const float CLEAR_FRAC = 0.75;  // over three quarters: clear again
const unsigned long HOLD_MS = 2000;  // sound on after it clears
const unsigned long EVERY_MS = 5;
const int WEAK = 50;            // a clear beam under this is too weak

int clearBeam;
bool broken = false;
unsigned long lastBroken = 0;
unsigned long lastRead = 0;
unsigned long breaks = 0;

void setup() {
  Serial.begin(115200);
  pinMode(BUZZER_PIN, OUTPUT);
  digitalWrite(BUZZER_PIN, LOW);
  pinMode(TX_PIN, OUTPUT);
  digitalWrite(TX_PIN, HIGH);   // the beam, on for good
  delay(10);

  long sum = 0;
  for (int i = 0; i < 20; i++) {
    sum += analogRead(RX_PIN);
    delay(EVERY_MS);
  }
  clearBeam = sum / 20;
  Serial.print(F("Clear beam "));
  Serial.print(clearBeam);
  Serial.print(F(": broken under "));
  Serial.println((int)(clearBeam * BREAK_FRAC));
  if (clearBeam < WEAK) {
    Serial.println(F("Too weak: face the domes at each other, "
                     "closer, and press reset."));
  }
}

void loop() {
  unsigned long now = millis();
  if (now - lastRead < EVERY_MS) return;
  lastRead = now;

  int r = analogRead(RX_PIN);
  if (!broken && r < clearBeam * BREAK_FRAC) {
    broken = true;
    breaks++;
    Serial.print(F("Beam broken ("));
    Serial.print(breaks);
    Serial.print(F("), reading "));
    Serial.println(r);
  } else if (broken && r > clearBeam * CLEAR_FRAC) {
    broken = false;
    Serial.println(F("Beam clear"));
  }
  if (broken) lastBroken = now;

  bool sound = broken || (breaks > 0 && now - lastBroken < HOLD_MS);
  digitalWrite(BUZZER_PIN, sound ? HIGH : LOW);
}

Keep the path clear while it starts: what it reads then is its idea of clear. If the clear beam reads under 50, it says so; face the domes at each other, bring them closer and press reset.

View on GitHub · kits/tkbu/beam-alarm/Beam_Alarm/Beam_Alarm.ino @ v1.14

When it does not work

It says the beam is too weak.

The receiver is not seeing the transmitter. Both domes stand straight up off their boards, so two blocks lying flat do not face each other: stand them on edge, domes facing, a few centimetres apart. A phone camera shows the TK63's dome glowing; its red LED lights with it.

It sounds with nothing in the beam.

The reading fell under half of what it learned. A lamp switched off, or the sun moved, since the room's infrared was part of the clear reading. Shade the receiver with a short black tube pointed at the transmitter, and press reset with the path clear.

A hand in the beam does nothing.

Light gets round or through it. At a few centimetres the beam is wider than a finger, and skin passes some infrared. Use something wider and opaque, such as a book, or move the blocks apart so the beam is weaker and a hand takes more of it.

An I2C module stopped working when the TK63 went in.

The four I2C sockets share SDA and SCL. While A5 is not driven, the TK63's base holds SCL near 1.5 V, which no device reads as HIGH. Keep I2C modules and the TK63 apart: one or the other on the shield.

Where this goes next

The cable, the socket, the PWR lamp and the pin the sketch names.

Troubleshooting the kit →

Edit this page — content/books/tkbu/beam-alarm.mdx

Community

Questions about this product

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

Ask a question ↗

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.

Browse Modules and blocks on the forum →