Back to Step by Step Course by Robojax

Lesson 87: Contrlling Servo Motor using a push button Swtich

If you don't like to see ads while video is being played you can purchase YouTube premium Here

Part 8: Using Servo Motors with Arduino

In this lesson we learn how to contorl as servo motor using a push button switch. when a push button is pressed, the servo start moving to the right or left until reachers 180 and then returnes to 0 degree. Code is fully explained and demonstrated. this code is for single sensor.

  • 00:00 Introduction
  • 00:37 Wiring Explained
  • 01:57 Code Explained
  • 05:01 Demonstration

 /*
 * Lesson 87: Contrlling Servo Motor using a push button Swtich
 * Full video details: https://youtu.be/qJebLpzLE24
 * This Arduino sktech to control with a push button
 * Controlling a servo with Push button with Arduino
 when a push button is pressed, the servo start moving to the right or left until
 reachers 180 and then returnes to 0 degree.

 * Written by Ahmad Shamshiri for Robojax.com 
 * on Jan 16, 2019 at 10:06 in Ajax, Ontario, Canada

 * 
 * This code is part of Arduino Step by Step Course which starts here:  https://youtu.be/-6qSrDUA5a8
 * 
 * for library of this code visit http://robojax.com/
 * 
If you found this tutorial helpful, please support me so I can continue creating 
content like this. Make a donation using PayPal by credit card https://bit.ly/donate-robojax 
 * 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 Robojax.com
    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
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    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 <https://www.gnu.org/licenses/>.
 */
#include <Servo.h>

Servo myservo;  // create servo object to control a servo
int servoPin = 9;// this pin must be of those with PWM ~
int pushButtonPin =2;// the pin where push button is connected
int angle =25;    // initial angle  for servo
int angleStep =5;

void setup() {
  // Servo button demo by Robojax.com
  Serial.begin(9600);          //  setup serial
  myservo.attach(servoPin);  // attaches the servo on pin 9 to the servo object
  pinMode(pushButtonPin,INPUT_PULLUP);
   Serial.println("Robojax Servo Button ");
   //Full video details: https://youtu.be/qJebLpzLE24
}

void loop() {
  while(digitalRead(pushButtonPin) == LOW){
  // change the angle for next time through the loop:
  angle = angle + angleStep;

    // reverse the direction of the moving at the ends of the angle:
    if (angle <= 0 || angle >= 180) {
      angleStep = -angleStep;
    }
    myservo.write(angle); // move the servo to desired angle
      Serial.print("Moved to: ");
      Serial.print(angle);   // print the angle
      Serial.println(" degree");    
  delay(100); // waits for the servo to get there
  }// while

  //Full video details: https://youtu.be/qJebLpzLE24
}


   

The least I expect from you is to thumb up the video and subscribe to my channel. I appriciate that. .I have spent months making these lectures and writing code. You don't lose anything by subscribging to my channel. Your subscription is stamp of approval to my videos and more people can find them and in it turn it helps me. Thank you

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

**** AFFILIATE PROGRAM **** We are a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for us to earn fees by linking to Amazon.com and affiliated sites.

Right Side
footer