yahboom
HomeLearn
Solution
Blog
Online Store
About Us
EN/中文
Roboduino V2📖
0.Instruction manual
  • Instruction manual
1.Get started quickly
  • 01.Assembly of the Car
  • 02.Remote Control Car
  • 03.Common Problem Analysis
  • 04. Lithium Battery Pack and Charger Usage Guidelines
  • 05. Important Notes (Read Before Starting)
2.Environment Construction
  • 01.Serial port driver installation
  • 02.Arduino IDE Installation
  • 03.Arduino IDE usage
  • 04.Using third-party libraries
  • 05.Program compilation and upload
3.Basic car course
  • 01.RGB Control
  • 02.Serial Print
  • 03.KEY Read
  • 04.Buzzer control
  • 05.Logo Breath
  • 06.Servos Control
  • 07.Motor Control
  • 08.Ultrasonic Ranging
  • 09.Track Print
  • 10.Infrared Remote Print
  • 11.WIFI Camera
4.Advanced car course
  • 01.RGB Breath
  • 02.Play Music
  • 03.KEY Control RGB
  • 04.KEY Control Buzzer
  • 05.Serial Parse Data
  • 06.Parse Data Control Motor
  • 07.Parse Data Control RGB
  • 08.Parse Data Control Buzzer
  • 09.Parse Data Control Servo
5.Car motion control
  • 00.Mecanum wheel motion principle
  • 01.Car Vertical Movement
  • 02.Car Diagonal Movement
  • 03.Car Sideways Movement
  • 04.Car Rotational Movement
  • 05.Car Patrol
6.Comprehensive gameplay of car
  • 01.Car tracking
  • 02.Car Obstacle Avoid
  • 03.Car Follow Hand
  • 04.Car tracking Avoid
  • 05.Car Infrared Remote
  • 06.WiFi Control Car
  • 07.IR WiFi Control Car
7.AI vision course
  • 1. Cat(dog) detection
  • 2. Color recognition
  • 3. QR code instructions
  • 4. Person Recognition
  • 5. Face Detection
  • 6. Color Follows
  • 7. Face following
8.Expand peripherals
  • 01.Digital Tube Display
  • 02.Color Recognition
Download
  • Code
  • Library
  • Hardware manual
  • Software
  • 3D Model
  • Instruction Manual
Download ZIP

Welcome to Roboduino V2 repository

08.Ultrasonic Ranging

Ultrasonic Ranging

Ultrasonic RangingDevice connectionHardware connectionSoftware connectionUltrasonic distance measurementControl principleDistance measurement formulaControl pinCode analysisExperimental results

Use the serial port to print the obstacle distance measured by the ultrasonic module.

Device connection

Hardware connection

Use the Type-B data cable to connect the Arduino Uno and the computer.

Software connection

Open the "Arduino IDE" software and select the model and serial port number corresponding to the development board.

Ultrasonic distance measurement

Ultrasonic waves are non-contact physical quantity measurement modules. They can accurately measure distances by transmitting and receiving ultrasonic signals.

The location selected by the red box is the location of the ultrasonic module interface:

image-20240605192556269

Control principle

Transmit (trigger) signal: provide a pulse trigger signal of more than 10us;

Receive signal: the received high level time is proportional to the measured distance.

Distance measurement formula

Distance=highleveltime∗soundspeed(340m/s)2

Control pin

The ultrasonic module and Robduino expansion board have special interfaces for installation and connection.

Peripheral moduleArduino Uno
VCCVCC
TRIG (trigger end)RGB (12)
ECHO (receiver end)ECHO (11)
GNDGND

Code analysis

Here is only a brief introduction to the code content. For detailed code, please refer to the corresponding code file, which is provided in the download area!

  • Define ultrasonic control pins and initial distance

​x
// 定义超声波模块控制引脚 Define ultrasonic control pins
#define TRIG_PIN 11
#define ECHO_PIN 12

float Distance = 0.0; // 初始化超声波距离变量 Initialize the ultrasonic distance variable
  • Get ultrasonic distance

x
/**
 * @brief 获取超声波距离 Get ultrasonic distance
 * @param trigPin: 触发测距引脚 Trigger pin
 * @param echoPin: 接收测距引脚 Echo pin
 * @retval 转换的距离 Measured distance (cm)
 */
float getDistance(int trigPin, int echoPin) {
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);

  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  long duration = pulseIn(echoPin, HIGH);
  float distance = (duration * 0.034 / 2);
  return distance;
}
  • Initialization Code

xxxxxxxxxx
void setup() {
  Serial.begin(115200);  // 初始化串口波特率115200 Initialize serial communication at 115200 bps
}
  • Looping code

xxxxxxxxxx
void loop() {
  delay(100);
  float newDistance = getDistance(TRIG_PIN, ECHO_PIN);
  // 获取的距离发生变化进行打印 Print only when the distance changes
  if (newDistance != Distance) {
    Serial.print("Distance: ");
    Serial.print(newDistance, 1);
    Serial.println(" cm");
    Distance = newDistance;
  }
}

Experimental results

After compiling the program successfully, upload the code to the Arduino Uno development board.

After the program is started, the serial port will print the real-time distance in front of the ultrasonic wave (data will be printed only when the distance in front changes)!

If there is no display content, you can check whether the serial port baud rate is consistent with the code setting, and then press the RESET button on the development board.

xxxxxxxxxx
The burning program cannot use other programs to occupy the serial port or an external serial port communication module (for example: WiFi camera module), otherwise the program cannot be burned or an error message will be prompted!

image-20240612143941583

Motor Control
Track Print

COMPANY

  • About us
  • Team Style
  • Blog

LEARNING & EXPLORATION

  • Tutorials
  • Edge AI hardware
  • Educational robots
  • Embedded intelligent hardware

WHERE TO BUY

  • Yahboom Online Store
  • Amazon
  • Alibaba
  • AliExpress
  • Mercado

TECHNOLOGY SUPPORT

  • Email: support@yahboom.com

BE A DISTRIBUTOR

  • Email: sales@yahboom.com
QR Code

Copyright © 2017-2026 Yahboom. All Rights Reserved.

EN/中文