how can i do light detection???

well hello!
well i have brought the Arduino board and some microcontrillers for it off ebay so while im waiting for them to arrive im just doing some practice coding :smiley:

im wanting to build a little light detecting solar "robot" as my first project something like this http://www.solarbotics.com/products/k_hm/

but i dont know what is used to detect the light :-[ or the code of how to code it onto Arduino

if someone would be able to tell me what is used and how it is coded or a link to someone that has alredy made

thankyou very much
Callum

You can use a light dependend resistor (LDR), sometimes incorectly called a photocell.

You hook it up as a voltage divider to an analog input. Reading the analog input will return a number i the 0 - 1023 range indicating the resiatamce of the LDR which is an indication of the amount of light hittng the LDR.

Keep in mind that the resiatance of the LDR might not have a linear relationship to the amount of light.

I prefer phototransistors. They're cheaper, have more gain and are much faster.

I prefer phototransistors. They're cheaper, have more gain and are much faster.

is infrared as good or better than them? and do i just use the code from here to use any of them?
http://www.arduino.cc/playground/Learning/LEDSensor

thanks and heres my code sofar... jus the light detecting bit idk if itl work though

//input leds
int led1_p = 13;  //led on top positave
int led1_n = 13;  //led on top negitave
int led2_p = 13;  //front led positave
int led2_n = 13;  //front led negitave
int led3_p = 13;  //rear led positave
int led3_n = 13;  //rear led negitave
int led4_p = 13;  //right led positave
int led4_n = 13;  //right led negitave
int led5_p = 13;  //left led negitave
int led5_n = 13;  //left led positave

//led timer ints
unsigned int led1;  //top led timer
unsigned int led2;  //front led timer
unsigned int led3;  //rear led timer
unsigned int led4;  //right led timer
unsigned int led5;  //left led timer

void setup(){
}

void loop(){
  timer();
  if(led1 > led2 && led3 && led4 && led5){
    digitalWrite(led1_p,HIGH);
    digitalWrite(led1_n,LOW);
    pinMode(led1_p,OUTPUT);
    pinMode(led1_n,OUTPUT);
    delayMicroseconds(1000);
  } else {
    if(led2 > led3 && led4 && led5){
      //front is lighter  move foward
      } else if(led3 > led4 && led5 && led2){
        //rear is brighter move back
        }else if(led4 > led5 && led2 && led3){
          //right is brighter turn right
          }else if(led5 > led2 && led3 && led4){
            //left is brighter turn left
            }else{
              //error!!!!!!!!!!!!!!!
              pinMode(led1_p,OUTPUT);
              pinMode(led1_n,OUTPUT);
              digitalWrite(led1_n,LOW);
                  digitalWrite(led1_p,HIGH);
                  delay(500);
                  digitalWrite(led1_p,LOW);
                  delay(500);
                  digitalWrite(led1_p,HIGH);
                  delay(500);
                  digitalWrite(led1_p,LOW);
            }        
  }
}

void timer(){
  //top led shit
  pinMode(led1_n,OUTPUT);
  pinMode(led1_p,OUTPUT);
  digitalWrite(led1_n,HIGH);
  digitalWrite(led1_p,LOW);
  pinMode(led1_n,INPUT);
  digitalWrite(led1_n,LOW);
  
  //front led shit
  pinMode(led2_n,OUTPUT);
  pinMode(led2_p,OUTPUT);
  digitalWrite(led2_n,HIGH);
  digitalWrite(led2_p,LOW);
  pinMode(led2_n,INPUT);
  digitalWrite(led2_n,LOW);
  
  //rear led shit
  pinMode(led3_n,OUTPUT);
  pinMode(led3_p,OUTPUT);
  digitalWrite(led3_n,HIGH);
  digitalWrite(led3_p,LOW);
  pinMode(led3_n,INPUT);
  digitalWrite(led3_n,LOW);
  
  //right led shit
  pinMode(led4_n,OUTPUT);
  pinMode(led4_p,OUTPUT);
  digitalWrite(led4_n,HIGH);
  digitalWrite(led4_p,LOW);
  pinMode(led4_n,INPUT);
  digitalWrite(led4_n,LOW);  
  
  //left led shit
  pinMode(led5_n,OUTPUT);
  pinMode(led5_p,OUTPUT);
  digitalWrite(led5_n,HIGH);
  digitalWrite(led5_p,LOW);
  pinMode(led5_n,INPUT);
  digitalWrite(led5_n,LOW);
  
  
  //led timers
  
  //top led timer
  for (led1 = 0; led1 < 30000; led1++) {
    if ( digitalRead(led1_n)==0) break;
  }
  
  //front led timer
  for (led2 = 0; led2 < 30000; led2++) {
    if ( digitalRead(led1_n)==0) break;
  }
  
  //rear led timer
  for (led3 = 0; led3 < 30000; led3++) {
    if ( digitalRead(led1_n)==0) break;
  }
  
  //right led timer
  for (led4 = 0; led4 < 30000; led4++) {
    if ( digitalRead(led1_n)==0) break;
  }
  
  //left led timer
  for (led5 = 0; led5 < 30000; led5++) {
    if ( digitalRead(led1_n)==0) break;
  }
  
  
}

thanks guys its nice to come into a forum and not be mocked strait away :slight_smile:

Hi Triffid Hunter

I have no experience with photo transistors, i thought they worked more like "light switches", i mean on / off.

Can they generate a changing voltage like an LDR depending on the amount of light ?

Do they just work as a "light depending amplifier", and then you somehow convert the changing current to a voltage (like an ordinary transistor in "amplifying mode") ?

This is very interesting since a lot of scarp electronics (computer mice for instance) have photo transistors in them, and i love to reuse components from other peoples garbage :slight_smile:

but i dont know what is used to detect the light :-[ or the code of how to code it onto Arduino

Common light sensors include photodiodes, phototransistors, CdS (Cadmium Sulfide) "photocells". They are all commonly available online, or at Radio Shack if you are in the US. LEDs can be used as photodiodes but I would consider it a novelty, not a part of a production device since the manufacturer doesn't guarantee operation that way.

To read the value into Arduino will depend on what you are connecting. With a CdS cell, it is simply a variable resistor. Connect it in series with a resistor and measure the voltage at that junction with an analog pin. Any of the "how to read a potentiometer" guides will show you how to do that.

You can get fancy and buy a light sensor, such as a light to frequency converter. Here is one that can measure the color of light: Light-To-Frequency Sensor

==============

is infrared as good or better than them? and do i just use the code from here to use any of them?

You ask to compare apples to electricity.

Infrared is a wavelength (inverse frequency). It is not a device. Each sensor device will have a sprectrum it responds to. IR, UV, visible, microwave, etc.

======================

I doubt your code will do what you want, but there is so little I can't tell what you are trying to do. All you are doing so far is defining a bunch of variables and stuffing an initial value into them (13). Note: you can't connect multiple LEDs to one pin (13) and read or control them individually just because you gave them different names.

I think you might be trying to do something like this:

// define input pins
int sensor1 = 13;
int sensor2 = 14;
int sensor3 = 15;

Thank you very much for the reply

i will look around some more at the types you suggested the main thing i want to achieve is pretty much turning the input from light into a int so say if its bright the int will be high if its dark the int will be low :slight_smile:

i also found this it seems like itl be helpful :slight_smile:
http://projects.dimension-x.net/technology-and-projects/ledsensors

thanks again for your replys and the reason i didn't define the pins yet is because i don't have the board and don't know what they go up to

cheers
callum

The current through a phototransistor depends on the amount of light falling on it. I use them on my robot's analog ports, and with a 47k pullup to +5v, they read 255 in darkness, <3 in full sun, and usefully different values in between, usually around 130 under the compact fluoro in my room and 160-170 under the incandescents in the lounge room.

I hooked up a bunch of ultrabright (16candela) red leds, and I use the difference in reading between leds on and leds off to detect obstacles. This way, ambient light is completely removed from the picture unless it's saturating the sensor.

ok as youl know better what would you recommend? here is a picture if you didn't understand me previously

so it reads from all the sensors and converts into an int and thinks OOH int 5 is higher than int 1 well rotate left

thanks so much guys im still waiting on the board and probably will be for a couple of weeks so i want to get as much learning crammed in as possible

thanks again
callum

I see what you are trying to do. An anti-cockroach. (seeks light rather than avoids light) That is fine. Great starter project. (What is sensor 1 for? Will it go up too?)

But since you need to learn electronics, programming, and how to build a chassis and motorize it, I would strongly advise you to use known and documented parts rather than trying to learn and use a sub-optimal solution.

i also found this it seems like itl be helpful :slight_smile:
Welcome to your SWAG instance

That isn't helpful. It is unneccessary complication. As Triffid Hunter & I pointed out all you need is the detector device, a resistor, and an analog input pin. {Just do it.}

+5V----/////-------detector------gnd
|
|
input pin

The value of R should be much higher than the low resistance state of the device (whether that be lit or dark) but also it should be much lower than the high resistance state of the device (opposite lighting condition). By using a defined light sensor, the docs will tell you R(dark) & R(light).

The part might have an R(dark) of 1.2M & R(light) of 10K. In that case I'd pick R to be 100K to 120K. That is, 1/10 the dark resistance but 10X the light resistance. Then, in the dark V out would be 90% of Vcc (4.5V) and in the light V out would be 10% of Vcc (0.5V). Another thought would be to get a 250K trimmer pot, set it mid-way and then trim one way or the other to get the best center point. (You might very likely need one value for indoor operations and another for outdoor.)

It worries me that you keep bringing up using an LED as a sensor. Yes, it can work. But it is a little like riding a goat to school / work. There are better, faster, more documented ways. Use a phototransistor or CdS cell. Read its docs for hints and tips.

Biting off more than you can chew has killed more potential projects than about any other cause. Save the LED as a photosensor thing for next year's science project.

Break it down into functional assemblies and build and test them separately. After you have 2 things working, bring them together. After you have all things working look for ways to reduce resources. Currently you are consuming 5 analog input pins just to decide where it is brightest. Can you think of a way to use just 1 or 2 sensors and spin the robot or a sensor on a turret to find the light then steer to it? etc.