To detect a ball or other object, one can use a photogate, consisting of an IR LED to generate light, and an IR Photodiode to detect the presence of an obstruction.
When the light is blocked by the ball, even for a short time, this creates a signal that can be detected by the Microbit.
The Photodiode is reversed -biased. When it detects IR light from the LED, it conducts in the reverse direction. When the light is blocked, the voltage changes, creating a signal.
diode circuit.pdf
diodelayout.pdf