A device to count the dispensing of objects such as pills by an automatic dispenser includes a base, a light emitting component installed on one end portion of the base, a light sensor installed on the other end portion of the base, a first lens, and a second lens. The light emitting component emits a point light ray towards the first lens, the point light ray is refracted into a parallel light ray and passes through the second lens, and is refracted again into the point light ray. The light sensor receives the reconverted point light ray and performs energy measurement to detect the dispensing of an object, the object as it passes having blocked a portion of the light ray.