START A BIT > Projects Library > This page
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.)