# LEDs and weird results from various Arduino pinModes

I'm currently repurposing an old Arduino project - it consisted of several super bright LEDs wired according to the following schematic:

The same configuration is repeated with pins 3, 5, 6 (and not shown to keep things simple).

I was controlling the lights via serial, and managed to PWM them without any issue.

For my current project, I want to work with code uploaded through the Arduino IDE. I kept getting weird results as I was trying to PWM them, and tried to simplify my code until I got to the following three test cases:

void setup() {
pinMode(6, OUTPUT);
}

void loop() {
digitalWrite(6, HIGH);
delay(500);
digitalWrite(6, LOW);
delay(500);
}


Expected result: the lights connected to pin 6 blink.

Actual result: the lights flicker once, and not do anything at all.

void setup() {
/* pinMode(6, OUTPUT); */
}

void loop() {
digitalWrite(6, HIGH);
delay(500);
digitalWrite(6, LOW);
delay(500);
}


Expected result: nothing should happen.

Actual result: nothing happens.

void setup() {
pinMode(3, OUTPUT);
}

void loop() {
digitalWrite(6, HIGH);
delay(500);
digitalWrite(6, LOW);
delay(500);
}


Expected result: nothing should happen.

Actual result: the lights connected to pin 6 blink. Wait, what?

My wiring solution may not be ideal, but it worked when I was sending instructions by serial. Any ideas as to what could cause this behaviour?

• Did you connect the wall-wart ground with the Arduino ground? Nov 12 '12 at 12:40
• Wall wart -ve/ground MUST be connected to Arduino ground. It may be, but you have not shown it so. Nov 12 '12 at 12:41
• The two aren't connected - as I wrote on @Anindo's answer, I'll try doing that and report back. Nov 12 '12 at 14:58

On Arduino, digitalWrite serves two different purposes:

• If used while pinMode is set to INPUT, using digitalWrite(pin, HIGH) enables the internal weak pull-up resistors (20 k Ohms) on the respective pin.
• If used while pinMode is set to OUTPUT, digitalWrite does the obvious, sets the respective pin to high or low as expected.

Thus, not explicitly setting pinMode (as is happening in your second and third test cases) results in one of two possible outcomes, depending on what the pinMode was last set to, or its default value at power-up, INPUT.

So without the pinMode(pin, OUTPUT), your digitalWrite code is enabling and disabling the weak pull-up resistors, thus raising and lowering the voltage at base of the transistors. This is expected behavior, and depending on the transistor parameters and any additional circuitry, the emitter/collector circuit should conduct (or not) based on this.

The critical missing factor here is the basis for the voltage seen by the base of each transistor - this is relative to its emitter, thus for any consistent test case evaluation, the emitter ground in your circuit needs to be connected to the Arduino ground.

If the emitters do not share common ground with the Arduino board, the base voltage is totally arbitrary, and the only relationship that holds true for this design is defined by the presence (and current through) the pull-up resistors.

In the serial data connection use-case where things were working satisfactorily earlier, the most likely reason is that the serial cable (or its shield) itself was providing a ground connection that was common with the ground of the power source: Not an ideal design, but it worked by happenstance.

• Thank you for the detailed answer - much appreciated! I'll try connecting the Arduino and wall wart grounds tonight and let you know what happens. Nov 12 '12 at 14:56
• That didn't help I'm afraid. The anomalous third result doesn't occur anymore, but there's no change with the first bit of code - not even an initial flicker there. Nov 12 '12 at 21:18
• Could you try wiring up a 10 kOhm pull-up resistor on pin 6 before retrying that code, please? Also, try adding an extra resistor in series with the one you have on each LED string, in case the wall wart or the transistor is unable to handle the current draw. Nov 12 '12 at 21:31
• Apologies there - connecting the two grounds worked very well, it's just I was doing something wrong. Would you still recommend putting up that 10kOhm pull-up resistor? Nov 13 '12 at 9:15
• @Andrey The pull-up would be needed only if your transistors are behaving kind-of iffy in their switching. If they switch reliably and quickly when the output goes high, then you don't need additional pull-up resistors. Nov 13 '12 at 9:24