Week 8 – Unusual Switch

Concept

For my project, I used the HC-SR04 ultrasonic sensor, which measures distance by sending out a sound wave and timing how long it takes for the echo to bounce back. I used it to detect how close a person’s body is to the sensor. When the person is far (but not too far), a yellow LED lights up. When they get close, a red LED turns on instead. I chose these two colors to mimic a childhood game where the closer you got to a hidden object, the ‘warmer’ you were, so red represents ‘hot,’ and yellow means ‘warm.’

Here’s my wiring and a video of my circuit in action 🙂
https://drive.google.com/drive/folders/1kgAL550ryRCarylolh-Xjpr2KqJABRaU?usp=drive_link

and here’s my GitHub repository

 Code I’m Proud Of

long readDistance() {
 //low for clean start
 digitalWrite(trigPin, LOW);
 delayMicroseconds(2);
 digitalWrite(trigPin, HIGH);
 delayMicroseconds(10);
 digitalWrite(trigPin, LOW);
 long duration = pulseIn(echoPin, HIGH);
 //convert to cm
 //sound speed ≈ 343 m/s → 0.034 cm/µs.
 //div 2 cause roundtrip
 return duration * 0.034 / 2;
}

The part of my code I’m most proud of is the readDistance() function. Through it, I learned how the ultrasonic sensor actually works, sending a pulse, waiting for the echo, and then calculating distance using the speed of sound. I followed a YouTube tutorial to understand the basics, and then used ChatGPT to help debug the issues I ran into. I even got to use some of my physics knowledge to convert time into distance, which made it extra fun since it reminded me of things I had learned before.

Further Improvements

Sometimes the sensor glitches a bit, and I suspect it’s because of my wiring. The HC-SR04 usually needs female-to-male jumper wires to connect properly to the Arduino, but I had to improvise with what I had. Using the Serial Monitor really helped me check if the sensor readings were accurate, but I’d like to clean up my circuit and test again to make it more stable. With proper connections, I think the readings would be much smoother and more consistent.

Another improvement I’d like to try is turning this setup into a Morse code interpreter. Instead of just showing colors for ‘close’ and ‘far,’ I could make the distance readings represent dots and dashes, and then have an LCD screen display the translated message. It would make the project more interactive and add a creative twist while still keeping the hands-free concept. I think it’d be really satisfying to build something that turns simple movements into an actual form of communication.

Leave a Reply