Arduino Sonar Object Counter

9,698

28

12

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

Be the First to Share

    Recommendations

    • Make it Glow Contest

      Make it Glow Contest
    • First Time Author Contest

      First Time Author Contest
    • PCB Challenge

      PCB Challenge

    12 Discussions

    0
    hannah66666
    hannah66666

    Question 2 years ago on Step 2

    how to you connect the wires to the lcd and is there a type of lcd that you need? i am i student and need you to answer asap thanks

    0
    UmairS20
    UmairS20

    Question 2 years ago on Step 3

    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.

    0
    anks0985
    anks0985

    3 years ago

    Hi,

    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.

    0
    djohansen3
    djohansen3

    Reply 3 years ago

    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

    0
    AayushS32
    AayushS32

    3 years ago

    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

    0
    AayushS32
    AayushS32

    3 years ago

    my counter is not working give me a solution

    0
    djohansen3
    djohansen3

    Reply 3 years ago

    Can you give me a little more info?

    0
    AayushS32
    AayushS32

    Reply 3 years ago

    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

    0
    MohdS119
    MohdS119

    3 years ago

    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.

    0
    djohansen3
    djohansen3

    Reply 3 years ago

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

    0
    MohdS119
    MohdS119

    Reply 3 years ago

    Count every 2 object.

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