Do you have a pesky sibling or pet that sneaks up on you? Are cars always in your blind spot? Create a proximity alarm that alerts you to approaching objects. This simple guide will teach you how to create a proximity alarm that will light up and make noise when something approaches.

An ultrasonic rangefinder and Arduino microcontroller will check for nearby objects and if need be, alert you with a piezo-buzzer and RGB LED. Learn about these components using electronics lab simulations and eventually design, code, simulate, and build the proximity alarm itself! This is great if you are looking for a quick project to learn about digital electronics.

## Step 1: Ultrasonic Rangfinder

To sense distance, we will use the Parallax PING))) ultrasonic rangefinder. To sense distance, the rangefinder rapidly sends out ultrasonic sound waves. Each wave will travel outwards, and if there is an object in its path, it will collide and bounce back to the rangefinder. The time it takes for the wave to bounce back determines the distance of that object. The Parallax PING))) can only sense objects from around 2 inches to 10 ft.

The PING))) has onboard ICs (integrated circuits) that do most of the heavy lifting. All we need to do is send the rangefinder a quick 5V pulse from the Arduino and use the pulseIn() function to check the time it took for the wave to bounce back. See the 'Code' step for more information on how to code the rangefinder.

## Step 2: Piezo-Buzzer

To alert the user we will use a piezo buzzer. This device will emit a click sound when a DC voltage is applied. To make a tone, we can use the Arduino tone() function. This will cause the device to emit an annoying sound at that tone. String enough of these tones together and you can create music!

Change the frequency in the simulation below to see how the tone changes:

## Step 3: RGB LED

RGB LEDs are light emitting diodes that can output a range of color combinations (using red, green, blue). They come in two configurations: common anode, and common cathode. See the second picture in this step (thanks moderndevice.com).

• Common Anode: The common lead, the longest lead, is positive (anode means positive lead). This lead should be connected to a positive voltage. To make the LED green, you would apply a 0V (aka GND) to the green lead. The same holds for the other colors. Different combinations of voltages on these three pins can create a range of colors.
• Common Cathode: The common lead, the longest lead, is negative (cathode means negative lead). This type is more intuitive and works just like a regular LED. The long lead is connected to GND and the other three leads can be connected to a positive voltage (depending on what color you want to make).

Remember that LEDs might need a resistor in series to limit the current through the device (so it won't blow up). It is easiest to put the resistor in series with the common lead (so you only need one, rather than putting a resistor in series with the red lead, green lead, and blue lead).

Move the dials in the simulation below to make different colors:

## Step 4: Parts List:

To make the blind spot detector you will need the following:

Some of the above links include extra pieces but you should only need one of each part.

## Step 5: Design, Simulate, Build

Now lets put the pieces together! We have a power system, distance sensor, alert system, and microcontroller. Use the following guide to connect the various parts to the Arduino microcontroller:

• Power System: Since this is a low powered, mobile project, we can use a 9V to power the Arduino which will then regulate down the power for the other components.
• Battery + ---> Arduino Vin
• Battery - ---> Arduino GND
• Distance Sensor:
• Rangefinder GND ---> Arduino GND
• Rangefinder 5V ---> Arduino 5V
• Rangefinder SIG ---> Arduino Analog Pin 5
• Alert System: Our system to alert the user is made up of two devices: the piezo-buzzer, and the RGB LED. This triggers one's auditory and visual senses.
• Piezo:
• Piezo Buzzer Black Wire ---> Arduino GND
• Piezo Buzzer Red Wire ---> Arduino Digital Pin 13
• RGB LED
• RGB LED Long Lead ---> 330 ohm resistor ---> Arduino GND
• RGB LED Red Lead ---> Arduino Digital Pin 12
• RGB LED Green Lead ---> Arduino Digital Pin 10
• RGB LED Blue Lead ---> Arduino Digital Pin 11

Try out the simulation below to see it in action. Click on rangefinder and move the dot to simulate an object:

Using this breadboard simulation as a guide, go ahead and build it!

## Step 6: Code

The code starts with a bunch of define statements that connect the note to its respective frequency in hertz. We use a bunch of define statements because it is easier to think of music in terms of notes, rather than its frequency representation.

```#define NOTE_B0  31 //NOTE_B0 = 31 Hz
```

Next we define the two arrays that hold our music. The first holds the notes, the second holds the 'note duration'. The example here has a snippet of the James Bond theme song (thanks GarageLab for the song code).

```int jb_m[] = { //this pattern of notes is a snippet from james bond
/*NOTE_E4,NOTE_F4,NOTE_F4,NOTE_F4,NOTE_F4,NOTE_E4,NOTE_E4,NOTE_E4,
NOTE_E4,NOTE_G4,NOTE_G4,NOTE_G4,NOTE_G4,NOTE_E4,NOTE_E4,NOTE_E4,*/
NOTE_E4,NOTE_F4,NOTE_F4,NOTE_F4,NOTE_F4,NOTE_E4,NOTE_E4,NOTE_E4,
NOTE_E4,NOTE_G4,NOTE_G4,NOTE_G4,NOTE_G4,NOTE_E4,NOTE_E4,NOTE_E4,
NOTE_DS5,NOTE_D5,NOTE_B4,NOTE_A4,NOTE_B4,
/*NOTE_E4,NOTE_G4,NOTE_DS5,NOTE_D5,NOTE_G4,NOTE_B4,
NOTE_B4,NOTE_FS5,NOTE_F5,NOTE_B4,NOTE_D5,NOTE_AS5,
NOTE_A5,NOTE_F5,NOTE_A5,NOTE_DS6,NOTE_D6,NO_SOUND*/
};

int jb_n[] = { //the corresponding note durations
/*8,16,16,8,4,8,8,8,
8,16,16,8,4,8,8,8,*/
8,16,16,8,4,8,8,8,
8,16,16,8,4,8,8,8,
8,2,8,8,1,
/*4,8,4,8,8,
8,8,4,8,4,8,
4,8,4,8,3*/
};
```

Now that we have the song defined, we can start to write the code that actually does the stuff!

The Boolean variable 'change' keeps track of the red zone. We only want the song to play when something enters the red zone. The variable 'change' will only be true when something enters the red zone. If that object stays in the red zone, 'change' will remain false and the song will not play.

Its important that the 'change', 'red', 'blue', 'green', 'buzzer', and 'distance' variables all be global variables because we will use them throughout many functions.

```bool change=false; //variable to check if something entered the red zone

double duration; //duration is used to determine the distance sensed from the rangefinder
int distance; //the distance found from the rangefinder

//the corresponding pins for the RBG LED
int red = 12;
int blue = 11;
int green = 10;

int buzzer = 13; //the corresponding pin for the buzzer
```

When the Arduino starts up, the setup function will start by initializing the serial monitor (baud rate 9600). It will make the RBG LED and buzzer pins outputs so we can control them.

```void setup() {
Serial.begin(9600); //enable the serial monitor at a 9600 baud rate

//make the LED and buzzer an output from the Arduino
pinMode(red,OUTPUT);
pinMode(blue,OUTPUT);
pinMode(green,OUTPUT);
pinMode(buzzer,OUTPUT);
}
```

Our infinite loop does nothing but call other functions and wait. It starts by getting the distance of any object in the rangefinder's path. It waits a short amount of time and then alerts the user based on the newly found distance.

```void loop() {
getDistance(); //find the distance of any object (if there even is an object)
delay(10); //wait for the computer to catch up
alert(); //let the user know what distance we found
}
```

To get the distance from the ultrasonic rangefinder we send out ultrasonic sound pulses. The amount of time it takes to bounce off an object and return to the rangefinder will correspond to the object's distance. The getDistance() function places the found distance in the global variable 'distance'.

```void getDistance(){
pinMode(A5, OUTPUT); //set the rangefinder pin to be an output in preparation to send a pulse

delayMicroseconds(2); //let the computer catch up
analogWrite(A5, 256); //send out the pulse
delayMicroseconds(5); //wait 5 ms before turning off the pulse
analogWrite(A5, 0); //turn off the pulse

pinMode(A5, INPUT); //set rangefinder pin back to input to be ready to catch the bounced sound wave

duration = pulseIn(A5, HIGH); //pulseIn() function reads the time it took for wave to bounce back
distance = duration/72/2; //converts the found time to distance (using physics and math)
delay(10); //this slows down the rate at which we take distance measurements

//Serial.println(distance); //debugging
}
```

Now that we have our distance, lets let the user know! Start by turning off all the LEDs to give us a clean slate. If the object is far away then we will make the LED green. If the object is not too close or too far, we will make the LED blue. Lastly, if the object gets close to the rangefinder, we turn the LED red and play the music (if the object just entered the red zone).

```void alert(){
//turn off all the LEDs
digitalWrite(red,LOW);
digitalWrite(green,LOW);
digitalWrite(blue,LOW);

if(distance > 120){ //if object is far away, LED set to green and allow change to be true
digitalWrite(green,HIGH);
}
if(distance < 115 && distance > 85){ //if the object not too far or too close,
digitalWrite(blue,HIGH);           //make LED blue and allow change to be true
change = true;
}
if(distance < 80){ //if the object gets close, make the LED red
digitalWrite(red,HIGH);
if(change) music(jb_m,jb_n,1500,sizeof(jb_n)/2); //if the object was not previously in the red zone
change = false; //disable change                 //then play the music
}
}
```

The music() function takes in the melody, note duration, pace, and length of song. This function is a loop that goes through and plays the buzzer for all of the set notes for their corresponding time durations. The 'pace' variable lets us speed or slow down the music. 'length' tells the for loop how many notes to play.

```void music(int melody[],int noteDuration[],int pace,int length){
for (int Note = 0; Note < length; Note++){ //loop through the length of the music)
int duration = pace/noteDuration[Note]; //take into account the pace
tone(buzzer, melody[Note],duration); //play the note using the tone() function
delay(duration*1.2); //let the note run for the corresponding duration
}
}
```

## Step 7: Conclusions and Future Improvments

You should now have a functioning proximity alarm to watch your back!

If you are looking to improve your project and skills, consider soldering the design onto a proto-board. This is a great project to learn soldering or enhance your current skills. If you would like to do this I would recommend using this proto-board. Using a proto-board is a more permanent solution; it will keep the components and wires securely in place. The first picture in this step shows what the proximity alarm looks like when soldered onto a proto-board.

If you are more advanced you could design your own PCB proximity alarm. This would allow you to make a compact, secure, and high quality product. Using Autodesk Circuit's PCB tool you can create and order a PCB. The second picture on this step shows a possible PCB design or you could follow this link to view the PCB project itself.

Whether you make a breadboard, proto-board, or PCB based proximity alarm, I hope you enjoyed the process and learned more about electronics!

<p>Are you using the Arduino Uno or which one of Arduino to be specific?</p>
<p>Arduino Micro</p>