I am trying to read the RGB from a VGA and when there is not a cable connected (besides ground), always gives 1 instead of 0.
Using a resistor (Or multiple resistors) makes the problem a little better, but i does not fixes it.
Test code when using only red
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(A0, INPUT);
}
void loop() {
// put your main code here, to run repeatedly:
int R = analogRead(A0);
if (R == HIGH) {
Serial.println(R);
}
}
(I have the A0 connected to the ground with a resistor)
With a 4.7K resistor from A0 to ground, I don't see anything printed unless I put my finger on the lead connecting A0 to the resistor, at which time I see some 1s. Which is not totally unexpected.
I tried that, also tried it in a design software and it worked correctly there, i think even with electrical noise or other parameters, the voltage in this test should be close to 0
There's all kinds of possible solutions. If a 0.005V difference is crucial, and if you're working in an electrically noisy environment, shielding comes to mind. A 0.1uF capacitor on the analog input is another. A leaky integrator in software to smooth out bumps is yet another. A voltage follower on the input to keep the impedance nice and low is yet another. And that's just what I can think of off the top of my head.
You can improve your resolution by using the internal voltage reference, it's around 1V so should give you 4x better resolution. Of course, you can't then measure any voltage greater than that reference value.
If you need 5V range, and < 5mV resolution, it's time for a better ADC, there are many available.
HIGH is normally used for digital inputs/outputs, where there are only two possible states: HIGH/LOW.
It is not normally used to refer to analogue inputs, which can have 1024 different values between 0 and 1023.
Somewhere HIGH is defined as '1', so only 1 value out of the 1024 possible analogue values is regarded as HIGH.
An ADC reading of 0 isn't regarded as HIGH, neither are ADC counts of 2, 3, 4, 5, 6, 7, . . . . . . . 1021, 1022, and 1023.
You only need approximately 5mV of noise to go from a reading of '0' to 'HIGH'.
So it is unlikely that you are always going to get a reading of '0', when the voltage is supposed to be 0V.
You are wrong about this, anything nonzero will be converted to HIGH.
So 0 = LOW, but 2, 3, 4, … 1023 are all HIGH.
That means that only if A0 < 5mv you can get LOW as result.
I agree with @van_der_decken that an Arduino is not a good instrument to measure that kind of voltages.
Correct, according the c++ standards a Boolean variable can be true of false, but the Arduino functions use HIGH and LOW, which are translated to 1 and 0 by the compiler.
When used in an if-statement there’s no need to store it as a variable.
When the need arises to store a Boolean variable, the Arduino IDE uses a 8 bit integer to store a 1 or a 0.
When a variable needs to be converted to Boolean, the variable is either nonzero or zero.
In if (R == HIGH) the compiler recognizes the right part as Boolean, so it will convert the left part to Boolean also.