Lesson 3, Topic 1
In Progress

Building the “Invisible Tripwire

Lesson Progress
0% Complete

Hardware: ESP32, Ultrasonic Sensor, Buzzer, Breadboard, and Jumper Wires.

Presentation:

SC_AI01_04_Build your Security System by Infinite Engineers

Step 1: From Paper to Prototype

  • The Goal: Today we take your drawing from Session 3 and turn it into a working machine.
  • The Tools: * Hardware: Your ESP32 “Brain” and Ultrasonic “Senses”.
    • Software: Arduino IDE (The language we use to talk to the ESP32).
  • The Mission: Program the ESP32 to sound the buzzer only when someone is closer than 50cm.

Step 2: The Circuit Map (The Wiring)

To make the “Pipeline” work, we must connect the pins correctly:

  • Ultrasonic Sensor (Input):
    • VCC to ESP32 5V/VIN.
    • GND to ESP32 GND.
    • Trig/Echo to Digital Pins (e.g., GPIO 26 and 25).
  • Buzzer (Output):
    • Positive (+) to a Digital Pin (e.g., GPIO 14).
    • Negative (-) to GND.

Step 3: Writing the “AI Logic” in Arduino IDE

We use code to tell the ESP32 how to “think”. This is the Processing step of your pipeline.

  • Reading the Sensor: The code tells the sensor to send a “Ping” and wait for the “Echo”.
  • Calculating Distance: The ESP32 measures the time and converts it to centimeters.
  • The Decision (The ‘If’ Statement): > if (distance < 50) { digitalWrite(Buzzer, HIGH); }

Step 4: Step-by-Step Instructions for Students

  1. Plug in the ESP32: Connect it to your laptop using the USB Data Cable.
  2. Open Arduino IDE: Select the “DOIT ESP32 DEVKIT V1” board.
  3. Build the Circuit: Follow the diagram carefully—don’t let the wires touch!
  4. Upload the Code: Click the “Upload” arrow and wait for the “Done Uploading” message.
  5. Test your AI: Wave your hand in front of the sensor. Does the buzzer sound?

Code:

// Define the Pins
const int trigPin = 26;
const int echoPin = 25;
const int buzzerPin = 14;

void setup() {
  pinMode(trigPin, OUTPUT); 
  pinMode(echoPin, INPUT);
  pinMode(buzzerPin, OUTPUT);
  Serial.begin(115200);
}

void loop() {
  // 1. INPUT: Trigger the sensor to send a sound wave
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  // 2. PROCESS: Measure the time and calculate distance
  long duration = pulseIn(echoPin, HIGH);
  int distance = duration * 0.034 / 2;

  // 3. OUTPUT: Logic-based decision
  if (distance > 0 && distance < 50) { 
    digitalWrite(buzzerPin, HIGH); // Alarm ON if closer than 50cm
  } else {
    digitalWrite(buzzerPin, LOW);  // Alarm OFF
  }
}