Most Simplest Toggle Switch With Arduino

57,144

11

6

Introduction: Most Simplest Toggle Switch With Arduino

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 students…

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

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

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
}

Step 2: Another Revised Code Edition With Notes

/*********************

Simple toggle switch

Created by: P.Agiakatsikas

*********************/


int button = 8;

int led = 13;

int status = LOW;


void setup(){

pinMode(led, OUTPUT);

pinMode(button, INPUT_PULLUP); // setting the internal Pull up resistor of the button, that is HIGH

}


void loop(){

// Initially the button is unpressed and is HIGH (pull up) and is not equal to LOW therefore

// the inversion will be bypassed and the LED status will be LOW

//If the button is pressed its status will become LOW that means (a) status = (b) status

// Therefore status=!status will inverse and the LED will Light (HIGH)


if (digitalRead(button) == LOW) { /

status = !status;

digitalWrite(led, status);

} while(digitalRead(button) == LOW);

delay(50); // keep a small delay

}

Be the First to Share

    Recommendations

    • New Year, New Skill Student Design Challenge

      New Year, New Skill Student Design Challenge
    • Photography Challenge

      Photography Challenge
    • Anything Goes Contest 2021

      Anything Goes Contest 2021

    6 Comments

    0
    Dyso
    Dyso

    1 year ago

    I changed it abit, see picture for pinning:

    int button = 7;
    int led = 12;
    int status = false;
    void setup(){
    pinMode(led, OUTPUT);
    pinMode(button, INPUT);
    }
    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;
    if(status==1){
    digitalWrite(led, HIGH);
    }
    else{
    digitalWrite(led, LOW);
    }
    } while(digitalRead(button) == true);
    delay(50); // keeps a small delay
    }

    Capture.PNG
    0
    eski55596
    eski55596

    1 year ago

    Can you please explain the use of INPUT_PULLUP.

    0
    Mursy Jesse
    Mursy Jesse

    Question 3 years 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

    0
    eski55596
    eski55596

    Answer 1 year ago

    if the button is kept presses, WHILE Loop takes the charge and doesn't allow any further action but only after a little delay.

    0
    pagaldunia
    pagaldunia

    Question 3 years ago

    HI,

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

    0
    JoshuaM285
    JoshuaM285

    3 years 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.