- Using IO trigger for at least 10us high level signal
- The Module automatically sends eight 40 kHz pulses and detects if there is a pulse echoed back. The timing between the transmit pulse and the receive pulse is used to calculate distance.
- Static current: less than 2mA
- The module performance is stable, measure distance accurately.
- Arduino compatible
There are 4 pins on the module: VCC , Trig, Echo, and GND . To use the sensor: pull the Trig pin to high level for more than 10us to trigger an impulse. The module will begin sending ultrasonic pulses. If an object is detected in front of the sensor the Echo pin will rise to a logic high level. The distance between the sensor and the object can then be calculated using the equation below.
Distance = ((Duration)*(Sonic :340m/s))/2
/* HC-SR04 Sensor
/*
Module: RCWL-9300A 16MM Split Type Open Type Ultrasonic Ranging Module
Version: V2.0
Date: 20220710
Master chip: RCWL-9300A
Function: In GPIO mode, RCWL-9610 ultrasonic ranging module measures distance and displays it on the serial port
Notice: Module default GPIO mode
Write: Wuxi Richen IoT Technology Co., Ltd.
Test board: RCWL-3310
Program customization: 13915288564
Connection :
-VCC = 3.3V/5.5V
-Trig_RX_SCL = A5
-Echo_TX_SDA = A4
-GND = GND
*/
float distance;
const int echo=A4; // echo to A4 pin
const int trig=A5; // trig connected to pin A5
void setup()
{
Serial.begin(9600); //Baud rate 9600
pinMode(echo,INPUT); //Set echo as input pin
pinMode(trig,OUTPUT); //Set trig as output pin
Serial.println("RCWL-9610-GPIO Ranging start:");
}
void loop()
{
digitalWrite(trig,HIGH);
delayMicroseconds(500);
digitalWrite(trig,LOW); // Trig pin outputs 10US high level pulse trigger signal
distance = pulseIn(echo,HIGH); // Count the received high time
distance = distance*340/2/10000; // Calculation distance
// 1: speed of sound: 340M/S
// 2: actual distance is 1/2 speed of sound distance
// 3: counting clock is 1uS
// temperature compensation formula:
// c=(331.45+0.61t/℃)ms-1 (where 331.45 is at 0 Spend)
Serial.print("distance: ");
Serial.print(distance); // Serial port output distance signal
Serial.print("CM");
Serial.println(""); // new line
delay(20); // After a single measurement is completed,
// add a 30mS delay before the next measurement.
// To prevent the aftermath of the last measurement
// When measuring at close range, resulting in inaccurate measurement.
delay(100); // Delay 200mS to measure again, delay is not necessary
}
*
*
*
*
//----------- I2C (SDA, SCL) R4 = 10K --------------
/*
module: RCWL-9300A 16MM Split Type Open Type Ultrasonic Ranging Module
Version: V2.0
date: 20220710
Master chip: RCWL-9300A
Function: RCWL-9300A ultrasonic distance measuring module distance measurement and serial port display in IIC mode
Notice: Need to set the module in IIC mode
test board: RCWL-3310
write: Wuxi Richen IoT Technology Co., Ltd.
Program customization: 13915288564
I2C data format: The IIC of RCWL-9300A outputs three 8BIT data, and the distance MM
value = 24-bit data is converted into decimal/10000.
connection :
-VCC = 3.3V/5.5V
-Trig_RX_SCL = A5
-Echo_TX_SDA = A4
-GND = GND
*/
#include "SoftwareSerial.h"
#include "Wire.h"
float distance = 0; // Distance data decimal value
float ds[3]; // [3] 8BIT distance data
void setup()
{
Serial.begin(9600); //Define serial port baud rate 9600 Factory default baud rate 9600
Wire.begin();
Serial.println("RCWL-9300A ranging start:");
}
void loop()
{
char i = 0;
ds[0]=0;
ds[1]=0;
ds[2]=0; // Initialize three 8BIT distance data as 0
Wire.beginTransmission(0x57); // The address is 0X57, write 8-bit data as AE, and read 8-bit data as AF
Wire.write(1); // Write command 0X01, 0X01 is the start measurement command
Wire.endTransmission(); // I2C end command
delay(150); //Measurement cycle delay, one cycle is 120mS, set 150MS, leave a margin
Wire.requestFrom(0x57,3); //The address is 0X57 to read three 8-bit distance data
while (Wire.available())
{
ds[i++] = Wire.read();
}
distance=(ds[0]*65536+ds[1]*256+ds[2])/10000; //Calculated as CM value
Serial.print("distance:");
if ((1<=distance)&&(distance<=900)) // Numerical display between 1CM-9M
{
#if 0
Serial.println();
Serial.print(ds[0]);
Serial.println();
Serial.print(ds[1]);
Serial.println();
Serial.print(ds[2]);
Serial.println();
#endif //#if 1, output 3 distance data of I2C
Serial.print(distance);
Serial.print(" CM ");
}
else
{
Serial.println();
Serial.print(ds[0]);
Serial.println();
Serial.print(ds[1]);
Serial.println();
Serial.print(ds[2]);
Serial.println();
Serial.print(" - - - - "); //Invalid value Numerical display - - - -
}
Serial.println(); //new line
delay(20); // After a single measurement is completed,
// add a 30mS delay before the next measurement.
// To prevent the aftermath of the last measurement
// when measuring at close range, resulting in inaccurate measurement.
delay(100); // Delay 200mS to measure again, delay is not necessary
}
*
*
//----------- UART (TX RX) R5 = 10K ---------------
/*
module: RCWL-9300A 16MM Split Type Open Type Ultrasonic Ranging Module
Version: V2.0
date: 20220710
Master chip: RCWL-9300A
Function: RCWL-9300A ultrasonic distance measuring module distance measurement and serial port display in UART mode
Notice: Need to set the module in UART mode (R5 = 10K)
test board: RCWL-3310
write: Wuxi Richen IoT Technology Co., Ltd.
Program customization: 13915288564
I2C data format: The IIC of RCWL-9300A outputs three 8BIT data, and the distance MM
value = 24-bit data is converted into decimal/10000.
connection :
-VCC = 3.3V/5.5V
-Trig_RX_SCL_I/O = A5
-Echo_TX_SDA = A4
-GND = GND
*/
#include "SoftwareSerial.h"
SoftwareSerial mySerial(A4, A5); // A4 is RX, A5 is TX; A4 is connected to module TX, A5 is connected to module RX
float Data_h = 0; // 16-23bit
float Data_m = 0; // 8-15bit
float Data_l = 0; // 0-7 bits
float distance = 0; // Distance data decimal value
void setup()
{
Serial.begin(9600); //Define serial port baud rate 9600 Factory default baud rate 9600
mySerial.begin(9600); //Define the analog serial port baud rate
Serial.println("RCWL-9300A-UART Ranging start:");
}
void loop()
{
Data_h = 0;
Data_m = 0;
Data_l = 0;
distance=0; //Initialize three 8BIT distance data and the distance value is 0
mySerial.flush(); //Clear analog serial port cache data
mySerial.write(0XA0); //Send the start ranging command 0XA0, 0XA0 is the start test command data
delay(150); //Measurement cycle delay, one cycle is 120mS, set 150MS, leave a margin
if (mySerial.available()>0) //Wait for 3 data to be received
{
Data_h= mySerial.read(); //read cached data
Data_m= mySerial.read();
Data_l= mySerial.read();
}
else
{
Data_h= 0; //The cache data cannot be read, and the data is cleared to 0
Data_m= 0;
Data_l= 0;
}
distance=(Data_h*65536+Data_m*256+Data_l)/10000; //Calculated as CM value
Serial.print("Distance:"); //Remove the "Distance:" and "CM" to draw on the serial port
if ((1<=distance)&&(900>=distance)) //Numerical display between 1CM-9M
{
Serial.print(distance);
Serial.print("CM"); //The serial port outputs the distance data, remove the "distance:" and "CM" to draw the serial port
}
else
{
Serial.println();
Serial.print(Data_h);
Serial.println();
Serial.print(Data_m);
Serial.println();
Serial.print(Data_l);
Serial.println();
Serial.print(" - - - - "); //Invalid value Numerical display - - - -
}
Serial.println(); //new line
delay(20); // After a single measurement is completed,
// add a 30mS delay before the next measurement.
// To prevent the aftermath of the last measurement
// when measuring at close range, resulting in inaccurate measurement.
delay(100); // Delay 200mS to measure again, delay is not necessary
}
*
*
