How to control 360 Servo with 3 push button switches

Project Video
Project Details

This code control the 360 Servo with 3 push buttons (CW, CCW and STOP). Watch video for details.

This is not real serveo. Use stepper motor or DC motor with encoder if you need this functionality.

Project Code

 *  S05-13 Servo with 3 push buttons
 *  Demonstration of Controlling Continuous Servo (360 servo)
 *  this code allows you to control 360 degree servo using push buttons
 * Modified by Ahmad Shamshiri for Robojax
 * on Jan 17, 2019 at 22:42 in Ajax, Ontario, Canada
 Watch the video instruction for this sketch:
 Watch Introduction to  360 Servo video with code:
You can get the wiring diagram from my Arduino Course at
Learn Arduino step by step with all library, codes, wiring diagram all in one place
visit my course now

If you found this tutorial helpful, please support me so I can continue creating 
content like this. You can support me on Patreon
or make donation using PayPal

Original code by BARRAGAN 
 * Code is available at

 * This code is "AS IS" without warranty or liability. Free to be used as long as you keep this note intact.* 
 * This code has been download from
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see . 


Servo myservo;  // create servo object to control a servo
// twelve servo objects can be created on most boards

int servoPin = 9;// this pin must be of those with PWM ~

#define STOPpin 3 // push button pin  for STOP
#define CWpin 2 //  push button for CW
#define CCWpin 4 // push button for CCW

int sc[]={106, 58, 0};// servo commands are in order 
        //CCW, STOP,CW
String scText[]={"CCW","Stop","CW"};// define texts for 3 action
int statusText;
      //CW button status (CWBS)
      //CCW button status (CCWBS)
      //stop button status (SBS)

void setup() {
  pinMode(STOPpin,INPUT_PULLUP);// set pin for push button STOP
  pinMode(CCWpin,INPUT_PULLUP);// set pin for push button CCW  
  pinMode(CWpin,INPUT_PULLUP);// set pin for push button CW
  myservo.attach(servoPin);  // attaches the servo on pin 9 to the servo object
  myservo.write(sc[1]);// send STOP command
  statusText=1;// initial value is STOP

void loop() {

    CCWBS = digitalRead(CCWpin);// read status of button CCW
    SBS = digitalRead(STOPpin);// read status of button STOP
    CWBS = digitalRead(CWpin);// read status of button CW
    if(CCWBS ==LOW)
    }else if(SBS ==LOW)
    }else if(CWBS ==LOW)
}// loop 

void servoCommand(int n)
   statusText = n;
   Serial.print("Going to  ");
   Serial.print( "(");

If you found this Arduino course helpful, please support me so I can continue creating content like this. support me via PayPal