Help with phototransistor

I'm trying to hook up a phototransistor to my arduino and have my arduino tell me the values. The general goal is to have the arduino add +1 to a list whenever there is no IR light hitting the transistor (something passes between an IR emitter and the phototransistor) . However I have little knowledge of coding and online tutorials haven't been of any help, does anybody have any idea how I could do this or know of a good, simple tutorial where I can see?

What do you mean, "a list"?

a list as in how many times the signal between the IR emissor and phototransistor has been interruped

pinMode(yourInputPin, INPUT); Assuming an NPN phototransistor...

  • Collector of transistor to yourInputPin - and to +5V via a 4K7 resistor
  • Emitter to 0V
  • Base left open circuit
  • readDigital(yourInputPin)

  • If yourInputPin state changes to LOW - means the transistor is 'exposed'

  • If yourInputPin state changes to HIGH - means the transistor is 'dark'

Use these to maintain a 'counter' variable - not a 'list'... unless you're planning to measure something else wrt the time between light/darkevents.

This should be enough to get you thinking in the right direction. Once you get past this - in some cases, you could lose the 4K7, and use pinMode(yourInputPin, INPUT_PULLUP);

Thanks for the help, I got going but now i'm stumped again, I'm actually using a 2 pronged photoresistor (the one that looks like a black LED). I coded an IF statement sketch and tested it, but the counter just goes up forever and does not take into account if the photoresistor is receiving light or not.

This is the code I came up with. int photoTran = A1; int reading = 0; int otro = 0;

void setup(){ pinMode(photoTran,INPUT); Serial.begin(9600); }

void loop(){ reading = analogRead(photoTran); if (reading == HIGH) { Serial.print(photoTran); Serial.print(" "); delay(1000); photoTran = photoTran + 1; } else {

} }

photoTran is your input which is A1 but then you increment photoTran and it no longer = A1. .

Got it to work! Thanks for you help everyone!!

And what was the problem? .

Well... apparently I had 2 variables that were called the same, so I just changed on of the "photoTrans" to another name and that seemed to do the trick. Thanks!

Your code has what might be a fundamental issue though, in that it doesn’t look for a change to high; it looks for the pin to be high.

It will continue to count as long as the pin is high. Now that might be what you want, but if you want it to count up once for each time the pin becomes high, you’ll need a rethink along these lines.

Sokles: a list as in how many times the signal between the IR emissor and phototransistor has been interruped

That would be called "a count".