Servo Motor – Xu (Week 8)

 

I want to use the potentiometer to control the servo motor. When the potentiometer rotates anticlockwise triggers the motor move.

Core components

Potentiometer
Servo Motor
Wires & jump wires
Arduino board
Breadboard

Circuit

How it works
When the potentiometer rotates anticlockwise triggers the motor move. if the potentiometer rotates clockwise at max, the motor will stop.

Code

#include <Servo.h>

Servo myservo;

int potpin = A0;
int val; // variable to read the value from the analog pin

void setup() {
myservo.attach(9);
}

void loop() {
val = analogRead(potpin);
val = map(val, 0, 1023, 0, 180);
myservo.write(val);
delay(15);
}

Leave a Reply

Your email address will not be published. Required fields are marked *