esp32 experiments

Поделиться
HTML-код
  • Опубликовано: 19 авг 2024
  • ultrasonic sensor interfacing with esp32 and display values on lcd

Комментарии • 3

  • @TOMTOM-nh3nl
    @TOMTOM-nh3nl Месяц назад +1

    Thank You

  • @RoboticAIprojects
    @RoboticAIprojects  Месяц назад

    code:
    #include
    LiquidCrystal_I2C lcd(0x27,16,2); // set the LCD address to 0x3F for a 16 chars and 2 line display
    #define TRIG_PIN 5 // ESP32 pin GPIO23 connected to Ultrasonic Sensor's TRIG pin
    #define ECHO_PIN 18 // ESP32 pin GPIO22 connected to Ultrasonic Sensor's ECHO pin
    float duration_us, distance_cm;
    void setup() {
    // begin serial port
    Serial.begin (9600);
    // configure the trigger pin to output mode
    pinMode(TRIG_PIN, OUTPUT);
    // configure the echo pin to input mode
    pinMode(ECHO_PIN, INPUT);
    lcd.begin();
    lcd.clear();
    lcd.backlight(); // Make sure backlight is on

    // Print a message on both lines of the LCD.
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print("Ultrasonic-Sensor");

    lcd.setCursor(0,1);
    lcd.print("Based distance");
    delay(1500);
    }
    void loop() {
    // generate 10-microsecond pulse to TRIG pin
    digitalWrite(TRIG_PIN, HIGH);
    delayMicroseconds(10);
    digitalWrite(TRIG_PIN, LOW);
    // measure duration of pulse from ECHO pin
    duration_us = pulseIn(ECHO_PIN, HIGH);
    // calculate the distance
    distance_cm = 0.017 * duration_us;
    // print the value to Serial Monitor
    Serial.print("distance: ");
    Serial.print(distance_cm);
    Serial.println(" cm");
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print("Distance:");
    lcd.setCursor(0,1);
    lcd.print(distance_cm);
    lcd.setCursor(4,1);
    lcd.print("Cms");

    delay(500);
    }