Voting resources, early voting, and poll worker information - VOTE. ... Adafruit is open and shipping.
0

Issue with using an LED as a sensor (Pi 4 - Python)
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Issue with using an LED as a sensor (Pi 4 - Python)

by Tyler_H on Mon Jun 29, 2020 4:28 pm

Hi!

I've been trying to get a simple proof-of-concept functioning for a larger project I'm working on. The idea is to use an LED as a light intensity sensor. I know that photocells exist for this purpose, but I would like to use an LED because of somewhat unrelated reasons described at the bottom of the post.

The basic principle is that an LED acts as a capacitor when reverse-biased and will discharge at a rate proportional to the intensity of light hitting it (this fact surprised me quite a lot when I found out!). Here are some references I read discussing this phenomena and using it in projects:


I designed a simple circuit to test this capability, but ran into some peculiar issues which I'm hoping someone would be kind enough to help out with. For starters, I will show the schematic and physical circuit below:

Image
Image

Here is the code I have written for this project. It produces the three states shown in the first image of this post, then saves the discharge time and repeats this process some number of times to find an average:

Code: Select all | TOGGLE FULL SIZE
import RPi.GPIO as GPIO
import time

# Initial Setup
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)

# Settings
pin_A = 8
pin_B = 10
initial_discharge_time = 2 # Time during which LED is lit
charge_time = 0.1 # Time during which LED is reverse-biased
sample_rate = 0.00001 # Time interval between samples
total_samples = 10
# First few samples are consistently different than the rest therefore
# The first sample to count towards average is define below
first_sample_count = 3 # First sample number to begin recording average

# Main Loop
total_discharge_time = 0
for i in range(total_samples):
   
   # A High - B Low - LED On (Discharging)
   GPIO.setup(pin_A, GPIO.OUT, initial=GPIO.HIGH)
   GPIO.setup(pin_B, GPIO.OUT, initial=GPIO.LOW)
   time.sleep(initial_discharge_time)

   # A Low - B High - LED Off (Charging)
   GPIO.output(pin_A,GPIO.LOW)
   GPIO.output(pin_B,GPIO.HIGH)
   time.sleep(charge_time)

   # A Low - B Input - Sampling Time Until B is Low
   GPIO.setup(pin_B, GPIO.IN)
   
   pin_B_high = True
   time_pin_B_high = 0
   while pin_B_high:
      pin_value = GPIO.input(pin_B)
      if pin_value == 1:
         time_pin_B_high += sample_rate
         time.sleep(sample_rate)
      elif pin_value == 0:
         pin_B_high = False
         if i >= first_sample_count:
            total_discharge_time += time_pin_B_high
         print('Discharge Time: %f ' % time_pin_B_high)

# Find average time for discharge
average_discharge_time = total_discharge_time / (total_samples - first_sample_count)
print('Average Discharge Time: %f' % average_discharge_time)


There are a few problems which occur.
1.) The discharge time is 0 when using the 10k ohm drop-down resistor. I've tried using a 500k ohm resistor as well but the discharge time was still 0.01 ms at most and this time didn't change at all when the LED was subjected to relatively bright light. Pulling the drop-down resistor out (Apparently the GPIO pins have built-in drop-down resistors anyways) resulted in larger discharge times but this leads to problem 2.
2.) The discharge time is relatively inconsistent and doesn't seem to correlate to the intensity of light the LED is subjected to in any meaningful way. Below is an image of 20 samples with no light as well as 20 samples with intense light:
Image
3.) The circuit is extremely sensitive to electric fields (or magnetic?). Placing my hand over the circuit to block the light resulted in extreme changes in the discharge time, but I realized it wasn't due to blocking the light because blocking the light with a piece of black shrink tubing doesn't affect the discharge time at all.

I know this is a lot to ask so I'm really not expecting much but if anyone has any insight they wouldn't mind sharing, it would be greatly appreciated! Additionally maybe this will be a helpful resource for people doing a similar project in the future. Thanks!

Also on an unrelated (but also related note), the reason I wanted to use an LED instead of a photocell is because I need to measure light intensity as a function of position (with relatively high precision). The LED arrays sold by Adafruit such as the one below would make this trivial assuming I could get the above problem figured out. If anyone has a recommendation on an alternative method for my measurements, that could also be useful. Thanks again for any help you can offer!

Image
Source

Tyler_H
 
Posts: 1
Joined: Mon Jun 29, 2020 2:10 pm

Please be positive and constructive with your questions and comments.