Parts & wiring

Thought there was nothing more annoying than the alarm clock? Well, you’re wrong. Here at EduKits, we’ve managed to build something even worse – an alarm clock that goes off at the wrong time. Luckily, you’ve got all the bits you need to build it in your Annoyatron kit.

This project uses a light sensor to tell the difference between day and night, or light and dark. This means that we can turn on an annoying buzzer whenever at first light in the morning.

But this invention isn’t just good for waking up friends or family members a little too early – it’s also great to put behind doors in rooms that are usually dark. The home office is a great place for this. Parents will be running around frantically as they struggle to find the reason for the mysterious alarm setting off.

What you’ll need for this project

To build this project, you will be using the following components:

Brain Board

Buzzer

Light Dependant Resistor

Light Sensor

Resistor

10k Resistor

How to connect the parts

To wire up this project, you’ll need the Brain Board, USB cable, buzzer, light sensor and 10k resistor. To connect your buzzer, slide its pins into the board as shown – the positive leg should go in PIN 11 and the negative leg should go in GND.

For both the LDR and the resistor, it doesn’t really matter which way you connect either of them. Slide one of the light sensor’s pins into the 5V pin on the board and the other into A0. Connect the resistor between GND and AO again.

Hover over the PLUS signs for extra tips

This is the positive leg of the buzzer. It will have a ‘+’ sign on top of it and will also be longer than the other leg.
Don’t forget to plug the board into your computer with the included USB cable.
For this to work, you’ll need to plug a leg from both the light sensor and the resistor into PIN A0.

The final program

Here’s the code we’ll be using to program this invention.
int LDR = 0;
int buzzer = 11;

void setup() {
  pinMode(LDR, INPUT);
  pinMode(buzzer, OUTPUT);
}

void loop() {

  if(analogRead(LDR) > 150) {
     alarmTrigger(1);
  }

}

void alarmTrigger(int i) {
  for(int x = 0; x < i; x++) {
    digitalWrite(buzzer, HIGH);
    delay(500);
    digitalWrite(buzzer, LOW);
    delay(500);
  }
}

Make this more awesome

Learn how to modify the code

Change how much light triggers the alarm

To trigger the alarm, the program first needs to see that there's enough light. However, you may want to fine-tune exactly how much light we need to set off the alarm. At the moment, it's set to a value of 150 on line 11 of the code.

if(analogRead(LDR) > 150) {

That line of code is just saying that we should run the next few lines of code which trigger the alarm if we have light over a value of 150. If we decrease this, it will take less light to trigger the alarm. If increase this, it will need to be brighter for the alarm to go off.

Change how long the alarm stays on

Once the alarm has been triggered by a certain level of light, we can control exactly how long it stays on for afterwards. In our program, we set the alarm to beep five times before resetting, ready to be triggered again. Take a look at line 12:

alarmTrigger(1); // Trigger the alarm for 1 beep

By changing this code, we can either increase or decrease the number of beeps that the alarm stays on for when triggered. If we wanted it to stay on for 10 beeps, we would use the following code:

alarmTrigger(10); // Trigger the alarm for 10 beeps

Change the sound of the alarm

We can change the tone - or in other words, the sound - of the beeping using something called the tone() function. Take a look at the code on lines 26-29, which is what we are currently using for our beeps:

analogWrite(buzzer, 255);
delay(500);
analogWrite(buzzer, 0);
delay(500);

To be able to change the tone of the beeps, replace lines 19-22 of your program with the following code:

tone(buzzer, 1000, 500);
delay(1000);

The following code works like this: tone(buzzer, pitch, length). Replace the word pitch with any number you like, and this will control the sound of the beep. Replace the word length with exactly how long you want the beep to go for in milliseconds.

Here's a different beeping sound that you can try out on your project:

tone(buzzer, 5000, 750);
delay(1000);