Hello!
I am wondering why the Arduino behaves differently when powered by a USB. One example of that is in this forum post I made here: http://arduino.cc/forum/index.php/topic,102717.0.html Another behaviour I've experienced recently is when using an LED as a light sensor (more details at http://provideyourown.com/2011/cheap-alternative-for-hard-to-find-cds-light-sensor/). The circuit is a green, reverse biased LED from pin 12 to GND (sensing LED), a forward biased LED from pin 9 to GND with 330 ohm resistor (brightness adjusted depending on ambient light [display LED]). Here is the code (not quote):
class AmbientLightSensor {
public**:**
AmbientLightSensor(int ledPin) : mLedPin(ledPin), mMeasureAnalog(false) {}void setAnalogMeasurement(int thresholdLevel); // measure from an analog pin
void setDigitalMeasurement(); // measure from a digital pin (default)int measure();
protected**:**
int mLedPin;
bool mMeasureAnalog;
int mAnalogThresholdLevel; // (0 to 1023)void charge();
void discharge();int measureUsingAnalogPin();
int measureUsingDigitalPin();
};void AmbientLightSensor::setAnalogMeasurement(int thresholdLevel)
{
mAnalogThresholdLevel = thresholdLevel;
mMeasureAnalog = true;
}void AmbientLightSensor::setDigitalMeasurement()
{
mMeasureAnalog = false;
}void AmbientLightSensor::charge() {
// Apply reverse voltage, charge up the pin and led capacitance
pinMode(mLedPin, OUTPUT);
digitalWrite(mLedPin, HIGH);
}void AmbientLightSensor::discharge() {
// Isolate the diode
pinMode(mLedPin, INPUT);
digitalWrite(mLedPin, LOW); // turn off internal pull-up resistor, see http://arduino.cc/en/Tutorial/DigitalPins
}int AmbientLightSensor::measure() {
charge();
delay(1); // charge it up
discharge();
return (mMeasureAnalog)? measureUsingAnalogPin() : measureUsingDigitalPin();
}int AmbientLightSensor::measureUsingDigitalPin() {
long startTime = millis();
// Time how long it takes the diode to bleed back down to a logic zero
while ((millis() - startTime) < 2000) { // max time we allow is 2000 ms
if ( digitalRead(mLedPin)==0) break;
}
return millis() - startTime;
}int AmbientLightSensor::measureUsingAnalogPin() {
long startTime = millis();
// Time how long it takes the diode to bleed back down to a logic zero
while ((millis() - startTime) < 2000) { // max time we allow is 2000 ms
if ( analogRead(mLedPin) < mAnalogThresholdLevel) break;
}
return millis() - startTime;
}
AmbientLightSensor led(12); // LED is hooked up to digital pin 12int led2 = 9; // led to indicate darkness is hooked up to digital pin 9
void setup()
{
Serial.begin(9600);
pinMode(led2, OUTPUT);
}void loop()
{
int ledVal = led.measure();
/if (ledVal > 300) // a decent level of darkness
digitalWrite(led2, HIGH);
else
digitalWrite(led2, LOW);/
analogWrite(led2, map(constrain(ledVal,0, 400), 0, 400, 0, 255));
Serial.println(led.measure());
}
According to my Serial Monitor debugging info, the sensing LED's "parasitic capacitor" takes about 64 ms to discharge in my fairly lit room. Works OK.
I did the following when the UNO was running on USB power from a desktop:
Action 1: touch only the short lead of the sensing LED with my finger. Result 1: display LED dimmed to a great extent, meaning that the "parasitic capacitance" of the sensing LED decreased significantly. Serial monitor prints ~15. Is the human body's capacitance, or earthing the cause?
Action 2: Touch an insulated solid core wire's exposed end to sensing LED's short lead, while holding the plastic part with my finger. Result 2: Nothing happened. Serial Monitor prints the normal discharging time.
Action 3: Touch the same wire's metal end to the short lead of the display LED, while holding the other metal end with my finger. Resulte 3a: Same as 1, but to a smaller extent.
When repeating actions 1-3 while the UNO was running on 9V battery power, they hardly affected the "parasitic capacitance". There is obviously a big difference in the behaviors in USB power and battery power. Also, in this link: Arduino Playground - HomePage it says "the grounding of the Arduino board is very important in capacitive sensing". When the Arduino board is powered by USB via desktop, the power has connection with the Earth (mains power), right? How can this cause these behaviors, or even more? Is there another cause? Any explaination is appreciated!
P.S. What does "Another solution that seems to have worked well on at least one installation, is to run a foil ground plane under the sensor foil (insulated by plastic, paper, etc.), and connected by a wire to ground. This worked really well to stabilize sensor values and also seemed to dramatically increase sensor sensitivity." from that link mean?