TINKER PiEDESIGN IN INDIA

Light alarm for a secret boxTinkerPieCircuit tutorial 16 of 24 · Intermediate · 20 minutes

Use a light sensor and a buzzer to build an alarm that beeps when the lid of a box is opened.

Open this circuit in TinkerPieCircuit (free, runs in your browser)

What you will learn

Parts

TinkerPie V1 board1 light sensor (LDR)1 resistor, 10 kΩ1 buzzer

Steps

  1. Make the light sensor

    Wire 3V3 → LDR → P11, and P11 → 10 kΩ → GND (the same divider as the night light).

  2. Add the buzzer

    Wire the buzzer + to P12 and − to GND.

  3. Write the code

    When analogRead(P11) is above 2500 (bright), play tone(P12, 880). Otherwise noTone(P12).

  4. Test in the dark

    Press ▶ Start with the LDR slider low: silence. Slide it up: the alarm beeps.

  5. Tune it

    Watch the Serial monitor numbers and pick a threshold that suits your box.

The code

function loop() {
  const light = analogRead(P11);
  Serial.println("light = " + light);
  if (light > 2500) {
    tone(P12, 880);
  } else {
    noTone(P12);
  }
  delay(100);
}

Copy this into the Code tab. It is Arduino-style JavaScript. The real board uses the same pin names (S1, P10…) in TinkerPie Studio.

How it works

The sensor value tells the program about the real world. The program then decides what to do. This pattern (sense, decide, act) is in every smart device.

Challenge: Make the alarm "latch": once it starts, it keeps beeping until you press button A on the board.

← Traffic light · All 24 circuit tutorials · Dot game on the LED matrix →