rajbuddy007
Posts: 54
Joined: Fri Apr 05, 2019 3:03 pm

light detector with photo transistor

Tue Jun 04, 2019 7:31 am

Hello,

I want to detect a red light from SMD Red LED with the help of photo transistor. I know the fact that in order to make it readable by Rpi i have to connect it to comparator. It would be nice if anybody help me with making a circuit with it.


Phototransistor: https://www.mouser.de/ProductDetail/Vis ... UVXeaSCA==

Comparator: https://www.mouser.de/ProductDetail/Tex ... TSCthGw%3D

PiGraham
Posts: 3715
Joined: Fri Jun 07, 2013 12:37 pm
Location: Waterlooville

Re: light detector with photo transistor

Tue Jun 04, 2019 8:52 am

You may not need a comparator if the the light from the LED is enough to turn the phototransistor on enough to pull a GPIO to a safe on or off level against a pullup, and allow the GPIO to return to the opposite state when the LED is off.

If youoptically couple the sensor closely to the LED and exclude ambient light you have made an optoisolator and they are typically used without comparators.


You would use a comparator where the swing in the signal is less han required for binary switching and/or you want a sensitivity adjustment.

rajbuddy007
Posts: 54
Joined: Fri Apr 05, 2019 3:03 pm

Re: light detector with photo transistor

Tue Jun 04, 2019 9:49 am

Thank for the reply.
I want to detect a light from this LED. Please do check and tell me what should be the best practice for it?

https://www.mouser.de/datasheet/2/216/A ... 102303.pdf

PiGraham
Posts: 3715
Joined: Fri Jun 07, 2013 12:37 pm
Location: Waterlooville

Re: light detector with photo transistor

Tue Jun 04, 2019 9:53 am

rajbuddy007 wrote:
Tue Jun 04, 2019 9:49 am
Thank for the reply.
I want to detect a light from this LED. Please do check and tell me what should be the best practice for it?

https://www.mouser.de/datasheet/2/216/A ... 102303.pdf
Connect it as the phototransitor part of an optoisolator.

http://raspberrypihobbyist.blogspot.com ... it_19.html.
Try it out and see if it works with your LED and phototransistor.

User avatar
Burngate
Posts: 6193
Joined: Thu Sep 29, 2011 4:34 pm
Location: Berkshire UK Tralfamadore
Contact: Website

Re: light detector with photo transistor

Tue Jun 04, 2019 10:47 am

Much depends on how far apart the LED and phototransistor are, how bright the LED is (how much current is going through it), how much light will reach the phototransistor from other sources, ...

Using PiGraham's suggestion, you may need to add an external pull-up in addition to the Pi's internal pull-up, and experiment with its value.

Only if, after experimenting, you find you cannot reliably detect light from the LED, should you consider using a comparator.

If that is what you decide to do, you will need to use a similar circuit - phototransistor emitter connected to ground, collector to comparator input, pull-up resistor from input to 3v3 - with a resistor divider between 3v3 and ground feeding the other comparator input. Make sure you use 3v3 for the comparator's supply.

User avatar
TimG
Posts: 297
Joined: Tue Apr 03, 2012 12:15 am
Location: Switzerland

Re: light detector with photo transistor

Tue Jun 04, 2019 11:16 am

A digital light detector would save you a lot of work. For example,
https://www.adafruit.com/product/4162
Image

On the other hand if you want to learn about phototransistors, here are a couple of useful links:
http://digitalmedia.risd.edu/pbadger/Ph ... transistor
http://www.circuitous.ca/PhotoDetectors.html
To read out your phototransistor circuit you can use something like this made-for-RPi 24V-tolerant ADC:
https://shop.pimoroni.com/products/automation-phat
Image
If you connect you phototransistor circuit directly to the RPi then be sure not to send more than 3.3V.

rajbuddy007
Posts: 54
Joined: Fri Apr 05, 2019 3:03 pm

Re: light detector with photo transistor

Wed Jun 05, 2019 7:47 am

TimG wrote:
Tue Jun 04, 2019 11:16 am
A digital light detector would save you a lot of work. For example,
https://www.adafruit.com/product/4162
Image

On the other hand if you want to learn about phototransistors, here are a couple of useful links:
http://digitalmedia.risd.edu/pbadger/Ph ... transistor
http://www.circuitous.ca/PhotoDetectors.html
To read out your phototransistor circuit you can use something like this made-for-RPi 24V-tolerant ADC:
https://shop.pimoroni.com/products/automation-phat
Image
If you connect you phototransistor circuit directly to the RPi then be sure not to send more than 3.3V.

Thanks for the recommendation. But i need to make something from the components which i have currently. The distance wont be a problem as i can place my phototransistor wherever i want. Please tell me how should i connect it to my GPIO and what would be the code for it. I am doing automated testing so i want something which gives the output flash detected when it detects. Please help
.

PiGraham
Posts: 3715
Joined: Fri Jun 07, 2013 12:37 pm
Location: Waterlooville

Re: light detector with photo transistor

Wed Jun 05, 2019 8:11 am

rajbuddy007 wrote:
Wed Jun 05, 2019 7:47 am
Please help
.
We have helped. I gave you a link to a tutorial for using an optoisolator with RPi that shows the connections for a phototransistor.
Burngate gave you useful suggestions for adapting to your specific use.
The code is like code for any digital gpio input like a button or a sensor.
Try it.
If you get stuck on something come back with details of what you tried, what happened and any questions to help you understand it.

pcmanbob
Posts: 7971
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: light detector with photo transistor

Wed Jun 05, 2019 9:46 am

The connection of an npn photo transistor to the gpio is the same as a normal npn transistor, you just don't have to worry about the base leg.

you would connect it to the gpio like this

Image

and you could see if its detecting anything with a very simple program like this

Code: Select all

import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(27, GPIO.IN)

while True:

    print (GPIO.input(27))
    time.sleep(0.2)
    
    
the program will just print out the state of the gpio input 0 = no light detected 1 = light detected
Last edited by pcmanbob on Thu Jun 06, 2019 7:41 am, edited 1 time in total.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

rajbuddy007
Posts: 54
Joined: Fri Apr 05, 2019 3:03 pm

Re: light detector with photo transistor

Thu Jun 06, 2019 7:30 am

Thanks a lot for the help guys. Much appreciated.

rajbuddy007
Posts: 54
Joined: Fri Apr 05, 2019 3:03 pm

Re: light detector with photo transistor

Thu Jun 06, 2019 8:55 am

pcmanbob wrote:
Wed Jun 05, 2019 9:46 am
The connection of an npn photo transistor to the gpio is the same as a normal npn transistor, you just don't have to worry about the base leg.

you would connect it to the gpio like this

Image

and you could see if its detecting anything with a very simple program like this

Code: Select all

import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(27, GPIO.IN)

while True:

    print (GPIO.input(27))
    time.sleep(0.2)
    
    
the program will just print out the state of the gpio input 0 = no light detected 1 = light detected


I have tried this circuit but on the GPIO pin its giving same constant 2.2 V irrespective of the light. Where am i going wrong?

pcmanbob
Posts: 7971
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: light detector with photo transistor

Thu Jun 06, 2019 9:08 am

Have you got the transistor connected the right way round ?

The shorter leg is the collector ( C )

The transistor is listed as an ambient light sensor so it will be triggered by the day light,

what happens if you cover the transistor to prevent any light reaching it ?
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

rajbuddy007
Posts: 54
Joined: Fri Apr 05, 2019 3:03 pm

Re: light detector with photo transistor

Thu Jun 06, 2019 9:26 am

pcmanbob wrote:
Thu Jun 06, 2019 9:08 am
Have you got the transistor connected the right way round ?

The shorter leg is the collector ( C )

The transistor is listed as an ambient light sensor so it will be triggered by the day light,

what happens if you cover the transistor to prevent any light reaching it ?
My bad i interchanged the circuit. Now its giving 0V and when i am exposing it to light its giving 0.001 V

pcmanbob
Posts: 7971
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: light detector with photo transistor

Thu Jun 06, 2019 9:45 am

If you illuminate it enough it should give you 3.3v on the emitter.

are you sure you have the circuit connected correctly ?

may be post some pictures so we can see how its connected.

just upload them to imgur.com and post links her.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

rajbuddy007
Posts: 54
Joined: Fri Apr 05, 2019 3:03 pm

Re: light detector with photo transistor

Thu Jun 06, 2019 9:47 am

pcmanbob wrote:
Thu Jun 06, 2019 9:45 am
If you illuminate it enough it should give you 3.3v on the emitter.

are you sure you have the circuit connected correctly ?

may be post some pictures so we can see how its connected.

just upload them to imgur.com and post links her.
No its not giving infact i have placed a red led very close to it. Still its not giving any voltage

rajbuddy007
Posts: 54
Joined: Fri Apr 05, 2019 3:03 pm

Re: light detector with photo transistor

Thu Jun 06, 2019 9:55 am

pcmanbob wrote:
Thu Jun 06, 2019 9:45 am
If you illuminate it enough it should give you 3.3v on the emitter.

are you sure you have the circuit connected correctly ?

may be post some pictures so we can see how its connected.

just upload them to imgur.com and post links her.

Please check the url https://imgur.com/a/0VZiSEK

pcmanbob
Posts: 7971
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: light detector with photo transistor

Thu Jun 06, 2019 10:04 am

Are you just trying to use the LED to turn the transistor on ?

if so read the data sheet about sensitivity area on page 4

https://www.mouser.co.uk/datasheet/2/42 ... 244748.pdf
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

PiGraham
Posts: 3715
Joined: Fri Jun 07, 2013 12:37 pm
Location: Waterlooville

Re: light detector with photo transistor

Thu Jun 06, 2019 11:08 am

The spec says Collector light current 200uA at 5V and 200lx illuminance, 70uA at 20 lx so you could expect 200uA in a bright daylit room, maybe <=100uA in the dark room in your photo.

https://en.wikipedia.org/wiki/Illuminan ... nce_levels

So connected as pcmanbob's circuit with a 10k pulldown and only 3.3v at most across CE you might not get a high level at the input. 100uA through 10k would give a voltage of 1V. 200uA would be 2V which is marginal for uncertain light levels.

Try more light to test then use a higher value resistor, 100k, say.
Given the small currents involved you can do without the 1k resistor and connect direct.

For sure you need to point the lens of the phototransistor at the light source. Start with them head to head. It's no good stand it next to an LED

If you have a multimeter start with just the phototransistor and 10k resistor. Leave out the Pi. Shine a bright light (the LED light on your camera phone would be a good test light) and see if the voltage across the resistor changes. If not then either you have wired it wrong or the device is broken.

rajbuddy007
Posts: 54
Joined: Fri Apr 05, 2019 3:03 pm

Re: light detector with photo transistor

Thu Jun 06, 2019 11:42 am

pcmanbob wrote:
Thu Jun 06, 2019 10:04 am
Are you just trying to use the LED to turn the transistor on ?

if so read the data sheet about sensitivity area on page 4

https://www.mouser.co.uk/datasheet/2/42 ... 244748.pdf
No i placesd just to see how it is reacting to it.

rajbuddy007
Posts: 54
Joined: Fri Apr 05, 2019 3:03 pm

Re: light detector with photo transistor

Thu Jun 06, 2019 11:46 am

PiGraham wrote:
Thu Jun 06, 2019 11:08 am
The spec says Collector light current 200uA at 5V and 200lx illuminance, 70uA at 20 lx so you could expect 200uA in a bright daylit room, maybe <=100uA in the dark room in your photo.

https://en.wikipedia.org/wiki/Illuminan ... nce_levels

So connected as pcmanbob's circuit with a 10k pulldown and only 3.3v at most across CE you might not get a high level at the input. 100uA through 10k would give a voltage of 1V. 200uA would be 2V which is marginal for uncertain light levels.

Try more light to test then use a higher value resistor, 100k, say.
Given the small currents involved you can do without the 1k resistor and connect direct.

For sure you need to point the lens of the phototransistor at the light source. Start with them head to head. It's no good stand it next to an LED

If you have a multimeter start with just the phototransistor and 10k resistor. Leave out the Pi. Shine a bright light (the LED light on your camera phone would be a good test light) and see if the voltage across the resistor changes. If not then either you have wired it wrong or the device is broken.


I dont think it will give desired output for the light which i have to detect. Please check the datasheet of the smd led. Should i use comparator for it?

SMD LED https://www.mouser.de/datasheet/2/216/A ... 102303.pdf

PiGraham
Posts: 3715
Joined: Fri Jun 07, 2013 12:37 pm
Location: Waterlooville

Re: light detector with photo transistor

Thu Jun 06, 2019 11:57 am

rajbuddy007 wrote:
Thu Jun 06, 2019 11:46 am

I dont think it will give desired output for the light which i have to detect. Please check the datasheet of the smd led. Should i use comparator for it?

SMD LED https://www.mouser.de/datasheet/2/216/A ... 102303.pdf
Before you do anything else make sure the sensor actually works. You should be able to measure a voltage. If you can't do that a comparator will do no good at all.


A I mentioned, the component parts here are essentially the same as are used in optoisolators and they work without comparators. I would expect any LED pointing at that phototransistor with small separation would give a voltage swing that a 3.3v gpio input would read

PiGraham
Posts: 3715
Joined: Fri Jun 07, 2013 12:37 pm
Location: Waterlooville

Re: light detector with photo transistor

Thu Jun 06, 2019 12:11 pm

Data for the LED shows luminous intensity of 110mcd

This calculator shows an illuminance of 27500lx at 2mm using 0.110 candela and 0.002m and 1100lx at 10mm if we just assume an isotropic radiation pattern.
So you should have planty of light from the LED to operate the sensor if you aim them right and get them close.

rajbuddy007
Posts: 54
Joined: Fri Apr 05, 2019 3:03 pm

Re: light detector with photo transistor

Thu Jun 06, 2019 12:14 pm

PiGraham wrote:
Thu Jun 06, 2019 12:11 pm
Data for the LED shows luminous intensity of 110mcd

This calculator shows an illuminance of 27500lx at 2mm using 0.110 candela and 0.002m and 1100lx at 10mm if we just assume an isotropic radiation pattern.
So you should have planty of light from the LED to operate the sensor if you aim them right and get them close.
What do you think? will this photo transistor detect that flash of SMD or should i look for another photo transistor??

rajbuddy007
Posts: 54
Joined: Fri Apr 05, 2019 3:03 pm

Re: light detector with photo transistor

Thu Jun 06, 2019 12:43 pm

PiGraham wrote:
Thu Jun 06, 2019 11:08 am
The spec says Collector light current 200uA at 5V and 200lx illuminance, 70uA at 20 lx so you could expect 200uA in a bright daylit room, maybe <=100uA in the dark room in your photo.

https://en.wikipedia.org/wiki/Illuminan ... nce_levels

So connected as pcmanbob's circuit with a 10k pulldown and only 3.3v at most across CE you might not get a high level at the input. 100uA through 10k would give a voltage of 1V. 200uA would be 2V which is marginal for uncertain light levels.

Try more light to test then use a higher value resistor, 100k, say.
Given the small currents involved you can do without the 1k resistor and connect direct.

For sure you need to point the lens of the phototransistor at the light source. Start with them head to head. It's no good stand it next to an LED

If you have a multimeter start with just the phototransistor and 10k resistor. Leave out the Pi. Shine a bright light (the LED light on your camera phone would be a good test light) and see if the voltage across the resistor changes. If not then either you have wired it wrong or the device is broken.
I have tried the cicrcuit with 100K resistor and exposing it to the camera flashlight but it is giving max 400mV.

PiGraham
Posts: 3715
Joined: Fri Jun 07, 2013 12:37 pm
Location: Waterlooville

Re: light detector with photo transistor

Thu Jun 06, 2019 1:33 pm

rajbuddy007 wrote:
Thu Jun 06, 2019 12:43 pm

I have tried the cicrcuit with 100K resistor and exposing it to the camera flashlight but it is giving max 400mV.
So it is definitely responding to light level then?

Return to “General discussion”