The best tools to make your project dreams come true

Login or Signup

Dear Diary Alarm

By Adafruit Industries

Courtesy of Adafruit

Guide by Carter Nelson

This is a fun and simple beginner project that uses the Circuit Playground to create a light activated alarm. A diary, or other object, can be placed on top of the Circuit Playground and an alarm will sound when the object is removed.

Required Parts

This project uses the sensors already included on the Circuit Playground. The only additional items needed are batteries for power and a holder for the batteries.

Dear Diary Alarm

Before Starting

If you are new to the Circuit Playground, you may want to first read these overview guides.

This project will use the Arduino IDE. Make sure you have added the board support for the Circuit Playground as well as installed the Circuit Playground library. MUST DO BOTH. This is covered in the guides linked above.

Hello Light Sensor

To get an idea of how the alarm works, let's start by exploring the light sensor itself. You can read some technical details in the Lesson #0 Guide. And for reference, here is where the light sensor is located on the Circuit Playground.

Dear Diary Alarm

There is an example sketch included with the Circuit Playground library that we can use to play around with the light sensor. It can be found in the following location:

File -> Examples -> Adafruit Circuit Playground -> Hello_CircuitPlayground -> Hello_LightSensor

With this sketch loaded and running on the Circuit Playground, open the Serial Monitor.

Tools -> Serial Monitor

The current value from the light sensor will be printed once a second.

Dear Diary Alarm

Play around with shining different lights on the light sensor and changing the distance. Also try covering the sensor with your hand, a sheet of paper, or anything else lying around (like a diary). The value can range from 0 to 1023, with higher numbers meaning more light is getting to the sensor. When you cover the light sensor, you should see the value go down.

Another way to watch the value of the light sensor is to use the Serial Plotter. To do this, let's first modify the code slightly. The code below removes the text from the output and increases the rate at which the value is displayed.

Copy Code
#include <Adafruit_CircuitPlayground.h>
#include <Wire.h>
#include <SPI.h>

int value;

void setup() {

void loop() {
value = CircuitPlayground.lightSensor();



With this sketch loaded and running on the Circuit Playground, open the Serial Plotter.

Tools -> Serial Plotter

The light sensor value will now be plotted like a strip chart as shown below. Again, play around with different lighting conditions and watch the value change.

Dear Diary Alarm

Now we can explain how the alarm is going to work. With a diary placed on top of the Circuit Playground, the light sensor will have a low reading. If the diary is removed, more light will get to the sensor and the reading will increase. An example of what this would look like is shown below.

Dear Diary Alarm

So if we simply monitor the value of the light sensor and watch for this increase, we can detect when the diary has been removed and sound an alarm.

Alarm Code:

The alarm code works as follows:

  • Wait for a button to be pressed.
  • Provide a countdown timer with the NeoPixels. (the diary should be placed on Circuit Playground during countdown)
  • Read and store the value of the light sensor.
  • Loop forever reading the light sensor value.
  • If value exceeds the stored value from step 3, sound the alarm.

Here is the full code listing. You can copy and paste it into the Arduino IDE.

Copy Code
// Circuit Playground Dear Diary Alarm
// Don't let sister/brother read your secrets! Place the "armed" Circuit
// Playground under your diary. If someone tries to remove the diary, an alarm
// will sound.
// Author: Carter Nelson
// MIT License (

#include <Adafruit_CircuitPlayground.h>
#include <Wire.h>
#include <SPI.h>

uint16_t lightValue; // current light sensor reading
uint16_t coveredValue; // the light sensor value with diary on top
uint16_t alarmThreshold = 20; // adjust to change alarm sensitivty
bool triggered = false; // becomes true when diary removed

void setup()
// Initialize Circuit Playground library.

// Turn on all the NeoPixels.
for (int p=0; p<10; p=p 1) {
CircuitPlayground.setPixelColor(p, 255, 0, 0);

// Wait for button press.
while ( (CircuitPlayground.leftButton() == false) &&
(CircuitPlayground.rightButton() == false) ) {
// Do nothing.

// Countdown timer.
for (int p=0; p<10; p=p 1) {
CircuitPlayground.setPixelColor(p, 0, 0, 0);

// Compute covered light sensor value as average of 5 readings.
coveredValue = 0;
for (int i=0; i<5; i=i 1) {
lightValue = CircuitPlayground.lightSensor();
coveredValue = coveredValue lightValue;
coveredValue = coveredValue / 5;

// Beep to indicate ARMED.
CircuitPlayground.playTone(1000, 1000);

void loop()
// Check to see if alarm has been triggered.
if (triggered) {
// Sound the alarm.
CircuitPlayground.playTone(2000, 500);
CircuitPlayground.playTone(3000, 500);
} else {
// Get current light sensor value.
lightValue = CircuitPlayground.lightSensor();

// Check light sensor value to determine if alarm should be triggered.
if (lightValue > coveredValue alarmThreshold) {
triggered = true; // Trigger the alarm.

Using the Alarm

Once the sketch has been loaded on the Circuit Playground, it is ready for use. Install the batteries, plug in the power cable, and turn the switch on. You should see all of the NeoPixels come on and turn red.

Dear Diary Alarm

All the lights are on. Ready to use.

Dear Diary Alarm

Press either button to start the countdown timer.

Dear Diary Alarm

The NeoPixels will turn off one at a time.

Dear Diary Alarm


Make sure the diary covers the light sensor before the countdown ends

When the countdown ends a short BEEP will sound.

If you now remove the diary from the Circuit Playground, the alarm should sound. You can press the RESET button to stop the alarm and start again. Yes, this is an easy way to defeat the alarm, but hopefully you will hear the alarm before someone figures that out.

Questions and Code Challenges

The following are some questions related to this project along with some suggested code challenges. The idea is to provoke thought, test your understanding, and get you coding!

While the alarm sketch provided works, there is room for improvement and additional features. Have fun playing with the provided code to see what you can do with it.


  • Would the alarm work at night or in a dark room?
  • Why is computing a covered value for the light sensor necessary?
  • What would happen if the alarm threshold were set too low or too high?

Code Challenges

  • Increase the time of the countdown timer.
  • Change the alarm sound.
  • Use the NeoPixels to add some lights to the alarm.


Key Parts and Components

Add all Digi-Key Parts to Cart
  • 1528-1640-ND