Finding an appropriate sensor for a pill counter

Hey friends,

I’m quite new to development with Raspberry Pi 4 but I decided to pick it up to make a pill counter, since many industrial & accurate ones are quite expensive.
I’m looking for the best sensor & technology to use to capture small falling objects (perhaps passing down a tube or small space) and to add them up accurately.

So far I have tried out the PiicoDev distance sensor and while I quite like it, I dont think im convinced of its ability to be reliable with fast moving objects. (a few hours of testing only haha)

I don’t currently have a design made, so apologise if the question is broad but you can assume that there will be a gap between pieces/pills falling. Extra points if there’s an accurate solution for if there is no gap.

The purpose is to organise hundreds of pieces (number of pieces must be exact).

Much appreciated!


Hi Zacary,

Welcome to the forum!!

A little aside before jumping into the nitty-gritty. With most projects there will be quite a few ways to complete it and finding the ‘best’ solution can take some serious time and brainstorming.

One solution that comes to mind is taken from the book of tracking a motors current position using an encoder. Mainly the optical variant, you will need a tube small enough to make sure that the beam of light is cut on every single fall though. Using an interrupt to capture and increment this count will also be essential. The only downside is that the units will have to move in individual units, i.e. one at a time.

Another would be the path of using some kind of pattern recognition although doing that in real-time can be difficult!

PS: in regards to the distance sensor it has a ‘ranging frequency’ (I think that is similar to the polling frequency) of 50 Hz so if the object passes through faster than that it won’t capture it.



You haven’t indicated why you want to count the pills. For instance, if the purpose is to count out a specific quantity then it would likely be simpler to weigh them than to count each one. That is, you continue adding pills until the correct weight is achieved.


Edited the post, the purpose is to organise hundreds of pieces (number of pieces must be exact) many times.
Weighing may not be suitable due to the extra work that goes into manually taking out a piece everytime to fine tune the weight and make sure its exact, this would be good if I were only doing this a couple of times.
To also be clear it isnt pills, but small plastic pieces, like 1x1 lego.


What sort of organising? Do you mean you want to organize a bulk pack into several smaller packs of an exact count each, or do you mean that you need to identify different items and count how many of each, and if so does that mean that you need to handle them in some way depending on what gets identified (sorting items by type)? Identifying items will be a very different task to simply counting them, and will depend heavily on exactly how different items can be differentiated.

1 Like

Hi Zacary
I think there would be quite a few through beam sensors that would do the job. One important thing to know is how fast (number of items per unit time) you anticipate this to operate. How you intend to shut off supply when quota reached. Transport mechanism, conveyor, chute etc.

Another curly but quite important point as you want something like absolute accuracy is item separation. Each item must be counted separately with a space in between. you will have to devise a means to do that. Stopping supply is also tricky. It will need to stop very very quickly. If supply involves a motor spinning you could have a real problem as if a motor (particularly one of any size) is spinning at any sort of speed it will take some stopping.

I am not trying to kill off a project before it starts but there could be a lot more involved than counting items. In fact the actual counting is probably the easy part. There are quite a few more things to consider.
Cheers Bob