Arduino projects

Arduino sound wave ranging

ultrasonic sensor

In this tutorial we will learn how the HC-SR04 ultrasonic sensor works and how to use it with Arduino. This is the most popular sensor for measuring distance and making obstacle avoiding robots with Arduino.

 

 

#include <Arduino.h>
#include <U8g2lib.h>
#include <Wire.h>
#include <Ultrasonic.h>

U8G2_SSD1306_128X64_NONAME_F_SW_I2C u8g2(U8G2_R0,A5,A4, U8X8_PIN_NONE); // SDA:21 scl:22
Ultrasonic ultrasonic(2, 3);

void setup(void) {
u8g2.begin();
// 初始化超声波传感器
u8g2.enableUTF8Print(); // enable UTF8 support for the Arduino print() function
}

void loop(void) {
u8g2.setFont(u8g2_font_wqy12_t_chinese3); // use wqy chinese2 for all the glyphs of “你好世界”
u8g2.setFontDirection(0);
u8g2.clearBuffer();
// 读取距离
float distance = ultrasonic.read();

// 打印距离(单位:厘米)
u8g2.setCursor(10, 40);
u8g2.print(“距离:”+String(distance)); // Chinese “Hello World”
u8g2.sendBuffer();

delay(1000);
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
twenty one
twenty two
twenty three
twenty four
25
26
27
28
29
30
31
32
33

————————————————

Check Also
Close
Back to top button