How to Control a Servo With a Single Pushbutton

Introduction: How to Control a Servo With a Single Pushbutton

hi, this is my first instructable it is on how to control a single servo with a single pushbutton. I am using this circuit to animate a spiderman mask so it has moving lens. Please comment if you have any advice or uses for this circuit.

Supplies:

Servo

Jumper Wires

Arduino Uno

Arduino IDE

Step 1: Wiring the Servo

Brown wire from servo to ground

Red to 5v

orange/yellow to digital 2

one wire from the button should go to ground and the other to digital 3

Step 2: Arduino Code

#include

const int buttonPin = 3;

int servoPin=2; Servo Servo1; i

nt buttonState = 0; int flag=0;

void setup(){

pinMode(buttonPin, INPUT_PULLUP);

Servo1.attach(servoPin); }

void loop(){

buttonState = digitalRead(buttonPin);

if (buttonState == LOW) {

if ( flag == 0)

{ Servo1.write(0);

delay(500); flag=1; //change flag variable }

else if ( flag == 1){

Servo1.write(70);

delay(500);

flag=0; //change flag variable again } } }

Make it Move

Participated in the
Make it Move

Be the First to Share

    Recommendations

    • Make it Glow Contest

      Make it Glow Contest
    • First Time Author Contest

      First Time Author Contest
    • PCB Challenge

      PCB Challenge

    8 Discussions

    0
    sharmarobin504
    sharmarobin504

    7 months ago on Step 2

    Please help me if I am using 16 chanal servo controller how i controll each servo with separate switch

    0
    Penolopy Bulnick
    Penolopy Bulnick

    1 year ago

    Nice job on your first Instructable :)

    0
    Avengers3648
    Avengers3648

    Reply 1 year ago

    I am happy to hear this thank you so much

    0
    kirase
    kirase

    1 year ago

    A good start You got there. Go.. Go.. for the future. You'r on the right track.

    0
    Avengers3648
    Avengers3648

    Reply 1 year ago

    thanks for the encouragement

    0
    u20417
    u20417

    1 year ago

    Very nicely done! A short video clip of the servo being deminstrated might be an enhancement to consider on future Instructables.

    0
    Avengers3648
    Avengers3648

    Reply 1 year ago

    Thanks for the posotive feedback