Introduction: 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
For this project, you will need:
- An Arduino with a USB cable
- A Breadboard
- Male Jumper Wires (At least 11 or 12).
- 2 Servo Motors
- A Push Botton
- An LED
- A Resistor
- 9v adapter (Only if you are using VIN as a power source. Recommended for Servo Motors)
Step 2: Setup
Following are the steps to connect a servo motor to the Arduino:
The servo motor has a female connector with three pins.
- The darkest or black one is usually the ground. Connect this to the Arduino GND.
- 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).
- 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
#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
}
}