[Diao Ye learns programming] Arduino hands-on (58) --- HC-SR04 ultrasonic sensor module 4

insert image description here

The reference to 37 sensors and modules has been widely circulated on the Internet. In fact, there must be more than 37 sensor modules compatible with Arduino. In view of the fact that I have accumulated some sensor and actuator modules on hand, in accordance with the concept of true knowledge (must be hands-on), for the purpose of learning and communication, here I am going to try and do more experiments one by one. Whether it is successful or not, it will be recorded ——Small progress or unsolvable problems, I hope to be able to throw bricks and spark jade.

[Arduino] 168 kinds of sensor module series experiment (data code + simulation programming + graphic programming)
Experiment 58: wide voltage HC-SR04 ultrasonic module 3.3V-5V distance sensor board with UART IIC interface

insert image description here
Experimental environment of HC-SR04 ultrasonic ranging sensor module
1. Hardware list required for HC-SR04 ultrasonic ranging sensor module experiment

TM1637 four-digit digital tube X1
Arduino Uno development board X1
DuPont lines (9 pieces are prepared)
LED light-emitting diode (blue, green) X2
IIC/I2C 1602 LCD screen module X1
HC-SR04 ultrasonic distance sensor module X1
Proto Shield prototype Expansion board (with mini breadboard) X1

insert image description here

Experimental wiring diagram

insert image description here

insert image description here

Several experiments of HC-SR04 ultrasonic ranging sensor module
Procedure 1: HC-SR04 2022.5.14 ranging start
(1) Arduino reference open source code

/*
  【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
  程序一:HC-SR04 2022.5.14测距开始
  Arduino------HG-SR04
  5V------------VCC
  GND-----------GND
  D2------------Echo
  D3------------Trig
*/

float    distance;
const int  echo = 2;                        //echO接D2脚
const int  trig = 3;                        //trig接D3脚

void setup() {
    
    
  Serial.begin(9600);                       //波特率9600
  pinMode(echo, INPUT);                      //设置echo为输入脚
  pinMode(trig, OUTPUT);                     //设置trig为输出脚
  Serial.println("HC-SR04 2022.5.14测距开始:");
}

void loop() {
    
    
  digitalWrite(trig, LOW);
  delayMicroseconds(20);
  digitalWrite(trig, HIGH);
  delayMicroseconds(20);
  digitalWrite(trig, LOW);   //发一个20US的高脉冲去触发Trig

  distance  = pulseIn(echo, HIGH);    //计数接收高电平时间
//计算距离 1:声速:340M/S  2:实际距离1/2声速距离 3:计数时钟为1US
  distance  = distance * 340 / 2 / 10000;       
Serial.print("距离: ");
  Serial.print(distance);
  Serial.println("cm");
  delay(20);  //单次测离完成后加20mS的延时再进行下次测量。防止近距离测量时,测量到上次余波,导致测量不准确。
  delay(500);                             //500mS测量一次
}

(3) The return status of the experimental serial port

insert image description here
(3) Experimental scene diagram

insert image description here
Program 2: Threshold 50cm to control onboard LED
(1) Arduino reference open source code

/*
  【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
  程序二:阙值50厘米控制板载LED
  Arduino------HG-SR04
  5V------------VCC
  GND-----------GND
  D2------------Echo
  D3------------Trig
*/

int inputPin = 2; // 定义超声波信号接收接口
int outputPin = 3; // 定义超声波信号发出接口
int ledpin = 13;

void setup() {
    
    
  Serial.begin(9600);
  pinMode(ledpin, OUTPUT);
  pinMode(inputPin, INPUT);
  pinMode(outputPin, OUTPUT);
}

void loop() {
    
    
  digitalWrite(outputPin, LOW); // 使发出发出超声波信号接口低电平2μs
  delayMicroseconds(2);
  digitalWrite(outputPin, HIGH); // 使发出发出超声波信号接口高电平10μs,这里是至少10μs
  delayMicroseconds(10);
  digitalWrite(outputPin, LOW); // 保持发出超声波信号接口低电平
  int distance = pulseIn(inputPin, HIGH); // 读出脉冲时间
  distance = distance / 58; // 将脉冲时间转化为距离(单位:厘米)
  Serial.println(distance); //输出距离值
  delay(50);
  if (distance >= 50)
  {
    
     //如果距离大于50厘米小灯亮起
    digitalWrite(ledpin, HIGH);
    Serial.println("LED ON");
  }//如果距离小于50厘米小灯熄灭
  else
    digitalWrite(ledpin, LOW);
}

(2) The return status of the experimental serial port (the LED is on when it is greater than or equal to 50 cm)

insert image description here
(3) Open the Arduino IDE——Tools——Serial Port Plotter, and view the experimental waveform. Experimental
serial port plotter returns (the distance change waveform generated by moving a book)

insert image description here
(4) Experimental scene diagram

insert image description here
Program 3, the serial port outputs the raw data of the waiting time
(1) Arduino reference open source code

/*
  【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
  程序三:串口输出等待时间的原始数据
*/

#define Echo 2 // 定义超声波信号接收接口
#define Trig 3 // 定义超声波信号发出接口

float cm; //距离变量
float temp; //返回原始数值

void setup() {
    
    
  Serial.begin(115200);
  pinMode(Trig, OUTPUT);
  pinMode(Echo, INPUT);
}

void loop() {
    
    
  //给Trig发送一个低高低的短时间脉冲,触发测距
  digitalWrite(Trig, LOW); //给Trig发送一个低电平
  delayMicroseconds(2);    //等待 2微妙
  digitalWrite(Trig, HIGH); //给Trig发送一个高电平
  delayMicroseconds(10);    //等待 10微妙
  digitalWrite(Trig, LOW); //给Trig发送一个低电平

  temp = float(pulseIn(Echo, HIGH)); //存储回波等待时间,
  //pulseIn函数会等待引脚变为HIGH,开始计算时间,再等待变为LOW并停止计时
  //返回脉冲的长度

  //声速是:340m/1s 换算成 34000cm / 1000000μs => 34 / 1000
  //因为发送到接收,实际是相同距离走了2回,所以要除以2
  //距离(厘米)  =  (回波时间 * (34 / 1000)) / 2
  //简化后的计算公式为 (回波时间 * 17)/ 1000

  cm = (temp * 17 ) / 1000; //把回波时间换算成cm

  Serial.print("原始 ");
  Serial.println(temp);//串口输出等待时间的原始数据
  Serial.print("距离 ");
  Serial.println(cm);//串口输出距离换算成cm的结果

  delay(50);
}

(3) Open the Arduino IDE——Tools——Serial Port Plotter, and view the experimental waveform
and the return status of the experimental serial port plotter (here is the waveform of the original value returned by the distance measurement)

insert image description here
insert image description here

Guess you like

Origin blog.csdn.net/weixin_41659040/article/details/132333723