The distance sensor in code
Section 10: Sensors and screens in codeLesson 10.2 · ⏱ 8 min
The sensor sends a sound we can't hear and catches its echo. pulseIn measures how long the echo takes to come back, and dividing by 58 gives the distance in centimetres.
const int TRIG = 9;
const int ECHO = 10;
void setup() {
Serial.begin(9600);
pinMode(TRIG, OUTPUT);
pinMode(ECHO, INPUT);
}
void loop() {
digitalWrite(TRIG, LOW);
delayMicroseconds(2);
digitalWrite(TRIG, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG, LOW);
long t = pulseIn(ECHO, HIGH);
int d = t / 58;
Serial.println(d);
delay(300);
}

🖥️ What happens on the stage?
We ran this code in Robo Maker and the challenge passed: “Bravo! It works”.
📌 Another example: near or far
We write a word instead of the number: “near” below 20 cm, otherwise “far”.
const int TRIG = 9;
const int ECHO = 10;
void setup() {
Serial.begin(9600);
pinMode(TRIG, OUTPUT);
pinMode(ECHO, INPUT);
}
void loop() {
digitalWrite(TRIG, LOW);
delayMicroseconds(2);
digitalWrite(TRIG, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG, LOW);
int d = pulseIn(ECHO, HIGH) / 58;
if (d < 20) {
Serial.println("near");
} else {
Serial.println("far");
}
delay(300);
}
far
far
far
far
far
far
near
near
near
near
near
✍️ Practise
Print the distance.
✍️ Practise here in the lesson✖ Close the labor open the lab in a new page ↗
🎯 Challenge yourself
Easy
Write the distance with “cm”.
👀 Show the solution🙈 Hide the solution
const int TRIG = 9;
const int ECHO = 10;
void setup() {
Serial.begin(9600);
pinMode(TRIG, OUTPUT);
pinMode(ECHO, INPUT);
}
void loop() {
digitalWrite(TRIG, LOW);
delayMicroseconds(2);
digitalWrite(TRIG, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG, LOW);
int d = pulseIn(ECHO, HIGH) / 58;
Serial.print(d);
Serial.println(" cm");
delay(300);
}
37 cm
37 cm
35 cm
35 cm
35 cm
35 cm
35 cm
35 cm
Result: like 35 cm.
Medium
Turn it into metres: write d ÷ 100.0.
👀 Show the solution🙈 Hide the solution
const int TRIG = 9;
const int ECHO = 10;
void setup() {
Serial.begin(9600);
pinMode(TRIG, OUTPUT);
pinMode(ECHO, INPUT);
}
void loop() {
digitalWrite(TRIG, LOW);
delayMicroseconds(2);
digitalWrite(TRIG, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG, LOW);
int d = pulseIn(ECHO, HIGH) / 58;
Serial.print(d / 100.0);
Serial.println(" m");
delay(300);
}
0.37 m
0.37 m
1.50 m
1.50 m
1.50 m
1.50 m
1.50 m
Result: like 1.50 m.
Hard
Three zones: “danger” below 10, “careful” below 30, and “safe”.
👀 Show the solution🙈 Hide the solution
const int TRIG = 9;
const int ECHO = 10;
void setup() {
Serial.begin(9600);
pinMode(TRIG, OUTPUT);
pinMode(ECHO, INPUT);
}
void loop() {
digitalWrite(TRIG, LOW);
delayMicroseconds(2);
digitalWrite(TRIG, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG, LOW);
int d = pulseIn(ECHO, HIGH) / 58;
if (d < 10) {
Serial.println("danger");
} else if (d < 30) {
Serial.println("careful");
} else {
Serial.println("safe");
}
delay(300);
}
safe
safe
safe
safe
safe
safe
careful
careful
careful
careful
danger
danger
Result: safe, then careful, then danger.

