Pages: [1]   Go Down
Author Topic: Simple Relay. What am I doing wrong?  (Read 1241 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 4
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
[/list]

My code:
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;
      }
    }
  }
}

« Last Edit: November 11, 2010, 11:18:24 am by Sepulvader » Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 4
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Here is the project broken out on a breadboard:

Logged

Offline Offline
God Member
*****
Karma: 32
Posts: 507
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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


Left Coast, CA (USA)
Offline Offline
Brattain Member
*****
Karma: 361
Posts: 17263
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.  smiley
« Last Edit: November 10, 2010, 11:06:18 pm by retrolefty » Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 603
Posts: 33408
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Good form posting both your code and a picture
Here here!  smiley

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.
« Last Edit: November 11, 2010, 06:42:24 am by Grumpy_Mike » Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 291
Posts: 25860
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The Gods are pleased.
You are blessèd and righteous amongst Noobkind..
 ;D
« Last Edit: November 11, 2010, 07:49:56 am by AWOL » Logged

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

0
Offline Offline
Newbie
*
Karma: 0
Posts: 4
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

Logged

Offline Offline
God Member
*****
Karma: 32
Posts: 507
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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


0
Offline Offline
Newbie
*
Karma: 0
Posts: 4
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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
Logged

Pages: [1]   Go Up
Jump to: