My ultra sonic range finder is not working

This is my code
//Define pins
//US1
const int trigPin =1;
const int echoPin = 3; // Echo pin of the sensor
const int redPin = 4; // Red LED pin
const int yellowPin = 6; // Yellow LED pin
const int greenPin = 7; // Green LED pin
//US2
const int trig2Pin =2;
const int echo2Pin = 5;
const int red2Pin = 8;
const int yellow2Pin = 9;
const int green2Pin = 10;
//constants
//US1
long duration;
int distance;
//US2
long duration2;
int distance2;

void setup() {
Serial.begin(9600);
// Initialize serial communication
//US1
pinMode(trigPin, OUTPUT); // Set trigger pin as an output
pinMode(echoPin, INPUT); // Set echo pin as an input
pinMode(redPin, OUTPUT); // Set Red LED pin as an output
pinMode(yellowPin, OUTPUT); // Set Yellow LED pin as an output
pinMode(greenPin, OUTPUT); // Set Green LED pin as an output
//US2
pinMode(green2Pin, OUTPUT); // Set Green2 LED pin as an output
pinMode(yellow2Pin, OUTPUT); // Set Yellow2 LED pin as an output
pinMode(red2Pin, OUTPUT); // Set Red2 LED pin as an output
pinMode(echo2Pin, INPUT); // Set echo2 pin as an input
pinMode(trig2Pin, OUTPUT); // Set trig2 pin as an output
}

void loop() {
//US1
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
delayMicroseconds(10);
//US2
digitalWrite(trig2Pin,LOW);
delayMicroseconds(10);
digitalWrite(trig2Pin,HIGH);
delayMicroseconds(10) ;
digitalWrite(trig2Pin,LOW);

//Constants def US1
duration = pulseIn(echoPin, HIGH);
distance = duration * 0.034 / 2;
//constants def US2
duration2 = pulseIn(echo2Pin, HIGH);
distance2 = duration2 * 0.034 / 2;

// Print values for debugging
//US1
Serial.print("Duration: ");
Serial.print(duration);
Serial.print(" microseconds - Distance: ");
Serial.print(distance);
Serial.println(" cm");
//US2
Serial.print("Duration2: ");
Serial.print(duration2);
Serial.print(" microseconds - Distance2: ");
Serial.print(distance2);
Serial.println(" cm");
//US1
if (distance < 5) {
Serial.println("Close proximity!"); // Debug message
digitalWrite(redPin, HIGH); // Turn on the Red LED
digitalWrite(yellowPin, LOW); // Turn off the Yellow LED
digitalWrite(greenPin, LOW); // Turn off the Green LED
} else if (distance >= 5 && distance < 10) {
Serial.println("Moderate distance."); // Debug message
digitalWrite(yellowPin, HIGH); // Turn off the Red LED
digitalWrite(redPin, LOW); // Turn off the Red LED
digitalWrite(greenPin, LOW); // Turn off the Green LED
} else if (distance >= 10) {
Serial.println("Far away!"); // Debug message
digitalWrite(greenPin, HIGH); // Turn on the Green LED
digitalWrite(redPin, LOW); // Turn off the Red LED
digitalWrite(yellowPin, LOW); // Turn off the Yellow LED
} else {
Serial.println("Undefined distance!"); // Debug message
digitalWrite(redPin, LOW); // Turn off all LEDs
digitalWrite(yellowPin, LOW);
digitalWrite(greenPin, LOW);
}

// Control LEDs based on distance thresholds for US2
if (distance2 < 5) {
Serial.println("Close proximity!"); // Debug message
digitalWrite(red2Pin, HIGH); // Turn on the Red LED
digitalWrite(yellow2Pin, LOW); // Turn off the Yellow LED
digitalWrite(green2Pin, LOW);
} else if (distance2 >= 5 && distance2 < 10) {
Serial.println("Moderate distance."); // Debug message
digitalWrite(green2Pin, LOW); // Turn off the Red LED
digitalWrite(red2Pin, LOW); // Turn off the Red LED
digitalWrite(yellow2Pin, HIGH);
} else if (distance2 >= 10) {
Serial.println("Far away!"); // Debug message
digitalWrite(green2Pin, HIGH); // Turn on the Green LED
digitalWrite(red2Pin, LOW); // Turn off the Red LED
digitalWrite(yellow2Pin, LOW);
} else {
Serial.println("Undefined distance!"); // Debug message
digitalWrite(red2Pin, LOW); // Turn off all LEDs
digitalWrite(yellow2Pin, LOW);
digitalWrite(green2Pin, LOW);
}

delay(1000); // Delay before next reading
}

and this is my circuit:

Move this to the //US1 section, i.e. up several lines:


//Constants def US1
duration = pulseIn(echoPin, HIGH);
distance = duration * 0.034 / 2;

1 Like

ok thanks ill try

BTW, do not use UNO pin #1 as it is the TX pin for Serial Communications.

1 Like

its working now but the LEDS arent lighting up for sensor 2

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.