Introduction: Push Button Controlling Servo Motors

Picture of Push Button Controlling Servo Motors

Hi, my name is Leor Oved and today I will teach you how to wire and code my new cool instructable. In this lesson you will learn how to control servo motors with a push button and an LED. You will take an LED which blinks on and off. As the LED is on, Servo1 will turn to 10 degrees and Servo2 will turn 170. When the LED turns back off, Servo1 will turn to 170 degrees and Servo2 will turn 10 degrees. This whole process is controlled by a light.

To begin, follow all the steps provided below. Feel free to comment.

Good Luck!!

Step 1: Materials

Picture of Materials

For this project, you will need:

  1. An Arduino with a USB cable
  2. A Breadboard
  3. Male Jumper Wires (At least 11 or 12).
  4. 2 Servo Motors
  5. A Push Botton
  6. An LED
  7. A Resistor
  8. 9v adapter (Only if you are using VIN as a power source. Recommended for Servo Motors)

Step 2: Setup

Picture of Setup

Following are the steps to connect a servo motor to the Arduino:

The servo motor has a female connector with three pins.

  1. The darkest or black one is usually the ground. Connect this to the Arduino GND.
  2. Connect the red to VIN on the Arduino. (VIN is the input voltage to the Arduino board for an external power source (as opposed to 5 volts from the USB connection or other regulated power source).
  3. Connect the remaining line on the servo connector (yellow) to a digital pin (the signal) on the Arduino.,

*When using VIN as a power source, don't forget to use a 9v adapter

Step 3: Coding

Picture of Coding

#include

Servo myservo1;  //servo1
Servo myservo2;  //servo2
int pos1 = 0;  //variable for number of degrees for servo1
int pos2 = 0; //variable for number of degrees for servo2
int led = 13; //activating pin 13 for LED 
int buttonPin = 2; //activating pin 2 for Push Button 
int buttonState = 0; //variable for Push Button
void setup() {  
Serial.begin(9600); //activating Serial Monitor
myservo1.attach(9); //activating pin 9 for servo1
myservo2.attach(11); //activating pin 11 for servo2
pinMode(led , OUTPUT); //setting LED as an output	
pinMode(buttonPin , INPUT); //setting Push Button as an input
}
void loop() { 
buttonState = digitalRead(buttonPin); //reading buttonPin and storing it as buttonState.
Serial.println(buttonState); //telling computer to print buttonState. 
if (buttonState == HIGH){ //if button is pressed...
digitalWrite(led, HIGH); //turn LED on
myservo1.write(10); //setting servo1 to 10 degrees
myservo2.write(170); //setting servo2 to 170 degrees
delay(1000); //wait one second   
digitalWrite(led, LOW); //turn LED off
myservo1.write(170); //setting servo1 to 170 degrees
myservo2.write(10); //setting servo2 to 10 degrees
delay(1000); //wait one second

}

}

Comments

tliguori330 (author)2017-12-12

cool project, lots of different components, i learned alot

miberger (author)2017-12-05

Where did u get the circuit map?

leoroved (author)miberger2017-12-06

You can make it at Tinkercad

About This Instructable

343views

9favorites

More by leoroved :Controlling LED S Using Microphones TutorialPush Button Controlling Servo Motors
Add instructable to: