In the previous post, I use three tactile switches to rotate a servo motor to three angle positions. However, using a potentiometer, we can continuously rotates the angle of servo motor in accordance to the adjustment of the potentiometer.
![]() |
A picture of this example |
With the Arduino Servo.h and analogRead function, this task could be done easily. In this example, a potentiometer connects to pin A0 work as an analog input adjusting the angle of rotation. A servo again, connects to pin A3.
![]() |
Connections Diagram |
The analogRead() function return a 10-bit (1024) value from any reading from a specific analog input pin. Hence, this analog value must convert from 1024 to 180 in degree. This could be done by a simple equation as will be shown in source code.
#include<Servo.h> | |
//Create A servo object | |
Servo sg90; | |
//Servo connects to A3 | |
#define sg90Pin A3 | |
void setup() { | |
sg90.attach(sg90Pin); | |
} | |
void loop() { | |
//Read analog value from a POT | |
long angleInput=analogRead(A0); | |
//Convert to angle | |
angleInput=angleInput*180/1024; | |
//send angle value to servo | |
sg90.write(angleInput); | |
delay(50); | |
} |
Click here to download the sketch archive of this example.
No comments:
Post a Comment