Wednesday, September 17, 2014

Photonics at home using Arduino with regular led lights diy


Photonics to make a led light sensor using Arduino Uno diy

I am making a device to measure the RPM of a spinning part by using Photonics. I want to measure the RPM using two different colors on the spinning part. I have an Arduino kit that have a Photo resistor to measure light but because of the slow measure capabilities. I need to have something that measure faster and is also cheap to build. I have looked at Photodiodes, this would be a good alternative but I like my solution for now. where I can measure the RPM using a Led light and don't have to buy a Photodiode.

First things first, this is how I build the photonic Led Sensor.

If you have an Arduino photonics is easy to do, hook up a Led (red or blue are the best to use but white or yellow or green will work to). in my case. I read on the internet several led sensor examples but they where a bit limited. They want you to turn off internal pullup resistors for it to work. They also clame that not al led light will work. Hook up your Led anode with a 220 ohm resistor on a Output pin for example  pin = 2 and the cathode directly on a Analog input Pin = A5. (of cource use a breadboard)



Method of measuring Light using a Led.


1. First charge both anode and cathode to charge the Led. (not at the same time then you short circuit).
2. Second discharge the Led and start measure the time it takes to discharge. (this happens in microseconds!)
3. Third Smooth out the sensor readings (time to discharge) other wise is will fluctuate to much. to define switch states later for use of switching things on and off.
4. Fourth Print out sensor Value

Watch the video: the led also switch off when it picked up the light from the flashlight. (blinking of the led is not visible to the naked eye. frequency is to high. but on film it blinks. I have wrote a few functions that make the Led a  Light Sensor and Build it, Hack it and Share it.

This is the code have fun.....

Let me know what you have build with it!


code:

int aPin = 2;
int cPin = A5;
int counT = 0;

// smoothsensor
const int numReadings = 10;
int readings[numReadings];      // the readings from the analog input
int index = 0;                  // the index of the current reading
int total = 0;                  // the running total
int average = 0;

 //#smooth sensor


void setup(){

 Serial.begin(9600);  

// smoothsensor array
for (int thisReading = 0; thisReading < numReadings; thisReading++)
     readings[thisReading] = 0;         
}
// #smoothsensor array



void loop(){
  chargePulse();
  measureDischarge();
  smoothSensorreading();
  switchLed();
  Serial.println(average);
}



void chargePulse(){
  pinMode(aPin, OUTPUT);
  digitalWrite(aPin, HIGH);
  digitalWrite(cPin, LOW);
  delay(3);  // delay(1) = makes led also electrostatic sensitive
  digitalWrite(aPin, LOW);
  digitalWrite(cPin, HIGH);
}



void measureDischarge(){
  pinMode(cPin,INPUT);
  digitalWrite(cPin,LOW);
  counT = 0; 
  delayMicroseconds(500);       
  while(digitalRead(cPin)!=0) {
     counT++;
  delayMicroseconds(10);
  }
}



void switchLed() { 
if (average > 600){
  pinMode(aPin, OUTPUT);
  pinMode(cPin, OUTPUT);
  digitalWrite(aPin, HIGH);
  digitalWrite(cPin, LOW);
  }
else {
  digitalWrite(aPin, LOW);
}
}



void smoothSensorreading() {
   // subtract the last reading:
   total= total - readings[index];        
   // read from the sensor: 
   readings[index] = counT;
   // add the reading to the total:
   total= total + readings[index];      
   // advance to the next position in the array: 
   index = index + 1;                   

   // if we're at the end of the array...
   if (index >= numReadings)             
     // ...wrap around to the beginning:
     index = 0;                          

   // calculate the average:
   average = total / numReadings;        
   // send it to the computer as ASCII digits
  // Serial.println(average);   /////copy this line to void loop()
   delay(1);        // delay in between reads for stability            
}



No comments:

Post a Comment