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