#include <TM1637Display.h>
const int CLK = 13; // The ESP32 pin GPIO11 D2 connected to CLK
const int DIO = 12; // The ESP32 pin GPIO7 D1 connected to DIO
TM1637Display display(CLK, DIO); //set up the 4-Digit Display.
//define sound speed in cm/uS
#define SOUND_SPEED 0.034
const int trigPin = 5;
const int echoPin = 18;
long duration;
float distanceCm;
void setup() {
// put your setup code here, to run once:
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
Serial.begin(115200); // Starts the serial communication
Serial.println("Hello, ESP32!");
display.setBrightness(0x0a); //set the diplay to maximum brightness
// Set brightness (0-7):
}
void loop() {
// put your main code here, to run repeatedly:
// Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);
// Calculate the distance
distanceCm = round(duration * SOUND_SPEED/2);
// Prints the distance in the Serial Monitor
Serial.print("Distance (cm): ");
Serial.println(distanceCm);
display.showNumberDec(distanceCm); //Display the numCounter value
delay(1000);
}