Photocell vs PinMode out value weirdness

Not sure if this should go under programming or in electronics, but since the weirdness starts when I changed code... okay here it goes:

I've set up a photocell with a 1K resistor to output on A0.
Initially, I initialized it like this:

#define lcell A0

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

Later I run Serial.println(analogRead(lcell)); in a loop and it gives me values between 0-5 when I cover it with my hand and around 120 when I shine a flashlight on it.
Okay.

I looked at some other example code and saw they had their pins set up like this instead (with their wiring being identical to mine, i.e. on A0):

int lcell=0;

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

so I changed mine as well.

But now my outputs on the Serial.println(analogRead(lcell)); range between 400-700 when I cover it with my hand and reaches around 1020 when I shine a flashlight on it!

I have done no changes to the physical setup nor has any other part of the code changed.
Can anyone explain this?

(Also, as a side question: could someone explain why using 0 instead of A0 works? There is a 0 pin on the digital side - shouldn't 0 rather refer to that? And if it doesn't, then how would you access the digital 0 pin?)


This is the entire code:

//#define lcell A0
int lcell=0;

void setup() {
  pinMode(lcell, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  Serial.println(analogRead(lcell));
  delay(100);
}

A0 equals 0 for analogRead, but not for pinMode.

Setting an input to OUTPUT is rarely useful and seems to be wrong in your case.

Whandall:
A0 equals 0 for analogRead, but not for pinMode.

Ah, ofc, that makes sense

Whandall:
Setting an input to OUTPUT is rarely useful and seems to be wrong in your case.

Could you clarify what you mean here? Do you think the lcell should be an input?
I looked at the example again and realized that they actually don't set a pinmode for the lcell at all - I just assumed it was output because all pinMode lines read "output" and I didn't notice their lcell was missing.

I commented out the lcell pinmode line in my code and the behaviour is the same, so I guess I don't need it.

The whole idea of setting a pin that will be used as an analogue input as an output sounds nuts to me. pinMode() is a function that is used with digital pins to set the mode of operation. The A* analogue pins do not need to have their mode set when they are used as analogue inputs using analogRead() and like other digital pins they default to INPUT unless explicitly changed to INPUT_PULLUP or OUTPUT

You're both right of course, I was having a total brain blockage. Somehow I thought the photocell was an output (because it outputs ::slight_smile: )

Never mind, it's just another Wednesday...

Thanks for your help!