Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 5 of 5

Thread: LED as a light sensor

  1. #1
    Junior Member
    Join Date
    Nov 2016
    Location
    Brussels
    Posts
    3

    LED as a light sensor

    Hello,

    I have a Teensy 3.1 with two I/O pins driving an LED. I want to use the LED as a light sensor, and for
    that I am using the following typical scheme:

    Code:
         /* this runs in the loop() */
    
         /* LED lit for 1 ms */
        pinMode(LED_ANODE,OUTPUT);
        pinMode(LED_CATHODE,OUTPUT);
    
        digitalWrite(LED_ANODE,HIGH);
        digitalWrite(LED_CATHODE,LOW);
        delay(1);
    
        /* LED charging */
        digitalWrite(LED_ANODE,LOW);
        digitalWrite(LED_CATHODE,HIGH);
        delay(1);    
    
        /* count LED discharge time */
        pinMode(LED_CATHODE,INPUT);
        digitalWrite(LED_CATHODE,LOW);
    
        j = 0; 
        while(0 != digitalRead(LED_CATHODE) && j < 10000)
       {
           j++;
       }
    The problem is that the LED is not discharging through the LED_CATHODE pin, even if I wait longer in the while loop.
    I suspect this has to do with the input pin impedance, which is probably too high.

    I tried to search the forum for a similar project, but didn't find it. So, is it feasible to use a LED and a Teensy 3.1 as a light sensor ?

  2. #2
    Senior Member+ manitou's Avatar
    Join Date
    Jan 2013
    Posts
    2,620
    Take a look at https://forum.pjrc.com/threads/28407...l=1#post116332
    and the follow up posts. It is my guess that MCU's with 5v-tolerant pins don't discharge as hoped ...

  3. #3
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    23,968
    The 5V tolerant pins have a slight pullup leakage current when configured to INPUT mode.

    You could try INPUT_PULLDOWN, but that will probably destroy any chance of measuring the effect.

    You might be able to get this to work using one of the analog pins. You'll need to write a zero directly to the pin config register to turn off the input buffer (and thus the slight pullup current). Then you can use analogRead to detect the changing voltage.

    To get it to run fast, use analogReadResolution(8) and analogReadAveraging(1).

  4. #4
    Junior Member
    Join Date
    Nov 2016
    Location
    Brussels
    Posts
    3
    Thanks for the tips.
    I am going to try the pin analogue solution sometime this week and will report back them.

  5. #5
    Junior Member
    Join Date
    Nov 2016
    Location
    Brussels
    Posts
    3
    I tried to used the ADC to read the LED pin, but it wasn't reliable enough. I could only get it to distinguish between complete darkness and full light, but the thresholds depended on the LEDs being used.
    Because this was not an important feature for my project, I will drop it.

    Thanks again for the help.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •