Weekend Project – LEDs as Sensors

So I have been wanting to try this out for a while now and I finally got around to doing it this weekend.

The Theory:
Due to the semiconductor properties of LEDs they are capable of detecting light in the same or close to the wavelength they would normally emit. This allows you to use LEDs as a light sensor. Using an Arduino Mega and its analog input pins I was able to make this little circuit which exploits this property. The voltage that is produced is quite small so the accuracy and resolution is not very hight, but it does work quite well.

What it does:
I then hooked up another lead of my RGB-LEDs to a pwm output and made the LED light up according to how much light it is sensing. This allows for some interaction with the circuit instead of just looking at numbers on the screen.

Keep in mind, this was just a quick weekend project, so the code and the circuit are not very elegant or anything.

How the code works:

  • Configure all analog inputs to outputs and pull low to turn on all red LEDs.
  • Turn one analog input pin into an actual Input and read the amount of light that is reflected into it.
  • Turn that input back into an output and continue on to next.
  • After all inputs have been read adjust the PWM of the blue LEDs according to the amount of light that was measured.
  • loop forever…

Here is a short clip demonstrating how it works. Please excuse the flickering. This is due to the LEDs being switched on and off constantly and that interfering with the exposure time of the camera. Also I didn’t realize one of the LEDs wasnt working:

Advertisements

2 thoughts on “Weekend Project – LEDs as Sensors

  1. Sure,
    I’ll just copy paste it here:

    
    const int numLEDs = 10;
    int LEDinput[numLEDs] = { A0, A1, A2, A3, A4, A5, A6, A7, A8, A9};
    int LEDoutput[numLEDs] = { 11, 10, 9, 8, 7, 6, 5, 4, 3, 2};
    
    int sensorValue[numLEDs];
    int offset[numLEDs];
    int height[numLEDs];
    
    int sum = 0;
    
    void setup() {
    
      Serial.begin(115200); 
      
      for(int i=0; i<numLEDs; i++){
        pinMode(LEDinput[i], OUTPUT);
        digitalWrite(LEDinput[i], LOW);
        pinMode(LEDoutput[i], OUTPUT);
        digitalWrite(LEDoutput[i], LOW);
      }
      
      calculate_offset();
    
    }
    
    void loop() {
    
      read_sensors();
      set_LEDs();
      print_output();
    
    }
    
    
    void read_sensors(){
      
       uint16_t sum = 0;
       int val;
    
      for(int i=0; i<numLEDs; i++){ //turn on sensing leds
        digitalWrite(LEDinput[i], LOW);
      }
    
      for(int i=0; i<numLEDs; i++){
        analogWrite(LEDoutput[i],255);
        pinMode(LEDinput[i], INPUT);
        for (uint8_t j = 0; j < 16; j++){
          sum += analogRead(LEDinput[i]);
        }
        sensorValue[i] = sum/8;
        pinMode(LEDinput[i], OUTPUT);
        digitalWrite(LEDinput[i], LOW);
        sum = 0;
        
        height[i] = offset[i]-sensorValue[i];  
        if(height[i]<0)
          height[i] = 0;
      } 
      
      
    }
    
    
    void set_LEDs(){
      int val;
    
      for(int i=0; i<numLEDs; i++){ //turn off sensing leds
        digitalWrite(LEDinput[i], HIGH);
      }
    
      for(int i=0; i<numLEDs; i++){
        digitalWrite(LEDinput[i],HIGH);
        val = map(height[i],0,15,0,255);
        //Serial.print(val);
        //Serial.print("\t");
        
        analogWrite(LEDoutput[i],255-val);
      }
      
      //Serial.println();
    delay(2);
      
    }
    
    
    void print_output(){
      
      for(int i=0; i<numLEDs; i++){
        Serial.print(sensorValue[i]);
        //Serial.print(height[i]);
        Serial.print("\t");
      }
    
    Serial.println();
    
    }
    
    void calculate_offset(){
      
      read_sensors();
      
      for(int i=0; i<numLEDs; i++){
        offset[i] = sensorValue[i];
      }
      
    }
    

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s