I am having problems with analogRead when I am supplying power via the Vin pin.
Here is a very simple voltage divider circuit that should always return a value around 2047 when I am using 12 bit analog resolution. This is the case when I am supplying power (actual readings 2053 - 2046) via USB but when I am using Vin via an external source (other picture) then the values are 1954 - 1890 around 100 values lower.
(I am sending the values via Wifi)
Thanks for the hint, stability is indeed the problem. I took a closer look at the circuit and found that the +5V is going thru filtering before it hits the voltage regulator (see attached image). I am surprised that Vin does not do this.
Also the used regulator (AP7215-33YG-13) is only good for 5.5V and not 6V - faulty specs on the official page!
VIN is actually also filtered but using a different approach as VIN can be subject to more variations than the USB voltage. As seen on page 2 of the schematic.
If you really wanted to use VIN as a reference then you would have to adjust its supply and keep it at that reference standard which produced the correct results (providing it does not exceed the max limit for VIN).
Another choice often used is the AREF that was linked to earlier which is often less susceptible to the vagrancies of the supplies.
I did take a look at Vin but the only filtering I could find is C8 (1uF cap) on the input to the voltage regulator.
Maybe this was not clear in my description, I don't want to use Vin as a reference; I just want to use it to supply the power. I am using the 3.3V output of the MKR to supply my sensors.