步骤1:
该项目的草图在这里给出。
I2C总线的连接性
VCC-面包板上的5v电源
TRIG-Arduino的引脚12
ECHO-Arduino的引脚11
GND-面包板上的GND
蜂鸣器的连通性
到Arduino的引脚6和7
I2C的连接
GND-面包板上的GND
VCC-到5v电源在面包板上
SDA-连接到Arduino中的A4引脚
SCL-连接到Arduino中的A5引脚
LCD 16 x 2
如连接到I2C总线的草图1:1所示
下一步是找出它是I2C模块的十六进制地址,以便我们与之通信。
步骤2:
可以从以下位置找到获取十六进制地址的代码:此处给出的链接
https://brainy-bits.com/tutorials/connect-a-charac 。..
之后,我们需要在主代码中使用该代码她的e
#include
#include
#include
#include
#define I2C_ADDR 0x27 // Add your address here.
#define Rw_pin 1
#define En_pin 2
#define BACKLIGHT_PIN 3
#define D4_pin 4
#define D5_pin 5
#define D6_pin 6
#define D7_pin 7
#define ECHO_PIN 11 // Arduino pin tied to echo pin on the ultrasonic sensor.
#define TRIGGER_PIN 12 // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define MAX_DISTANCE 500 // Maximum distance we want to ping for (in centimeters)。 Maximum sensor distance is rated at 400500cm. NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.
LiquidCrystal_I2C lcd(I2C_ADDR,En_pin,Rw_pin,Rs_pin,D4_pin,D5_pin,D6_pin,D7_pin);
void setup()
{
pinMode(7,OUTPUT);
pinMode(6,OUTPUT);
lcd.begin (16,2); // 《《-- our LCD is a 20x4, change for your LCD if needed
// LCD Backlight ON
lcd.setBacklightPin(BACKLIGHT_PIN,POSITIVE);
lcd.setBacklight(HIGH);
lcd.home (); // go home on LCD
lcd.print(“Range Finder”);
}
void loop()
{
digitalWrite(TRIGGER_PIN, HIGH);
delayMicroseconds(1000);
digitalWrite(TRIGGER_PIN,LOW);
unsigned int uS = sonar.ping(); // Send ping, get ping time in microseconds (uS)。
unsigned int dist = sonar.convert_cm(uS); // Convert into centimeters if(dist》=200 || dist《=0){
lcd.clear();
lcd.setCursor (0,1); // go to start of 2nd line
lcd.print(“Distance:”);
lcd.setCursor (0,3); // go to start of 4th line
lcd.print(“Ping: ”);
lcd.print(“Out of Range”);
}else if(dist 》= 0 && dist 《=50){
lcd.clear();
beepFast();
lcd.setCursor (0,1); // go to start of 2nd line
lcd.print(“Distance:”);
lcd.setCursor (0,3); // go to start of 4th line
lcd.print(“Ping: ”);
lcd.print(dist);
lcd.print(“ cm ”);
}
else{
lcd.clear();
Silent();
lcd.print(dist);
lcd.print(“ cm ”);
}
delay(1000);
}
void beepFast(){
tone(8,440,200);
digitalWrite(7,HIGH);
delay(50);
noTone(8);
digitalWrite(7,LOW);
delay(300);
} void Silent(){
noTone(8);
digitalWrite(6,HIGH);
delay(300);
digitalWrite(6,LOW);
}
责任编辑:wv
-
Arduino
+关注
关注
187文章
6461浏览量
186569 -
警报
+关注
关注
0文章
89浏览量
9918
发布评论请先 登录
相关推荐
评论