Toggle Switch

Hi everyone, I really need your help ... Do anyone know how to connect such a switch to arduino.

Here's my code for the switch to work:

int switchPin = 8;
int ledPin = 11;
boolean lastButton = LOW;
boolean currentButton= LOW;
int ledLevel = 0;
 
void setup()
{
  pinMode(switchPin,INPUT);
  pinMode(ledPin,OUTPUT);
  digitalWrite(ledPin,LOW);
}

boolean debounce(boolean last)
{
  boolean current = digitalRead(switchPin);
  if ( last != current)
  {
    delay(5);
    current = digitalRead(switchPin);
  }
  return current;
}


void loop()
{
 currentButton = debounce(lastButton);
 if(lastButton == LOW && currentButton == HIGH)
{
 for(ledLevel = 0 ; ledLevel < 255 ; ledLevel ++)
 {
   analogWrite(ledPin,ledLevel);
 }
}
 lastButton = currentButton;
 

}

So from the code , basically I want to switch on and let the led increase it brightness steadily and off whenever I want. So I what I need is a toggle ON- OFF switch only.

What I usually do with switches is to connect the switch between the IO pin and ground, put the IO pin into input mode
pinMode(pin, INPUT);
then pull the pin up internally
digitalWrite(pin, HIGH);
So when the switch is ON the IO pin is grounded and thus will read LOW, when the switch is OFF the internal pullup asserts the pin HIGH when read.

You just have to be sure you NEVER put the pin into output mode and set it high or you'll blow the pin. You can avoid that possibility (old sketch on the arduino for example) by connecting a 220 ohm resistor between the switch and ground. Or configure it a different way with very high resistor like 1M between the IO pin and ground, and using the switch between +5V and the IO pin. Then the pin will read HIGH when the switch is on on and LOW when the switch is off. Probably best to use a lower resistor between the switch and +5V as well for safety.

First circuit almost correct, except that the resistor goes between the switch and the Arduino pin.

Paul__B:
First circuit almost correct, except that the resistor goes between the switch and the Arduino pin.

Yeah sorry, you're right. It wouldn't work the other way around would it?
Picture amended. Apologies to Paul if I put the switch common in the wrong place.

Remember never to catch java.lang.Exception as well.

See: How to connect  toggle switch - Interfacing - Arduino Forum

Hi, I think what luckystar wants is to know how to connect THAT switch pictured.

If it is like most miniature toggles switches it will be set up so that the centre terminal is common.
The outer terminals will be connected to the common depending on which way the toggle is set.
So to use it as a simple on off switch, use the centre and on of the outer terminals.
If you have a DMM you can quickly work out which way the toggle goes to connect either outer terminal to the centre.

Tom..... :slight_smile:
Steinie beat me by 9 seconds..

BapZannigan:
Yeah sorry, you're right. It wouldn't work the other way around would it?

It would work by all means, but it would not be achieving the maximum protection under all (adverse) circumstances.

Paul__B:

BapZannigan:
Yeah sorry, you're right. It wouldn't work the other way around would it?

It would work by all means, but it would not be achieving the maximum protection under all (adverse) circumstances.

Too right, well spotted.

Hi, I think what luckystar wants is to know how to connect THAT switch pictured.

Looks the same to me.
Then he can hook up the other side to another pin and have On/Off/ON.

If it's a DPDT switch then do this
http://highlightelectronics.com.au/products.php?product=5pcs-x-New-heavy-duty-15A-DPDT-double-pole-double-throw-toggle-switch-high-quality
Depends on what he wants to do.
He says On/Off so the above link will do that.

Hi, steinie44, my posting was made to this thread at the same time as yours, you just hit the save button, 9 seconds earlier than me.
No offense mate.
(Great minds think alike>)

Tom...... :slight_smile:
Damn neuropathy.

No problem Tom.