Simple Relay. What am I doing wrong?

Hello!
I'm new to electronics. I'm building a project so a relay emulates a mouse click. I've soldered two wires off the left mouse button. I can touch the wires together and it works like a charm. The plan is to have an arduino-controlled relay complete the circuit. Almost all the relay-related posts deal with AC. I'm dealing with an USB mouse. The relay has NClosed and NOpen posts. Just to check, I hooked the mouse to NC and it clicked (and held) like I expected.

My parts list:

  • Radio Shack 5VDC Micromini Relay (12P08)
    IN4004 Rectifier Diode
    2N2222 Transistor
    1K Resistor

My code:

#include <Servo.h> 
#include <Wire.h>
int mousePin = 7; 
int baudRate = 9600;

void clickMouse()
{
  Serial.println ("Mouse Press");
  digitalWrite(mousePin, HIGH);
  delay (500);
  Serial.println ("Mouse Release");
  digitalWrite(mousePin, LOW);
}

void setup() 
{ 
  Serial.begin(baudRate); 
}

void loop()
{
  if (Serial.available() > 0) {
    int serialInput = Serial.read();
    switch (serialInput) 
    {
    case 'c':
      {
        clickMouse();
        break;
      }
    }
  }
}

Here is the project broken out on a breadboard:

Looking at the photo it seems like you have the right hand connection to the relay coil going to GND whereas it should be at +5v.

You forgot to set up the I/O pin as an output pin, all I/O pins default to be input pins when reset or powered on. Add the following line in your setup function:

pinMode(mousePin, OUTPUT); // sets the digital pin as output

Even on I/O pins that you might be going to use as input pins, it's a good idea to set them as input pins in your setup function for better understanding of the code for yourself and others in the future.

Read all about pinMode function:

Lefty

PS: Good form posting both your code and a picture of the wiring when asking for help or advice. Saves lots of time for all involved and a great example for other newcomers to emulate. :slight_smile:

Good form posting both your code and a picture

Here here! :slight_smile:

To make it perfect you would only have had to select your code in the reply box and hit the # icon, the code would then appear in a scrolling box.

The Gods are pleased.
You are blessèd and righteous amongst Noobkind..
;D

Thank you all for your help!

...select your code in the reply box and hit the # icon

Done!

Looking at the photo it seems like you have the right hand connection to the relay coil going to GND whereas it should be at +5v.

Re-routed per your post.

You forgot to set up the I/O pin as an output pin

Ah, my mistake. Corrected.

I'm still not getting any response from the relay. I tested continuity on a multimeter and not a twitch.

The emitter of the transistor should be connected to GND, not +5v. (That is assuming it is an NPN transistor.)

The emitter of the transistor should be connected to GND, not +5v.

IT WORKS! :smiley:

I'll post up final build code/photos. All of you make this fun and it's the supporting environment which will keep attracting more tinkerers.

-S