Control a Servo with a Two-Way Toggle Switch

Description: A two-way toggle switch with an Arduino Nano is used to control the movement of a micro servo.

Servo with Two-Way Toggle Switch

Supplies:

1 – Breadboard 830 Points
1 – Arduino Nano
1 – Two Position Toggle Switch   >> Link <<
1 – Micro Servo   >> Link <<

Arduino Code:

#include <Servo.h>
Servo myservo;
int pos = 0;
int switch_pin = 5;

void setup() {

  pinMode(switch_pin, INPUT_PULLUP);
  myservo.attach(9);

}

void loop() {

  if(digitalRead(switch_pin) == HIGH) {
  myservo.write(0);
  delay(15);
  
}

  if(digitalRead(switch_pin) == LOW) {
  myservo.write(180);
  delay(15);
  
}
}

Leave a Comment

72 − 62 =