Not understanding my circuit using a transistor.

I created a learning circuit in which I am trying to use an NPN transistor in conjunction with a RGB LED.

When current through the base is full, the led outputs green, when its 66% full, its yellow, and when its 33% or less, its Red.

I am not sure this circuit works since when I go through the range of values for setting, I am seeing really strange results such as cycling from 800 to 100 in val. Any feedback would be appreciated.

I have 330ohm resistors to the RBG legs in the LED, and one 330 ohm resistor in the base leg of the transistor.

Thanks!

int RED_PIN = 11;
int GREEN_PIN = 10;
int BLUE_PIN = 9;
int BASE = 6;
int EMITTER = A0;
int setting = 5; //255 is max, 0 is low

void setup() {
// put your setup code here, to run once:
pinMode(RED_PIN, OUTPUT);
pinMode(GREEN_PIN, OUTPUT);
pinMode(BLUE_PIN, OUTPUT);
pinMode(BASE, OUTPUT);
Serial.begin(9600);
}

void loop() {

analogWrite(BASE, setting);
delay(100);
int val = analogRead(EMITTER);

Serial.println(val);

if(val<341)
{
digitalWrite(RED_PIN, LOW);
digitalWrite(GREEN_PIN, HIGH);
digitalWrite(BLUE_PIN, LOW);
}
else if(val<682)
{
digitalWrite(RED_PIN, HIGH);
digitalWrite(GREEN_PIN, LOW);
digitalWrite(BLUE_PIN, HIGH);
}
else if(val<=1023)
{
digitalWrite(RED_PIN, HIGH);
digitalWrite(GREEN_PIN, LOW);
digitalWrite(BLUE_PIN, LOW);
}
else{
digitalWrite(RED_PIN, HIGH);
digitalWrite(GREEN_PIN, HIGH);
digitalWrite(BLUE_PIN, HIGH);
}

}
}

I disassembled it. I was hoping the code could kinda tell the picture.

Basically for the transistor, I had a 222A, the emitter went to ground, the base received from pin 6 as output, and the collector went to analog pin 0.

For the RGB I had common going to ground, and each node going to their respective digital output above.

If the code looks clean I suppose it was a fat finger on the board.... thanks.

It something like this, the 3 resistors from the LED go to input pins 9,10,11.

Forgive the crudeness.

my_circuit.png

Georgeduino:
I disassembled it. I was hoping the code could kinda tell the picture.

Basically for the transistor, I had a 222A, the emitter went to ground, the base received from pin 6 as output, and the collector went to analog pin 0.

For the RGB I had common going to ground, and each node going to their respective digital output above.

If the code looks clean I suppose it was a fat finger on the board.... thanks.

Why are you using one Arduino pin as an output to gnd another input?

Also Tr base R is normally much higher than 330 ohm. 10k is normal.

Weedpharma

Also Tr base R is normally much higher than 330 ohm. 10k is normal.

There is no single 'normal'. For switching a base resistor of 330 ohms is much more likely
that 10k, but its something you calculate rather than handwave about. For switching
the transistor ideally is saturated. Saturation is a different mode of operation from linear
amplification (where high base resistor values are much more likely). Expect current
gains of 5 to 20 or so in saturation (CB junction forward biased), 100 to 500 or so for linear
operation (CB junction reverse biased)

If that's your actual circuit there are a number of problems. The transistor base has no limiting resistor which can damage the Arduino. The collector has no connection to anything except pin 6 through a resistor. What is that supposed to do? If pins 11-13 are really connected to the unlabeled ends of the resistors, then any of them could severely overload the red LED that is connected to ground. If all of them are high, the effective resistor load is 33.3 ohms, about 10 times the usual limit for continuous LED current. The RGB Led, while appearing interesting, isn't connected to anything so doesn't do anything.

Hi,

Can you please post a copy of your complete circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

A picture of your project would also be of assistance.

Thankyou ...Tom.... :slight_smile:

You say "Not understanding my circuit using a transistor."
Believe me, you're not the only one.