I want to use two ultrasonic sensors for counting incoming cars to a garage. What I want is: When both sensors detect the object then the counter should increase.

Can anyone help me and give me an example?

1 Answer 1


If you have two sensors, you use analogRead.

int sensor1 = A0;
int sensor2 = A1;
int distance = 50; // or whatever you want here
bool car_passing = false;

void setup(){
    int counter = 0;

void loop(){
    if(analogRead(sensor1) < distance && 
       analogRead(sensor2) < distance && 
        car_passing = true;

    if(analogRead(sensor1) > distance){
        car_passing = false;

To prevent the counter keep counting while the car is passing by, we need to make a boolean, so the counter does not wait for count again, before the car has passed.

  • In order not to count kids in a Kettcar, big dogs or birds, a smarter algorithm should be used to define, what a car is and what not.
    – ott--
    Commented Mar 26, 2016 at 20:36
  • @ott that is true. Personally I wouldnt go for two ultra sonic sensors. But yea, that is what he asks for.
    – el3ien
    Commented Mar 26, 2016 at 20:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.