Hackster is hosting Hackster Holidays, Ep. 5: Livestream & Giveaway Drawing. Watch previous episodes or stream live on Friday!Stream Hackster Holidays, Ep. 5 on Friday!
GoScavenger
Published

Servo controlled by potentiometer

Make a cool servo controlled by a potentiometer

BeginnerFull instructions provided12 minutes145
Servo controlled by potentiometer

Things used in this project

Hardware components

Arduino UNO
Arduino UNO
×1
Male/Male Jumper Wires
×1
Rotary potentiometer (generic)
Rotary potentiometer (generic)
×1
SG90 Micro-servo motor
SG90 Micro-servo motor
Most servos have the same operating parameters.
×1
Breadboard (generic)
Breadboard (generic)
×1

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Schematics

servo_character_wipg084m8s_dxslcrC6iB.avif

Code

Untitled file

Arduino
#include <Servo.h>

// create a servo object
Servo servo; 

//analog input from the potentiometer
int potPos = A0;

void setup() {
  // link the servo to pin 9, and set the pulse width limits (544ms and 2400ms in this case)
  servo.attach(9, 544,2400);  

  //set the analog pin as an input
  pinMode(potPos, INPUT);
}

void loop() {
  //store the potentiometer position as a float
  float level = analogRead(potPos);

  //calculate analog data as a voltage
  float voltage = 5*level/1024;

  //make sure the voltage isn't outside the acceptable range
  if(voltage < 0){
    voltage = 0;
  }
  if(voltage > 5){
    voltage = 5;
  }

  //scale voltage to 180 degrees
  servo.write(36 * voltage);

  //give the servo time to move to new position
  delay(15);
}

Credits

GoScavenger

GoScavenger

6 projects • 1 follower

Comments