Introduction: Standards, Benchmarks, and Learning Objectives

This instructable will walk a student through the construction of a parking sensor using an arduino. Specifically I will have a ultrasonic sensor constantly polling for distance, and along with a small code that takes this distance and puts it through some if else loops to determine what sounds are played at what distance.

This task covers standards 17 and 18 as it pertains to information technologies and transportation technologies.

By the end of this task, students should have a basic grasp of circuitry, and coding.

Step 1: Parts Required

Starting with an arduino, a ultrasonic range senor and a passive buzzer, you can create a parking sensor for your klutz of a brother. The arudino kit that I use for this instructable cost $30 on Amazon.

Step 2: Connecting Everything

After gathering the materials, they should be connected in the manner as shown. The different lanes of the circuit have been color coded for convenience. With red representing the live wire and brown representing ground. Blue and yellow wires represent the two digital pins that the ultrasonic sensor needs to be connected to. And the green is the digital pin that the passive buzzer must be connected to.

There is no requirement for them to take the shape of the drawing shown, as the components are to be placed in such a manner that the buzzer can be heard by the driver, and the ultrasonic sensor must be connected to the back of the car.

Step 3: Code

The idea behind this code is to make use of the library's given by Elegoo when you purchase a arduino kit from them. Specifically the library's SR04, which is for the ultrasonic sensor, and the library pitches, which is a library of notes that can be played on your buzzers. You can change what pins you use to connect the parts by modifying the trig and echo pins for the SR04 and by changing the first number in the tone function in your code you can change which pin your buzzer is connected to. The pins that I have currently set up are the default pins in set up by Elegoo.

#include "SR04.h"
#define TRIG_PIN 12

#define ECHO_PIN 11 SR04 sr04 = SR04(ECHO_PIN,TRIG_PIN);

int a;

#include "pitches.h"

int melody[] = { NOTE_C5, NOTE_D5, NOTE_E5, NOTE_F5, NOTE_G5, NOTE_A5, NOTE_B5, NOTE_C6};

int duration = 500;

void setup() { Serial.begin(9600); delay(1000); }

void loop() { a=sr04.Distance(); Serial.println(a); delay(500);

if (sr04.Distance() < 50){ tone(8, melody[7], 250); delay(250); } else

if (sr04.Distance() < 100){ tone(8, melody[3], 500); delay(500); } else

if (sr04.Distance() < 150){ tone(8, melody[0], 500); delay(500); }else

if (sr04.Distance() > 150){ delay(500); } }

Step 4: Modify Your Code to Fit Your Purpose

If necessary you may need to modify the code for your purpose. Because the code given is meant to give the user plenty of feedback through the serial monitor as to what it is doing. When it is disconnected from a computer it can bug out and stop working correctly. in such a case you would need to modify the code in such a manner that it doesn't rely on the serial monitor to work. In this case I stopped having the serial monitor print from the variable, and instead have it print directly. This can cause a decrease in accuracy of the serial monitor as the distance can change slightly between the reading and the tone, however this removes its necessity in the loop.

#include "SR04.h"
#define TRIG_PIN 12 #define ECHO_PIN 11 SR04 sr04 = SR04(ECHO_PIN,TRIG_PIN);

int a;

#include "pitches.h"

int melody[] = { NOTE_C5, NOTE_D5, NOTE_E5, NOTE_F5, NOTE_G5, NOTE_A5, NOTE_B5, NOTE_C6};

int duration = 500;

void setup() { Serial.begin(9600); delay(1000); }

void loop() { Serial.println(sr04.Distance());

if (sr04.Distance() < 50){ tone(8, melody[7], 250); delay(250); } else

if (sr04.Distance() < 100){ tone(8, melody[3], 500); delay(500); } else

if (sr04.Distance() < 150){ tone(8, melody[0], 500); delay(500); }else

if (sr04.Distance() > 150){ delay(500); } }

Step 5: Find a Use Case

Be creative with your creation. You can use this device with more than just a car. You could use it as a proximity sensor to your bedroom, or as a tool for Halloween. Once you get the hand of coding and wiring you can expand this device. If you wanted you could add a LCD to the arduino that displays a real-time distance readout. Once you get the hang of it, using the arduino is a fun and easy way to get comfortable with the process of building and coding with it.