This is very easy, basic but important project. Because with just a button we can actually rotate things like a knob, mechanical regulator etc. Here is Circuit Diagram, Code To Create Arduino Servo Motor Control With Pushbutton Project. We are using TowerPro SG 5010 servo motor in this project but any TowerPro servo motor will work fine. Here is video of the thing :
Arduino Servo Motor Control With Pushbutton : Needed Components
You need just the servo motor, push button switch and some obvious components like a breadboard, few jumper wires etc.
Arduino Servo Motor Control With Pushbutton : Circuit Diagram and Code
This is very easy to join the components by looking at this image (we used 1K Ohm resistor in our test) :
---
We have the full project on fritzing.org for you to download all the files and also the code is on Github.
Of course here is the code too :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | #include <Servo.h> const int servoPin = 8; // Servo pin const int buttonPin = 9; // Pushbutton pin int buttonState = 0; int directionState = 0; Servo myservo; int pos = 0; void setup() { myservo.attach(8); pinMode(buttonPin, INPUT); } void loop(){ buttonState = digitalRead(buttonPin); if (directionState == 0){ if (buttonState == HIGH) { directionState = 1; for(pos = 0; pos < 180; pos += 1) { myservo.write(pos); delay(15); // waits 15ms to reach the position } } } else if (directionState == 1) { if (buttonState == HIGH) { directionState = 0; for (pos = 180; pos>=1; pos -=1) { myservo.write(pos); delay(15); } } } } |
Further Improvement Ideas
Previously we have shown you how to perform multitasking in Arduino Traffic Light With LED Display Timer With Push Button Switch.
Idea 1 :
Can you modify the project yourself so that normally there will be green light. When you will push the button, the green light will be off and yellow will momentarily on, then red light will be on. Also the timer will start and servo motor will start to sweep. At the end of swap, again red light will be off and green light will be on.
That kind of usage is normal safety precaution for industrial tools.
Idea 2 :
Similar setup like Idea 1 but normally there will be green light. When you will push the button, the green light will be off and yellow will momentarily on, then red light will be on and timer will start. When timer will end the servo will sweep.
That kind of usage is for making a mechanical knob digitally controlled with timer.
Tagged With controlling servo with arduino and switch , button operated motor program arduino , arduino servo button code , arduino servo push button , arduino push button code , arduino servo with one push button code , arduino push button servo control , arduino nano servo sweep button , arduino control servo with push button , arduino servo motor codes with buttons