Pages: [1]   Go Down
Author Topic: Simple click switch operation  (Read 838 times)
0 Members and 1 Guest are viewing this topic.
Los Angeles
Offline Offline
Newbie
*
Karma: 0
Posts: 12
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Well, I've had my UNO r3 for only 2 days and even though I can envision all kinds of ambitious projects with it, I'm already struggling with simple operations.

I've got a bunch of click switches lying around and figured I'd use one to turn on an LED before I go connecting motors and such, but I'm not seeing the results I expect. Visual attached.

The click switch has three prongs: common, normally open and normally closed. I wired the C to the ground and the NO to Pin 5. Then I wired the LED to the ground rail and Pin 9. Set pin 9 to output, Pin 5 to input and run.

The issues I'm seeing are twofold: WHEN the switch works, it's opposite of what I want. Clicking turns the LED off, while leaving it untouched keeps it on. Oh, and if I take out the leads to the switch altogether, the LED seems to turn on and off by moving my hand near it.

I use a basic (modified) sketch to test it. I'm sure I'm doing something wrong and I'm hoping someone will see it immediately. Or is it possible, what with the light going on an off when my hand gets close or jiggles the board), that I've got a bad UNO or breadboard?

Sketch:
Code:

int pushButton = 5;

void setup() {
  Serial.begin(9600);
  pinMode(pushButton, INPUT);
  pinMode(9, OUTPUT);
}

void loop() {
  int buttonState = digitalRead(pushButton);
  Serial.println(buttonState);
  if(buttonState==1){
    digitalWrite(9, HIGH);
  }else{
    digitalWrite(9, LOW);
  }
  delay(300);
}


* click_switch_bb.jpg (525.88 KB, 1716x918 - viewed 16 times.)
Logged

Brisbane, Australia
Offline Offline
Edison Member
*
Karma: 33
Posts: 1123
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi

There are a few issues that jump out from this.  Firstly the switch, when activated, pulls the line connected to pin 5 to ground however there's nothing to force it to the alternate state when it's off.  One way to do this is to use a pull-up resistor.  That would look like a 10,000 Ohm (10kR) resistor between the leg of the switch that connects to D5, and the +5V rail which would make the "default" forced to 5V or HIGH when you use digitalRead() on that pin.  A very convenient way to do that is by activating the internal pull-up resistor that's a feature of the Arduino's processor.  To do that, after setting the pin to OUTPUT as you have in setup(), follow it up with the line digitalWrite(pushButton, HIGH);.  For a full description of that feature, check this page.

So that will give you a solid result to your digitalReads every time.  But be aware, buttonState will only be 1 when your switch is off (ie it's pulled high).  So swap the logic around or it will be backwards as you're seeing.

Next, be sure to use a current limiting resistor on your LED or bad things can happen to it.  A good explanation of that is available from this sparkfun page and many other places elsewhere when you google the topic.

To find all of these things in one place, checkout the Arduino tutorial series by Jeremy Blum on youtube.

All the best,
Geoff
Logged

"There is no problem so bad you can't make it worse"
- retired astronaut Chris Hadfield

Los Angeles
Offline Offline
Newbie
*
Karma: 0
Posts: 12
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks so much for taking a look at my setup. Guess I was missing more than I thought.

I see just about every tutorial suggesting a resistor for any LED I put on the board, but I've put a half dozen or more in during the basic blink and fade sketches and had no problems. The very first thing I did was put a 220 Ohm resistor between the 5v and an LED for the blink (choosing to use the breadboard, not pin 13) and the LED wouldn't light until I removed the resistor. Does that make any sense?

Anyway, thanks again and thanks for the links too. I'll be futzing this weekend, hopefully with better results.
Logged

Copenhagen / Denmark
Offline Offline
Edison Member
*
Karma: 6
Posts: 2360
Do it !
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

You should also be checking out debouncing.

http://www.arduino.cc/en/Tutorial/Debounce

This is very important for getting reliable results from switches.
Logged

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

The very first thing I did was put a 220 Ohm resistor between the 5v and an LED for the blink (choosing to use the breadboard, not pin 13) and the LED wouldn't light until I removed the resistor.

What pin exactly? What sketch? Normally you would do this:


Code:
Output pin ----> Resistor ----> LED ----> Ground

So I don't see where 5V comes into it.
Logged


Los Angeles
Offline Offline
Newbie
*
Karma: 0
Posts: 12
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

It's quite possible I screwed it up too, but I went:

Code:
Arduino 5v --> Breadboard Pos. Rail. --> 220 ohm -->LED -->Breadboard Ground Rail --> Arduino Ground

Should be constant power, correct?
Logged

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

Yes but where does "The very first thing I did was put a 220 Ohm resistor between the 5v and an LED for the blink" come into it? That wouldn't blink.
Logged


Los Angeles
Offline Offline
Newbie
*
Karma: 0
Posts: 12
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Yes but where does "The very first thing I did was put a 220 Ohm resistor between the 5v and an LED for the blink" come into it? That wouldn't blink.

I hadn't put it on the output pin yet, it was just to make sure my LED wasn't hosed. I'll give it another go when I get home tonight.
Logged

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

Hmm. Try measuring the resistor with a meter.
Logged


UK
Offline Offline
Shannon Member
****
Karma: 223
Posts: 12630
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The very first thing I did was put a 220 Ohm resistor between the 5v and an LED for the blink (choosing to use the breadboard, not pin 13) and the LED wouldn't light until I removed the resistor. Does that make any sense?

It's possible to get LEDs with integral current-limiting resistors, but it seems unlikely that you'd have got one of those without knowing it.
Logged

I only provide help via the forum - please do not contact me for private consultancy.

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

Even two resistors shouldn't be enough to not see anything. I've used LEDs with 1K resistors.
Logged


Pages: [1]   Go Up
Jump to: