Analog Reading Wrong When Disconnected From PC

Using the Mega 2560 and pin A0 to measure 0 - 4.5 volts. Analog reference set to DEFAULT.

The problem is that the measurement with analogRead() works as it should only as long as the Arduino is connected to the PC via the USB cable.

As soon as I disconnect the USB cable, the measurement returns either 0 or 1023 but nothing in between.

I don't have anything connected to the AREF pin, but I think I don't have to if I'm using DEFAULT reference.

I'm using a serial LCD display to print the measurement values. I'm powering the board through its Vin pin via an external 5 volt regulated supply. Do you think that might be the cause?

Any pointers on what I might be doing wrong? Why would the analogRead() work fine when the board is connected to the PC, and not work when it's disconnected from the PC?

"I'm powering the board through its Vin pin via an external 5 volt regulated supply. Do you think that might be the cause?"

Yes. Vin needs >= 6.5 to 7V for the onboard 5V regulator to work. Otherwise feeding 5V into the 5V regulator does not result in a workable 5V coming out of the regulator.

If you want to power the board from 5V, connect the source to the 5V pin, or power it via the USB connector.

CrossRoads:
Vin needs >= 6.5 to 7V for the onboard 5V regulator to work. Otherwise feeding 5V into the 5V regulator does not result in a workable 5V coming out of the regulator.

If you want to power the board from 5V, connect the source to the 5V pin, or power it via the USB connector.

Thank you! I didn't realize I was powering it from the wrong pin. It works now.