Go Down

Topic: Earthing of Arduino Board (Read 7070 times) previous topic - next topic

dkl65

May 03, 2012, 03:16 am Last Edit: May 03, 2012, 03:18 am by dkl65 Reason: 1
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):
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 12

int 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: http://www.arduino.cc/playground/Main/CapSense 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?
Visit http://www.glenof.webs.com/!

PaulS

Code: [Select]
  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());

I will never understand why people do this. Read a value. Constrain that value. Map that value. Act on that value. Then, read another value and print it. Why? You won't learn a damned thing about the value that was constrained, mapped, and acted on. Print ledVal BEFORE mucking with it.

kf2qd

The human body can function as a large ground or a source of 60 cycle noise and as a floating voltage source. And sometimes all at the same time. You are a large blob of water. When that large blob of water approaches a circuit you have an effect on it. You can affect capacitance, you can add noise and you can act as a ground for different signals. Those properties are what makes smart phones work - you touch the screen and it is able to detect that big blob of liquid affecting that portion of the display.

dkl65

Okay... but why does it happen only when the Arduino is connected to the Earth via USB power on desktop? When connected to a battery, the human body has a much less effect on the circuit.

To PaulS:
Read it carefully! I am printing the raw led.measure() ! The constrain and map is only to set the brightness of the LED, which I am not so concerned about! I didn't change the variable ledVal; look carefully! led.measure() is the value of ledVal; it never changed (I didn't "muck" with variable ledVal)!. The brightness of the display LED is not contained in a variable! What I want to know is why earthing the Arduino Board will cause different behaviours!
Visit http://www.glenof.webs.com/!

PaulS

Quote
Read it carefully! I am printing the raw led.measure() !

But, you are NOT printing the value that you are constraining and mapping. You are printing a different reading. Big difference.

Quote
look carefully! led.measure() is the value of ledVal

I did look carefully, and I see that you are taking two readings. You use one, and print the other. Like I said, I do not understand why you do this.

Quote
I didn't "muck" with variable ledVal)!.

You didn't print it either.

SirNickity

I don't fully understand some of the more subtle grounding effects, but I figure what's happening is this:

With USB, your ground reference is the actual Earth -- through the PC, mains wiring, and breaker panel at your house.  When battery powered, ground is floating.  (From a battery, all that matters is the difference between the reference and the V+ supply.)

So, when you touch a Gnd lead, you equalize grounds with the battery-operated circuit, but you're more likely a source of noise for a mains-powered circuit.  After all, you're a lower resistance to Earth than the surrounding air (battery), but not lower than copper cable (USB).

I think.  Someone might need to put me in my place.

dkl65

#6
May 03, 2012, 09:28 pm Last Edit: May 03, 2012, 09:33 pm by dkl65 Reason: 1
Quote
When battery powered, ground is floating.

So you think that this will cause the readings to be more stable? That seems to be my case.

Quote
You can affect capacitance, you can add noise and you can act as a ground for different signals.

Why is the grounding, capacitance, etc. much more evident when the UNO is connected to the Earth? I simply take out the USB cord, plug the battery in, without changing the location of the UNO, and my body now has very little effect on the circuit.

Quote
Quote

Read it carefully! I am printing the raw led.measure() !
But, you are NOT printing the value that you are constraining and mapping. You are printing a different reading. Big difference.


Quote

look carefully! led.measure() is the value of ledVal
I did look carefully, and I see that you are taking two readings. You use one, and print the other. Like I said, I do not understand why you do this.


Quote

I didn't "muck" with variable ledVal)!.
You didn't print it either.


Sorry, you are mislead by my code, PaulS. I am not taking 2 readings. The ledVal is useless; forget about it. I don't really care about the values, I just want to know why the Earthing of the board will cause this stuff to happen. I am using only one value: led.measure() the whole time (led.measure() is the time the sensing LED's "parasitic capacitor" takes to fully discharge). The map and constrain stuff is a value not contained in a variable, just for the display LED. Bottom line: that isn't the problem. I am not actually having a problem at all, I just want to know how this happens.

Another question: If I connect my UNO's GND pin to a metal water pipe in my basement, is my UNO's GND now "Earthed"?
Visit http://www.glenof.webs.com/!

Nick Gammon


I am not taking 2 readings. The ledVal is useless; forget about it. I don't really care about the values, ...


Code: [Select]
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));


I think you might have confused PaulS by having all these lines of code for a variable that is "useless" and you don't care about. Maybe delete them?

However you did in fact analogWrite the mapped, constrained value that is called ledVal.

Quote
I simply take out the USB cord, plug the battery in, without changing the location of the UNO, and my body now has very little effect on the circuit.


I'm not particularly surprised. Personally I am sitting at a keyboard that is plugged into USB, so there is a proximity between me, the keyboard, the screen, USB and via USB the Arduino. If I unplug the Arduino it moves into a different electrical space.
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

dkl65

Let's forget about the whole ledVal thing. From this froum topic, I have learned that the human body can act as some kind of capacitor or ground for the electricity, or conductor of mains 50/60 Hz AC EMF.  To use the Capsense library, the Arduino should be connected to the Earth for best results, as said on the playground page. Now, I "speculate" that connecting the Arduino to the Earth enchances this grounding effect on circuits. Second question: if I connect a GND pin of my Arduino into the earth socket in a power outlet, is my Arduino "Earthed"?
Visit http://www.glenof.webs.com/!

SirNickity

Quote
Quote
When battery powered, ground is floating.

So you think that this will cause the readings to be more stable? That seems to be my case.


Look at it this way.  Let's say you have a ladder that's ten feet tall, and you put something on top of it.  I don't care what... maybe a rock.  How high off the ground is that rock?  Ten feet, right?  You know this because you have a reference point (Gnd), and a known height via the ladder (+10ft).

Now let's take that ladder and throw it into deep space.  You have no idea where.  Which way is Earth?  Could be any way, any distance.  It might be in a completely different galaxy, or just behind Uranus.  (Sorry, that joke will never get old.)

You still know the rock is +10ft above the bottom of the ladder, but "Gnd" no longer has any meaning to you.

In this case, the ladder on Earth is your USB-powered Arduino.  The ladder in space is your battery-powered Arduino.

If you take a lead from the Arduino's Gnd socket and connect it to USB, a water pipe, the ground hole in a wall outlet, or a copper stake 50ft deep in the back yard -- it doesn't matter how, the Arduino now has a universal reference.  It doesn't change the relationship between 0v and 5v on the board, but ...

If you take your battery-powered, but grounded, Arduino and hook up a +5v lead from your computer to a digital pin and take a reading, now the Arduino knows it's +5v.  If you remove the lead between Gnd and your bath tub faucet, however, the Arduino no longer has any idea what the voltage on that pin might be.  It could register as anything, since it's one piece of wire with no return path, and no current can flow on a circuit with only one wire.

If you have trouble wrapping your mind around that particular line, think of this:  Imagine a single dot.  Which direction is it pointing?  Is it a straight line?  None, and no.  It's a single dot.  It takes two dots to make a line.  Circuits are like this.  With one wire between two independent circuits, there's no frame of reference, and no path for current to make a round-trip.  You need two wires before you can quantify its value.

Another way, if you like math, is to think of Gnd as X.  What's X + 5?  You can only solve this if you know what X is.  Otherwise, the answer is undefined.  That doesn't keep stuff within the circuit from working -- for instance, if one voltage is X + 5, and another is X + 2, you know the difference between them is 3.  Is it +3 or -3?  It depends on which one is your reference point.

So, back to your question:  Why does the battery-powered circuit register no change when you touch it?  Because it has no reference to the Earth.  You do, so through you, it will establish one.  (A weak one, but nonetheless.)  When you plug it back in via USB, it has a strong reference to Earth.  You become a second reference to Earth.  Since you're higher resistance than the USB port, motherboard, PSU, power cable, and house wiring, your ground potential is slightly different than its own ground reference.  Thus, you induce some voltage (the difference) and it is read as a signal.

This is how I understand it, and I reserve my right to be completely, totally wrong.  (I would appreciate a kind nudge in the right direction if I'm guilty of rewriting physics.)

dkl65

Thanks SirNickity! Even if I am upstairs, sitting on a chair, I am still "Earthed"? The Earth is a giant conductor? How much resistance does it have? So if I connect a lead from the Earth socket of a wall outlet to the GND pin on my UNO, it will behave somewhat like when the UNO is powered by a USB? I also (think I) understand that the Capsense works by measuring the potential difference between an "Earthed" person, and the Earth connected to the Arduino.
Visit http://www.glenof.webs.com/!

SirNickity

It becomes splitting hairs at some point.  All real-world objects conduct.  It's just that some conduct more than others.  So, yes you're still technically earthed in a chair, upstairs, with rubber underwear.  But you probably have enough resistance that it is no longer significant.  At that point, the battery Arduino and your body are equalizing, but the resistance to earth is high enough not to have much influence.

dkl65

But I shouldn't be earthed (or it is extremely insignificant) if I am standing on a high voltage bare wire, without touching anything else, even if the wire is connected to a concrete post, touching the Earth. You mean "equalizing" as having a common ground, right? If the Arduino is connected to a USB, the equalizing is much more significant, I "speculate". I connected a wire from my battery powered Arduino's GND pin to the Earth socket in a wall outlet or water pipe, then my finger had more of an effect on a circuit then when it was unconnected to the earth, but not as much as USB power.
Visit http://www.glenof.webs.com/!

SirNickity

Once the resistance between any object and ground is over 1M-ohm or more, it's hardly a ground anymore.  So if you're doing your high-wire act on a 10kV power line, you're not conducting much power to earth.  The resistance of those wooden poles, ceramic standoffs, and the air around you just don't conduct enough energy to cause any harm.  You'll notice, though, at ridiculously high voltages, an air gap becomes less and less effective.  Check out a YouTube video of a power station disconnect switch and you'll see what I mean.

But, here where we're dealing with 5v DC, the space between you and the nearest metal object grounded to earth presents enough impedance to effectively isolate you from earth.

The water pipe is earthed, but probably has higher resistance than copper wiring from your AC outlet.  After all, it's a water pipe.  Its primary purpose is to carry water, not power.  That's merely a convenient (usually) side effect.  In any case, it wasn't plumbed with the express intent to be as close to 0 ohms as possible.  House wiring, on the other hand, is.  (Not that it's 0 ohms, otherwise ground loops would never exist, but ideally it's closer.)

Now why would you make a more significant difference when powered by USB than when grounded to the wall?  I don't know exactly, but I speculate it has something to do with the amount of noise on the USB circuit.  Ground isn't a black hole.  It's subject to physical limitations, and can be noisy.

About the equalizing bit...  Let's say you have two bedrooms.  It's winter out, and in one room you have the window wide open.  In the other room, you're well insulated and the heating vent is open.  One room will be cold, the other warm.  Now let's say there's a door between them -- like one of those hotel rooms that is connected to the next suite.  If you open this door, the cold room will become warmer, and the warm room cooler, until they reach equilibrium.

This is what happens when you take two isolated circuits and connect their grounds together.  (Or, you touch ground on a battery-operated circuit.)  The two will equalize, and you now have a common reference.  If you didn't connect grounds, and you tried to read the voltage on circuit A with the ground on circuit B as your reference, the results are undefined.

It's like telling someone that a jar in one of your rooms is 10 degrees above ambient.  Well, what's ambient?  Without that, you can't know the absolute temperature of the jar.

dkl65

#14
May 04, 2012, 09:36 pm Last Edit: May 04, 2012, 11:11 pm by dkl65 Reason: 1
Do you mean "undefined" as unable to measure the voltage? http://arduino.cc/forum/index.php/topic,102717.0.html is another forum post I made that talks about me doing an EMF detector, but when I touch the wire to the A0 pin (A0 is tied to ground via 3.6M ohm resistance), the output reads ~0.3V only when the computer is connected to the Arduino!

Quote
Since you're higher resistance than the USB port, motherboard, PSU, power cable, and house wiring, your ground potential is slightly different than its own ground reference.  Thus, you induce some voltage (the difference) and it is read as a signal.

So is this supposed to cause some ground loop (I don't really understand ground loops)? So when I touch the wire, I should establish some circuit like Ground Loop.png, right? I tried recreating this circuit (Ground Loop 2.png), and measure no difference (even in the 200mV range of my multimeter). Am I doing it incorrectly, or am I completely wrong? I am using battery power.

In Ground Loop 2.png, I took a GND lead from my Arduino, connect it to another GND lead via 3.6M ohm resistance. I tried to measure a voltage across the resistance (2 1.8M ohm resisters in series).
Visit http://www.glenof.webs.com/!

Go Up