Devices & Components
16x2 LCD display with I²C interface
Box 525 1% precision resistors - 17 values
Arduino Sensor Kit - Base
Arduino Uno Rev3
Set of 70 assorted color 5mm LEDs
Software & Tools
Arduino IDE
Project description
Code
Arduino Code
cpp
1/* 2 ============================================================ 3 Parking Sensor / Distance Alert 4 CS 220: Computer Architecture & Embedded Systems 5 Capstone Project 6 ============================================================ 7 COMPONENTS: 8 - Arduino Uno 9 - HC-SR04 Ultrasonic Sensor 10 - LCD 16x2 Display 11 - Potentiometer (LCD contrast) 12 - Green, Yellow, Red LEDs 13 - Piezo Buzzer 14 - 470Ω resistors (LEDs), 220Ω resistor (LCD backlight) 15 16 PIN MAPPING: 17 Ultrasonic → TRIG=7, ECHO=6 18 LCD → RS=12, EN=11, D4=5, D5=4, D6=3, D7=2 19 LEDs → Green=8, Yellow=9, Red=10 20 Buzzer → Pin 13 21 ============================================================ 22*/ 23 24#include <LiquidCrystal.h> 25 26// ---------- LCD Setup ---------- 27LiquidCrystal lcd(12, 11, 5, 4, 3, 2); 28 29// ---------- Ultrasonic Sensor Pins ---------- 30const int TRIG_PIN = 7; 31const int ECHO_PIN = 6; 32 33// ---------- LED Pins ---------- 34const int GREEN_LED = 8; 35const int YELLOW_LED = 9; 36const int RED_LED = 10; 37 38// ---------- Buzzer Pin ---------- 39const int BUZZER_PIN = 13; 40 41// ---------- Distance Thresholds (cm) ---------- 42const int SAFE_DIST = 40; // Green → far away, all clear 43const int CAUTION_DIST = 20; // Yellow → getting close 44const int DANGER_DIST = 10; // Red → very close, beep fast 45 46// ---------- Timing for beeping ---------- 47unsigned long lastBeepTime = 0; 48int beepInterval = 500; // ms between beeps (gets shorter as closer) 49 50// ============================================================ 51// SETUP 52// ============================================================ 53void setup() { 54 // LED pins as OUTPUT 55 pinMode(GREEN_LED, OUTPUT); 56 pinMode(YELLOW_LED, OUTPUT); 57 pinMode(RED_LED, OUTPUT); 58 59 // Buzzer as OUTPUT 60 pinMode(BUZZER_PIN, OUTPUT); 61 62 // Ultrasonic pins 63 pinMode(TRIG_PIN, OUTPUT); 64 pinMode(ECHO_PIN, INPUT); 65 66 // Start LCD 67 lcd.begin(16, 2); 68 69 // Welcome message 70 lcd.setCursor(0, 0); 71 lcd.print(" PARKING SENSOR "); 72 lcd.setCursor(0, 1); 73 lcd.print(" Initializing "); 74 delay(2000); 75 lcd.clear(); 76} 77 78// ============================================================ 79// MAIN LOOP 80// ============================================================ 81void loop() { 82 // Get distance reading 83 long distance = getDistance(); 84 85 // Update LCD display 86 updateDisplay(distance); 87 88 // Update LEDs based on distance 89 updateLEDs(distance); 90 91 // Update buzzer based on distance 92 updateBuzzer(distance); 93 94 delay(100); // Small delay between readings for stability 95} 96 97// ============================================================ 98// FUNCTIONS 99// ============================================================ 100 101// --- Measure distance using HC-SR04 --- 102long getDistance() { 103 // Send a 10 microsecond pulse to TRIG 104 digitalWrite(TRIG_PIN, LOW); 105 delayMicroseconds(2); 106 digitalWrite(TRIG_PIN, HIGH); 107 delayMicroseconds(10); 108 digitalWrite(TRIG_PIN, LOW); 109 110 // Read the ECHO pulse duration 111 long duration = pulseIn(ECHO_PIN, HIGH); 112 113 // Convert duration to distance in cm 114 // Speed of sound = 343 m/s = 0.0343 cm/µs 115 // Divide by 2 because pulse travels to object AND back 116 long distance = duration * 0.0343 / 2; 117 118 // Cap at 200cm for display purposes 119 if (distance > 200) distance = 200; 120 if (distance < 0) distance = 0; 121 122 return distance; 123} 124 125// --- Update the LCD with current distance and status --- 126void updateDisplay(long distance) { 127 // Row 1: Distance reading 128 lcd.setCursor(0, 0); 129 lcd.print("Dist: "); 130 lcd.print(distance); 131 lcd.print(" cm "); // Extra spaces clear old characters 132 133 // Row 2: Status message 134 lcd.setCursor(0, 1); 135 if (distance > SAFE_DIST) { 136 lcd.print(" ALL CLEAR! "); 137 } else if (distance > CAUTION_DIST) { 138 lcd.print(" SLOW DOWN... "); 139 } else if (distance > DANGER_DIST) { 140 lcd.print(" CAUTION!!! "); 141 } else { 142 lcd.print(" ** STOP! ** "); 143 } 144} 145 146// --- Update LEDs based on distance --- 147void updateLEDs(long distance) { 148 if (distance > SAFE_DIST) { 149 // Far away — green only 150 digitalWrite(GREEN_LED, HIGH); 151 digitalWrite(YELLOW_LED, LOW); 152 digitalWrite(RED_LED, LOW); 153 } else if (distance > CAUTION_DIST) { 154 // Getting close — yellow only 155 digitalWrite(GREEN_LED, LOW); 156 digitalWrite(YELLOW_LED, HIGH); 157 digitalWrite(RED_LED, LOW); 158 } else if (distance > DANGER_DIST) { 159 // Close — yellow + red 160 digitalWrite(GREEN_LED, LOW); 161 digitalWrite(YELLOW_LED, HIGH); 162 digitalWrite(RED_LED, HIGH); 163 } else { 164 // Very close — red only, flashing 165 digitalWrite(GREEN_LED, LOW); 166 digitalWrite(YELLOW_LED, LOW); 167 digitalWrite(RED_LED, HIGH); 168 } 169} 170 171// --- Update buzzer beep rate based on distance --- 172void updateBuzzer(long distance) { 173 // No beep when safe distance 174 if (distance > SAFE_DIST) { 175 noTone(BUZZER_PIN); 176 return; 177 } 178 179 // Beep interval gets shorter as object gets closer 180 if (distance > CAUTION_DIST) { 181 beepInterval = 600; // Slow beep 182 } else if (distance > DANGER_DIST) { 183 beepInterval = 250; // Fast beep 184 } else { 185 beepInterval = 80; // Very fast beep (almost solid tone) 186 } 187 188 // Non-blocking beep using millis() 189 unsigned long currentTime = millis(); 190 if (currentTime - lastBeepTime >= beepInterval) { 191 lastBeepTime = currentTime; 192 tone(BUZZER_PIN, 1000, 50); // 1000Hz tone, 50ms duration 193 } 194}
Comments
Only logged in users can leave comments