This project simulates an interesting parking assistance system, it uses an analog ultrasonic sensor US-016 to detect the distance, and 3 LEDs as well as buzzer to indicate the urgency. It includes a few tasks, bringing the students basic understanding of the ultrasonic sensor, the analog input in Arduino, and finally a working system. Goals:
Analog ultrasonic sensor US-016, what and how
Understand analog signal, and how to get analog input in Arduino
Buzzer and LED as digital output
START A BIT > Projects Library > This page
[ ] Simple: 30-60 minutes
[x] Moderate: 1-2 hours
[ ] Challenging: more than 2 hours
The estimation bases on average situation without unexpected troubleshooting, assuming that the student meets prerequisites. It only indicates rough time needed to complete this project, but not about technical difficulty, not covering discussion and sharing time. Possible breakdown:
Task 1: 20 minutes
Task 2: 15 minutes
Task 3: 30-45 minutes
One should have prior knowledge
1x Arduino UNO board (R3 revision or WiFi Rev2) , with USB cable
3x LEDs and current limit resistors
Breadboard, and a few jumping wires
Purpose: Get to know what is ultrasonic sensor US-016, and how to setup hardware connection
Purpose: Understand how to read analog input, and inspect the converted numeric values by serial printing
analogRead(pin) to read in analog signal
What are the min and max value in your test?
Have we missed pinMode(A0, INPUT)?
int sonic_input;
void setup() {
Serial.begin(9600);
}
void loop() {
// Read analog input from pin A0
// resulted as integer ranged 0-1023
sonic_input = analogRead(A0);
Serial.println(sonic_input);
delay(300);
}
Purpose: Use analog input value, practice compound if-else logic
if-else
digitalWrite(pin, value) to control LEDs as ON/OFF
tone(pin, frequency) to trigger buzzer playing a sound
noTone(pin) to stop the buzzer sound
Fix the incomplete /*FIX ME*/ or /*FIX MISSING CODES HERE*/ to make it working
This example solution is clear with the logic, but not very neat, can you figure out another solution still using if-else?
#define BUZZER 7
#define LED_GREEN 8
#define LED_YELLOW 9
#define LED_RED 10
int sonic_input;
void setup() {
Serial.begin(9600);
pinMode(BUZZER, OUTPUT);
pinMode(LED_GREEN, /*FIX ME*/);
pinMode(LED_YELLOW, /*FIX ME*/);
pinMode(LED_RED, /*FIX ME*/);
}
void loop() {
// Read analog input from pin A0
// Resulted as integer ranged 0-1023
sonic_input = analogRead(/*FIX ME*/);
Serial.println(sonic_input);
if (sonic_input < 300) {
// High Warning Level:
// LEDs: GREEN ON, YELLOW ON, RED Fast-blinking
// Sound: Fast-beeping, same pace as RED LED blinking
/*FIX MISSING CODES HERE*/
}
else if (sonic_input >= 300 && sonic_input < 600) {
// Mid Warning Level
// LEDs: GREEN ON, YELLOW Mild-blinking, RED OFF
// Sound: Mild-beeping, same pace as YELLOW LED blinking
/*FIX MISSING CODES HERE*/
}
else if (sonic_input >= 600 && sonic_input < 900) {
// Low warning level
// LEDs: GREEN Slow-blinking, YELLOW OFF, RED OFF
// Sound: Slow-beeping, same pace as GREEN LED blinking
/*FIX MISSING CODES HERE*/
}
else if (sonic_input >= 900) {
// No Warning, safe distance so far
// LEDs: ALl LEDs OFF
// Sound: No sound
/*FIX MISSING CODES HERE*/
}
delay(100);
}
Purpose: Enhance basic knowledge understanding, reflect and summarize what we have learnt
How does ultrasonic sensor work to detect distance?
How do we get analog input? What are the values we get there in the program?
Tips: Review knowledge from here if needed
Follow the tutor to take 5-10 minutes to review the questions and answers, correct some possible misunderstood issues or faults
Take 1-2 minutes to explore anything on the tools and the making flow, with curiosity, no matter how much prior knowledge you have, you should make
At least 1 interesting finding
At least 1 curious question
Used by LP2519: Analog Input, 25w19
pinMode(LED_GREEN, OUTPUT);
pinMode(LED_YELLOW, OUTPUT);
pinMode(LED_RED, OUTPUT);
sonic_input = analogRead(A0);
if (sonic_input < 300) {
// High Warning Level:
// LEDs: GREEN ON, YELLOW ON, RED Fast-blinking
// Sound: Fast-beeping, same pace as RED LED blinking
digitalWrite(LED_GREEN, HIGH);
digitalWrite(LED_YELLOW, HIGH);
digitalWrite(LED_RED, HIGH);
tone(BUZZER, 1000);
delay(30);
digitalWrite(LED_RED, LOW);
noTone(BUZZER);
delay(30);
}
else if (sonic_input >= 300 && sonic_input < 600) {
// Mid Warning Level
// LEDs: GREEN ON, YELLOW Mild-blinking, RED OFF
// Sound: Mild-beeping, same pace as YELLOW LED blinking
digitalWrite(LED_GREEN, HIGH);
digitalWrite(LED_YELLOW, HIGH);
tone(BUZZER, 600);
delay(60);
digitalWrite(LED_YELLOW, LOW);
noTone(BUZZER);
delay(60);
digitalWrite(LED_RED, LOW);
}
else if (sonic_input >= 600 && sonic_input < 900) {
// Low warning level
// LEDs: GREEN Slow-blinking, YELLOW OFF, RED OFF
// Sound: Slow-beeping, same pace as GREEN LED blinking
digitalWrite(LED_GREEN, HIGH);
tone(BUZZER, 300);
delay(100);
digitalWrite(LED_GREEN, LOW);
noTone(BUZZER);
delay(100);
digitalWrite(LED_YELLOW, LOW);
digitalWrite(LED_RED, LOW);
}
else if (sonic_input >= 900) {
// No Warning, safe distance so far
// LEDs: ALl LEDs OFF
// Sound: No sound
digitalWrite(LED_GREEN, LOW);
digitalWrite(LED_YELLOW, LOW);
digitalWrite(LED_RED, LOW);
}