Skip to main content
Live robotics updates
Q2 2026 robotics demand increased across industries, reports A3The Robot ReportStrengthening U.S. Army sustainment: TALUS to deliver autonomous distributionThe Robot ReportDAF Trucks to integrate Einride Driver to scale autonomous electric freightThe Robot ReportRobotics roadmaps from around the worldRobohubKapsch TrafficCom expands satellite tolling to the NetherlandsRobotics & Automation NewsChina racing ahead in autonomous car adoption, says new reportRobotics & Automation NewsDefense drone developer Cambridge Aerospace raises $300MThe Robot ReportPlusAI reaches key milestones ahead of launching its autonomous trucksThe Robot ReportWebinar to discuss safety and scaling robot fleets in the warehouseThe Robot ReportThe 370-billion-dollar race: How integrated design can help humanoid manufacturers succeed in a rapidly growing marketThe Robot ReportQ2 2026 robotics demand increased across industries, reports A3The Robot ReportStrengthening U.S. Army sustainment: TALUS to deliver autonomous distributionThe Robot ReportDAF Trucks to integrate Einride Driver to scale autonomous electric freightThe Robot ReportRobotics roadmaps from around the worldRobohubKapsch TrafficCom expands satellite tolling to the NetherlandsRobotics & Automation NewsChina racing ahead in autonomous car adoption, says new reportRobotics & Automation NewsDefense drone developer Cambridge Aerospace raises $300MThe Robot ReportPlusAI reaches key milestones ahead of launching its autonomous trucksThe Robot ReportWebinar to discuss safety and scaling robot fleets in the warehouseThe Robot ReportThe 370-billion-dollar race: How integrated design can help humanoid manufacturers succeed in a rapidly growing marketThe Robot Report
View all
Back
Arduino sample guide

roboda-sample-0357 - Ultrasonic Distance 15

Measure distance with an ultrasonic trigger/echo module.

Step-by-step build

Measure obstacle distance with trigger and echo timing.

1

Wire TRIG and ECHO to the listed pins.

2

Send a short trigger pulse.

3

Measure echo pulse time with pulseIn().

4

Convert travel time into distance in centimeters.

Important code parts

These are the sections learners should understand before changing the sketch.

delayMicroseconds(10)

Creates the trigger pulse required by HC-SR04 style modules.

pulseIn(echoPin, HIGH)

Measures how long the echo pin stays HIGH.

duration * 0.0343 / 2

Uses speed of sound and divides by two for the out-and-back trip.

Pin mapping for this sample

Use this first table for the exact sample wiring, then use the board-family table when moving the sketch to another Arduino board.

Code nameSample pinModeConnect toNote
trigPinD5OUTPUTHC-SR04 TRIGSends a 10 microsecond pulse.
echoPinD7INPUTHC-SR04 ECHO3.3V boards need level shifting for 5V echo modules.

Arduino board pin reference

The code names stay the same; only the physical board pins may change by family. Always confirm with the board silkscreen when using clones or special variants.

Board familyDigitalPWMAnalogI2CSPIInterrupt
Uno / Nano / Pro MiniDefault target for these samples.D0-D13D3, D5, D6, D9, D10, D11A0-A5A4 SDA, A5 SCLD10 SS, D11 MOSI, D12 MISO, D13 SCK / ICSPD2, D3
Mega 2560Best for many sensors or multiple serial ports.D0-D53D2-D13, D44-D46A0-A15D20 SDA, D21 SCLD50 MISO, D51 MOSI, D52 SCK, D53 SS / ICSPD2, D3, D18, D19, D20, D21
Leonardo / MicroUSB serial can behave differently because the main MCU handles USB.D0-D13D3, D5, D6, D9, D10, D11, D13A0-A5 plus extra analog-capable pins by boardD2 SDA, D3 SCLICSP headerD0, D1, D2, D3, D7
Nano Every / Uno WiFi Rev2Check the board package pinout for exact PWM labels.D0-D13PWM-marked digital pinsA0-A7 on Nano Every, A0-A5 on Uno WiFi Rev2SDA/SCL marked pinsICSP/SPI marked pinsUse pins supported by digitalPinToInterrupt()
Uno R4 Minima / Uno R4 WiFi5V logic board; still confirm shield/library compatibility.D0-D13PWM-marked digital pinsA0-A5SDA/SCL marked pinsICSP/SPI marked pinsUse digitalPinToInterrupt(pin)
Due3.3V logic only; protect pins from 5V sensors.D0-D53PWM-marked digital pinsA0-A11SDA/SCL and SDA1/SCL1SPI headerUse digitalPinToInterrupt(pin)
MKR / Nano 33 family3.3V logic. Some AVR-only libraries may need alternatives.Board-marked digital pinsPWM-marked pinsBoard-marked analog pinsSDA/SCL marked pinsSPI marked pinsUse digitalPinToInterrupt(pin)

Full sample code

/*
  Roboda verified Arduino sample 0357
  Feature: Ultrasonic Distance
  Generated for online build, classroom practice, and hardware upload testing.
*/

const byte trigPin = 5;
const byte echoPin = 7;

void setup() {
  Serial.begin(9600);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
}

void loop() {
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  unsigned long duration = pulseIn(echoPin, HIGH, 30000UL);
  float distanceCm = duration * 0.0343 / 2.0;
  Serial.println(distanceCm);
  delay(150);
}

Teacher notes

  • Compile the sample before changing wiring or constants.
  • When changing boards, keep the code names the same and update only the pin number constants.
  • For 3.3V boards, level-shift 5V sensors and modules before connecting signal pins.
  • Use the first compiler error and the Roboda diagnostic suggestion before fixing later warnings.
Roboda support Leave a message

New messages appear in the Finite LiveChat Ops Android app.