Pradana tasdyattaNovan Ferdiyansyah
Published © GPL3+

(HC-SR04) sensor with buzzer and LED integrated telegram

The HC-SR04 sensor uses esp32 to read the distance then the distance data will be sent or displayed on the user's telegram

BeginnerProtip383
(HC-SR04) sensor with buzzer and LED integrated telegram

Things used in this project

Hardware components

Ultrasonic Sensor - HC-SR04 (Generic)
Ultrasonic Sensor - HC-SR04 (Generic)
×1
ESP32S
Espressif ESP32S
×1
Buzzer
Buzzer
×1
LED (generic)
LED (generic)
×1
Jumper wires (generic)
Jumper wires (generic)
×1

Software apps and online services

telegram
Arduino IDE
Arduino IDE

Story

Read more

Schematics

screenshot_2023-12-07_163025_ziz6NLhyoZ.png

Code

Untitled file

Arduino
#include "CTBot.h"
#define TRIGGER_PIN 2
#define ECHO_PIN 4
#define LED_PIN 18
#define BUZZER_PIN 13

CTBot myBot;
long duration;
int distance;

String ssid = "hooh";
String pass = "0123456789";
String token = "6816683011:AAH6yDfdAE0wjGCtYkKy1YYnYPsnj_P5aF8";

void setup() {
  Serial.begin(9600);
  pinMode(TRIGGER_PIN, OUTPUT);
  pinMode(ECHO_PIN, INPUT);
  pinMode(LED_PIN, OUTPUT);
  pinMode(BUZZER_PIN, OUTPUT);

  if (myBot.wifiConnect(ssid, pass)) {
    Serial.println("\nOk wifi");
  } else {
    Serial.println("\nnot Ok wifi");
  }

  myBot.setTelegramToken(token);

  if (myBot.testConnection()) {
    Serial.println("\ntestConnection OK");
  } else {
    Serial.println("\ntestConnection NOK");
  }
}

void loop() {
  TBMessage msg;
digitalWrite(TRIGGER_PIN, LOW);
  delayMicroseconds(2);
  digitalWrite(TRIGGER_PIN, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIGGER_PIN, LOW);
  
  duration = pulseIn(ECHO_PIN, HIGH);
  distance = duration * 0.034 / 2;

  // Mencetak jarak ke Serial Monitor
  Serial.println("Jarak: " + String(distance) + " cm");

  // Jarak 50 cm: Menyala LED
  if (distance <= 50) {
    digitalWrite(LED_PIN, HIGH);
  } else {
    digitalWrite(LED_PIN, LOW);
  }

if (myBot.getNewMessage(msg)) {
    if (msg.text.equalsIgnoreCase("Connect")) {
      myBot.sendMessage(msg.sender.id, "Welcome " + msg.sender.username + ". Try READ TEMP or READ HUM.");
    } else {
      float distance = duration * 0.034 / 2;
      String reply = "JARAK: " + String(distance) + "Cm";
      myBot.sendMessage(msg.sender.id, reply);
    }
  }
  
  // Jarak 25 cm: Menyala Buzzer
  if (distance <= 25) {
    digitalWrite(BUZZER_PIN, HIGH);
  } else {
    digitalWrite(BUZZER_PIN, LOW);
  }

  delay(500); // Mengukur jarak setiap 500 milidetik
}

Credits

Pradana tasdyatta

Pradana tasdyatta

1 project • 0 followers
Novan Ferdiyansyah

Novan Ferdiyansyah

1 project • 0 followers

Comments