0

Using Photo Resistor Cell and Transistor to turn on/off Game
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Using Photo Resistor Cell and Transistor to turn on/off Game

by CaseyScalf on Wed Oct 14, 2015 6:26 am

Hi there. I've made a few of your Game of Life kits.

I was hoping to wire it up such that when the room became dark the game would initiate. I found a few ways to use a photo resistor cell and a transistor. I used some of your photo cells and this tutorial: http://www.instructables.com/id/Blue-Ba ... LED-light/

I could wire these up so that when it became dark the LED would turn on. I figured that I could swap the LED for the power plug on the Game of Life board and that would work. However, I have found this is not the case. It does not turn on even though th battery, cable, and board are all working nicely.

Here is the video of what I just described: https://youtu.be/a5j8xLgJXvM
Here is a photo of what I jut described: https://photos.google.com/u/0/photo/AF1QipNVSebzL6fHF6z4Z3_tWd4eI8wH5ZhfugVmMnP8

Could you provide some pointers on if there's anything I am missing coming need to add, or any conceptual things I am missing so that I can make this work.

I am wondering if the transistor is appropriate. Also, if there are the right resistors and such in place. I would love to understand this more so that I can make more of my projects light reactive. I.e., any project could be set to turn on when dark. Maybe there are purpose made circuits for this, but I would love to learn as well.

Thank you so much,

CaseyScalf
 
Posts: 134
Joined: Wed Nov 05, 2014 3:07 am

Re: Using Photo Resistor Cell and Transistor to turn on/off

by adafruit_support_mike on Thu Oct 15, 2015 12:14 am

The GoL boards probably want more current than the transistor can supply.

For the sake of this discussion, you can think of a BJT as current amplifier. A small amount of current flowing into the base controls a larger amount of current flowing into the collector. The exact ratio varies, but 100:1 is a good rule of thumb these days.

It looks like you have a 47k resistor sending current from the 3.7v rail to the transistor's base, with the photoresistor providing another path to GND. The photoresistor's resistance falls when it's exposed to light, so it steals current from the transistor's base, keeping the transistor shut off. When it gets dark, the phototransistor's resistance rises, and eventually most of the current flowing through the 47k resistor goes into the transistor's base.

The base voltage will be about 0.6v when that happens, so you'll have about 3.1v across the 47k resistor. That means about 66 microamps will flow into the base.

If we multiply 66uA by 100, we get about 6.6mA. That's enough to light an LED, but not enough for an array of GoL boards.

You can solve that by doing the multiplication thing again:

two-stage.jpg
two-stage.jpg (16.69 KiB) Viewed 1436 times
Sending the current from the first transistor into another one's base gives you another factor of 100 in current gain.

You lose a little current through the 47k resistor because both transistor's bases need to be about 0.6v, but the current gain of 10,000 more than makes up the difference.

That circuit does have one weakness though: the current will change gradually as the photoresistor's resistance changes. You can see the same effect in the LED setup you have. The LED fades in and out as the light changes. The circuit above will do the same thing with a hundred times as much current.

You can improve things by putting a PNP transistor in the middle:

fast-change.jpg
fast-change.jpg (15.77 KiB) Viewed 1436 times
A hundred times as much current will flow through the PNP's collector as through its base, so the voltage across the 330 ohm resistor between the PNP and the PN2222 will change about a hundred times as quickly as the voltage across the 330 ohm resistor tied to the PNP's base. As a result, the PN2222 will turn on and off much more quickly than the output transistor in the previous circuit.

The PN2222 is an NPN transistor by the way, despite its name. I've called it out specifically because it can handle up to 1A through its collector.

I'd personally make one more change to the circuit though:

hysteresis.jpg
hysteresis.jpg (16.82 KiB) Viewed 1435 times
The 100k resistor between the PNP's collector and the sensor input creates a weak positive feedback loop, and gives the circuit a property called 'hysteresis'.

The first transistor turns on when the voltage between the 47k resistor and the photoresistor rises above about 0.6v. When that happens, the voltage at the top of the 330 ohm resistor tied to the PNP's collector also rises. When that happens, the 100k resistor sends even more current into the first transistor's base, turning it on even harder. That raises the voltage at the PNP's collector even more, sending even more current through the 100k to the input, and so on.

The process forces a fast turn-on as soon as the input rises above the level necessary to get the loop started.

It does something else though.

When the first transistor is turned off, the 100k resistor is more or less in parallel with the photoresistor (through the 330 and 10k resistors to GND). That pulls the voltage at the bottom of the 47k resistor a bit lower. When the first transistor turns on and the positive feedback loop takes over, the 100k resistor is more or less in parallel with the 47k (through the PNP's collector to VCC). That pulls the voltage at the bottom of the 47k resistor a bit higher.

That voltage gap between 'pulling a little lower' and 'pulling a little higher' makes the trigger less sensitive to noise near the turn-on/turn-off level.

Putting numbers on it, the voltage between the 47k resistor and the photoresistor will reach 0.6v when the photoresistor's value is about 9k. If we put the 100k resistor in parallel with that, the photoresistor's value can go a bit higher (to 9.9k) before the transistor turns on.

When the circuit turns on and puts the 100k in parallel with the 47k, the parallel resistance is about 32k. To pull the voltage at the midpoint down below 0.6v, the photoresistor's value has to fall to about 6.2k.

In other words, the photoresistor's value has to rise above 9.9k to turn the circuit on, but then has to fall below 6.2k to turn it off. Once the circuit turns off, the photoresistor has to rise above 9.9k to turn it on again. The on/off values aren't at the same point any more, so the circuit can't flicker back and forth if the photoresistor hovers between 8.99k and 9.01k.

That separation of on/off values is called 'hysteresis'.

adafruit_support_mike
 
Posts: 64815
Joined: Thu Feb 11, 2010 2:51 pm

Re: Using Photo Resistor Cell and Transistor to turn on/off

by CaseyScalf on Thu Oct 15, 2015 2:57 am

This is SO THOROUGH!

I am so stoked to finally have a better understanding about the circuit. Especially the solving the flickering on and off at the end. I am very excited to dig in and try this out. Thank you so much for the schematics as well. This is going to be a fun project. I will try this out tomorrow morning!

This is also why I really enjoy buying from Adafruit. The support is top notch!

CaseyScalf
 
Posts: 134
Joined: Wed Nov 05, 2014 3:07 am

Re: Using Photo Resistor Cell and Transistor to turn on/off

by CaseyScalf on Sun Oct 18, 2015 7:03 pm

It worked so well! I was blown away!

Thank you so much I!

I was wondering, what parts of this circuit could allow, tuning? As in the threshold for light and dark?

In all actuality I'll be looking at adding an IR sensitive cell in there. How might I look at this circuit as far as tweaking it goes? What can be changed what needs to stay essentially?

CaseyScalf
 
Posts: 134
Joined: Wed Nov 05, 2014 3:07 am

Re: Using Photo Resistor Cell and Transistor to turn on/off

by adafruit_support_mike on Mon Oct 19, 2015 3:38 am

CaseyScalf wrote:I was wondering, what parts of this circuit could allow, tuning? As in the threshold for light and dark?

The 47k resistor controls light sensitivity. Replacing the it with a higher value will make the sensor trip at a darker light level. Replacing it with a lower value will make the sensor trip at a brighter light level.

The 100k feedback resistor controls the separation of on/off values. Using a higher value moves the on/off thresholds closer together. Using a lower value moves them farther apart.

CaseyScalf wrote:In all actuality I'll be looking at adding an IR sensitive cell in there. How might I look at this circuit as far as tweaking it goes? What can be changed what needs to stay essentially?

First, make sure the IR sensor sees what you want it to. The whole point of IR security cameras is that dark-with-strong-IR and dark-with-no-IR look exactly the same to the human eye.

Other than that, the 47k resistor and photoresistor are the 'sensor' part, and everything past that is the 'convert an analog value to a digital value' part. If your IR sensor works the same way as the photoresistor, you can just swap those components.

adafruit_support_mike
 
Posts: 64815
Joined: Thu Feb 11, 2010 2:51 pm

Re: Using Photo Resistor Cell and Transistor to turn on/off

by CaseyScalf on Mon Oct 19, 2015 4:34 am

Very awesome. I was just playing around with that. I ended up replacing the 47k with a 68k. I can imagine how a potentiometer would work nicely for fine tuning!

I also have a power supply where I can switch voltages. I noticed the sensitivity affected when I went to higher voltages. This is really nice to know I can scale this circuit up to turn on a Arduino or other electrical project!

And the width separator is key as well. That's a very nice place to dial in.

So, about that IR Photocell. I would like to choose this because I am worried that a street light may affect if it's in a window that faces the street. When I search for "Infrared Photocell" I don't see really any IR version of the CDC Cell seen here on Adafruit and elsewhere. I see some photo transistor but that's about it. Is that expected? Could the first transistor in the circuit be replaced with a "photo transistor"?

Thanks once again!

CaseyScalf
 
Posts: 134
Joined: Wed Nov 05, 2014 3:07 am

Re: Using Photo Resistor Cell and Transistor to turn on/off

by adafruit_support_mike on Tue Oct 20, 2015 2:45 am

You'll need to leave all the transistors where they are, because a phototransistor doesn't have a connection for its base.

Instead, the phototransistor exposes the silicon to light, and the photoelectric effect produces the current that controls the base. You don't get much photoelectric current, so most phototransistors can only send about 10mA through their collectors. They're great as sensors, but you want to connect them to a following stage that does the heavy lifting.

Silicon is naturally sensitive to IR, so you get that part for free.

Also be aware of the difference between the way humans sense light and the way electronics sense light.

Our eyes respond logarithmically, which means we see ratios of light intensity rather than absolute levels. "Twice as many photons" looks like the same increase in brightness (and barely visible) whether you're stepping from 10 photons to 20, or 10e6 to 20e6. As a result, we can see across an amazing range of light conditions (easily a hundred million to one).

Silicon responds to absolute light levels, so they can respond to changes we don't even see. The streetlamp outside may look bright to you, but the absolute light level will be far below the level of your shadow at midday.

A phototransistor's off-resistance is about 10M, so you can use resistors up to that value to control the sensitivity. With a bit of playing, you'll probably find a value near 330k that considers a moderately lit room 'bright' and a room lit only by the streetlamp 'dark'.

adafruit_support_mike
 
Posts: 64815
Joined: Thu Feb 11, 2010 2:51 pm

Re: Using Photo Resistor Cell and Transistor to turn on/off

by CaseyScalf on Tue Oct 20, 2015 3:24 am

Excellent once again. I'm glad this circuit can be tweaked in a few different ways.

For reference's sake, here are the IR Phototransistors I ordered: http://www.ebay.com/ulk/itm/151062986492

In any case, where exactly should I out this? In place of the photo cell is what I'm thinking.

CaseyScalf
 
Posts: 134
Joined: Wed Nov 05, 2014 3:07 am

Re: Using Photo Resistor Cell and Transistor to turn on/off

by adafruit_support_mike on Wed Oct 21, 2015 1:24 am

Put the phototransistor where the CdS sensor is now. Emitter goes to GND, collector goes to the 47k resistor and the base of the first NPN.

adafruit_support_mike
 
Posts: 64815
Joined: Thu Feb 11, 2010 2:51 pm

Re: Using Photo Resistor Cell and Transistor to turn on/off

by CaseyScalf on Thu Oct 22, 2015 5:27 pm

Perfect, I was imagining so!

I just received them. I will try that soon!

CaseyScalf
 
Posts: 134
Joined: Wed Nov 05, 2014 3:07 am

Re: Using Photo Resistor Cell and Transistor to turn on/off

by CaseyScalf on Thu Dec 03, 2015 6:20 pm

This has been working like a gem.

I uploaded a Fritzing Sketch to make it clear for others.

Thanks again!

http://fritzing.org/projects/day-night-photo-cell-switch

Image

CaseyScalf
 
Posts: 134
Joined: Wed Nov 05, 2014 3:07 am

Re: Using Photo Resistor Cell and Transistor to turn on/off

by CaseyScalf on Fri Dec 11, 2015 3:32 pm

How efficient is the circuit above? The one that was developed in the schematic above?

I am running it with the system described in this thread: https://forums.adafruit.com/viewtopic.php?f=47&t=85614

I am getting about a one volt drop through it. 5.05v at the start and about 4.1v at the end. This runs the NeoPixels fine but it throws off the PIR sensor.

I thought about bypassing the regulator on the PIR to allow 3v operation instead of needing 5v as described here: https://forums.adafruit.com/viewtopic.php?f=19&t=45260
But that looked rather difficult to solder and I'd like to head off the problem further upstream is possible.

Is there a way to tweak the light sensing circuit so that it is more efficient?

I also looked into using a photo cell and sleep mode. But that seemed like it would take up a good amount of energy if you think it might need to sleep for 12 hours. I.e. ca. 35ma x 12hrs = 420ma sleeping.

Thanks again for the support

CaseyScalf
 
Posts: 134
Joined: Wed Nov 05, 2014 3:07 am

Re: Using Photo Resistor Cell and Transistor to turn on/off

by adafruit_support_mike on Sun Dec 13, 2015 5:03 am

CaseyScalf wrote:I am getting about a one volt drop through it. 5.05v at the start and about 4.1v at the end. This runs the NeoPixels fine but it throws off the PIR sensor.

Where are you taking the measurement?

CaseyScalf wrote:Is there a way to tweak the light sensing circuit so that it is more efficient?

The term 'efficiency' doesn't have a clear meaning in this context.

As drawn above, the circuit should only draw current through the 47k resistor when the output is shut off. If the transistors are shut off, the only paths from VCC to GND are through the light sensor and through the 100k-330-10k string. From a 5v supply, those should draw well below 1mA.

adafruit_support_mike
 
Posts: 64815
Joined: Thu Feb 11, 2010 2:51 pm

Re: Using Photo Resistor Cell and Transistor to turn on/off

by CaseyScalf on Tue Dec 15, 2015 2:41 pm

Let me gather some notes and illustrate this idea more clearly.

CaseyScalf
 
Posts: 134
Joined: Wed Nov 05, 2014 3:07 am

Re: Using Photo Resistor Cell and Transistor to turn on/off

by CaseyScalf on Tue Dec 22, 2015 5:08 pm

I figured if I drew up the diagram it would help us visualize the workflow.

On the top is the system that makes use of the awesome light sensor circuit we have discussed in this thread.
Below it is a version in which the Arduino itself does the light sensing.

My problem is that when I use the light sensing circuit the Powerboost 1000C will give me a low battery warning when it tries to run even a few pixels. The animation then gets stuck and has to be manually reset. I've also noticed that when booting up at night the NeoPixels are random colors and need to be cleared. I think this is requesting too much current for the night and day circuit to handle...

Do I need to reverse the order of the Powerboost and the Night and day circuit?

I read the light sensing circuit. with a voltage meter. It will reliably put out whatever voltage is coming through. But when it is in the scenario where the Powerboost is throwing the low battery signal and the system freezes the voltage will drop 1 volt after running through the night and day circuit.

My easy fix was to have the Arduino just read the light and run the code when it is dark. This is great. But when fully drained the battery has a hard time climbing out of the hole if the sun is providing slightly less current than the ca. 50ma the Arduino consumes. It gets into that kind of hysteresis loop where it's enough to turn on but drains what little energy was there to begin with too quickly.

Here is a picture. I simplified a few things but it gets the essence across.

Thank you for all the help and wisdom!
Image

CaseyScalf
 
Posts: 134
Joined: Wed Nov 05, 2014 3:07 am

Please be positive and constructive with your questions and comments.