How do I program my toggle switch to Arduino leonardo?

I am connecting one pin of the toggle switch to pin 9 and another to ground in Arduino leonardo, will this code work ?And I am not using any LEDs ,is that alright? I don't know much about Arduino programming and I am trying to use this switch for flight simulator.

int button=9;

void setup() {
pinMode(button, INPUT_PULLUP);

}

void loop() {
int state = digitalRead(button);
}

Wired like this should work. The input will read LOW when the switch is closed (as shown) and HIGH when the switch is open.

spdt dig in

in setup():
pinMode(switchPin, INPUT_PULLUP); // enable internal pullup

in code:
bool switchState = digitalRead(switchPin); // read pin state

Edit; changed to SPDT in schematic from SPST. Thanks for the catch @anon57585045

Your code should work correctly. Just remember that state==LOW means the switch is closed and state==HIGH means the switch is open.

const byte ButtonPin = 9;

void setup() {
pinMode(ButtonPin, INPUT_PULLUP);
}

void loop() {
int state = digitalRead(ButtonPin);
}

Nitpick, but you show a SPDT.

I shall fix that, thanks.

How do I connect toggle switch to Arduino leonardo?Can I connect one pin to any of the digital pins and another to ground ? And I am not using any LEDs and resistance is that alright ? I am trying to use toggle switch for flight simulator.

Yes.

No resistors needed just enable the internal pull up resistors when you use the pin mode call on the input pin in the setup function.

This is my code will this work then?

int button=9;

void setup() {
pinMode(button, INPUT_PULLUP);

}

void loop() {
int state = digitalRead(button);
}

Looks like it will

Why did you start a new thread. There were answers in the other thread. Did I waste my time answering your question?

@aaditya1, do not cross-post.

Threads merged.

I was trying to get the code and how to connect it ,so I posted in 2 different categories .I apologize for that and I didn't actually understand your explanation .sorry about that.

ballscrewbob has a "stock" explanation, but I will develop it.

"Cross posting" refers to posting more than one thread on either the same or different forum sections, regarding parts of the same project.

Answers may be provided for each of the aspects discussed, but a number of things happen. It is often necessary to ask the OP (Original Poster) for more information regarding the project though that information was given in an alternate posting. Since the question postings overlap, different people may go to some trouble - quite possibly up to half an hour or more of effort - to draft a detailed explanation of the same solution, and these unfortunate consequences overlap, excess expenditure of time simply accounting for the lack of information that was provided in the wrong place.

So such cross posting is essentially, simply being rude to those who are trying to help by wasting their time. A single discussion of a project where all the necessary background is provided - even if it requires back and forth question and answer and the introduction of further problems - is the most efficient approach because it is concise and contained. :grin:

There is a tutorial on this site for using switches and other items as inputs, written by Perry - have a look for it , it gives you the answers you’ll need and sample code

Then you could have asked for clarification. I would have gladly provided it.