/********* Pleasedontcode.com **********
Pleasedontcode thanks you for automatic code generation! Enjoy your code!
- Terms and Conditions:
You have a non-exclusive, revocable, worldwide, royalty-free license
for personal and commercial use. Attribution is optional; modifications
are allowed, but you're responsible for code maintenance. We're not
liable for any loss or damage. For full terms,
please visit pleasedontcode.com/termsandconditions.
- Project: Ultrasonic Detection
- Source Code NOT compiled for: Arduino Nano 33 BLE
- Source Code created on: 2025-11-05 11:48:22
********* Pleasedontcode.com **********/
/****** SYSTEM REQUIREMENTS *****/
/****** SYSTEM REQUIREMENT 1 *****/
/* Use the Ultrasonic library with HC-SR04 sensor */
/* connected to digital pins D2 (Trigger) and D3 */
/* (Echo) on Arduino Nano 33 BLE for obstacle */
/* detection, allowing system actions based on */
/* measured distance values. */
/****** END SYSTEM REQUIREMENTS *****/
/* START CODE */
// Project code for obstacle detection using HC-SR04 ultrasonic sensor
// connected to digital pins D2 (Trigger) and D3 (Echo) on Arduino Nano 33 BLE
// System Requirements:
// Use the Ultrasonic library with HC-SR04 sensor at pins D2 and D3
// Detect obstacles and perform system actions based on distance measurements
#include
// Define the ultrasonic sensor pins
const uint8_t ultrasonic_sensor_HC_SR04_Echo_PIN_D3 = 3;
const uint8_t ultrasonic_sensor_HC_SR04_Trigger_PIN_D2 = 2;
// Create an instance of Ultrasonic class with echo and trigger pins
Ultrasonic ultrasonicSensor(ultrasonic_sensor_HC_SR04_Trigger_PIN_D2, ultrasonic_sensor_HC_SR04_Echo_PIN_D3);
// Threshold distance in centimeters to consider an obstacle
const float obstacleThresholdCm = 20.0; // 20 cm
void setup() {
// Initialize serial communication at 9600 baud
Serial.begin(9600);
// Configure the pin modes
pinMode(ultrasonic_sensor_HC_SR04_Echo_PIN_D3, INPUT);
pinMode(ultrasonic_sensor_HC_SR04_Trigger_PIN_D2, OUTPUT);
}
void loop() {
// Read the distance from the ultrasonic sensor
float distanceCm = ultrasonicSensor.read();
// Output the distance value
Serial.print("Distance: ");
Serial.print(distanceCm);
Serial.println(" cm");
// Check if an obstacle is detected within the threshold distance
if (distanceCm > 0 && distanceCm < obstacleThresholdCm) {
// Obstacle detected - perform system action
Serial.println("Obstacle detected! Taking action.");
// Example action: turn on an LED or trigger other system responses
// digitalWrite(LED_BUILTIN, HIGH); // Uncomment if using an onboard LED
} else {
// No obstacle - system is clear
// digitalWrite(LED_BUILTIN, LOW); // Uncomment if using an onboard LED
}
// Wait for 200 milliseconds before next reading
delay(200);
}
/* END CODE */