Project #3

Broken Night Light

Build and program a nightlight that automatically turns off whenever someone walks past! This invention will lead to lots of frustration as people try to navigate the darkness and bump into stuff on the way…


Parts & wiring

Ever bumped into something during the night and wished you had a light in the hallway? Well, now you can! The new EduKits night light automatically turns off when someone walks past, ensuring yelps of pain as unsuspecting siblings make child-shaped holes in the walls.

It would be cool if this project had that sort of effect, but we think that it’s at least something close to it. In this project, you will be building a night light that, instead of making things easier to navigate in the dark, makes it harder. We’ll be using an ultrasonic sensor to measure the distance, telling us is someone has walked by, as well as an RGB LED which serves as the night light itself.

What you’ll need for this project

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

UNO R3 Board & USB Cable
UNO R3 board & USB cable
Ultrasonic Distance Sensor
ultrasonic sensor
assorted wires
mini breadboard

How to connect the parts

To wire up this project, you’ll need your UNO R3 board, USB cable, RGB LED and ultrasonic sensor. The longest pin of the RGB LED is the ‘negative’ pin, and this needs to be connected to GND on the UNO R3 board. The LED’s other 3 legs can be connected to the board’s pins as shown in the diagram below. For the ultrasonic sensor, we will only be using 4 of its 5 pins in this project. Connect its ‘Vcc’ pin to 5V on the board, its ‘Trig’ to PIN 7, ‘Echo’ to PIN 6, and ‘GND’ to GND on the board.

Have you downloaded the Arduino software?

You’ll need to download the Arduino software to be able to code your invention. But don’t worry – click here for instructions.

Move the slider to see the breadboard wiring

Here’s what your invention should look like once it’s been assembled.

We decided to use slightly different colours of wires in ours, but this is what yours should roughly look like.


Our sample code

You’ll see what this is for in just a moment

int triggerDistance = 20;
int confusionTime = 2;

int trigPin = 7;
int echoPin = 6;
int redPin = 9;      // Where the 'red' LED pin is connected
int greenPin = 10;   // Where the 'red' LED pin is connected
int bluePin = 11;    // Where the 'red' LED pin is connected
int i = 0;

void setup() {
  Serial.begin(9600); // Let the board talk to the computer
  for(int l=redPin; l<=bluePin; l++) { pinMode(l, OUTPUT); }
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);

void loop() {
  long duration, inches, cm;
  digitalWrite(trigPin, LOW);
  digitalWrite(trigPin, HIGH);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);

  cm = duration / 29 / 2; // Calculate sensor distance

  Serial.print(cm); Serial.print("cm"); Serial.println();

  if(cm < triggerDistance) { i++; if(i>3) {
    analogWrite(redPin, 0);     // Turn the brightness to
    analogWrite(greenPin, 0);   // 0 for all of our LEDS
    analogWrite(bluePin, 0);    // (we set them up earlier)
    delay(confusionTime * 1000);
    i = 0; }
  } else {
    analogWrite(redPin, 100);    // Set the brightness
    analogWrite(greenPin, 100);  // of the LEDs to the
    analogWrite(bluePin, 100);   // max (100)
    i = 0;



Let’s start coding

Like with previous projects, you’ll need to add and upload the sample code to your Arduino board to get it to work. Forgotten how to do this? Click on the titles below to expand the instructions for each step of the process.

Forgotten how to do these steps? Click on the titles to expand.

Connecting & setting up your Arduino board

1. Connect to your computer

The Amazing Annoyatron kit includes a mini computer that you can send code (instructions) to, called an ‘EduKits UNO’ board. We might also call it an ‘Arduino’ board because this is the program that we program it with. Start by connecting this board to your computer using the supplied USB cable. This will give the board power and will allow it to ‘talk’ to your computer. If the cable is connected correctly, you will see small lights flashing on the board.

Always select your Arduino board before uploading a program

2. Select your board

Every time you open the Arduino software, make sure that you select the option that says ‘Arduino/Genuino Uno’ from the ‘Tools’ menu.

Creating and saving a new 'code' document

Like in other programs you may have used like Microsoft Word, we need to create a new document to work in whenever we start something new. First launch the Arduino software that you downloaded earlier (if you didn’t, learn how to here) and select File > New in the menu at the top of the window.

You’ll notice that a new window will have popped up. It already contains some code, but we need to delete this because you will be pasting in some sample code that EduKits has written already. Select all the code using your cursor and then hit the ‘delete’ key on your keyboard to clear the existing code.

Next, select File > Save and give your project a name to save it to your account. This makes saving your work easier for later on.

Now you’re ready to upload the sample code!

Adding and uploading the sample code

Delete the existing code, then copy and past the code from our website. You can then upload the program to your board.

1. Add the sample code

We’re going to start off with some sample code, just so that you can see exactly what the project does. You can find this by scrolling down the page to section ’02’. Copy and paste the code from here into the blank program in the Arduino software.

2. Upload the program

Now that you’ve got your code, you can send it to your Arduino board. Find the button with a right arrow on it and click it to upload. If you haven’t already saved your document, a window will then pop up prompting you to do so. Type in a name like ‘Ticking Clock’ and then hit okay.

The program will ‘think’ for a moment, and then you will see lights flashing on the board very quickly for a few seconds before turning off. This means that the program has now been uploaded.


Make it more annoying

Chaotic coding

We’ll guide you through changing the code to make the project more annoying.

3D Printed Case

Design your own 3D printed case for this project or use one of ours.

Connect to a battery

Learn how to connect your project to a 9V battery (not included).

See it in action

Get some cool ideas on how you can use this project.

Change how far away the light will turn off

In the program, the light turns off if someone comes within 20cm of our ultrasonic sensor. However, unless you are expecting your family members to run straight into the light, you will want to change this so that it triggers from further away. Take a peep at line 1 where the trigger distance for the sensor is set.

int triggerDistance = 20; // The trigger distance in cm

As the line of code above notes, the trigger distance value needs to be in centimetres. If we wanted our sensor to trigger from a metre away, we would need to use the following code, as 1 metre = 100 cm:

int triggerDistance = 100; // The trigger distance in cm

Change how long the light will turn off

In this program, there’s something I call the ‘confusion time’. This is basically how long we want to turn off the night light after someone has walked in front of them. In the demo program, this ‘confusion time’ is set to 2 seconds. This can be found on line 2 of the code.

int confusionTime = 2; // Turn the light off for 2 seconds

Don’t think that one second is enough? Want to cause more confusion? Not a bad idea, really. Let’s change it up a little so that now the light will turn off for 3 and a half seconds when triggered. We will need to use the following code:

int confusionTime = 3.5; // Turn the light off for 3 and a half seconds

How do I make this thing stop?!?

Click here to find out how to clear the program and make that super-annoying sound finally stop.

Connect your project to a battery

Once you’ve finished building and programming your Annoyatron project, you can connect it to a battery and take it with you. Make sure that you have disconnected the board from the computer using the blue USB cable and instead connect the included battery clip. You will also need a 9V battery (not included in the kit) to connect to the other end of the cable.

Make sure that the UNO R3 board is never connected to both the computer and the 9V battery at the same time. This could cause damage to both the board or your computer.

3D print a case for the Annoyatron

Pimp your Annoyatron with this new case that you can 3D print in any colour! Feeling adventurous? Open the STL files in your favourite CAD program and really make them your own.

This case holds the included UNO R3 board and has slots for a buzzer, wires, power and USB.

Download now
Design and make your own 3D printed case for The Amazing Annoyatron

See the Annoyatron in Action

Let’s have some fun! Here are some cool tips, tricks and ideas for how you can use this project.

Help someone get a good night’s sleep

What about putting your invention near someone’s pillow? They’ll get a real surprise when they try to get to sleep.

This will really tick them off

Digital clocks don’t tick, do they? This one’s sure to have most people confused. Just make sure that you hide your Annoyatron better than we did.

Aaaaargh!?! Where is that noise coming from?

Try hiding your Annoyatron in a drawer full of paperwork! This ticking sound is especially annoying when you’re trying to get some work done.

It’s a gift from above. Or maybe not.

We used some blue-tack and a ladder (make sure you have a parent around if you use one) to stick our Annoyatron behind one of the lights in the roof. It’s well hidden and will have people very confused.

Share your creations with us on Instagram!

We love to see what people are making with the Annoyatron. Snap a pic and share it on Instagram with the hashtag #Amazing Annoyatron!

How do I actually make this thing stop?


Create a new document in Arduino

Open the Arduino software and select File > New in the menu or use the keyboard shortcut Ctrl + N to create a new document.

Upload the program to your board

Make sure that the correct board is selected in the tools menu and then click the upload button to send the 'blank' program to the board, overwriting the current one.