Most Simplest Toggle Switch With Arduino

26,410

9

3

About: An Electrical Engineering Teacher in Athens Greece. Most of these small projects here, are constructed for enhancing the learning of the use of Arduino as well as basic electricity and electronics for studen...

Simple! I hope you like it! Enjoy to fool around!

Nothing more to say than in the commenting code...

Teacher Notes

Teachers! Did you use this instructable in your classroom?
Add a Teacher Note to share how you incorporated it into your lesson.

Step 1: The Code

/*********************
Simple toggle switch
Created by: P.Agiakatsikas
*********************/

int button = 8;
int led = 13
int status = false;

void setup(){
pinMode(led, OUTPUT);
pinMode(button, INPUT_PULLUP); // set the internal pull up resistor, unpressed button is HIGH
}

void loop(){
//a) if the button is not pressed the false status is reversed by !status and the LED turns on
//b) if the button is pressed the true status is reveresed by !status and the LED turns off

if (digitalRead(button) == true) {
status = !status;
digitalWrite(led, status);
} while(digitalRead(button) == true);
delay(50); // keeps a small delay
}

Share

    Recommendations

    • Indoor Lighting Contest

      Indoor Lighting Contest
    • Make It Fly Challenge

      Make It Fly Challenge
    • Growing Beyond Earth Maker Contest

      Growing Beyond Earth Maker Contest

    3 Discussions

    0
    None
    pagaldunia

    Question 1 year ago

    HI,

    Can we connect 8 push buttons to the Arduino Uno and Control 8 outputs as LED's ?

    1
    None
    Mursy Jesse

    Question 1 year ago on Step 1

    A great help, thanks! could you please xapin thw purpose of the "while" statemet at the end? i had to remove it as the code didnt seem to work with it in

    1
    None
    JoshuaM285

    1 year ago

    Thank you for this, but I noticed two things that should be corrected.

    1. "int led = 3" is missing a ;

    2. The LED in the diagram is backwards.

    Great sample for a project I'm working on nonetheless. With a little troubleshooting I got it to work.