Ultrasonic Distance Sensor w/ LED System

Hi There,

I'm currently creating a project where I use 3 x Ultrasonic Distance Sensors in a line to detect when an object is placed on each individual one and then the results co-ordinate to a traffic light system.

RED- All sensors have an object on them
YELLOW- some have objects on them but not none and not all, e.g 010, 001, 101, 011, 100 =YELLOW
GREEN - None of the sensors have object on them.

Below is my basic logic circuit that works with the dip switches in place of the sensors.

Once i had the above working i switched to attempting to attach and the code the ultrasonic sensors.

Below is the circuit:

I changed none of the logic circuit and only attached the sensors to their pins. Unfortunately, I cannot get the code to work. I cannot see where my error would lie as i've researched the different codes and then applied them to this project but it seems that the sensors just aren't giving an output. Currently, when the simulation starts, the Green LED is on for a few seconds, the Yellow blips on, and the Red LED stays on. I have a feeling this may be something to do with the sequencing of how its reading and completing tasks but I don't know how to solve the issue. Any help would be much appreciated.


const int trigPinA = A0;
const int trigPinB = A1;
const int trigPinC = A2;
const int AechoPin = A3;
const int BechoPin = A4;
const int CechoPin = A5;
const int logicPinA = 2;
const int logicPinB = 3;
const int logicPinC = 4;


float timingA = 0.0;
float timingB = 0.0;
float timingC = 0.0;
float distanceA = 0.0;
float distanceB = 0.0;
float distanceC = 0.0;


void setup() {
  pinMode(trigPinA, OUTPUT);
  pinMode(trigPinB, OUTPUT);
  pinMode(trigPinC, OUTPUT);
  pinMode(AechoPin, INPUT);
  pinMode(BechoPin, INPUT);
  pinMode(CechoPin, INPUT);
  pinMode(logicPinA, OUTPUT);
  pinMode(logicPinB, OUTPUT);
  pinMode(logicPinC, OUTPUT);
  
  digitalWrite(trigPinA, LOW);
  digitalWrite(trigPinB, LOW);
  digitalWrite(trigPinC, LOW);
  digitalWrite(logicPinA, LOW);
  digitalWrite(logicPinB, LOW);
  digitalWrite(logicPinC, LOW);
  

  Serial.begin(9600);
  
}

void loop() {
  digitalWrite(trigPinA, LOW);
  delay (2);
  digitalWrite(trigPinA, HIGH);
  delay(10);
  digitalWrite(trigPinA, LOW);
  
  digitalWrite(trigPinB, LOW);
  delay (2);
  digitalWrite(trigPinB, HIGH);
  delay(10);
  digitalWrite(trigPinB, LOW);
  
  digitalWrite(trigPinC, LOW);
  delay (2);
  digitalWrite(trigPinC, HIGH);
  delay(10);
  digitalWrite(trigPinC, LOW);
  
  timingA = pulseIn(AechoPin, HIGH);
  timingB = pulseIn(BechoPin, HIGH);
  timingC = pulseIn(CechoPin, HIGH);
  
  distanceA = (timingA * 0.034 / 2);
  distanceB = (timingB * 0.034 / 2);
  distanceC = (timingC * 0.034 / 2);
  

  
  if (distanceA < 20 ) {
  digitalWrite (logicPinA, HIGH);
  }
  else if (distanceA >= 20) {
    digitalWrite (logicPinA, LOW);
  }
  
    if (distanceB < 20 ) {
  digitalWrite (logicPinB, HIGH);
  } 
   else if (distanceB >= 20) {
    digitalWrite (logicPinB, LOW);
  }
    if (distanceC < 20 ) {
  digitalWrite (logicPinC, HIGH);
  } 
   else if (distanceC >= 20) {
    digitalWrite (logicPinC, LOW);
  }
}

Take a look at the theory of that device. Search for the term “lobe”.

In other words, does ONE ultrasonic sensor work properly if the other two are disconnected?

Look at this code, see any differences:

// Jim

// Speed of sound in air at 25C is 343m/s
// or 0.343mm/us or 0.0343 cm/us

// Change to whatever pins you are using
#define trigger_pin 2
#define echo_pin 3

unsigned long echo_time;

void setup() 
{
  pinMode (trigger_pin, OUTPUT);
  pinMode (echo_pin, INPUT);
  digitalWrite (trigger_pin, LOW);
  delay(2);
 
  Serial.begin(9600);
}

void loop() 
{
  digitalWrite (trigger_pin, HIGH);
  delayMicroseconds(10);
  digitalWrite (trigger_pin, LOW);
  echo_time = pulseIn (echo_pin, HIGH);
  Serial.println (echo_time);

  delay(200);
  
}
//

Your trigger pulse is way too long and you need to do the pulsIn right after you set the trigger pin LOW

You need to trigger and read the sensors one at a time, AND you need a short delay (50 ms) between each one so that echos from a preceeding sensor don't confuse the subsequent one.

Arrays will give you tighter control of all the sensors...

Are you using Tinkercad?

To put it simply, you're trying to "fire" all three sensors' pings at once and then think you're "reading" each sensor's echo in sequence, but this is wrong.
Let me try to explain in detail.

First, unless the sensors are all pointing in completely different directions, the "ping" from one sensor could be picked up by another sensor, giving you completely unreliable measurements.

Second, since you do this in the loop(), the operation is repeated hundreds of times per second, which could lead a sensor to receive an echo from a more distant obstacle, related to a ping sent in one of the previous loops. Obviously, it depends on the type of obstacles present, but you only want to detect the first one, so theoretically, after each ping, you should wait a certain timeout to ensure that sensor doesn't receive any further echoes.

So after each individual ping, you must wait for the corresponding "pulseIn" (i.e., the first "echo" received) before switching to the other sensor, and avoid doing this more than a couple of times per second (unless you intend to use that over a fast moving object, like planes or choppers...).
Or switch to other TOF distance sensor technology (e.g. laser).

If yes, did you click on the sensor?

But now your circuit makes no sense. You are using 3 Arduino pins to light 3 LEDs. You no longer need any of those and-gates, or-gates or inverters. The Arduino can perform that logic. Microcontrollers were invented to simplify complex logic circuits like this.