How to connect 10 toggle switches to an arduino board?

Hello,

I am in the process of making a remote control. I would like to connect 10 toggle switches (see image)


to my arduino boards so that I have 10 separate digital inputs of 0 or 1. How should I connect these? Is that also with resistors? If so, how?
Thanks in advance!

Greetings Yorbe

Center pin to GND, one of the other pins to an Arduino pin. Use the built-in pull-up resistor.

Yes, but you have to use also the VCC right?

No, no VCC needed.

Example code:

#define SWITCHON LOW

const uint8_t switchPins[] = {2, 3, 4, 5, 6, 7, 8, 9, A0, A1};

void setup()
{
  Serial.begin(115200);
  for (uint8_t cnt = 0; cnt < sizeof(switchPins) / sizeof(switchPins[0]); cnt++)
  {
    pinMode(switchPins[cnt], INPUT_PULLUP);
  }
}

void loop()
{
  for (uint8_t cnt = 0; cnt < sizeof(switchPins) / sizeof(switchPins[0]); cnt++)
  {
    uint8_t pinStatus = digitalRead(switchPins[cnt]);

    Serial.print(F("Switch "));
    Serial.print(cnt);
    if (pinStatus == SWITCHON)
    {
      Serial.println(F(" is on"));
    }
    else
    {
      Serial.println(F(" is off"));
    }
  }
  delay(1000);
}

The code starts with a macro to make your code a little more readable. LOW and HIGH just mean that, SWITCHON makes it clear that LOW means that the switch is on.

The two for-loops use a calculation (sizeof(switchPins) / sizeof(switchPins[0]) to calculate the number of pins and iterate through the pins.

It's there but it's connected through a resistor internal to the processor. See S3 in the diagram below.

1 Like

FYI

  • You also have the option of using one GPIO pin connected to an IR receiver.

image

@LarryD , is the image posted by @dougp yours? I would like to give credit if I use it.

  • Yes I drew it up.
    It can be found here:
  • It’s just a summary I put together.
2 Likes

It is VCC when you set pin mode to INPUT_PULLUP

pinMode( pin, INPUT_PULLUP );

that pin sources VCC through 20K to 50K resistance. It is very weak but it is VCC that you can safely ground. The pin reads LOW when grounded.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.