IR Led and Phototransistor connection give always 0 value

Firstly hello everyone,

I planned make a wheat grain counter. So I will use phototransistor and IR led for count them all. I have arduino uno, I used 470 ohm resistor for IR Led, 10K ohm resistor for three pin phototransistor and connecting phototransistor to A0. My codes are

int Tran=A0;
int reslt;
void setup()
  pinMode(Tran, INPUT);

void loop()
  reslt = analogRead(A0);

And on serial monitor I always get 0 value. So what ? have to do. I want from phototransistor that when grains pass through between phototransistor and IR led, a counter increase but I havent connected them truely yet...

Sounds like a wiring error. How is everything wired?

Schematic? Please trim white space and resample to 600 or 800 pixels wide. Take voltage measurements and post those, too.

I’m no expert, but I thought phototransistors had only two pins, since the base is the incoming light?

470 ohm resistor for IR Led,

That is too high, there will not be enough IR light with that value.

I dont have electronic knowledge so enough. I am just new. So I dont have a item for measure a voltage. But I will buy it.

Here is my scheme.

One of the phototransistor pin is empty, and how much ohm I need to use ?

I ve done it with LDR and laser. There were no problem It was working, just problem was LDR is not fast enough for grain counter..

Can't see it very clear. But your green wire seems to be in row 60, one pin of the IR receiver is in 60 too. The other pin of the receiver is in row 61, together with a connection to GND and some resistor that does nothing at all but waste some energy. Because that resistor is connected between power and GND rail. I'm quite sure that is not what was in the tutorial. But i could be mistaking because your picture isn't really clear.

Sorry for bad picture, my phone camera is not so good.

I try to explain;

I get +5V power with white wire to + line (which is red line), then connect with 10K ohm resistor to one of the phototransistor pin, other pin goes to GND and analog 0.

With 470 ohm resistor I get +5V to red wire and then connect IR led long pin, other pin goes to GND.

I was use 220 ohm for IR led, value of phototransistor has changed to approx 50 but when I interrupt led there were no changes in value of phototransstor.

With 470 ohm resistor I

Too high

I was use 220 ohm for IR led

Still too high. Try a 33 ohm resistor.

then connect with 10K ohm resistor to one of the phototransistor pin

You sure about that, it does not look like that in the photo. It has to be +5V to resistor other end of resistor to the photo transistor - that is not what you have got. Anyway 10K is too low, try 100K.

but when I interrupt led there were no changes in value of phototransstor.

That is because you have to interrupt it with something opaque to IR. Try something metal, like a foil covered card or put it in a tube and cover all but the open end in foil.

jeolex: void loop() {   reslt = analogRead(A0);   Serial.println(Reslt); }


void loop()
  Reslt = analogRead(A0);

Firstly thank you for all reply, I had lot of idea and knowledge..

I used 47 OHM resistor for IR LED, 47K OHM resistor for phototransistor and connect analog 0 with 3 th pin of phototransistor, it works !

I read approx. 100 and when wheat grain pass through between them the value changes to approx 90. So How can I count the grain number, which codes I need please ?


void setup(){

int count=0;


void loop(){
unsigned long duration=pulseIn(A0,HIGH);

if (duration>0){ 



Maybe that could work?

Johnny010: PulseIn()?

No, you do not want that. Anyway it only works on digital inputs.

First you need to increase the difference, having just 10 does not give you much. Try with that higher resistor and try butting both sensor and emitter in tubes.

Then you write code to detect when the reading crosses the threshold of half way. You are interested in the edge, that is when the last reading is one side of the threshold and the next is the other. Then all you do is increment a variable when you see one of these edge transitions occur.

I used 330K ohm for phototransistor and use pipe, It's now change between 110-113. when I interrupt with particule such wheat grain It almost not changed, But for example interrupt with knife I can see 50-60.

And I am sorry I couldn't get what you mean about threshold and edge. Can you please explain with codes, it will more comprehensible for me.



No, you do not want that. Anyway it only works on digital inputs.

Yeah my bad!

Anyways, as a seed passes infront of your IR source, the sensor value will go from 0->110 then as the seed passes the source 110->0 again.

The rising edge is where a signal starts to increase:

Oh and as a side note for my own (but related) question. Could he not set a pin as high when the 50% threshold is reached and low when the threshold dips below 40% in to a second pin using pulsein() to trigger a count?

A threshold is halfway between the two states.

It’s now change between 110-113.

So your threshold would be 117.
You then compare what you are getting from the IR sensor with this threshold

threshold = 117;
value = analogRead(yourSensor);
if(value > threshold) { // increment count

But that would make the count race every time it was above the threshold so the last thing you do in the loop is say:-

oldValue = value;

then you can say:-

if(value > threshold && oldValue < threshold) {
grains +=grains; // increment the grains count

Try putting something together using those notes.

Some phototransistors bring the Base lead out for biasing.


I made a simple system for count grains. I can get value from phototransistor and. Normally value is approx 90 when I interrupt it is approx 800-900. So I have now threshold. But I have a problem. When I pour the grains from the hole severally, phototransistor didn’t detect and didn’t increase counter or detect the wheat grain and increase counter one or two times.

So what I have to do. Is there a more sensitive phototransistor.

int pd=2;                      
 int buzz=13;                   
 int senRead=0;               
 int limit=120; 
int counter=0; 
 void setup()    
 void loop()  
  int val=analogRead(senRead); 
  if(val <= limit)              
  else if(val > limit)         
 }  .

I think, for one thing, that analogRead takes too long. IMO, you ought to be digitalRead'ing.

That delay(20) that you have is pretty big, too, The time between one grain and the next is probably (much) shorter than that. Get rid of the buzzer thing right now and get the counts right.

[I worked with optical sorting machines once upon a time.]