1
\$\begingroup\$

I am trying to change a design that I have on a PCB (Raspberry Pi 4/5) hat from a photoresistor to a phototransistor. The RPi has only digital input and no ADC inputs. So I need to transform the values from analog to a timed based system that I can detect.

I am using a capacitor as a "battery" that I fill with a GPIO signal (3.3V) and then time how long it takes to "deplete" (technically to 1.6V) and this gives me a very usable value for the light intensity. I don't need anything more precise than that (It's used to dim the screen gradually in low light conditions and so not an on/off switch). It works very well with the photoresistor, but it's not an SMD component and can't easily (cheaply) be mass produced.

I am tying to move to a phototransistor. However, those give a variable amount of current and not resistance. I am trying to find a suitable circuit that translates that to a similar design (hard to redesign the whole layout) and I don't want to use another IC for A/D conversion as it's still hard to read with a single digit pin.

Here's a circuit I came up with, but I'm not sure it will work. The idea is that once the capacitor is filled, the rate of decrease is changed by how much current the phototransistor emits. As there are significant costs to make test PCBs to see it the design is correct, I would love some expert advice on this design or anything similar using only passive components.

schematic

simulate this circuit – Schematic created using CircuitLab

Suggested Circuit

My main concern is knowing if this circuit will "drain" the capacitor correctly once the GPIO pin goes to input mode (like my photoresistor design).

If you know a better design, I'm also all ears.

\$\endgroup\$
5
  • \$\begingroup\$ Your application seems non-critical. You might consider using a LED diode as a light detector if cost is of importance, in a similar circuit...(one resistor, one LED, one capacitor). \$\endgroup\$ Commented Jan 5 at 17:39
  • \$\begingroup\$ Can you give an example part. I don't want an on/off switch but a gradual range. \$\endgroup\$ Commented Jan 5 at 17:42
  • \$\begingroup\$ If you are talking about photodiodes, my understanding is that they are used for high speed transmission (too fast for my software) and need amplification to really register. The phototransistor has built-in amplification and actually slower response time which is what I need for human interaction. \$\endgroup\$ Commented Jan 5 at 17:53
  • \$\begingroup\$ The advantage of a photodiode is that it's much more linear so it's easier to predict the actual brightness. You can charge a capacitor with or without gain, you'll just need a smaller capacitor with less gain. Your circuit has no way to discharge the capacitor while in input mode, but you can reset to zero by switching to output and sinking the accumulated current through the pin. Note that 1 uF is pretty big, might want to limit current into the GPIO. \$\endgroup\$ Commented Jan 5 at 18:27
  • \$\begingroup\$ "Your circuit has no way to discharge the capacitor while in input mode" Yes, that was my main fear. I think I need a parallel resistor to the C for that purpose (it's actually in the datasheet as a way to measure Voltage). No idea how the rest of the design will behave. \$\endgroup\$ Commented Jan 5 at 19:48

2 Answers 2

1
\$\begingroup\$

One might use a photodiode, or even a light emitting diode as an ambient-light detector. A silicon photodiode with clear lens likely has peak wavelength response in the infrared region, less sensitive to blue.

Of concern is leakage current of RPI's GPIO pin, which can't be distinguished from photodiode current that discharges the capacitor. RPI-5 specifications of GPIO pins seem unobtanium. How leakage changes with temperature might also be of interest - low temperatures tend to reduce leakage current of both photodiode AND GPIO.

schematic

simulate this circuit – Schematic created using CircuitLab
Using a different, older PIC processor - this circuit arrangement could effectively distinguish light levels from very dim room light, to sunlight, using a 3mm red LED as detector. For very high light levels, substitute a larger capacitor.
The GPIO is set for output, raised to logic high so as to charge C1 to Vdd. Then GPIO is switched to input and a counter started. When GPIO falls to logic low, counter is stopped. A large count indicates low light level.

The PIC data sheet specified GPIO leakage current as:

  • +/-5 nA @ 85 degrees C.(typical)
  • +/- 125 nA @ 85 degrees C. (maximum)

Leakage current limit was determined by noting at what light level the LED diode was unable to discharge the capacitor to achieve a valid logic low...street light entering a window at night could still be detected. It would seem that the PIC leakage current at room temperature was far below the spec listed above.

It seems one would have to do this kind of leakage-current test with RPI-5 since no leakage spec is available.

schematic

simulate this circuit


A phototransistor would have less trouble with RPI-5 leakage currents. A larger capacitor is required since photo current is much higher than a photodiode. However, a phototransistor has leakage current of its own, which is also temperature-depenedent.
At very high light levels, GPIO might have difficulty dragging transistor collector up to logic high, since GPIO must not only charge the capacitor, but must buck photocurrents.

\$\endgroup\$
0
\$\begingroup\$

Ok.. I gave up trying to make this work with passive devices.

I took the expert advice and chose an I2C ADC chip that takes very low currents and measures it and send out the result on the I2C bus when requested. It's way overkill for what I want to do, but simpler in electronics and software. I already have a bunch of devices on the I2C bus and my rust code seems to handle it pretty well. (It's a TI chip - ADC081C027CIMK/NOPB) I've ordered a new PCB and should receive it soon (with extra costs with the new tariffs).

If anybody is interested, let me know and I'll put the outcome and the circuitry here

\$\endgroup\$

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.