Introduction: Multiple Ultrasonic Sensor Using Single Arduino

About: Hye, I am Ajay. & it's all about Electronics and Programming.

Abstract :

In this project, I am controlling 5 LEDs and 5 ultrasonic sensors using a single Arduino board. each sensor is pointed to a particular led and all 5 LEDs maintain their state for a particular period of time.

The purpose of this project is to get distance data from multiple sensors simultaneously and using this data to control LEDs.

Components Required:

  1. Arduino board
  2. HC-SR04 Ultrasonic Sensor
  3. LEDs
  4. jumper Wire (M-M),(M-F)
  5. Resistor
  6. Breadboard

Step 1: Wiring and Schematic

I am using an ultrasonic sensor in single-wire mode.

using only one wire for trigger and echo

Ultrasonic Sensor 1,2,3,4,5

US1-----Echo------Trigger = Signal 1 (Short Both echo and trigger of 1 ultrasonic sensor)

US2-----Echo-------Trigger = Signal 2

US3-----Echo-------Trigger = Signal 3

US4-----Echo-------Trigger = Signal 4

US5-----Echo-------Trigger= Signal 5

Arduino----------HC-SR04

Pin 8------------US1 = Signal 1 = Echo1+Trigger1

Pin 9------------US2 = Signal 2 = Echo2+Trigger2

Pin 10----------US3 = Signal 3 = Echo3+Trigger3

Pin 11----------US4 = Signal 4 = Echo4+Trigger4

Pin 12----------US5 = Signal 5 = Echo5+Trigger5

5v---------------US 1,2,3,4,5 (5V)

GND-----------US 1,2,3,4,5 (GND)


Arduino------------LEDs

pin3------------------LED1

pin4------------------LED2

pin5------------------LED3

pin6------------------LED4

pin7------------------LED5

GND----------------LED GND

Attachments

Step 2: Code and Working

The main heart of this project is the new ping library.= Download Library from here

Code = Download code from here

This Code doesn't use any delays and is event-driven.

You can think of it as multitasking.

void loop() {
  for (uint8_t i = 0; i < SONAR_NUM; i++) { // Loop through all the sensors.
    if (millis() >= pingTimer[i]) {         // Is it this sensor's time to ping?
      pingTimer[i] += PING_INTERVAL * SONAR_NUM;  // Set next time this sensor will be pinged.
      if (i == 0 && currentSensor == SONAR_NUM - 1) 
      
      oneSensorCycle(); // Sensor ping cycle complete, do something with the results.
      led (); //Turn ON the LEDs according to State.

    
    if((millis() >= time_now + period) && (led1State == HIGH || led2State == HIGH || led3State == HIGH || led4State == HIGH || led5State == HIGH ) ){//15s delay using millis function and it only starts when any one LED is ON.
        time_now = millis();

        /*after 15s set, all LEDs state to LOW.*/
        led1State = LOW;
        led2State = LOW;
        led3State = LOW;
        led4State = LOW;
        led5State = LOW;
         
    }
    
      
      sonar[currentSensor].timer_stop();          // Make sure previous timer is canceled before starting a new ping (insurance).
      currentSensor = i;                          // Sensor being accessed.
      cm[currentSensor] = 0;                      // Make distance zero in case there's no ping echo for this sensor.
      sonar[currentSensor].ping_timer(echoCheck); // Do the ping (processing continues, interrupt will call echoCheck to look for echo).
    
    }
  }
  
}
for (uint8_t i = 0; i < SONAR_NUM; i++) { // Loop through all the sensors.

for() loop in void loop() polls each sensor array waiting for when it's that sensor's time to ping.

void echoCheck() 
{ // If ping received, set the sensor distance to array.
  if (sonar[currentSensor].check_timer())
    cm[currentSensor] = sonar[currentSensor].ping_result / US_ROUNDTRIP_CM;
}


echoCheck() is where the background ping is polled every 24uS to see if ping was received, it sets the ping distance in CM in the cm array.

 oneSensorCycle(); // Sensor ping cycle complete, do something with the results.

oneSensorCycle() uses the distance data collected from different sensors to turn ON-OFF the LEDs.

void oneSensorCycle() { // Sensor ping cycle complete, do something with the results.

/*Store sensor data in Array*/
  for (uint8_t i = 0; i < SONAR_NUM; i++) 
  {
    sensors[i] = cm[i];
   }

/*LED Logic Start Using if Statemet*/
if (sensors[0] > 0 && sensors[0] <= dis)
{
led1State = HIGH;

  }
if (sensors[1] > 0 && sensors[1] <= dis)
{

led2State = HIGH;
  }
if (sensors[2] > 0 && sensors[2] <= dis)
{
 led3State = HIGH;
  }
if (sensors[3] > 0 && sensors[3] <= dis)
{
 led4State = HIGH;  
 }
if (sensors[4] > 0 && sensors[4] <= dis)
{
 led5State = HIGH;  
 }
if ((sensors[0] > 0 && sensors[0] <= dis) && (sensors[1] > 0 && sensors[1] <= dis))
{
led1State = HIGH;
led2State = HIGH;
  }
if ((sensors[1] > 0 && sensors[1] <= dis) && (sensors[2] > 0 && sensors[2] <= dis))
{
 led2State = HIGH;
 led3State = HIGH;
  }
if ((sensors[2] > 0 && sensors[2] <= dis) && (sensors[3] > 0 && sensors[3] <= dis))
{
  led3State = HIGH;
  led4State = HIGH;
   }
if ((sensors[3] > 0 && sensors[3] <= dis) && (sensors[4] > 0 && sensors[4] <= dis))
{
  led4State = HIGH;
  led5State = HIGH;
  }
if ((sensors[4] > 0 && sensors[4] <= dis) && (sensors[0] > 0 && sensors[0] <= dis))
{
  led1State = HIGH;
  led5State = HIGH;
  }
}

The Block diagram of this LEDs logic is shown above.

if((millis() >= time_now + period) && (led1State == HIGH || led2State == HIGH || led3State == HIGH || led4State == HIGH || led5State == HIGH ) ){//15s delay using millis function and it only starts when any one LED is ON.
        time_now = millis();

        /*after 15s set, all LEDs state to LOW.*/
        led1State = LOW;
        led2State = LOW;
        led3State = LOW;
        led4State = LOW;
        led5State = LOW;
         
    }

Turn OFF the LEDs after 15 Seconds using millis() function.