Help with a phototransistor circuit

Hi
I am trying to make a cicuit where an LED should brighten or dim as per the light incident on a phototransistor.

I have written the following sketch for it:

int photot = 0;
int light =0;
int red=10;
int green=9;


void setup()
{
  pinMode(photot, INPUT);  // to store the analogvalue read from phototransistor
  pinMode(red,OUTPUT); //red led
  pinMode(green,OUTPUT); //green led
  Serial.begin(9600); 
}

void loop()
{
  
  light = analogRead(photot); //store value from phototransistor
    Serial.println(light); //print value to serial monitor
  if (light >0)
  {digitalWrite(red,LOW);                             
  analogWrite(green,light/4);
  }
  
  else 
  {
analogWrite(green,light/4);
  digitalWrite(red,HIGH);
  }
  delay(100);  
}

In the phototransistor I have, when light falls on it, the value goes to 0. When I make the room dark, the value goes above 0. hence the code like above.

Now, since I am writing the value of light to green LED, it should get brighter as I make the phototransistor darker. But I observed that it was actually going dim. Actually, it wasn’t even a steady glow, it was puslating slowly, even though the values of light in the serial monitor were fairly constant. Why is this happenning?

Just to make it clear, earlier while experimenting I had not set analogWrite to light/4. I was passing the value of light to it straight, which was more than 255 at some times. May this have damaged the output pin?

Thanks and help is appreciated.

Take one thing at a time. make a sketch where you read the analog input and send the value over serial to make it human readable. Then experiment with different lightning situations to get a feeling of how your circuit works. Do the same with the led and then combine the two.

light = analogRead(photot); //store value from phototransistor

photot is 0. You need it to be A0.

You might do:

define PHOTO_TRANSISTOR_PIN A0

and

light = analogRead(PHOTO_TRANSISTOR_PIN);

terryking228:

light = analogRead(photot); //store value from phototransistor

photot is 0. You need it to be A0.

You might do:

define PHOTO_TRANSISTOR_PIN A0

and

light = analogRead(PHOTO_TRANSISTOR_PIN);

I thought light = analogRead(0); will properly read analog pin 0, but I agree that using A0 is an improvement and better.

Lefty

hi terryking228 and retrolefty

I tried both ways, and am getting the same result.

I think my issue is more with the connections I should be doing.

Right now, I have the pins configured as follows:

5V output - going to emitter A0: Input pin, coming from collector Base lead: GND

i have cycled through all the different combinations for this (taking BASE pin to A0, changing resistances, switching supply between collector and emitter etc), but I am not getting a good 'range' of values. Even though it's set to analogRead, even a dim light shining on the phototransistor gives me a 0. What am I doing wrong?

Hi uberjoker,

I think you need a pull-down resistor from A0 to ground, try 10K. You need it to form a voltage divider so A0 can read the voltage at the junction of the phototransistor and the fixed 10K resistor.

Using a value over 255 with analogWrite can't damage the pin. The higher value bits will be lost, so 256 = 0, 257 = 1 etc.

Paul