We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpElectronics,  Serial Library › LED as sensor through Arduino
Page Index Toggle Pages: 1
LED as sensor through Arduino (Read 5028 times)
LED as sensor through Arduino
Feb 15th, 2009, 3:06pm
 
I found a great script for using a LED as photovaltic sensor. Its here below. I have tried to translate that into processing, but something seems to go wrong.

As you can tell I'm pretty new at this.


// ARDUINO

//TOUCH SENSING BETA - LED TURNS ON WHEN LIGHT IS PRESENT
//BY: RICARDO DE LEMOS 1/17/2007
int led1 = 13;
int cath = 2; // negative
int ando = 3;  // positive

void setup()
{
  pinMode(led1, OUTPUT);
  pinMode(cath, OUTPUT);
  pinMode(ando, OUTPUT);
  Serial.begin(9600);
}

void loop()
{
  //REVERSE BIAS
  digitalWrite(cath, HIGH);
  digitalWrite(ando, LOW);
  //CHARGE LED
  delay(6);
  //READ LED CAP
  pinMode(ando, INPUT);
  delay(6);
  //WRITE TO LED
  if (digitalRead(ando) == LOW)
  {
    digitalWrite(led1,HIGH);
  }
  else
  {
    digitalWrite(led1,LOW);
  }
  //RESET
  pinMode(ando, OUTPUT);
}

And the translated version for Processing.

//Processing


//biblio

import processing.serial.*;

import cc.arduino.*;

Arduino arduino;

//variabler

int turnOnLED = 13;

int sensorP = 3;

int sensorN = 2;

//setups

void setup(){
 
 size (500,500);
 
 println(Arduino.list());
 
 arduino = new Arduino(this, Arduino.list()[0], 9600);
 
 arduino.pinMode(turnOnLED, Arduino.OUTPUT);
 
 arduino.pinMode(sensorP, Arduino.OUTPUT);
 
 arduino.pinMode(sensorN, Arduino.OUTPUT);
 
}

 //drawing

void draw(){
 //charging LED
 arduino.pinMode(sensorP, Arduino.OUTPUT);
 arduino.digitalWrite(sensorN, Arduino.LOW);
 arduino.digitalWrite(sensorP, Arduino.HIGH);
 
 delay(6);
 arduino.pinMode(sensorP, Arduino.INPUT);
 if(arduino.digitalRead(sensorP) == Arduino.LOW){
   background(255);
 }
println((arduino.digitalRead(sensorP)));
println((arduino.digitalRead(sensorN)));
}

So what I wanna do is just to make Processing tell me the amount of light there is in my room and react whenever.
Hope somebody sees something I dont. Cheers.
Re: LED as sensor through Arduino
Reply #1 - Feb 15th, 2009, 4:07pm
 
Okay, this is where I am so far. Is there anybody out there that has a clue why the results come back so unstable. The LED reckognizes the ligth, it works so to speak, but only half of the results are true.
Right now Im thinking that LEDs just sucks as sensors....
Hope there are better suggestions out there.

import processing.serial.*;
import cc.arduino.*;
Arduino arduino;
int turnOnLED = 13;
int sensorP = 3;
int sensorN = 2;
int x = 0;
void setup(){
 size (1200,500);
 println(Arduino.list());
 arduino = new Arduino(this, Arduino.list()[0], 115200);
 arduino.pinMode(turnOnLED, Arduino.OUTPUT);
 arduino.pinMode(sensorP, Arduino.OUTPUT);
 arduino.pinMode(sensorN, Arduino.OUTPUT);
}
void draw(){
 arduino.pinMode(sensorP, Arduino.OUTPUT);
 arduino.digitalWrite(sensorN, Arduino.HIGH);
 arduino.digitalWrite(sensorP, Arduino.LOW);
 delay(2);
 arduino.pinMode(sensorP, Arduino.INPUT);
 delay(2);
 println(arduino.digitalRead(sensorP));
 int y = arduino.digitalRead(sensorP);
 noStroke();
 fill(0);
 rect(x, (y*250)+125, 2, 2);
 x = x+1;
 if(x>width){x=0; background(125);}
 if(arduino.digitalRead(sensorP) != 0){
   arduino.digitalWrite(turnOnLED, Arduino.LOW);
}
else{arduino.digitalWrite(turnOnLED, Arduino.HIGH);}
}
Re: LED as sensor through Arduino
Reply #2 - Mar 13th, 2009, 6:53pm
 
I had worked on something similar. The sensor code is from an example I found before... I think on the Arduino site... I don't remember.

Anyway, I used separate code for the board and for Processing. Because the reading is based on how long it takes the LED to 'discharge', a dark room will cause a longer delay in updating the reading.

The Processing sketch simply scales the read value to the display window and shows it as a line plot.

You'll notice that the light will blink when there is less light.


Arduino code:
Quote:
#define LED_N_SIDE 2
#define LED_P_SIDE 5

void setup() {
 Serial.begin(9600);
}

void loop() {
 unsigned int j;

 // Apply reverse voltage, charge up the pin and led capacitance
 pinMode(LED_N_SIDE,OUTPUT);
 pinMode(LED_P_SIDE,OUTPUT);
 digitalWrite(LED_N_SIDE,HIGH);
 digitalWrite(LED_P_SIDE,LOW);

 // Isolate the pin 2 end of the diode
 pinMode(LED_N_SIDE,INPUT);
 digitalWrite(LED_N_SIDE,LOW);  // turn off internal pull-up resistor

 // Count how long it takes the diode to bleed back down to a logic zero
 for ( j = 0; j < 30000; j++) {
   if ( digitalRead(LED_N_SIDE)==0) break;
 }
 
 Serial.println(j);//send reading to Processing

 // Turn the light on for 1000 microseconds
 digitalWrite(LED_P_SIDE,HIGH);
 digitalWrite(LED_N_SIDE,LOW);
 pinMode(LED_P_SIDE,OUTPUT);
 pinMode(LED_N_SIDE,OUTPUT);
 delayMicroseconds(1000);
}





Processing code:
Quote:
// Capacitive LED touch sensor

import processing.serial.*;

Serial port;
String buff = "";
int NEWLINE = 10;
int px = 0;
int peak = 0;
int threshold = 50;

int[] values = new int[512];
int val = 0;

void mouseReleased() {
 px = mouseX;
}

void setup() {
 size(512, 512);
 
 textFont(createFont("Arial", 14),14);
 
 println("Available serial ports:");
 println(Serial.list());
 
 port = new Serial(this, "COM5", 9600);
 
 frameRate(30);
 
}

void draw()
{
 background(54);
 
 // Graph the stored values by drawing a line between them.
 stroke(0,255,0);
 for (int i = 0; i < 511; i++) {
   line(i, values[i], (i + 1), values[i + 1]);
 }
 
 while (port.available() > 0)
   serialEvent(port.read());
   
 stroke(0,128,192);
 fill(255);
 line(px,0,px,height);
 text(values[px],20,10);
 
}

void serialEvent(int serial) {
 //println(serial);
 if (serial != NEWLINE) {
   // Store all the characters on the line.
   buff += char(serial);
 } else {
   
   //println(buff);
   buff = buff.trim();//sometimes gets strange readings
   val = int(buff);
   //println(val);//debug
   
   //Could use peak and threshold for a light change trigger
   //if (val > peak) peak = val;
   
   // Clear the value of "buff"
   buff = "";
   
   // Shift over the existing values to make room for the new one.
   for (int i = 0; i < 511; i++) {
     values[i] = values[i + 1];
   }
   
   // Add the received value to the array.
   values[511] = (int)map(val, 0, 30000, 0, 511);//val;
 }
 
}


Page Index Toggle Pages: 1