Counting small objects falling through tubes

I know there is a huge brain trust here so I am looking for advice/help

Little background first:
I have played with Uno and Trinket Pro boards for projects previously and found using/tweaking pieces and parts of programming from tutorials or other folks projects would make things work. Other than that I am very bad at writing code but I am getting better. I am totally stumped on code that requires very precise timing using "interrupts".

Part of my latest project is to be able to accurately count 1/2" round, 1 gram pellets as they fall through a tube.
The fastest they will fall is once every 0.5 second.

Right now I have an ambient light sensor on one side of tube with a super bright white LED shining on it from other side. The tubes are solid black but since the ALS is clear it is affected by outside light. I supposed I could used some black sealant on it to cover it.
I used a 10k pull down resistor to ground and tapped point between to Analog A0.
Using the serial monitor I see that it displays approx 890 when unobstructed and falls to about 90 when blocked.

I need to count three separate tubes and display those counts individually on a 16x2 LCD. I think I have the LCD part figured out by telling it which row and column to print to. Followed a Hello world tutorial that moved the cursor around the LCD using the LCD library.

Things I am figuring out by reading posts: Please advise if it is wrong or can be done better.

Cannot use delay function because it stops processor and will miss counts
Need to use a IR style emitter/receiver with a 38khz signal on emitter to keep ambient light out of the equation.
Need to convert analog signal to a 1 or 0.
Use a comparator board for this??

Any ideas/suggestions would be great.
Thanks in advance.

My approach would start with IR rather than visible, indeed to have less issues with ambient light.
Secondly, try to go digital. You have a big change in voltage, sounds like you should be able to connect this to a digital pin and read HIGH/LOW values.
When that works, I'd use interrupts for the counting. I think that's more reliable than polling, especially if the object falls fast and only for a very short time blocks the light, which means you may miss an event if you're happening to have to poll two more pipes and then update the LCD before you're back to polling the first.

Use hardware counters (the AMTEL chips have some...depends how many you need...).

The hardware counters can be set up and run in the background. Code can then be executed without causing issues with polling...and then you can use a Timer to say cause the reading (via an ISR) to be read every x seconds for example and stored in global variable.

If you set connect your sensor to one of the external interrupt pins and use attachInterrupt() to link that pin to a function (an Interrupt Service Routine - ISR) all you need in the ISR will be something like this

void pelletCounter() {
  pelletCount++;
}

An Uno has only 2 external interrupts so if you want to detect 3 tubes you would also need to use one pinChange interrupt. Or you could use a Mega which has more external interrupt pins.

You have not said what you want to do with the data so I won't confuse matters with suggestions for other parts of your code.

...R

Thank you for the multiple suggestions.
I will go the IR route.
I bought quite a few Trinket Pro 16Mhz 5v for a previous project and still have 12 of them.
Would they be usable for this?
I believe the problem comes with interrupts and lack of them??
I tested with output connect to digital pin7 and it does register 1 unblocked and 0 when blocked.

Plans for the data:
Tube count will be displayed on a LCD to show the total drops for each tube.

I'd like to use a momentary switch to clear ALL totals.

If a signal has been given to drop and a drop is not detected for 3 consecutive times I will sound a buzzer. This will alert that a jam has occurred somewhere.

Possible to use one switch to clear totals and buzzer alarm?
Totals are important so you wouldn't want to accidentally clear them.

Make it so the button needs to be pressed quickly for alarm clear and very long to clear count?

Would this be an OK choice? I ordered these from Digikey.
TSSP4038 - Infrared Receiver, Light Barrier, 38 kHz, 25m, 45 , 2.5 V, 5.5 V, 700 A
TSAL6100 Infrared (IR) Emitter 940nm 1.35V 100mA 80mW/sr @ 100mA 20° Radial, 5mm Dia (T 1 3/4)

Looks like it has a digital output.

For detecting a falling object you don't need that type of IR receiver - you just need an IR photo-transistor that will give a LOW or HIGH depending on whether the light is shining on it or obstructed.

Just make sure that your IR LED and your photo-transistor use the same IR wavelength.

...R

Robin2:
For detecting a falling object you don't need that type of IR receiver - you just need an IR photo-transistor that will give a LOW or HIGH depending on whether the light is shining on it or obstructed.

Just make sure that your IR LED and your photo-transistor use the same IR wavelength.

...R

These IR's will do be able to that. Both are rated for 940nm.

if you use a 38KHz recvr, you will need to modulate the emitter at 38KHz. just use an IR photo detector for the recvr......

Kurt1:
These IR's will do be able to that. Both are rated for 940nm.

Yes, but you don't need a 38kHz carrier frequency for detecting objects - that is for sending serial data. It won't detect anything unless the transmitting LED is modulated at 38kHz.

It's just not the right tool for the job.

...R

Thank you for the suggestion Robin2. Can you make a recommendation on what to use that will not be affected by outside light?

I was searching for a product that would keep outside light interference close to zero and that was reason for going this route. I read an article about another persons project and he was fighting erratic signals because of sunshine and he used this product. Actually he used TSOP4038 but it's been superseded with the TSS number.

This will be outdoors and 2 of the sensors need to be located right at the end of the tube so they will definitely be subject to bright sunlight.

They could play a part in which method to use for accurate counting since now I have to deal with generating a 38Khz signal. GrumpyMike posted code that used ISR Timer2 for setting up modulation.

Reading up on counting methods and using interrupts and timers and I am in over my head I think for the little experience I have with programming.

I sure appreciate the suggestions.

If you're allowed to drill into the side of the tube.... a hole....and another hole on the opposite side, you could probably mount a laser on one hole, and a receiver on the other side.

For your signal, would 38 kHz PWM at 50% duty cycle work? That would make your LED flicker at that frequency with equal on/off time.

Kurt1:
Thank you for the suggestion Robin2. Can you make a recommendation on what to use that will not be affected by outside light?

I don't have any suggestion.

Can you not shield the device from the sun? Perhaps if you put the detector at the end of a narrow black tube so that it is facing your LED but is shielded from the sun?

By all means try the 38kHz device, but I am concerned that it may miss some of your falling objects.

...R

Thanks again for all suggestions.
I will post over in the gigs section and see if I can get someone to come up with the complete code. I know I am in way over my head.
Thanks again.

The TSOP part is easy. I got curious and as I happen to have an TSOP31238 on hand I wired it up, and easy enough to detect a remote control.

The IR led part didn't work so well; the only IR LED that I have it didn't react to. My phone camera has too good an IR filter to really be able to see it, unfortunately, and it's just a bit too much work to wire up a TSL2561, which should detect IR very well. I'm not entirely sure this LED is working at all.

This is my code - some ESP8266 specifics included but you'll get the idea. Note that the TSOP output is active low. Arduinos should also be able to do 38kHz PWM - this is actually a weakness of the ESP.

The moment a signal is detected by the TSOP, the sketch will print "triggered" on the Serial output. The output of the TSOP I connected directly to a digital pin.

#define TSOP D6
#define LED D5

void setup() {
  pinMode(TSOP, INPUT);
  Serial.begin(115200);

  // Set up the pulsing LED
  pinMode(LED, OUTPUT);
  analogWrite(LED, 127);
  analogWriteFreq(38000);
}

void loop() {
  while (digitalRead(TSOP)) {
    yield();
  }
  Serial.println("triggered!");
  delay(100);
}

This thread about IR serial comms may be of interest.

It generates a 38kHz signal and modulates it.

I suspect the 38kHz receiver is only sensitive to a modulated signal.

...R