Arduino Code for 'Knob'

The code to make the servo follow the knob's position is simpler than to make it sweep.

Adafruit Arduino - Lesson 14. Knob

#include <Servo.h> 

int potPin = 0;  
int servoPin = 9;
Servo servo; 
void setup() 
void loop() 
  int reading = analogRead(potPin);     // 0 to 1023
  int angle = reading / 6;              // 0 to 180-ish

There is now a second variable called 'potPin'.

To set the position of the servo, we take an analog reading from A0. This gives us a value of between 0 and 1023. Since the servo can only rotate through 180 degrees, we need to scale this down. Dividing it by six will give us an angle between 0 and 170, which will do just fine.

This guide was first published on Dec 18, 2012. It was last updated on Nov 16, 2018. This page (Arduino Code for 'Knob') was last updated on May 04, 2015.