CODING: ULTRASONIC SENSOR (HC-SR04)
Before we learn to code the Ultrasonic sensor let’s learn about the serial monitor on your Arduino IDE.
The Serial Monitor is like a chat room between you and the Arduino that lets you:
talk to the Arduino (send messages from your computer)
listen to the Arduino (see what it wants to tell you)
One important use is that we can use it to display information from sensors.
INFO BYTE: “serial” basically means one-at-a-time, single-file line, one main data path.
To work, both sides need to use the exact same speaking speed.
If the speeds don’t match → it’s just zzzyxzgshhhh garbage on the screen
That “speaking speed” is called the
baud rate.
Most people just use 9600. We speak at 9600 words per minute (or more exactly: 9600 bits per second).
Don’t go faster, don’t go slower, or we won’t understand each other!
Before we use the serial monitor, there are 2 things to note:
1
When you see this // (2 forward slashes) in code the computer ignores what comes after it (on the same line). It’s a way to make notes for yourself or someone else reading the code. I will leave comments on each line like this, it will not effect your code, so you can leave it there.
2
Everything you put inside inverted commas (those ” “ things) is called a string.
The computer mostly ignores what the letters and words actually mean — it doesn’t try to do math with them or think they’re instructions.
Instead, it just treats the whole thing like a message written for humans.
When the Arduino sends that string to the Serial Monitor using Serial.print() or Serial.println(), it’s basically saying:
‘Hey computer, show this exact text on the screen exactly like I wrote it!’
Without the quotes the computer would get confused and think you’re trying to give it commands.
Copy the code below to your IDE, then open your serial monitor and see what it says. Change the message to whatever you like.
➕➕
filename.cpp
void setup() {
Serial.begin(9600); // Line 1: Turn on the chat between Arduino and computer
Serial.println("I LOVE ROBOTS!"); // Line 2: Say something
}
void loop() {
}
What does it mean:
Serial.begin(9600);
This is like turning on your phone’s Wi-Fi so you can chat. 9600 is the speed we both agree to talk at.
Think of it as: “Okay computer and Arduino — we’re both going to speak at normal walking speed (9600 bits per second). Don’t talk like chipmunks or turtles!”
Serial.println(“I LOVE ROBOTS!”);
This sends the sentence “I LOVE ROBOTS!” to your computer.
println means: “write this AND press enter afterwards” (so the next message starts on a new line).
print works too but will keep writing on the same line.
To open the serial monitor click the serial monitor icon (little magnifying glass) on the top right corner of your IDE. A black window will open on the bottom of your screen.
There seems to be a glitch in the new Adruino IDE. You may have problems uploading code to the Arduino if your serial monitor is open. No access to COM port warning! So open the serial monitor only after uploading. If you have problems you will have to unplug the Arduino and then re-plug it. If that doesn’t work close the IDE and open it again, to refresh. Sometimes many times.. It’s irritating!
Now for the exciting stuff!
The code below will measure out the distance of objects in front of the Ultrasonic Sensor and display it in the serial monitor.
Copy it into your IDE, upload it to TURL-X and then open the serial monitor on your computer. Move your hand in front of the sensor and watch it track the distance to your hand.
Very Cool!
➕➕
filename.cpp
void setup() {
pinMode(13, OUTPUT); // We send a signal OUT from this pin
pinMode(12, INPUT); // We listen for a signal coming IN on this pin
Serial.begin(9600); // Start talking to the computer at speed 9600
Serial.println("Distance Sensor Starting..."); // Just a hello message
}
void loop() {
// Step 1: Make a very short "beep" (10 microseconds - 1 microsecond is 1 millionth of a second)
digitalWrite(13, LOW); // Make sure it's quiet first
delayMicroseconds(5); // Wait a tiny bit
digitalWrite(13, HIGH); // BEEP starts!
delayMicroseconds(10); // Beep lasts 10 millionths of a second
digitalWrite(13, LOW); // Beep stops
// Step 2: Listen how long the echo takes to come back
long duration = pulseIn(12, HIGH);
// Step 3: Turn travel time into distance (cm)
// Sound travels about 29 microseconds per centimeter (one way)
int distance = duration / 58;
// Step 4: Show the answer on the screen
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm");
// Wait a little bit before measuring again
delay(500); // 500 ms = half a second
}
Next, let’s combine what we have learnt about LEDs & Ultrasonic Sensors and make TURL-X flash a warning when something gets too close.