Last active
January 10, 2019 02:54
-
-
Save clive520/cb52b76f31ae9532827fdfed2f413962 to your computer and use it in GitHub Desktop.
超音波HCSR04_顯示距離於LCD_NodeMCU_ESP8266
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Sketch was generated by motoblockly | |
Website: http://www.motoblockly.com | |
Author: www.motoduino.com | |
Date: Wed Jan 09 2019 19:12:47 GMT+0800 | |
*/ | |
//HC-SR04接線方式{GND ==> GND VCC==>5V Ting==>D7(13) Echo==>D8(15)} | |
//LCD接線方式{GND ==> GND VCC==>5V SDA==>D2(04) SCL==>D1(05)} | |
#include <Wire.h> | |
#include <esp_LiquidCrystal_I2C.h> | |
esp_LiquidCrystal_I2C mylcd(0x27, 16, 2); | |
long distance; | |
float ultrasonic_distance_13_15() { | |
digitalWrite(13, LOW); | |
digitalWrite(15, LOW); | |
delayMicroseconds(5); | |
digitalWrite(13, HIGH); | |
delayMicroseconds(10); | |
digitalWrite(13, LOW); | |
unsigned long sonic_duration = pulseIn(15, HIGH); | |
float distance_cm = (sonic_duration / 2.0) / 29.1; | |
return distance_cm; | |
} | |
void setup() | |
{ | |
mylcd.init(); | |
mylcd.backlight(); | |
pinMode( 13 , OUTPUT); | |
pinMode( 15 , INPUT); | |
distance = 0; | |
} | |
void loop() | |
{ | |
distance = ultrasonic_distance_13_15( ); | |
mylcd.clear(); | |
mylcd.setCursor(0,0); | |
mylcd.print(String("Distance:") + String(distance) + String("cm")); | |
delay(5000); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment