Using a toggle switch to move between two operations

My project has 2 modes: one uses a speaker as output, the other uses a vibration motor. The user can choose which output to use by flipping a toggle switch.

My thought right now was to connect the toggle switch to a pin and to ground, then always read it to check if its on or off. Then use if/else statements to choose the output.

I haven't used toggle switches before. Is this how they are meant to be used? Will the connection I described work?

Connect one terminal of your switch to GND, the other to a digital pin, 2 for example, then, in setup():


Then, when you read it with:


It will read LOW if closed and HIGH otherwise.

Whether the input comes from a toggle switch or a momentary contact switch makes no difference to the Arduino. it is the state of the input pin that the switch is connected to that matters.

Wire the switch to take the pin to GND when the switch is turned on and use INPUT_PULLUP in the pinMode() for the pin. digitalRead() will return HIGH or LOW depending whether the switch is open or closed.

It is common for toggle switches to have 3 contacts to allow a change between two states to be implemented in wiring but by using the Arduino this can be done using software as above so one of the switch pins is not used.