Build it step by step
0 of 8 doneInstall the TM1637Display library
From the Library Manager.
Wire the sensor
OUT to D2, 5V and GND.
Wire the display and buzzer
Display on D5 and D6, buzzer on D8.
Wire the reset button
D3 and GND.
Upload the sketch
The display shows 0.
Pass a hand under the hoop
The count goes to 1 with a beep.
Turn the sensor's knob
Until it sees the ball only.
Press reset
The count returns to zero.
How do you build a hoop shot counter?
An infrared sensor under a toy basketball hoop spots each ball falling through the net, and a four-digit display counts the shots and plays a short beep. A button resets the count. Fun for a bedroom hoop, and a good sensor-and-display exercise. You need Arduino Nano, IR obstacle avoidance sensor module, TM1637 4-digit display, Passive buzzer, Tactile push button 6 mm, Breadboard 830 Tie Points and Jumper wires male to male (40 pcs, 20 cm). It takes 8 steps and about 70 minutes.
- Level
- Beginner
- Time
- About 70 min
- Parts
- 7
Parts list
- Development BoardsBuyArduino Nano × 1
- SensorsBuy
- DisplaysBuy
- Motors & ActuatorsBuyPassive buzzer × 1
- SwitchesBuy
- PrototypingBuy
- PrototypingBuy
Tick what you already own. It saves to My Garage, so “Build with what I have” knows.
Wiring
| From | To | Why |
|---|---|---|
| IR sensor OUT | Nano D2 | Goes low when a ball passes in front of it |
| IR sensor VCC and GND | Nano 5V and GND | Sensor power |
| TM1637 CLK and DIO | Nano D5 and D6 | Score display |
| TM1637 VCC and GND | Nano 5V and GND | Power |
| Passive buzzer + and - | Nano D8 and GND | Short beep |
| Reset button | Nano D3 and GND | Sets the count to zero |
Code
The sensor is mounted just under the hoop rim, looking across the opening, so a ball falling through the net breaks its beam for a moment. The sketch counts one shot each time the beam is broken, then waits until the beam has been clear for 80 milliseconds before it is ready again, and also ignores the sensor for 400 milliseconds after a count, so a ball bouncing in the net is not counted twice. Each count shows on the TM1637 display with a short beep, and the reset button sets it back to zero. Turn the sensor's small knob until it sees the ball but not the net, the wall behind or the room lights, and try shielding it from direct sunlight, which can fool this type of sensor. Install "TM1637Display" by Avishay Orpaz from the Library Manager. This is a toy hoop counter for fun. Use a light ball and a soft hoop that cannot hurt anyone, keep the wiring out of the way of the ball, and play where the ceiling and furniture are safe. Try a version that also counts misses by listening for the rebound.
Runs on a virtual Arduino Nano in your browser, with sliders for the sensors. Happy with it? Verify and upload to your real board from the same page (Chrome or Edge on a computer).
// Library: TM1637Display (by Avishay Orpaz).
#include <TM1637Display.h>
const uint8_t IR_PIN = 2;
const uint8_t BTN_RESET = 3;
const uint8_t CLK = 5;
const uint8_t DIO = 6;
const uint8_t BUZZ = 8;
const unsigned long LOCKOUT_MS = 400; // ignore the sensor for a moment after each shot
const unsigned long CLEAR_MS = 80; // the beam must be clear this long before the next count
TM1637Display display(CLK, DIO);
unsigned int shots = 0;
bool blocked = false;
unsigned long lastCount = 0;
unsigned long clearSince = 0;
bool armed = true; // ready for the next ball
void setup() {
pinMode(IR_PIN, INPUT);
pinMode(BTN_RESET, INPUT_PULLUP);
pinMode(BUZZ, OUTPUT);
Serial.begin(9600);
display.setBrightness(3);
display.showNumberDec(0, false);
}
void loop() {
unsigned long now = millis();
bool seen = (digitalRead(IR_PIN) == LOW);
if (seen) {
if (armed && now - lastCount > LOCKOUT_MS) {
shots++;
lastCount = now;
armed = false; // wait for the beam to clear before counting again
display.showNumberDec(shots, false);
tone(BUZZ, 1500, 80);
Serial.print(F("Basket! Total "));
Serial.println(shots);
}
clearSince = now;
} else if (!armed && now - clearSince > CLEAR_MS) {
armed = true;
}
if (digitalRead(BTN_RESET) == LOW) {
shots = 0;
display.showNumberDec(0, false);
tone(BUZZ, 600, 150);
delay(300);
}
delay(5);
}What the serial monitor should show
Open the serial monitor at the baud rate in the code. This is what our simulator prints for a virtual Arduino Nano with its sensors at their default values. Your numbers will follow your real readings, but the format should match.
Basket! Total 1
Test checklist
What you should see at each check. If a reading is far off, stop and use the troubleshooting list.
| Check | Expected |
|---|---|
| Ball through the hoop | Count goes up once with a beep |
| Ball bouncing in the net | Not counted again |
| Reset button | Count returns to zero |
| Serial monitor | Prints the total |
| Strong sunlight | May need the sensor shaded |
If it does not work
Counts without a ball
Turn the knob to reduce the range or shade the sensor.
Never counts
Turn the knob up and check OUT goes to D2.
Counts twice
Increase LOCKOUT_MS.
Display stays blank
Check CLK to D5 and DIO to D6.
Library not found
Install TM1637Display.
Still stuck? Find the fault step by step with your multimeter →
Common questions
What parts do I need for the hoop shot counter?
Arduino Nano, IR obstacle avoidance sensor module, TM1637 4-digit display, Passive buzzer, Tactile push button 6 mm, Breadboard 830 Tie Points and Jumper wires male to male (40 pcs, 20 cm).
How long does the hoop shot counter take to build?
About 70 minutes for a beginner maker, in 8 steps.
How do I know the hoop shot counter works?
Ball through the hoop: Count goes up once with a beep. Ball bouncing in the net: Not counted again. Reset button: Count returns to zero.
Counts without a ball: what should I check?
Turn the knob to reduce the range or shade the sensor.
Never counts: what should I check?
Turn the knob up and check OUT goes to D2.
Counts twice: what should I check?
Increase LOCKOUT_MS.
Display stays blank: what should I check?
Check CLK to D5 and DIO to D6.
