Pages: [1]   Go Down
Author Topic: Switch giving wrong/no output  (Read 596 times)
0 Members and 1 Guest are viewing this topic.
Perth, WA
Offline Offline
Newbie
*
Karma: 0
Posts: 9
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I've been trying to follow LadyAda's tutorial on switches (http://www.ladyada.net/learn/arduino/lesson5.html) as best I can, though I'm getting some very strange results. I've attached a picture of my set up.

First of all, I am confused by the wiring because I am using a different switch to the tutorial. My switch has 3 pins (C-NO-NC) and from what I could find online C has the power source attached, NO means it returns LOW for an open switch and NC will return HIGH until pressed. With this in mind I've attached 5V to C, and NO to the breadboard.

Quote
int goPin = 2;

int goState;       //The current state of the switch
int val;           //Stores the current reading of the state of the switch


void setup() {
  Serial.begin(9600);
  pinMode(goPin, INPUT);           //Sets pin 2 to read the switch
  goState = digitalRead(goPin);    //Gets the current state of the switch
  Serial.println("Connection established..");
}

void loop() {
  val = digitalRead(goPin); //Read input value and store it in val
  
  if (val != goState) {
   if(val == LOW) {
    Serial.println("Button just pressed");
   } else {
     Serial.println("Button just released");
   }
  }
  
  goState = val;
}  


The problem I'm having is that nothing seems to happen in the serial monitor when the button is pressed. I have "Connection established" programmed to display so I know that it is communicating correctly, which it does. However I do get a HUGE amount of printing to the serial monitor whenever I touch the 10ohm resistor, or the yellow wire going from NO to the breadboard.

Can someone please explain where I might be going wrong?

Cheers



* forum.jpg (458.04 KB, 717x538 - viewed 7 times.)
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 484
Posts: 18767
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

What does the 10 ohm resistor do, for a start? It looks like the breadboard would short it out.

What does the 10K resistor do? It just drains some of the current (0.5 mA) from +5V to ground but doesn't do much else.

I'm not that keen on the twisted connections on the switch, might not be doing much - try soldering the wires on.

Quote
C has the power source attached, NO means it returns LOW for an open switch and NC will return HIGH until pressed.

C is common. NO is connected to C when you close the switch (it is Normally Open) and NC is connected otherwise (it is Normally Closed).

Throw away the 10 ohm resistor, that isn't doing anything. Connect the 10K between the C point and 5V. Connect the NO point to ground. Connect the C point to pin 2.

Now the C will have a pull-up (the 10K) which will make it high unless you push the switch. That then grounds it, giving you a low.



Quote
However I do get a HUGE amount of printing to the serial monitor whenever I touch the 10ohm resistor, or the yellow wire going from NO to the breadboard.

You are just measuring noise, because you have neither a pull-up nor pull-down on the pin.
« Last Edit: May 09, 2011, 02:27:55 am by Nick Gammon » Logged


Perth, WA
Offline Offline
Newbie
*
Karma: 0
Posts: 9
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks so much for all that, I was actually trying to create a pull-up on C through following the tutorial but I obviously went wrong. I still haven't soldered the switch connections because I don't want to solder until I have a 'working' circuit.

I'm fairly sure I've created the circuit that you've showed me, however the Arduino turns off whenever I connect 5V and turns back on as soon as I remove it. This makes me think I'm doing something unhealthy to the Arduino..

EDIT: I've attached a picture of what I made based on your diagram.


* forum2.jpg (472.73 KB, 717x538 - viewed 8 times.)
« Last Edit: May 09, 2011, 03:44:30 am by waley » Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 484
Posts: 18767
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Look carefully at what you've done. All the holes in a line are connected together. In other words, both sides of the 10K resistor are connected together. So it doesn't do anything.

Meanwhile you've connected the NC part of the switch to ground, and the C pin to 5V. So the moment you power it up you are shorting out the power supply.*

You haven't done what I had in the diagram at all. For one thing, I had nothing connected to the NC pin. Attention to detail is everything.

*(edit) You are doing something unhealthy to your battery / power supply / computer. NC means normally closed. So I don't know why you would wire one end of a switch to +5V and the NC end to the ground. You may as well just short out your battery with a wire.

See attachment - the red lines show where the electrons are going to go.


* switch_problem_60648.jpg (57.33 KB, 500x375 - viewed 9 times.)
« Last Edit: May 09, 2011, 04:35:54 am by Nick Gammon » Logged


Perth, WA
Offline Offline
Newbie
*
Karma: 0
Posts: 9
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thank you very much for taking the time to help me. I did make some very stupid errors in my 2 previous circuits, it hadn't occurred to me to think of the electron flow like that.

I downloaded Fritzing and built your circuit diagram and then rendered a 'real life' breadboard version and saw how I went very, very wrong. You have taught me plenty and today's learning curve has pretty steep for me, thanks! It's works like charm smiley
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 484
Posts: 18767
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Glad I could help!

Oh, and just for future information, the resistor you labelled 10K which is brown/black/red. That is 1K.

Brown = 1, black = 0, and red = x 100. So 1000 ohms, not 10K ohms.
Logged


Pages: [1]   Go Up
Jump to: