Go Down

Topic: Simple Relay. What am I doing wrong? (Read 1 time) previous topic - next topic

Sepulvader

Nov 11, 2010, 12:25 am Last Edit: Nov 11, 2010, 05:18 pm by Sepulvader Reason: 1
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:
Code: [Select]

#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;
     }
   }
 }
}


Sepulvader

Here is the project broken out on a breadboard:


stimmer

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.

retrolefty

#3
Nov 11, 2010, 05:01 am Last Edit: Nov 11, 2010, 05:06 am by retrolefty Reason: 1
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:

http://arduino.cc/en/Reference/PinMode

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.  :)

Grumpy_Mike

#4
Nov 11, 2010, 12:42 pm Last Edit: Nov 11, 2010, 12:42 pm by Grumpy_Mike Reason: 1
Quote
Good form posting both your code and a picture

Here here!  :)

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.

AWOL

#5
Nov 11, 2010, 01:44 pm Last Edit: Nov 11, 2010, 01:49 pm by AWOL Reason: 1
The Gods are pleased.
You are blessèd and righteous amongst Noobkind..
;D
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Sepulvader

Thank you all for your help!

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

Quote
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.

Quote
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.


stimmer

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

Sepulvader

Quote
The emitter of the transistor should be connected to GND, not +5v.
IT WORKS! :D

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

Go Up