Arduino 与HC-SRO4超声波传感器的OLED握手

本文涉及的产品
云解析 DNS,旗舰版 1个月
全局流量管理 GTM,标准版 1个月
公共DNS(含HTTPDNS解析),每月1000万次HTTP解析
简介: Arduino 与HC-SRO4超声波传感器的OLED握手

1.0 简介

  • 超声波传感器:超声波传感器是将超声波信号转换成其他能量信号(通常是电信号)的传感器。超声波是振动频率高于20kHz的机械波。它具有频率高、波长短、绕射现象小,特别是方向性好、能够成为射线而定向传播等特点。
  • 有机发光二极管(OrganicLight-Emitting Diode,OLED),又称为有机电激光显示、有机发光半导体(OrganicElectroluminesence Display,OLED),是指有机半导体材料和发光材料在电场驱动下,通过载流子注入和复合导致发光的现象。本篇通过HC-SRO4超声波传感器采样得到距离,再通过串口和0.98寸OLED将距离打印显示。有超程LED(可换成蜂鸣器)报警提示

image.gif

超声波传感技术的学习

1.超声波传感技术应用在生产实践的不同方面,而医学应用是其最主要的应用之一,下面以医学为例子说明超声波传感技术的应用。

2.超声波在医学上的应用主要是诊断疾病,它已经成为了临床医学中不可缺少的诊断方法。超声波诊断的优点是:对受检者无痛苦、无损害、方法简便、显像清晰、诊断的准确率高等。因而推广容易,受到医务工作者和患者的欢迎。超声波诊断可以基于不同的医学原理,我们来看看其中有代表性的一种所谓的A型方法。这个方法是利用超声波的反射。当超声波在人体组织中传播遇到两层声阻抗不同的介质界面时,在该界面就产生反射回声。每遇到一个反射面时,回声在示波器的屏幕上显示出来,而两个界面的阻抗差值也决定了回声的振幅的高低。


  • 工作原理:

传感器采用HC-SRO4超声波传感器传感器,可以距离的信息(也可以判断前方是否有物体检测);输出元件有OLED和报警LED。首先传感器将获取前方到传感器的距离,并将其传输至Arduino,Arduino获得数据后进行数据解析,将解析出的距离信息进行OLED显示器的显示,接着展示OLED字幕“Happy new year”,最后展示一个爱心💕,希望大家有所收获😊😊😊,此次已优化动作。


  • HC-SRO4超声波传感器相关参数如下:

2.jpg

2.0 实验材料

Arduino Uno R3开发板

HC-SRO4超声波传感器

0.98寸OLED

六根公母线

USB数据线

一台安装Arduino开发环境的电脑

3.0 实验步骤

3.1 根据原理图搭建电路

HC-SRO4超声波传感器为5V供电,其中Echo接3号,Trig接2号;OLED显示器的通讯方式为IIC通讯,因此除了5V供电的两根线外,只需要一根信号线SDA(uno 的A4也可以)和一根时钟线SCL(uno 的A5也可以)就可以,报警LED接4号。

LE D —————— 4

Echo —————— 3

T r ig —————— 2

SD A —————— SDA(uno 的A4也可以)

SC L —————— SCL(uno 的A5也可以)

实验原理图:

3.jpg

实验接线图:

4.jpg

3.2 新建sketch,拷贝如下代码并进行保存编译上传

代码:

MIxly模块

5.jpg

Arduino IDE代码所需U8g2lib库DHT库

#include <U8g2lib.h>
#include <Wire.h>
U8G2_SSD1306_128X64_NONAME_1_HW_I2C u8g2(U8G2_R0, U8X8_PIN_NONE);
volatile float item;
const static unsigned char FACE_Heart_large[] PROGMEM ={0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x7E,0x00,0x00,0x00,0xFC,0x01,0x00,0x00,0x00,0x00,0x00,0xE0,0xFF,0x03,0x00,0x00,0xFF,0x0F,0x00,0x00,0x00,0x00,0x00,0xF8,0xBF,0x07,0x00,0xC0,0xFF,0x3F,0x00,0x00,0x00,0x00,0x00,0xFC,0x3F,0x1C,0x00,0xF0,0xFF,0xE1,0x00,0x00,0x00,0x00,0x00,0xFE,0xFF,0x71,0x00,0xF8,0xFF,0xC3,0x01,0x00,0x00,0x00,0x00,0xFF,0xFF,0xE7,0x00,0xFC,0xFF,0x8F,0x03,0x00,0x00,0x00,0x80,0xFF,0xFF,0xCF,0x01,0xFE,0xFF,0x3F,0x07,0x00,0x00,0x00,0xC0,0xFF,0xFF,0x9F,0x03,0xFF,0xFF,0x7F,0x06,0x00,0x00,0x00,0xE0,0xFF,0xFF,0x3F,0x83,0xFF,0xFF,0xFF,0x0C,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,0x0F,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xC0,0xFF,0xFF,0xFF,0x07,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0xFF,0xFF,0xFF,0x03,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xFF,0xFF,0xFF,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xFE,0xFF,0xFF,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xF8,0xFF,0x7F,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xF0,0xFF,0x3F,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xE0,0xFF,0x1F,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xC0,0xFF,0x0F,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0xFF,0x03,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xFF,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xFE,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x7C,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x38,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x10,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 };
void page1() {
  u8g2.setFont(u8g2_font_timR12_tf);
  u8g2.setFontPosTop();
  u8g2.setCursor(20,10);
  u8g2.print("Distance: ");
  u8g2.setFont(u8g2_font_timB24_tf);
  u8g2.setFontPosTop();
  u8g2.setCursor(20,30);
  u8g2.print(item);
}
float checkdistance_2_3() {
  digitalWrite(2, LOW);
  delayMicroseconds(2);
  digitalWrite(2, HIGH);
  delayMicroseconds(10);
  digitalWrite(2, LOW);
  float distance = pulseIn(3, HIGH) / 58.00;
  delay(10);
  return distance;
}
void page2() {
  u8g2.setFont(u8g2_font_timB18_tf);
  u8g2.setFontPosTop();
  u8g2.setCursor(40,10);
  u8g2.print("Happy");
  u8g2.setCursor(25,30);
  u8g2.print("new year");
  delay(50);
}
void page3() {
  u8g2.drawXBMP(20,0,89,64,FACE_Heart_large);
  u8g2.setFont(u8g2_font_open_iconic_all_1x_t);
  u8g2.drawGlyph(102,45+1*8,64);
  delay(50);
}
void setup(){
  u8g2.begin();
  Serial.begin(9600);
  item = 0;
  //OLED 两页切换
  //打印温湿度和爱心
  u8g2.enableUTF8Print();
  pinMode(2, OUTPUT);
  pinMode(3, INPUT);
  pinMode(4, OUTPUT);
}
void loop(){
  item = checkdistance_2_3();
  if (item > 300) {
    item = 300;
    digitalWrite(4,HIGH);
  } else {
    digitalWrite(4,LOW);
  }
  u8g2.clearDisplay();
  u8g2.firstPage();
  do
  {
    page1();
  }while(u8g2.nextPage());
  delay(1000);
   Serial.print("Distance: ");
  Serial.println(item);
  u8g2.firstPage();
  do
  {
    page2();
  }while(u8g2.nextPage());
  delay(1000);
  u8g2.firstPage();
  do
  {
    page3();
  }while(u8g2.nextPage());
  delay(1000);
}

3.3 实验现象

打开窗口监视,观察OLED显示屏和串口的数据是否一致 ,然后改变HC-SRO4超声波前方遮挡物的距离 ,当距离大于300cm就显示300cm,报警LED亮起;否则报警LED熄灭,达到提醒的效果,快观察其两者动态变化 😃😃😃

视频演示

5.png

Arduino 与HC

6.jpg

4.0 总结

  • 首先传感器将获取距离,并将其传输至Arduino,Arduino获得数据后进行数据解析,当距离大于300cm就显示300cm,报警LED亮起;否则报警LED熄灭,达到提醒的效果。将解析出的距离进行OLED显示器的显示,接着展示OLED字幕“Happy new year”,最后展示一个爱心💕。难度综合性强,值得新手一试!!!😃😃😃
  • 在以后的博文中我们将学会用arduino常用传感器和执行器,从而实现对外部世界进行感知,充分认识这个有机与无机的环境,科学地合理地进行创作和发挥效益,然后为人类社会发展贡献一点微薄之力。
目录
相关文章
|
6月前
|
监控 数据处理
基于STC89C52RC单片机的MODBUS RTU协议从机的实现
基于STC89C52RC单片机的MODBUS RTU协议从机的实现
137 0
野火RA6M5开发板 IIC-OLED测试学习
野火RA6M5开发板 IIC-OLED测试学习
101 0
野火RA6M5开发板 IIC-OLED测试学习
51单片机学习-HC-05蓝牙模块-LCD12864显示
51单片机学习-HC-05蓝牙模块-LCD12864显示
158 0
51单片机学习-HC-05蓝牙模块-LCD12864显示
STM32+ESP8266+QT客户端上位机显示DHT11温湿度与点灯
STM32+ESP8266+QT客户端上位机显示DHT11温湿度与点灯
|
传感器
Arduino与LED深度握手
Arduino与LED深度握手
104 1
野火STM32F1开发板 IIC 0.96OLED 波形显示
野火STM32F1开发板 IIC 0.96OLED 波形显示
102 0
|
传感器
STM32通过IIC协议控制MPU6050
根据B站UP主“江科大自动化协”的教程总结
125 0
|
传感器 芯片 异构计算