#include <Servo.h>
Servo myservo; // create servo object to control a servo
int potPin = A0; // analog pin used to connect the potentiometer
void setup() {
myservo.attach(9); // attaches the servo on pin 9 to the servo object
}
void loop() {
int potVal = analogRead(potPin); // reads the value of the potentiometer (value between 0 and 1023)
int angle = map(potVal, 0, 1023, 0, 180); // scale it to use it with the servo (value between 0 and 180)
myservo.write(angle); // sets the servo position according to the scaled value
delay(15); // waits for the servo to reach the position
}
{
"version": 1,
"author": "robo_AI",
"editor": "wokwi",
"parts": [
{
"id": "uno",
"type": "wokwi-arduino-uno",
"top": 100,
"left": 20
},
{
"id": "potentiometer",
"type": "wokwi-potentiometer",
"top": 100,
"left": 400,
"rotate": 90
},
{
"id": "servo",
"type": "wokwi-servo",
"left": 400,
"top": 200,
"attrs": { "hornColor": "black" },
"rotate": 0
}
],
"connections": [
["uno:9", "servo:PWM", "orange", ["v-20", "*", "h0", "h-52"]],
["uno:5V", "servo:V+", "red", ["v20", "h0", "*", "h-20"]],
["uno:GND.1", "servo:GND", "black", ["v-28","h0", "*", "h-35"]],
["uno:GND.1", "potentiometer:GND", "black", ["v-28","h0", "*", "h-44"]],
["uno:5V", "potentiometer:VCC", "red", ["v20", "h0", "*", "h-30"]],
["uno:A0", "potentiometer:SIG", "green", ["v16","h20", "*", "h-90"]]
]
}