Phototransistor [Light Detection]

Hi, I am using an Arduino Uno with a phototransistor.

-The longer end (of the phototransistor) is connected to + on my breadboard and then to 5V on my Arduino.
-The shorter end is connected to a 10k Ohm resistor, after that to - on my breadboard and then to ground.
-In between the shorter leg and the resistor, I have attached a cable to pin A0 to read the sensor value.

I want to achieve this:
When the sensor reading drops below 50, it is considered dark.
When it is dark, an LED with turn on. ("Or print out a message.")

Else, nothing happens.

When I Serial print my sensor value, I always get a value around 360. It doesn't matter if it is dark or light. The value always stays the same, that is really strange.
I wonder what my mistake might've been?

I appreciate your aid :3.

Which phototransistor? Datasheet link please.

Try to draw a schematic. A pencil sketch photographed with your phone is acceptable.

Also, post your code.

Hi, I personally don't know what type of phototransistor it is, I have included a photo of my phototransistor. What I can tell you is the following, it is included in my Arduino Starter Kit. I have included a picture of my setup (only the phototransistor is hooked up and a quick schematic.)

Also possible to see in this video, I used the same type of phototransistor: Arduino Project 04 - Color Mixing Lamp - YouTube

const int lightSensorPin = A0;

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

void loop() {
  
  int lightSensorReading = 0;
  lightSensorReading = analogRead(lightSensorPin);
  Serial.println(lightSensorReading);
  delay(100);
  
  if(lightSensorReading < 50){            
    Serial.print("It is dark.")
  }

  else {
  Serial.print("It is light".)
  }
}

That looks like a photodiode and may be wired backwards.

If so, you can use your multimeter to determine the correct orientation. Set the multimeter to the 2V scale, and connect the leads to the photodiode either way around.

The lead that is connected to the positive multimeter terminal and produces a positive voltage in strong light is the anode, the end that you connect to the resistor and the ADC input.

You may need to use a larger resistor, for example 100K or even 1 MegOhm.

The illustration below shows the orientation as a schematic, but the resistor value shown is too small.

I just tried this using an ATTiny85 as MCU.

Firstly, if it is indeed a phototransistor, we assume a NPN device, the long leg is the emitter (for my unit - I assume for most) and must go to the resistor, 10K in my case, the other end of which goes to ground. The short leg, collector, is connected to +5V. Using it like that I get readings as expected. If I connect the phototransistor the other way round I read 0 (as expected).

If I leave the ADC pin floating, no connection, I analogRead about 360. Make sure you read the ADC pin actually connected to the phototransistor/resistor junction and have a good connection.

Hope that helps.

Willem.