Tzarls
Posts: 224
Joined: Tue Feb 26, 2013 6:59 am

Phototransistor and GPIO input

Fri Dec 06, 2013 6:10 pm

Hi. I need to have the pi detect when a light beam is interrupted. For that I wanted to use a phototransistor. I searched the forum to find how to correctly connect it to the Pi, and found this topic:

http://www.raspberrypi.org/phpBB3/viewt ... transistor

After reading it I was left with this idea: If I connect a GPIO pin (configured as input with the pull-up resistor on) directly to the transistor´s collector and the emitter connected to ground, will it work as desired? I guess I could add a resistor between the emitter and ground just in case the pin is accidentally set to output (and HIGH).

What do you think?

User avatar
FLYFISH TECHNOLOGIES
Posts: 1750
Joined: Thu Oct 03, 2013 7:48 am
Location: Ljubljana, Slovenia
Contact: Website

Re: Phototransistor and GPIO input

Fri Dec 06, 2013 6:48 pm

Hi,
Tzarls wrote:What do you think?
I think that you shoud/could add also schmitt trigger circuit.

Between GND and 3.3V there are two voltage ranges on both ends ("mapped" to 0 and 1) and a wide "restricted" range between. If your voltage is inside this "restricted" area anyway, it can be interpretted as logical 1 or 0 - sometimes 0, a moment later it could be 1, etc. There is no guarantee...
The nature of referenced simple transistor-resistor circuits is that it is very likely that voltage on output can be everywhere between GND and 3.3V... also inside "restricted" range - therefore, repeatability is poor...

Schmitt trigger circuit fixes this unpredicted behaviour by decreasing mentioned "restricted" range. Consequently, you drastically increase predictability - the same brightness will most likely be "mapped" equally each time.
So, in your case this simply means that photo interrupter would have improved reliability.


Best wishes, Ivan Zilic.
Running out of GPIO pins and/or need to read analog values?
Solution: http://www.flyfish-tech.com/FF32

Tzarls
Posts: 224
Joined: Tue Feb 26, 2013 6:59 am

Re: Phototransistor and GPIO input

Fri Dec 06, 2013 7:24 pm

Thanks for your answer Ivan. So if I´m getting this right, the schmitt trigger would improve the repeatability (and accuracy?) of the system. I´ll keep this in mind.

Now mine was more the "am I probably going to blow my Pi with this circuit?" kind of question... So, even if not 100% reliable (without the schmitt trigger, and in terms of detection), is the described circuit safe?

User avatar
ZacharyI123
Posts: 263
Joined: Sun Jun 17, 2012 1:09 pm
Location: Ilford, Essex
Contact: Website

Re: Phototransistor and GPIO input

Fri Dec 06, 2013 7:57 pm

This post is a very good way of using a photo transistor with a Pi: http://www.raspberrypi.org/phpBB3/viewt ... 94#p440294

It is especially good because you chose the sensitivity.
Don't be mean, I'm only thirteen 8-)

User avatar
ZacharyI123
Posts: 263
Joined: Sun Jun 17, 2012 1:09 pm
Location: Ilford, Essex
Contact: Website

Re: Phototransistor and GPIO input

Fri Dec 06, 2013 8:00 pm

Tzarls wrote:Hi. I need to have the pi detect when a light beam is interrupted. For that I wanted to use a phototransistor. I searched the forum to find how to correctly connect it to the Pi, and found this topic:

http://www.raspberrypi.org/phpBB3/viewt ... transistor

After reading it I was left with this idea: If I connect a GPIO pin (configured as input with the pull-up resistor on) directly to the transistor´s collector and the emitter connected to ground, will it work as desired? I guess I could add a resistor between the emitter and ground just in case the pin is accidentally set to output (and HIGH).

What do you think?
Yes that would work with a pull up resistor. The problem with it is that you do not chose the sensitivity. You will only get a high reading if your photo transistor resists more than whatever the pull up resistor resists.
Don't be mean, I'm only thirteen 8-)

User avatar
mahjongg
Forum Moderator
Forum Moderator
Posts: 12010
Joined: Sun Mar 11, 2012 12:19 am
Location: South Holland, The Netherlands

Re: Phototransistor and GPIO input

Fri Dec 06, 2013 8:30 pm

you can "fake" the hysteresis provided by a schmitt-trigger by inverting what you read from the phototransistor and outputting it on another GPIO, then connecting a resistor from that output GPIO to the photodetector input GPIO. Use a value for this resistor that is a large multiple of the pull-up resistor of the photo-transistor (so if the pull-up is say 10K, use a feedback resistor of say 47K). This will then act as a positive feedback, and give hysteresis to the input, without needing extra hardware, except the resistor.

Tzarls
Posts: 224
Joined: Tue Feb 26, 2013 6:59 am

Re: Phototransistor and GPIO input

Fri Dec 06, 2013 9:55 pm

mahjongg wrote:you can "fake" the hysteresis provided by a schmitt-trigger by inverting what you read from the phototransistor and outputting it on another GPIO, then connecting a resistor from that output GPIO to the photodetector input GPIO. Use a value for this resistor that is a large multiple of the pull-up resistor of the photo-transistor (so if the pull-up is say 10K, use a feedback resistor of say 47K). This will then act as a positive feedback, and give hysteresis to the input, without needing extra hardware, except the resistor.
Thanks for the suggestion - I guess I need to check the Pi´s schematics in order to understand what you describe! The problem is that the project in hand needs 8 inputs with a photoresistor each and 4 outputs, so there´s no posibility of using this technique. Anyway, I´ll make some tests and hope I don´t blow anything in the process! Thanks again - and if there are some more comments let me know.

User avatar
mahjongg
Forum Moderator
Forum Moderator
Posts: 12010
Joined: Sun Mar 11, 2012 12:19 am
Location: South Holland, The Netherlands

Re: Phototransistor and GPIO input

Sat Dec 07, 2013 3:03 pm

then just use two 74hc14's, they can be operated directly at 3.3V. http://www.onsemi.com/pub/Collateral/74HC14.REV1.PDF

Tzarls
Posts: 224
Joined: Tue Feb 26, 2013 6:59 am

Re: Phototransistor and GPIO input

Sat Dec 07, 2013 4:31 pm

Good idea - now, how do I get an SMD component working with a protoboard? I guess there is a non-SMD version, so I´ll look for it. And while I´m at it, I think I´ll add some buffering to my inputs and outputs, just in case. Or get a Gertboard.

boyoh
Posts: 1297
Joined: Fri Nov 23, 2012 3:30 pm
Location: Selby. North Yorkshire .UK

Re: Phototransistor and GPIO input

Sat Dec 07, 2013 6:58 pm

This is just a basic circuit of a light detector.
Rigg it up on a bread board, and see if it
Fills your requirements, Just using one GPIO I/P
You can use the GPIO I/P To give you a
Active High (1) or a Active Low (0) Depending
0n the light sorce On or Of
Sugest you shroud the Photo Transistor against
Surounding ambient light

Switching Transistor ON GPIO I/P Active Low (0v)

Switching Transistor OFF GPIO I/P Active High (1)

BoyOh
Attachments
img024.jpg
img024.jpg (23.64 KiB) Viewed 9319 times
BoyOh ( Selby, North Yorkshire.UK)
Some Times Right Some Times Wrong

User avatar
mahjongg
Forum Moderator
Forum Moderator
Posts: 12010
Joined: Sun Mar 11, 2012 12:19 am
Location: South Holland, The Netherlands

Re: Phototransistor and GPIO input

Sat Dec 07, 2013 11:59 pm

Tzarls wrote:Good idea - now, how do I get an SMD component working with a protoboard? I guess there is a non-SMD version, so I´ll look for it. And while I´m at it, I think I´ll add some buffering to my inputs and outputs, just in case. Or get a Gertboard.
I guess the DIP versions of the 74HC14 are just as easy, or even easier to buy than the SMT versions:
order code 380404 at Farnell, for € 0,61 each (if you buy less than 10, cheaper otherwise).

Return to “Beginners”