This project belongs to the series of "Fun Learning with Sumo Robot", and is based on PJ033: 1st Experience with Nano, this project uses ultrasonic sensor HC-SR04 and Arduino Nano to build a proximity detection system with LEDs indication, the fun learning provides basic knowledge of the object detection solution used for Sumo Robot later on. Goals:
Extended experience with the tiny Nano board, and fix all basic toolchain issues in the development with Nano
Ultrasonic sensor HC-SR04 basic knowledge and how to use it via the library interface
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.
Experience with Arduino Nano, preferably after PJ033: 1st Experience with Nano
1x NANO board
2x LEDs (blue, red)
2x resistors
Breadboard, a few jumping wires
Purpose: A self study topic, find out what is HC-SR04 and discuss about how it works
❓Explore by yourself:
What is HC-SR04? Why does it have 2 "heads"? Which is Transmitter and Receiver?
How does it work theoretically with ultrasonic wave?
How to wire it with microcontrollers?
What can we use ultrasonic sensors for?
What is a Library in program? What lib available for HC-SR04?
References:
TM019: Ultrasonic Sensor HC-SR04
... and lots of information from internet and AI
Challenge:
If the time cost for go-back is 500 µs, then how long distance is it away?
Purpose: Physical wiring of the HC-SR04
Trig <--> D12
Echo <--> D11
Blue LED <--> D3
Red LED <--> D2
Purpose: First experience using HCSR04 library and the interface to get distance measurement
Install library “HCSR04” by Martin Sosic
Compare the program output and physical tape measuring result
Why does it sometimes return -1?
Why does the number jump?
Why does black cloth behave differently?
How does angle, surface, max range affect the measurement?
#include <HCSR04.h>
UltraSonicDistanceSensor hcsr04(12, 11);
void setup() {
Serial.begin(9600);
}
void loop() {
float distance = hcsr04.measureDistanceCm();
Serial.println(distance);
delay(200);
}
Purpose: Involve the LEDs for a reactive distance alarming system
Requirement:
< 15 cm 🔴 Red alarm
15–40 cm ⚪ No alarm
> 40 cm 🔵 Blue alarm
⁉️Challenge:
Try to understand this example code, test it
Does it work correctly? If not, what bugs 🐛and how to fix?
What distance is ideal for a Sumo Robot to start pushing?
Is this example code reliable and stable enough to be used for actions decision like Sumo Robot's attacking and backing?
#include <HCSR04.h>
#define RED_LED 2
#define BLUE_LED 3
UltraSonicDistanceSensor hcsr04(12, 11);
void setup()
{
pinMode(RED_LED, OUTPUT);
pinMode(BLUE_LED, OUTPUT);
}
void loop()
{
float distance = hcsr04.measureDistanceCm();
// FIX BUG FOR BELOW ALARMING LOGIC!
if (distance < 15)
{
digitalWrite(RED_LED, HIGH);
digitalWrite(BLUE_LED, LOW);
}
else if (distance > 40)
{
digitalWrite(RED_LED, LOW);
digitalWrite(BLUE_LED, HIGH);
}
else
{
digitalWrite(RED_LED, LOW);
digitalWrite(BLUE_LED, LOW);
}
delay(100);
}
if (distance > 0 && distance < 15) {
digitalWrite(RED_LED, HIGH);
digitalWrite(BLUE_LED, LOW);
}
else if (distance > 40 || distance < 0) {
digitalWrite(RED_LED, LOW);
digitalWrite(BLUE_LED, HIGH);
}
else {
digitalWrite(RED_LED, LOW);
digitalWrite(BLUE_LED, LOW);
}
Purpose: An optional challenge for fast students, think about a technique to get much better stability
Since the measurement sometimes jumps, if use it for decision, it could be tricky and risky, what can we do in program to redue this noise?
Here is an example snippet, can you integrate into your code and test? Share with the class about what you find.
float getAverageDistance(int samples) {
float sum = 0;
for (int i = 0; i < samples; i++) {
float d = hcsr04.measureDistanceCm();
if (d > 0) sum += d;
delay(20);
}
return sum / samples;
}
Purpose: Enhance basic knowledge understanding, reflect and summarize what we have learnt
How does ultrasonic measure distance?
How to get better measurement result? (Physically placement and installation, environment control, program technique, etc.)
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