2
\$\begingroup\$

I've done small circuits for a long time, although nothing more than a few basic ones. I am now trying to control an IO-signal to Raspberry Pi. Since Raspberry Pi doesn't have analog pins, I came across this circuit when researching:

Circuit

Source: peppe8o - Using Photoresistor From Raspberry PI To Detect Light

I get the underlying idea, that using the photoresistor as part in a voltage divider, so when it's bright, the base of the transistor gets pulled to ground and GPIO 14 will read 1, and in the dark current will flow from collector to emitter which pulls GPIO 14 low and it reads a zero.

My transistor is a PN2222A, and my photoresistor ranges from 4 kΩ to 7 kΩ (I have another with a larger range, from 18 kΩ to 50 kΩ). But when I try to put the circuit together, I always read the same value on pin 14. I've tried to swap the resistors, but I feel that I lack the knowledge how to calculate the right values for the resistors and tune the circuit properly. I know it in part depends on the strength of the light, but can someone help me in the right direction?

\$\endgroup\$
1
  • \$\begingroup\$ Ljuslykta - Hi, (a) As you're new here, please see the tour & help center for the main rules. (b) Please note the site rule which requires that when a post includes content (e.g. text, image, photo etc.) copied or adapted from elsewhere, that content must be correctly referenced. As a minimum, the source webpage or PDF etc. should be linked (see that rule regarding references for books / articles etc.). In order to help you, I found what I believe to be the source PDF link & added it for you. For the future, please remember it's your responsibility to do that :) Thanks. \$\endgroup\$
    – SamGibson
    Sep 11, 2023 at 21:01

3 Answers 3

1
\$\begingroup\$

If I were you, I'd use a different circuit. Instead of using a discrete BJT, use a comparator IC. A comparator IC will clamp your output either high or low.

Consider using something similar to the circuit below:

LDR Comparator circuit

Source: Electronics Hub

This circuit compares the voltage at the photoresistor (LDR) with a voltage set with a potentiometer. The voltage divider created with the potentiometer can be tuned to adjust your light sensitivity. When the voltage at the LDR is higher than the voltage at the potentiometer, the output is high. The output is low when the opposite is true.

Note, this circuit must be adapted to work with your Pi, so be sure to use a power supply of 3.3V (the circuit is using 9V) and drive your IO pin instead of an LED.

I think you'll find this solution much more intuitive than trying to calculate the correct DC biasing needed for your discrete BJT circuit.

\$\endgroup\$
2
  • \$\begingroup\$ Since my ambient light might change, I feel that this gives me the headroom to tune the circut depending on the surroundings. Do I need to calculate new resistances or will the ones shown above work when changing the supply voltage? \$\endgroup\$
    – Ljuslykta
    Sep 12, 2023 at 16:51
  • \$\begingroup\$ Those values should be fine. Only one you could consider changing is the resistor forming the divider with the LDR. If your LDR has a resistance much higher than 10k, you could consider increasing that resistor. \$\endgroup\$ Sep 12, 2023 at 20:59
1
\$\begingroup\$

That circuit should change its output state if the resistor values are chosen carefully. The most critical resistor is the one from +3.3V to transistor base. Its default value of 100k is too high for the 4k-7k photoresistor.

Aim for an I/O change when photoresistor is half-way: at 5.4k. Although its light-to-resistance is not linear, and temperature-dependent, 5.4k is a starting point for threshold.
The transistor will barely start to turn on when its base voltage is around 0.68V. Consider the two resistors as a voltage divider. What upper resistor R1 will drop (3.3 - 0.68) when lower resistor is 5.4k ? This is a rough calculation that ignores base current of the transistor:
\$ 3.3 \times {{R2}\over{R1+R2}} = 0.68\$

schematic

simulate this circuit – Schematic created using CircuitLab
The collector resistor (R5) is far less critical. Its value could likely be set anywhere between 1k to 50k, since the general-purpose I/O pin doesn't load it down much.

\$\endgroup\$
1
  • \$\begingroup\$ Interesting. I will try this and see how robust it is compared to ambient changes or how much I will need to block unwanted light. \$\endgroup\$
    – Ljuslykta
    Sep 12, 2023 at 16:54
1
\$\begingroup\$

Use two transistors in the Darlington pair configuration. This will both increase gain and raise the threshold voltage (~1.4V, 2 B-E diode drops: 2 x 0.7V). Increasing the gain makes the circuit "snappier". In other words, it gives you a sharper edge on what's considered on and what's considered off when the transistors pass between cutoff (off state) and saturation (on state).

Strictly speaking, the circuit is technically slower (lower bandwidth) using 2 transistors like this instead of one, but based on you application (driving a GPIO of a Pi), this is orders of magnitude faster than what you will need. I needed to include this mention to inhibit the analog guys from roasting me.

The other benefit you get is now your "trigger" voltage is conveniently half of VCC or 1.4V. And 1.4V is basically half of 3.3V (close enough for what we're trying to do here.

The collector resistor doesn't matter so much; it's simply a pull up for Pi input. Rule of thumb: lower value resistor = faster, higher value resistor = lower current draw. Considering your light sensor is terribly slow, I wouldn't worry about speed. And The Pi is going to dominate the power consumption so I wouldn't optimize that either. Instead, choose the resistor so that you get something like 10mA - 30mA when the transistor pair is "on". So without getting fancy, it's going to be 3.3V/20mA = 165 ohms or 180 or even 220 ohms as popular values. That handles your pull -up resistor.

Now, you need to select first transistor's base resistor. This one is important because it sets your operating point. You say your LDR goes from 4K to 7K. Assuming the resistance of the LDR is proportional to the incident light on the active surface (Which it's not. They tend to be more sensitive in the dark regime of their dynamic range. Hook it up to multimeter and screw around with different light intensities and you will get an intuitive feel about what I'm talking about. You might want to do this anyway to get a more accurate operating point than what I'm about to explain). Take half the resistance in the dynamic range. In you case that's going to be MEAN(4k,7k) or 5k5. Or, 5k6 which is popular. Depending weather you want a logic "1" or a logic "0" going into the PI when the LDR is in light, you tie one end of the resistor to VCC or GND (and consequently, the LDR to the complement power polarity).

This analysis works because we're assuming the threshold voltage of 1.4V is VCC/2, which is not quite true (1.65V vs 1.4V). But I knew you had enough change in resistance that this won't matter in first order. Also, I'm assuming the input impedance of the base-emitter junction is infinite which is also not true (that part is almost true when it's unbiased, but not when it's conducting). For this application, the impedance doesn't really matter for what you are trying to do.

You may also need to add a high value resistor (10's of kohms to low 100's k ohms) from the emitter of the first resistor to GRD. This has the effect of reducing sensitivity (gain) and adding control (it basically makes sure the transistor stays off and bleeds down any leakage and charge that may accumulate causing weird behavior. Also helps with mitigating interference in a noisy environment.).

TLDR; Create a voltage divider with your LDR and "other" resistor such that when the LDR is at the operating point, its resistance is stacked with the "other" resistor so that the voltage divided by these 2 resistors equals the B-E voltage drop of 1 or 2 transistors, respectively.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.