Arduino and phototransistor

Hi everybody, first of all I’m french and new in the world of Arduino so forgive me for all my possible mistakes…

I would like to make a simple program: I would like to connect a LED (the on-board LED in fact) and a phototransistor to an Arduino Nano, and when the ambient light is hitting the phototransistor, nothing happens. But when I put light on the phototransistor (such as laser light for example), I’d like the the on-board LED to light.

So I begin for example with:

int ledPin=13; // The LED is connected to digital pin 13
int photoPin=7; // The phototransistor is connected to digital pin 7
void setup()
{
pinMode(ledPin,OUTPUT);
pinMode(photoPin,INPUT);
}

void loop()
{

But I don’t know exactly what writting in the void loop…

Moreover, if you could give an advice on the value of the resistor to use in order to make it easy, it would be great!

Thanks a lot.

But I don’t know exactly what writting in the void loop…

It depends on how you have wired up the hardware. I suggest you don’t wire it up to Pin 7 but to an analogue input pin instead.

Then in the loop you read the value of the analogue input pin.
Compare that value with some threshold, say 512.
If it is less than this turn the LED on, otherwise turn it off.

However I suggest you look at the tutorials on the main pages or in the playground first to get an idea of what to do.

I think you’d need to shield the photo transistor from ambient light (inside a tube ?) so only the laser can reach it. Otherwise you’re going to have problems differentiating between the two types of light. I’d hook the phototransistor up to and analogue pin and try a 100k resistor for starters and have a play with light levels and resistor values. You’d need to get a good swing either side of 2 volts to make it work with a digital pin;

voltm = analogRead(1); // phototrasistor on pin 1
Serial.println(voltm); // put it out the serial port whilst watcing it on serial monitor
if (voltm > 400){ digitalWrite(ledPin, HIGH); }
else { digitalWrite(ledPin, LOW); }; // adjust 400 to suitable threshold

You’d need t initialise the anlogue pin and serial interface in the Setup{} section.

5V- 100k-analogue pin- pt anode- pt cathode - ground.

OK, I know that officially the leads on a photo transistor aren’t anode and cathode, but it makes life simple…

Thanks, but I don’t know why but it doesn’t work. My LED is always light on, whatever the value I write. My code is:

// On-Board LED and Phototransistor

int analogPin=3; //phototransistor connected to analog pin 3
int ledPin=13; //led is connected to pin 13 (on-board led)
int voltm=0;

void setup()
{pinMode(ledPin,OUTPUT);

Serial.begin(9600);
}

void loop()
{voltm=analogRead(analogPin); //read the phototransistor
Serial.println(voltm);
if (voltm>200)
{digitalWrite(ledPin,HIGH);}
else
{digitalWrite(ledPin,LOW);}
}

Maybe I’m wrong with the connection of the composants.

If you see values printed out both above 200 and below 200 then I can’t see why the LED is not switching on and of.

What values are being printed?

Where I can see values printed?

The last icon on the top of the arduino application is the serial monitor. Click that and you see the returned serial data at the bottom of the window.

Ah OK thanks. But now I can’t check this before Monday. So I’ll let you know in a few days.

But Thanks a lot!

Well, it works! Thanks.

But now, I’d like to use a blue filter in front of my phototransistor, and when we have the ambiant light, I’d like the On-board LED not to be lighted, and when I put a blue led in front of my filter, I’d like the LED to light.

But it doesn’t work…

So, I have something to change in the code or no? Because, the values when there is nothing and when I put the blue LED are the same, whereas it should be different no?

It is not too clear to me what is happening. Are you saying that the blue LED makes no difference to the reading of the photo transistor irrespective of if it is on or off?
It could be your photo transistor is not sensitive to blue light.