# Change modes using 2 toggle switch

**URL:** <https://forum.arduino.cc/t/change-modes-using-2-toggle-switch/1280748>\
**Category:** General Guidance\
**Created:** [July 11, 2024, 8:42pm UTC](https://forum.arduino.cc/t/change-modes-using-2-toggle-switch/1280748 "2024-07-11T20:42:19Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![ryerye90](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ryerye90/32/979473_2.png) [@ryerye90](https://forum.arduino.cc/u/ryerye90)\
**Post date:** [July 11, 2024, 8:42pm UTC](https://forum.arduino.cc/t/change-modes-using-2-toggle-switch/1280748/1 "2024-07-11T20:42:19Z")

</div>

Hello

I am trying to use a 2 pin toggle switch [https://www.amazon.co.uk/dp/B07RQ4ZBQJ?psc=1&ref=ppx\_yo2ov\_dt\_b\_product\_details](https://www.amazon.co.uk/dp/B07RQ4ZBQJ?psc=1&ref=ppx_yo2ov_dt_b_product_details) to change modes for the programme I am making however i cannot get it to register a change when i flip the switch

```arduino
const int switchPin = A6;

void setup() {
  Serial.begin(9600);
  pinMode(switchPin, INPUT_PULLUP); // Enable internal pull-up resistor on A6
}

void loop() {
  int switchState = digitalRead(switchPin);
  
  if (switchState == LOW) {
    Serial.println("Switch ON");
  } else {
    Serial.println("Switch OFF");
  }

  delay(100); // Adjust delay as needed
}

```

This is the code im using to debug it, i have tried connecting the switch to 5v & A6, Gnd & A6 and A6 & Gnd using a 10k resistor but each time it just reads as switch on.

Any help would be greatly appreciated

---

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [July 11, 2024, 8:44pm UTC](https://forum.arduino.cc/t/change-modes-using-2-toggle-switch/1280748/2 "2024-07-11T20:44:32Z")

</div>

Welcome to the forum

```arduino
const int switchPin = A6;

```

Which Arduino board are you using ?

If it is a classic Nano then the A6 cannot be used as a digital input

---

<div class="post-metadata">

**Author:** ![ryerye90](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ryerye90/32/979473_2.png) [@ryerye90](https://forum.arduino.cc/u/ryerye90)\
**Post date:** [July 11, 2024, 8:58pm UTC](https://forum.arduino.cc/t/change-modes-using-2-toggle-switch/1280748/3 "2024-07-11T20:58:05Z")

</div>

Thanks for the speedy reply!!

ELEGOO Nano Board 340/ATmega Nano328P compatible with offical Nano (pack of 3) [https://amzn.eu/d/06e7V3Y6](https://amzn.eu/d/06e7V3Y6)

This is the board I'm using, I assume it's a nano classic?

---

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [July 11, 2024, 8:59pm UTC](https://forum.arduino.cc/t/change-modes-using-2-toggle-switch/1280748/4 "2024-07-11T20:59:21Z")

</div>

Yes, it's a Nano classic

A5 and A6 A6 and A7 are analogue only on that board. You could change the code to use analogRead() if you really needed to

**EDIT** - corrected pin numbers. See reply #6

---

<div class="post-metadata">

**Author:** ![ryerye90](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ryerye90/32/979473_2.png) [@ryerye90](https://forum.arduino.cc/u/ryerye90)\
**Post date:** [July 11, 2024, 9:14pm UTC](https://forum.arduino.cc/t/change-modes-using-2-toggle-switch/1280748/5 "2024-07-11T21:14:20Z")

</div>

Would I still be able to use a toggle switch. If not could I potentially use A7 instead?

Sorry completely new to all this so I really appreciate the help

---

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [July 11, 2024, 9:18pm UTC](https://forum.arduino.cc/t/change-modes-using-2-toggle-switch/1280748/6 "2024-07-11T21:18:25Z")

</div>

Sorry, I see that I made a mistake in my previous reply

It is pins A6 and A7 that can only be used as analogue inputs

If you change the code a little then you could use analogRead() to detect whether one of those pins is currently HIGH or LOW

HIGH will return a value of close to 1023 and LOW a value close to zero

---

<div class="post-metadata">

**Author:** ![alto777](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/alto777/32/317385_2.png) [@alto777](https://forum.arduino.cc/u/alto777)\
**Post date:** [July 11, 2024, 9:45pm UTC](https://forum.arduino.cc/t/change-modes-using-2-toggle-switch/1280748/7 "2024-07-11T21:45:23Z")

</div>

> [@UKHeliBob](#):
>
> HIGH will return a value of close to 1023 and LOW a value close to zero

HIGH will return a value way bigger than 512, and LOW a value way smaller.

But why use analog pins at all? Maybe when you are pinched and have run out, just use digital pins and know that most so-called analog pins work perfectly well with digital read and write.

a7

---

<div class="post-metadata">

**Author:** ![ryerye90](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ryerye90/32/979473_2.png) [@ryerye90](https://forum.arduino.cc/u/ryerye90)\
**Post date:** [July 11, 2024, 10:20pm UTC](https://forum.arduino.cc/t/change-modes-using-2-toggle-switch/1280748/8 "2024-07-11T22:20:13Z")

</div>

The only pins I have spare are A6 and A7. The rest are being used by the programme & hardware

---

<div class="post-metadata">

**Author:** ![ryerye90](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ryerye90/32/979473_2.png) [@ryerye90](https://forum.arduino.cc/u/ryerye90)\
**Post date:** [July 11, 2024, 10:25pm UTC](https://forum.arduino.cc/t/change-modes-using-2-toggle-switch/1280748/9 "2024-07-11T22:25:08Z")

</div>

So am I right in thinking I connect the toggle switch to A6 and ground and the change the original code `switchState = digitalRead(switchPin);`

To

```arduino
switchState = analogRead(switchPin);

```

---

<div class="post-metadata">

**Author:** ![alto777](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/alto777/32/317385_2.png) [@alto777](https://forum.arduino.cc/u/alto777)\
**Post date:** [July 12, 2024, 1:40am UTC](https://forum.arduino.cc/t/change-modes-using-2-toggle-switch/1280748/10 "2024-07-12T01:40:54Z")

</div>

> [@ryerye90](#):
>
> So am I right in thinking

No. Read the responses. We may have been hinting too quietly:

```arduino
switchState = analogRead(switchPin) < 512;

```

would set _switchState_ _true_ if a pulled-up input pin was connected to ground by your switch closed contacts.

a7

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [January 8, 2025, 1:41am UTC](https://forum.arduino.cc/t/change-modes-using-2-toggle-switch/1280748/11 "2025-01-08T01:41:05Z")

</div>

Closed on January 8, 2025, 1:41am UTC

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