ESP32

Objective:

Circuit

20240505_114147.jpg

Code

//broadcast ultrasonic sensor telemetry to webpage hosted by ESP32
//by Rico Kanthatham, Skylabworkshop 2024

//pin connections
#define trigPin 5
#define echoPin 18
#define blueLed 2
#define greenLed 22
#define redLed 23

void setup(){

  //init Serial Monitor
  Serial.begin(9600);

  //pin mode specifications
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(blueLed, OUTPUT);
  pinMode(greenLed, OUTPUT);
  pinMode(redLed, OUTPUT);

}

void loop() {

  ultrasonicSense(); //run ultrasonic sensor function

}

void ultrasonicSense(){

  //send out ultrasonic pulse
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  //receive ultrasonic pulse
  duration_us = pulseIn(echoPin, HIGH);
  distance_cm = 0.017 * duration_us;

  //print ultrasonic telemetry
  Serial.print("distance: ");
  Serial.print(distance_cm);
  Serial.println(" cm");
}