Skip to content

Instantly share code, notes, and snippets.

@psychemist
Last active December 5, 2025 13:44
Show Gist options
  • Select an option

  • Save psychemist/bf0c97baa071391200042c0bf7386653 to your computer and use it in GitHub Desktop.

Select an option

Save psychemist/bf0c97baa071391200042c0bf7386653 to your computer and use it in GitHub Desktop.
Arduino Starter Project 5
// import Servo library
#include <Servo.h>
// create Servo object from library
Servo myServo;
// declare variables for potentiometer pin, analog input value and servo angle
int const potPin = A0;
int potVal;
int angle;
void setup() {
// connect servo and digital pin 9
myServo.attach(9);
// initialize serial port between board and computer
Serial.begin(9600);
}
void loop() {
// read out analog input and read to serial monitor
potVal = analogRead(potPin);
Serial.print("PotVal: ");
Serial.print(potVal);
// scale analog potentiometer values to usable range
angle = map(potVal, 0, 1023, 0, 179);
Serial.print(", Angle: ");
Serial.print(angle);
// move servo to specified angle
myServo.write(angle);
delay(15);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment