Robotics & Arduino
Lesson 3 · Hands on

Project 06

Read the ultrasonic sensor

The code.

//Note:
// US Range - 0cm to 2cm = Blind range.
// US Range - 2cm to 4m  = Effective range.

//Declare
const int TRIG_PIN = 11;  // Start burst
const int ECHO_PIN = 12;  // Return value

//Setup
void setup() {

  // Init serial comm 
  Serial.begin(9600);         // Run at 9600 bits per second

  // Set US pin modes
  pinMode(TRIG_PIN, OUTPUT);
  pinMode(ECHO_PIN, INPUT);

}
 
//Run
void loop() {

  // 1. Clear trigger to ensure clean pulse
  digitalWrite(TRIG_PIN, LOW);
  delayMicroseconds(2);
  
  // 2. Send a 10-microsecond pulse to trigger sonic blast
  digitalWrite(TRIG_PIN, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIG_PIN, LOW);
  
  // 3. Measure (in microseconds) echo return time
  long duration = pulseIn(ECHO_PIN, HIGH);
  
  // 4. Calc distance in centimeters 
  // - Speed of sound is ~0.0343 cm per microsecond. 
  // - ping goes out and back - Divide by 2 for one-way trip
  float distance = duration * 0.0343 / 2.0;
  
  // 5. Prt to serial
  Serial.print("Distance: ");
  Serial.print(distance);
  Serial.println(" cm");
  
  // Wait a bit.
  delay(100);

}

//Logic
// - we can conolidate teps 1 and 2
// - We can move print here
HC-SR04 ultrasonic sensor wiring: Arduino 5V to VCC, GND to GND, D11 to Trig, and D12 to Echo.

These “eyes” are acoustic transducers.

  • The Transmitter (T): a speaker, usually operating around 40 kHz.
  • The Receiver (R): a microphone, specifically listening for the echo.

The 4-Step Process

  1. The Trigger: pulse the TRIG pin to start.
  2. The Sonic Blast: the transmitter sends an 8-pulse burst of ultrasonic sound waves into the air.
  3. The Echo: the waves hit an object in front of the sensor and bounce back toward it.
  4. The Stop Sign: the receiver detects the echo, and the sensor signals the Arduino via the ECHO pin.
  • Arduino records the total travel time in microseconds.
  • For this calculation, the speed of sound in air is approximately 340 meters per second, or roughly 0.034 centimeters per microsecond.
  • Distance is (speedOfSound × time) / 2.
  • Why 2? Out and back: two-way travel. Dividing by two gives the one-way distance to the target.

Events that happen incredibly fast.

In millionths of a second: microseconds.

1. delayMicroseconds()

  • The ultrasonic sensor needs a short, precisely timed trigger signal to start a measurement.
  • We use a 10-microsecond pulse.
  • delay(1); means 1 millisecond = 1,000 microseconds — 100 times longer than the 10-microsecond pulse we use.

2. pulseIn()

  • A built-in Arduino function.
  • Like starting a stopwatch.
  • Blocking: our code waits inside the function. The next line does not run until it returns.
  • The timeout argument limits how long it waits. 30000 means 30,000 microseconds, or about 30 milliseconds.

Wait for a specific pin to switch states, then measure how long it stays in that state before switching back.

long duration = pulseIn(ECHO_PIN, HIGH, 30000);

This example explicitly sets the timeout. The two-argument call in the sketch above uses the default timeout of one second. A timeout returns 0.

All together:

  1. Wait until ECHO_PIN goes HIGH — the sensor's echo-timing pulse begins.
  2. Start an internal stopwatch in microseconds.
  3. Stop the stopwatch when ECHO_PIN goes LOW — the sensor ends the echo-timing pulse.
  4. Return the pulse duration as a number: the round-trip travel-time measurement used to calculate distance.

Arduino pulseIn() reference