According to the reference, the digitalWrite(pin, HIGH) command would set 5v or 0v to the pin when the command is digitalWrite(pin,LOW), but the waveform of the output pin, captured with a virtual scope, differs from the conclusion above, the waveform indicates the output voltage is set to 5v when digitalWrite(pin, HIGH) is executed, and then set to about 0v until next command is executed.
The code snippet as follows:
void loop()
{
if (val)
digitalWrite(P13,HIGH);
else
digitalWrite(P13,LOW);
delay(1500);
val = !val; //Serial.println(val);
}
What I am confused is that the LED connected to the output pin could still light before it is set to LOW. Can anyone help me understand this command correctly?
I was seeing something very similar when I was testing the digital output functionality. A short while back I was trying to figure out why my relay driver circuit was always ON whether I set the digital pin to "0" or "1".
After a lot of scouring around, I learned the hard way that I had to explicitly set the digital pin as OUTPUT (duh). In a nutshell, the Arduino digital pins are all initiaized to INPUT by default, which is a High Impedance state (~10MOhms). Long story short, due to the much lower input impedance of my driver circuit, it seems to draw just enough extra current that it will be activated even if the digital output is set to "0".
Oddly, my observation with a DVM is similar in that the "0" state output is also around 1.5-2V, which was higher than one would expect. I tried adding pull-up and pull-down resistors but I still got glitchy results. But after reading the description of the I/O pins:
I just had to explicitly set the pin to OUTPUT, an everything worked like a charm afterwards. Since I didn't see your setup() function, I'm not sure if you are already setting the pit to OUTPUT - but it's worth a shot because the solution is not obvious.