Created
March 25, 2019 18:41
-
-
Save tenergyinnovation/1cdbe3606ec81743d69e4995e173ea86 to your computer and use it in GitHub Desktop.
Arduino_ESP32_ULTRASONIC_HC-SR04
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
/*********************************************************************** | |
* Project : Arduino_ESP32_ULTRASONIC_HC-SR04 | |
* Description : Ultrasonic sensor test | |
* Author : Tenergy Innovation Co., Ltd. | |
* Date : 14 Nov 2018 | |
* Revision : 1.0 | |
* Rev1.0 : Original | |
* Facebook : https://www.facebook.com/tenergy.innovation | |
* Email : tenergy.innovation@gmail.com | |
* TEL : +6689-140-7205 | |
***********************************************************************/ | |
/**************************************/ | |
/* GPIO define */ | |
/**************************************/ | |
#define ULTRA_TRIG_PIN 4 | |
#define ULTRA_ECHO_PIN 2 | |
/*********************************************************************** | |
* FUNCTION: setup | |
* DESCRIPTION: setup process | |
* PARAMETERS: nothing | |
* RETURNED: nothing | |
***********************************************************************/ | |
void setup() { | |
// put your setup code here, to run once: | |
pinMode(ULTRA_TRIG_PIN, OUTPUT); | |
pinMode(ULTRA_ECHO_PIN, INPUT); | |
Serial.begin(9600); | |
Serial.println("************************************************"); | |
Serial.println("Project : Arduino_ESP32_ULTRASONIC_HC-SR04"); | |
Serial.println("Author : Tenergy Innovation Co., Ltd."); | |
Serial.println("Date : 14 Nov 2018"); | |
Serial.println("Facebook : https://www.facebook.com/tenergy.innovation"); | |
Serial.println("Email : tenergy.innovation@gmail.com"); | |
Serial.println("TEL : +6689-140-7205"); | |
Serial.println("************************************************"); | |
} | |
/*********************************************************************** | |
* FUNCTION: loop | |
* DESCRIPTION: loop process | |
* PARAMETERS: nothing | |
* RETURNED: nothing | |
***********************************************************************/ | |
void loop() { | |
digitalWrite(ULTRA_TRIG_PIN, LOW); | |
delayMicroseconds(5); | |
digitalWrite(ULTRA_TRIG_PIN, HIGH); | |
delayMicroseconds(10); | |
digitalWrite(ULTRA_TRIG_PIN, LOW); | |
unsigned int PulseWidth = pulseIn(ULTRA_ECHO_PIN, HIGH); | |
unsigned int distance = PulseWidth * 0.0173681; | |
// Serial.print("Distance is "); | |
Serial.print(distance); | |
Serial.println(" cm."); | |
delay(100); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment