0% found this document useful (0 votes)
32 views5 pages

Ultrasonic Train Alert System Lab

The document outlines a laboratory activity for a microprocessors course focusing on creating a Train Alert System using an ultrasonic sensor. Students will measure distance and activate a buzzer when an object is detected within 15 cm. The activity includes pin connections, coding examples, and a materials list for implementation.

Uploaded by

Raleigh Riego
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views5 pages

Ultrasonic Train Alert System Lab

The document outlines a laboratory activity for a microprocessors course focusing on creating a Train Alert System using an ultrasonic sensor. Students will measure distance and activate a buzzer when an object is detected within 15 cm. The activity includes pin connections, coding examples, and a materials list for implementation.

Uploaded by

Raleigh Riego
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

CPE309/309L: MICROPROCESSORS

LABORATORY ACTIVITY NO. 4

S.Y. 2025 – 2026, 2nd Semester

Student Name and Signatures: Score:


_______________________________/________
_______________________________/________
_______________________________/________
Course, Year, and Section: Group No.: Date:

Activity No. 4: Train Alert System (Ultrasonic-Based Warning)


Overview:
This activity simulates a railway crossing alert system. An ultrasonic sensor detects an
incoming "train" (any object in motion). When the object enters a defined proximity range,
the buzzer activates and stays on until the object moves away. Students will practice real-time
distance measurement and apply control logic using sensors.

Practice A: Measure Distance Using Ultrasonic Sensor


Objective: Learn how to measure and display the distance using the ultrasonic sensor.
Pin Connections:
 Ultrasonic Trig → GPIO5
 Ultrasonic Echo → GPIO18
 VCC → 5V
 GND → GND
Code:
#define trigPin 5
#define echoPin 18

void setup() {
[Link](115200);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}

void loop() {
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);

long duration = pulseIn(echoPin, HIGH);


float distance = duration * 0.034 / 2;

[Link]("Distance: ");
[Link](distance);
[Link](" cm");
delay(500);
}

Practice B: Turn On Buzzer If Object Is Near


Objective: Activate a buzzer if an object is detected closer than 15cm.
Pin Connections:
 Buzzer → GPIO27
Code:
#define trigPin 5
#define echoPin 18
#define buzzerPin 27
void setup() {
[Link](115200);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(buzzerPin, OUTPUT);
}

void loop() {
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);

long duration = pulseIn(echoPin, HIGH);


float distance = duration * 0.034 / 2;

if (distance <= 15) {


digitalWrite(buzzerPin, HIGH);
} else {
digitalWrite(buzzerPin, LOW);
}

delay(300);
}
Activity Proper: Complete Train Detection Alert System
Objective: Create a system where a buzzer activates continuously as long as the ultrasonic
sensor detects an object ("train") within a 15 cm range, and turns off when it moves away.
Pin Connections Recap:
 Trig → GPIO5
 Echo → GPIO18
 Buzzer → GPIO27
Code:
#define trigPin 5
#define echoPin 18
#define buzzerPin 27

void setup() {
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(buzzerPin, OUTPUT);
}

void loop() {
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);

long duration = pulseIn(echoPin, HIGH);


float distance = duration * 0.034 / 2;
if (distance <= 15) {
digitalWrite(buzzerPin, HIGH);
} else {
digitalWrite(buzzerPin, LOW);
}

delay(300);
}

Materials List:
 1x ESP32 Dev Board
 1x HC-SR04 Ultrasonic Sensor
 1x Buzzer
 Jumper wires
 Breadboard

Schematic:

You might also like