Fluctuating digital readings

I'm relatively new to Arduino, and now that I'm finally working on a project, I ran into a bit of a problem. I can't seem to get stable digital readings when powering from my laptop with USB. I realise that I won't ultimately be using USB as a power source, but I would still like an explanation. When I power it from a different source this doesn't happen. It also doesn't occur when I ground myself to the board itself. So I've resorted to using analog reading instead of digital as then I can tell to wich level the fluctuations happen. It never goes up to a full 1023 reading, so I test whether it's a full 1023 or not. But I really feel like that's a waste of analog pins and it's too much effort for one simple thing. Does anyone have an answer?

Without a circuit diagram and code my Crystal Ball shows:
Hang it higher to increase the level, or ground it properly.

You didn't say where the "digital readings" are coming from or what kind of connection you have or anything...

It never goes up to a full 1023 reading, so I test whether it's a full 1023 or not.

If you connect the analog input to Vcc it should read 1023. Or, if the analog voltage is greater-than or equal-to the ADC reference (usually Vcc ~5V) it will read 1023. (No input should be higher than Vcc + 0.5V or the chip can be damaged.)

If you ground the input it will read zero. If the voltage is stable and the reference is stable the ADC reading should be stable +/-1 count. (With analog, there is always a chance of being on the "hairy edge" where it jumps up-and-down by one count.)

What is connected to your input?

You probably forgot to connect the grounds. Happens all the time.

Re: John was ser

A bunch of push switches connected directly to +5V

Then you should be wired like S1 below.

.

Thanks!

This is how my setup is wired at the moment. (I know the arduino isn't accurate it's just to quickly demonstrate).
All I struggle with is the digital readings. They are not stable. It randomly reads HIGH and LOW.

Look at LarryD's diagram - you need an external pulldown resistor for each switch. Or in other words, you have floating inputs.

Alternatively, use the internal pull-ups and wire your switches to gnd.

This is how my setup is wired at the moment.

When your switches are on (closed) you have a solid connection to +5V and the inputs will read high (so-far so-good).

When your switches are off (open) the inputs are not connected to anything. These are called "floating inputs" and the state is undefined.

Unconnected analog inputs will also float to undefined values.

Open connections are also very-high impedance which makes them susceptible to noise (which can give random-changing readings).

The easiest solution is to [u]enable the internal pull-up resistors[/u]. With the pull-ups enabled, unconnected inputs read high. Then the switch is wired to ground. (Of course you have to reverse the logic in software or if you have a toggle switch you can install it upside down, etc.)

Or, the [u]Button Example[/u] shows you how to use an external pull-down resistor. Or of course, you can use an external pull-up but there's rarely a need for that.

Compare ‘your’ S1 cct. to the S1 cct. in the image I posted.

What difference do you notice?

.

Your power connection to the UNO needs to be revisited :wink: .

Also, your LEDs each need a series dropping resistor.

.