4 Simple Servo Project With Arduino
by Robert 7320 in Circuits > Arduino
40301 Views, 70 Favorites, 0 Comments
4 Simple Servo Project With Arduino
![sku_35764_1.jpg](/proxy/?url=https://content.instructables.com/F0H/F0Z5/IFR1XU55/F0HF0Z5IFR1XU55.jpg&filename=sku_35764_1.jpg)
![20151105_212232.jpg](/proxy/?url=https://content.instructables.com/FOB/HI35/IGL4D2IP/FOBHI35IGL4D2IP.jpg&filename=20151105_212232.jpg)
What you need
1. Arduino or Arduino Clone
2. 9 gram Servo
3. about 12-20 Male to Male wires
4. 2 pot (potentiometers)
5. the servo.h and VarSpeedServo.h libraries.
6. if you want to you can make a servo shield to make it a bit neater.
Controlling a Servo
![servo.jpg](/proxy/?url=https://content.instructables.com/FN8/WMQ5/IFR1XWU5/FN8WMQ5IFR1XWU5.jpg&filename=servo.jpg)
this code will make it so when you upload it the servo will turn to the right then to the left #include <Servo.h> void setup(){ Servo servoMain; { servoMain.attach(10); } void loop() { servoMain.write(180); delay(1000); servoMain.write(0); delay(1000); }
Random Servo
![servo.jpg](/proxy/?url=https://content.instructables.com/FN8/WMQ5/IFR1XWU5/FN8WMQ5IFR1XWU5.jpg&filename=servo.jpg)
this code will make it so when you upload it the servo will randomly turn left and right. #include <Servo.h> Servo servoM; long randomNumber; void setup() { servoM.attach(10); randomSeed( analogRead(A0) ); } void loop() { randomNumber = random(0,181); servoM.write(randomNumber); delay(500); }
Controlling Servo Speed
![servo.jpg](/proxy/?url=https://content.instructables.com/FN8/WMQ5/IFR1XWU5/FN8WMQ5IFR1XWU5.jpg&filename=servo.jpg)
this code will make it so the servo will turn from 0 to 180 slowly.
#include <VarSpeedServo.h> VarSpeedServoservo; void setup() { servo.attach(10); } void loop() { servo.write(0,30,true);//you can change the speed of the servo by changing the second number. servo.write(180,30,true);//you can change the angle of the servo by changing the first number. }
Controlling 2 Servos With 2 Potentiometers.
![2 servo 2 knob.jpg](/proxy/?url=https://content.instructables.com/F6F/9IBO/IGM5LPCR/F6F9IBOIGM5LPCR.jpg&filename=2 servo 2 knob.jpg)
this code makes it so when using the pots(potentiometers) the servos will turn the same amount you turn them, and you can also use a joystick instead #include <VarSpeedServo.h> VarSpeedServo servo1; VarSpeedServo servo2; int potpin = 0; int val; int potpin2 = 1; int val1; void setup() { servo1.attach(9); servo2.attach(10); } void loop() { val = analogRead(potpin); val = map(val, 0, 1023, 0, 180); servo1.write(val); val1 = analogRead(potpin2); val1 = map(val1, 0, 1023, 0, 180); servo2.write(val1); }