Project
Detecting Obstacles with HW-201 on ESP32
Overview
Learn how to use the HW-201 infrared obstacle sensor with an ESP32 to detect nearby objects. This project is useful for obstacle-avoiding robots, line-following robots, and proximity alarms.
Instructions
Step by step
1. Connect the HW-201 VCC pin to 3.3V.
2. Connect the HW-201 GND pin to GND.
3. Connect the HW-201 OUT pin to D15.
4. Run the code and open the Serial Monitor.
5. Click or toggle the obstacle sensor in the simulator.
6. When an obstacle is detected, the output should print "Obstacle detected!".
7. When the path is clear, the output should print "Path clear".
Code
Source code
const int obstaclePin = 15;
void setup() {
Serial.begin(115200);
pinMode(obstaclePin, INPUT);
Serial.println("HW-201 Obstacle Sensor Test");
}
void loop() {
int obstacleState = digitalRead(obstaclePin);
if (obstacleState == LOW) {
Serial.println("Obstacle detected!");
} else {
Serial.println("Path clear");
}
delay(500);
}
Related