步驟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模塊的十六進(jìn)制地址,以便我們與之通信。
步驟2:
可以從以下位置找到獲取十六進(jìn)制地址的代碼:此處給出的鏈接
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);
}
責(zé)任編輯:wv
-
Arduino
+關(guān)注
關(guān)注
187文章
6461瀏覽量
186534 -
警報
+關(guān)注
關(guān)注
0文章
89瀏覽量
9914
發(fā)布評論請先 登錄
相關(guān)推薦
評論