Skip navigation

Monthly Archives: February 2010

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.