Introduction :
Here is how you can control your servo motor to Arduino. As you will rotate potentiometer knob, values (resistance) changes, you have to convert that value into angle either wit map function available built-in in Arduino or by formula
(pot val * 180 / 1024 ) Here 180 is max angle in degrees and 1024 is maximum value read by potentiometer and pot val is current value which is variable
Circuit Diagram :
Servo motor : Brown wire is Gnd So connect it to ground of Arduino, Red wire to +5V of Arduino and Orange Wire to Output pin (here i have used pin 9) of Arduino.
Potentiometer : For potentiometer connect 2 ends to Gnd and +5V of Arduino (doesn't matter which end where, as polarities will get exchanged). and connect middle pin to analog input (here A0).
Know More about potentiometers :
Code :
#include "servo .h" // Including servo Library
Servo myServo; // Create servo objectto control servo motor
int pot = A0; // Potentiometre attached to pin A0 (14)
int val; // variable to store potentiometer value (from 0 to 1023)
void setup() {
myServo.attach(9); // Attach servo to pin 9
Serial.begin(9600);
}
void loop() {
val = analogRead(pot); // Reading potentiometer and storing value in val
Serial.println(val); // Printing that value to serial monitor just for reference (you can skip this line of code)
val = map(val, 0, 1023, 0, 180); // Conversion of value for servo motor (0 degree to 180 degree)
myServo.write (val); // Servo will change its angle to val
delay (15); // 15 mils delay for servo to get there
}
Post a Comment