Go Down

Topic: Simple click switch operation (Read 948 times) previous topic - next topic

jedimasta

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: [Select]


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

strykeroz

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
"There is no problem so bad you can't make it worse"
- retired astronaut Chris Hadfield

jedimasta

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.

MikMo

You should also be checking out debouncing.

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

This is very important for getting reliable results from switches.

Nick Gammon


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: [Select]

Output pin ----> Resistor ----> LED ----> Ground


So I don't see where 5V comes into it.
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

jedimasta

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

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

Should be constant power, correct?

Nick Gammon

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.
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

jedimasta


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.

Nick Gammon

Hmm. Try measuring the resistor with a meter.
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

PeterH


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.
I only provide help via the forum - please do not contact me for private consultancy.

Nick Gammon

Even two resistors shouldn't be enough to not see anything. I've used LEDs with 1K resistors.
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

Go Up