Transistor not working on 5v Input

Hello all,

I have an Arduino Mega 2560 and i'm trying to use pin 2 to send voltage to a transistor.

#define right_pin 2
#define led 3

void setup() {
// put your setup code here, to run once:
pinMode(right_pin, OUTPUT);
pinMode(led, OUTPUT);
}

void loop() {
// put your main code here, to run repeatedly:
digitalWrite(right_pin, HIGH);
digitalWrite(led, HIGH);
delay(250);
digitalWrite(right_pin, LOW);
digitalWrite(led, LOW);
delay(random(5)*1000);
}

The code sends power to 2 for the transistor and 3 for an LED.. It is simple enough and works great when i have USB plugged into the Arduino for power.. When i remove the USB and replace it with a 5v power supply to the 5v pin, the code starts working, the LED flashes normally, but the transistor will not fire.

The transistor is 2N3904

I'm sure it is something dumb, but i keep banging my head on this. I even added the LED just to know the code was still churning. Any thoughts?

Thanks,
Jay

Figured it out. Needed to ground the transistor. The other two pins of the transistor were attached to another device that wasn't grounded to the arduino.

Diegar:
Figured it out. Needed to ground the transistor. The other two pins of the transistor were attached to another device that wasn't grounded to the arduino.

Always connect the grounds.

Before, it probably worked only because USB ground was somehow connected to the other device's ground (probably through the computer).

I hope you had a resistor between pin 2 and the base of the transistor.

Grumpy_Mike:
I hope you had a resistor between pin 2 and the base of the transistor.

I do now that you mention it. 1k enough?

Yes that is fine. :slight_smile: