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

Comments

author
MohdS119 (author)2017-07-16

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.

author
djohansen3 (author)MohdS1192017-07-17

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

author
MohdS119 (author)djohansen32017-07-31

Count every 2 object.

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