Car Park Sensor

This project uses an ultrasonic distance sensor to measure the distance to an object in centimeters, which is displayed on a seven-segment display. An RGB LED changes colors dynamically based on the measured distance, providing a visual. When the pushbutton is pressed, it turns off the seven-segment display, allowing for user interaction to control the display's functionality. This setup demonstrates a simple way to combine sensors, displays, and LEDs in an Arduino project.
Downloads
Supplies
- Arduino UNO R3
- Jumper Wires
- Seven Segment Display (common anode)
- Push Button
- RGB LED (common anode)
- Ultra Sonic Distance sensor HC-SR04
- 3 330 ohm resistor
- 1 1k ohm resistor
Wiring


Here is a breakdown of the wiring.
- Seven-Segment Display: Connect segments a to g to Arduino pins 4, 5, 6, 7, 8, 9, and 10, each through a 330-ohm resistor.
- Ultrasonic Sensor: Connect the trigger pin to Arduino pin 3 and the echo pin to pin 2.
- RGB LED: Connect the red, green, and blue pins to Arduino pins 12, 13, and A0 with 330-ohm resistors. The common anode connects to 5V.
- Pushbutton: Connect one side to Arduino pin 11 and the other side to ground, with the internal pull-up resistor enabled in the code. Including a 1k ohm resistor.
Initial Coding
Variable Declaration
At the beginning of the code, I implement variables to make my code easy to understand.
- Seven-Segment Display Pins (a-g):
- The variables a, b, c, d, e, f, and g are assigned to specific pins on the Arduino that control each segment of the seven-segment display. For instance, a = 4 and b = 5. These variables allow the Arduino to turn on or off individual segments to show numbers.
- Ultrasonic Sensor Pins:
- The trigger_pin variable is for the pin that sends out ultrasonic pulses, while echo_pin is for the pin that listens for the reflected pulses. These are connected to pins 3 and 2. The max_dist_cm variable defines the maximum distance the sensor can measure, which is set to 10 cm.
- RGB LED Pins:
- The pins for the red, green, and blue parts of the RGB LED are controlled using the variables red_pin, green_pin, and blue_pin. These are connected to pins 12, 13, and A0, respectively.
- Pushbutton Pin:
- The button_pin variable represents the pin connected to the pushbutton, which is set to pin 11.
- Library Object (NewPing):
- The sonar object is created using the NewPing library to handle the ultrasonic sensor. It uses the trigger_pin, echo_pin, and max_dist_cm variables to measure distances accurately.
void setup
The setup function is run once when the Arduino starts or resets. It sets up the Arduino so its ready for the void loop.
- Serial Communication Initialization:
- The command Serial.begin(9600); sets up a connection between the Arduino and your computer’s Serial Monitor. This is useful for checking data like distance readings in real time.
- Seven-Segment Display Pin Configuration:
- Each segment pin (a to g) is set as an output using the pinMode function. For example, pinMode(a, OUTPUT); tells the Arduino it will control the segment connected to pin 4. This setup allows the Arduino to control the display and show numbers.
- RGB LED Pin Configuration:
- The pins controlling the red, green, and blue channels of the RGB LED are also set as outputs with pinMode(red_pin, OUTPUT);, and similar lines for the other colors. This lets the Arduino change the LED’s color.
- Pushbutton Configuration:
- The pushbutton pin is configured as an input with the internal pull-up resistor enabled using pinMode(button_pin, INPUT_PULLUP);. This ensures that when the button is not pressed, the pin reads as HIGH, and when pressed, it reads as LOW.
Void Codes
Here is a quick rundown of each void.
void blink_all_segs()
- This function blinks all segments of the seven-segment display three times.
- It alternates between turning all segments off and on with a delay, creating a flashing effect.
void turn_off_all_segs()
- Turns off all segments of the seven-segment display by setting their pins to LOW.
- This ensures no segments are lit before displaying a new number.
void turn_on_all_segs()
- Turns on all segments of the display by setting their pins to HIGH.
- This is useful for testing or resetting the display to ensure all segments work.
void display_dig(int *arr, int arr_len)
- Lights up specific segments of the display based on an array of pin numbers (arr).
- The arr array contains the segment pins to turn on, and arr_len is the number of segments to light.
void display_num(int num)
- Displays a specific number (0-9) on the seven-segment display.
- It uses predefined segment combinations for each digit and calls display_dig to light the appropriate segments.
void test_blink_all_segs()
- Calls blink_all_segs() to blink all segments and includes a delay before and after the blinking sequence.
- Used for testing the display’s ability to blink.
void test_display_all_nums()
- Cycles through and displays all digits (0-9) on the seven-segment display, one at a time, with a one-second delay between numbers.
- This tests the ability to show all numbers correctly.
void set_rgb_color(int red, int green, int blue)
- Adjusts the brightness of the red, green, and blue channels of the RGB LED using analogWrite.
- Each parameter (red, green, blue) determines the brightness (0-255) for the respective color.
void handle_button_press()
- Checks the button state and changes the RGB LED color if the button is pressed.
- Ensures the LED cycles through predefined colors only when the button is actively pressed.
Void Loop Code
The void loop function is the part of the program that runs repeatedly after the setup function runs. It continuously checks the sensor and button inputs, updates the display, and manages the RGB LED based on the current state.
- Distance Measurement with the Ultrasonic Sensor:
- The line int dist_cm = sonar.ping_cm(); uses the sonar object to measure the distance of an object in centimeters.
- This value is stored in the variable dist_cm. If no object is detected or the distance is beyond the sensor’s range, dist_cm will return 0.
- Display Distance on the Seven-Segment Display:
- The function display_num(dist_cm); is called to show the measured distance on the seven-segment display.
- If the distance exceeds the single-digit range (greater than 9 cm), only the least significant digit will be displayed.
- Button Press Handling:
- The handle_button_press() function checks if the button is pressed and, if so, cycles the RGB LED to the next predefined color.
- This ensures the LED responds interactively to the button press.
- Delay Between Iterations:
- The line delay(500); pauses the program for 500 milliseconds (half a second) to prevent the sensor and display from updating too quickly, ensuring smooth operation.
Final Code
#include <NewPing.h> // Include the NewPing library for working with ultrasonic sensors.
// Define which pins correspond to each of the seven segments: a - g.
int a = 4;
int b = 5;
int c = 6;
int d = 7;
int e = 8;
int f = 9;
int g = 10;
// Ultrasonic sensor pins and settings.
int trigger_pin = 3; // Trigger pin of the ultrasonic sensor.
int echo_pin = 2; // Echo pin of the ultrasonic sensor.
int max_dist_cm = 10; // Max measurable distance (in cm).
NewPing sonar(trigger_pin, echo_pin, max_dist_cm); // Create a NewPing object to interface with the ultrasonic sensor.
// Define pins for the button and RGB LED.
int button_pin = 11; // Button pin.
int red_pin = 12; // Red LED pin.
int green_pin = 13; // Green LED pin.
int blue_pin = A0; // Blue LED pin.
bool display_on = true; // Track the state of the seven-segment display.
void setup() {
Serial.begin(9600); // Initialize serial communication at 9600.
// Configure the seven-segment pins as output.
pinMode(a, OUTPUT);
pinMode(b, OUTPUT);
pinMode(c, OUTPUT);
pinMode(d, OUTPUT);
pinMode(e, OUTPUT);
pinMode(f, OUTPUT);
pinMode(g, OUTPUT);
// Configure the button as input with an internal pull-up resistor.
pinMode(button_pin, INPUT_PULLUP);
// Configure the RGB LED pins as output.
pinMode(red_pin, OUTPUT);
pinMode(green_pin, OUTPUT);
pinMode(blue_pin, OUTPUT);
// Ensure the RGB LED is off initially (common anode setup: HIGH = off).
digitalWrite(red_pin, HIGH);
digitalWrite(green_pin, HIGH);
digitalWrite(blue_pin, HIGH);
}
// Blink all segments of the display on and off three times.
void blink_all_segs() {
int delay_time_ms = 200; // Delay time between blinks.
for (int i = 0; i < 3; i++) {
turn_off_all_segs(); // Turn off all segments.
delay(delay_time_ms);
turn_on_all_segs(); // Turn on all segments.
delay(delay_time_ms);
}
turn_off_all_segs(); // Ensure all segments are off at the end.
}
// Turn off all segments of the seven-segment display.
void turn_off_all_segs() {
int seg_len = 7; // Number of segments.
int segs[seg_len] = {a, b, c, d, e, f, g}; // Array of segment pins.
for (byte i = 0; i < seg_len; i++) {
digitalWrite(segs[i], LOW); // Set each segment pin to LOW (off).
}
}
// Turn on all segments of the seven-segment display.
void turn_on_all_segs() {
int seg_len = 7; // Number of segments.
int segs[seg_len] = {a, b, c, d, e, f, g}; // Array of segment pins.
for (byte i = 0; i < seg_len; i++) {
digitalWrite(segs[i], HIGH); // Set each segment pin to HIGH (on).
}
}
// Light up specific segments based on an array of segment pins.
void display_dig(int *arr, int arr_len) {
turn_off_all_segs(); // Clear the display.
for (byte i = 0; i < arr_len; i++) {
digitalWrite(arr[i], HIGH); // Turn on specified segments.
}
}
// Display a specific number on the seven-segment display.
void display_num(int num) {
// Arrays defining which segments are lit for each digit.
int one_dig[2] = {b, c};
int two_dig[5] = {a, b, g, e, d};
int three_dig[5] = {a, b, g, c, d};
int four_dig[4] = {f, g, b, c};
int five_dig[5] = {a, f, g, c, d};
int six_dig[5] = {f, e, g, c, d};
int seven_dig[3] = {a, b, c};
int eight_dig[7] = {a, b, c, d, e, f, g};
int nine_dig[5] = {a, b, c, f, g};
int zero_dig[6] = {a, b, c, d, e, f};
turn_off_all_segs(); // Clear the display.
// Use a switch statement to display the correct number.
switch (num) {
case 0 :
display_dig(zero_dig, 6);
break;
case 1 :
display_dig(one_dig, 2);
break;
case 2 :
display_dig(two_dig, 5);
break;
case 3 :
display_dig(three_dig, 5);
break;
case 4 :
display_dig(four_dig, 4);
break;
case 5 :
display_dig(five_dig, 5);
break;
case 6 :
display_dig(six_dig, 5);
break;
case 7 :
display_dig(seven_dig, 3);
break;
case 8 :
display_dig(eight_dig, 7);
break;
case 9 :
display_dig(nine_dig, 5);
break;
}
}
// Update the RGB LED based on the distance measured.
void update_rgb_led(int dist_cm) {
if (dist_cm <= 2) { // Close range: Green.
digitalWrite(red_pin, HIGH);
digitalWrite(green_pin, LOW); // Green on.
digitalWrite(blue_pin, HIGH);
} else if (dist_cm <= 5) { // Medium range: Blue.
digitalWrite(red_pin, HIGH);
digitalWrite(green_pin, HIGH);
digitalWrite(blue_pin, LOW); // Blue on.
} else { // Far range or no object detected: Red.
digitalWrite(red_pin, LOW); // Red on.
digitalWrite(green_pin, HIGH);
digitalWrite(blue_pin, HIGH);
}
}
void loop() {
// Check if the button is pressed to toggle display state.
if (digitalRead(button_pin) == LOW) {
display_on = !display_on; // Toggle the display state.
delay(300); // Debounce delay to avoid rapid toggling.
}
// Measure distance using the ultrasonic sensor.
int dist_cm = sonar.ping_cm();
Serial.print("Distance from sensor in cm: ");
Serial.println(dist_cm);
// Update the RGB LED based on distance.
update_rgb_led(dist_cm);
// Update the seven-segment display only if it's enabled.
if (display_on) {
display_num(dist_cm); // Display the distance on the seven-segment.
}
else {
turn_off_all_segs(); // Turn off the seven-segment display.
}
delay(500); // Delay before the next loop iteration.
}