Arduino Sonar Object Counter


Introduction: Arduino Sonar Object Counter

This tutorial will teach you how to use a sonar distance sensor as an object counter. It uses the HC-SR04

Step 1: Here Is What We Need for This Project.

The parts for this project is:

1. Arduino UNO or simmular board.
2. Sonar HC-SR04
3. Breakout board.
4. Hookup wire.
5 . LCD Display (I use a 20 * 4 IC2 display as it only needs 4 wires)

Step 2: Connecting It All Up.

Connecting the arduino, sonar and the display as show'n in the picture. Then connect your Arduino to your computer.

Step 3: The Code

Before you upload your code, you also need to download the LiquidCrystal_I2C.h library.

You can download it from this link

After you uploaded the code feel free to modify the code if you like.

Here are some of the things you can change:

  1. int setCounter = 20; // this is the setpoint for the sonar counter, when it reach this point it will trigger the led on pin 13
  2. if (distance <= 20 && lastsensorDistance >= 40){ // this is where you finetune your sonar. If distance is smaller than or equal to 20 and lastsensorDistance is bigger or equal to 40, it will count +1

If you open the serial monitor on your arduino ide and write R, it will reset the counter.

Thanks for watching



    • Water Contest

      Water Contest
    • Oil Contest

      Oil Contest
    • Clocks Contest

      Clocks Contest

    10 Discussions

    Hi there.
    I am unable to open the file you uploaded. I am sure its a great piece of work, Can you please reupload it.


    The project u share looks very nice, I want setup the same. Can I use 16X2 LCD instead of the one u used, can u please share the skech and connects for the same.

    1 reply

    Hi. Yes you can use any display you want as long as you change the code to match your type. The sketch can be downloaded from the project. Have a look at numbet 3. The Code

    hi the thing is that i want to combine your code with my code but the thing is it does not work. if i try your code seperately it works but if i try mine and yours together it does not. i am sending a code merged with your code plz check if it is correct

    so the thing is that i want it to count cans but when i put a can or any object in front of it it just does no count it idk why. i tried different distances but still does not work. i also thought it might be a problem with my sensor but the sensor is working when i tried another code for testing perposes

    how to make LCD display 1 after two objects detected by ultrasonic sensor?

    Example: 'object 1' detected by sensor. The LCD display "Count:0". Then sensor detected 'object 2'. The LCD display "Count:1" and next the sensor detect two another objects, LCD display "Count: 2", and next after detected two another objects, LCD display "Count: 3"... next two another objects.. Count:4.. 5.. and so on.

    What I actually want is that ultrasonic sensor count when detect first object, but on LCD will still display as "Count:0". Right after ultrasonic sensor detected second object, LCD display "Count:1".

    So, right now, what should I add or modify to code? My hardware and code is same as you told in tutorial above. Your help is greatly appreciated.

    2 replies

    Hi, so you want your counter to only count every 2 object?
    Or just the first 2, then evry object after that?

    Count every 2 object.

    I'm sorry but I already got the output I want (code). Anyway, thanks for trying to help.