Skip navigation

today, I got a 12volt relay working with my arduino and a 2n3904 transistor.  It is only controlling a bi-color LED. when i send “r” or “b” over serial, the relay switches to light that part of the LED.

Total Parts:  Arduino, 2n3904 NPN transistor, common cathode bi-color LED, 1N4001 diode, 12v DPDT relay(SPDT would work), 100ohm resistor, 1Kohm transistor, assorted wire, breadboard.
here is the code:

int led = 19;
int incomingbyte;
void setup() {
pinMode(led, OUTPUT);

void loop() {
if (Serial.available() > 0) {
incomingbyte =;
if(incomingbyte == 98){
digitalWrite(led, HIGH);
if(incomingbyte == 114){
digitalWrite(led, LOW);

the construction is pretty simple, the hard bit is the need of a 12v wall wart/battery.  Both that and the computer need to be connected, so this might not work for some clones.

  1. put the relay in the center of the board.
  2. connect one of the coil contacts with the Vin pin, 12v.
  3. put the relay in parallel with the coil contacts, cathode towards Vin.
  4. connect the other coil contact to the collector of the 3904(right when facing the flat side).
  5. connect your Arduino pin to the base(middle pin) of the 2n3904 with the 1K resistor, for this ocd I used pin 19 aka analog 5.
  6. connect the emitter(left pin facing flat side)of the transistor to ground.
  7. connect the common pin of the relay to 5v.
  8. connect the anodes of the LEDs to the N/C and N/O pins of the arduino
  9. connect the common cathode to ground through the 100ohm resistor.
  10. connect the board to the 12v wallwart and your computer.

all you have to do now is open the Arduino IDE and upload the code.  Open the serial monitor. send “r” or “b”.  The relay should make a clicking noise and the LED will change color.

for a 5v relay, substitute 5v for 12v on the transistor.  You won’t need a wall wart.



  1. Can you provide more information on this? cheers

    • the arduino board( a microcontroller board good for beginners. a relay is an electronically controlled switch, with a magnetic coil pulling/pushing a set of contacts when power is supplied to it. This coil needs 12, so I used a transistor(less power, not electrically isolated but easier to use) to switch 12v with the 5v logic of the arduino. The Relay itself can safely switch 5amps, I think it was, at up to 250 VAC. This made it suitable for my other project, the arduino controlled wall outlet at Due to being in that blue box, I can’t recall the exact ratings. The diode is placed across the contacts of the coil with anode to ground so that reverse voltage spikes(complicated) from the relay don’t the the relay or $30 arduino. The resistor is so the transistor doesn’t drop too much voltage, and is between the arduino and base of the transistor.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: