2 Video Views·Apr 21, 2025
Learn to Control Servo Motors using a Potentiometer with this fun Arduino Beginner project!!
If you want to follow along with this project exactly and need the same hardware kit, you can grab it here from Amazon!
https://amzn.to/3Tgfj6i
Check out the full Arduino Tutorials Playlist on the channel for more automation and electronics content!
https://www.youtube.com/watch?v=Q7zPm2Eqtw8&list=PLsFyHm8kJsx0wNcgdb9mSZHqwQoC9HBlc
Consider becoming a super supporter of the channel and helping us grow into bigger and better projects!!!
https://www.patreon.com/LeMasterTech
The Arduino beginner project manual calls this a 'mood cue' because they suggest using the servo to point to how you're feeling, but I use it for a little more fun displaying of some cool stuff!
The circuit build is pretty straightforward, with just a few capacitors, some jumper wires, a potentiometer and a 3-wire servo motor. Then the code really just boils down to reading the potentiometer position in, mapping the raw signal into a formatted angle command, and then controlling the state of our Servo based on that!
We also sneak a pretty good lesson in there on how capacitors work!
Thanks for watching, don't forget to like the video and subscribe to the channel, and see you next time!
TIMESTAMPS:
00:00 Intro and Description
01:09 Quick Science-y Lesson: How Capacitors Work!
02:40 Physical Potentiometer - Servo Circuit Build!
04:41 Programming the Sketch!
07:00 Showcase and Outro!
welcome to Master Tech this is a
potentiometer this is a Servo and in
today's video we're going to learn to
control one of these with one of these
using an Arduino microcontroller
[Music]
so this tutorial does stand completely
on its own but if you'd like to follow
along with all the Arduino tutorials
we've done on the channel I'll leave a
link to the playlist in the description
of the video a card to it somewhere at
the top of the window now and the kit
we're using in these initial few
projects is the official Arduino starter
kit I got it from Amazon I'll put a link
to that in the description section of
the video as well if you want to follow
along exactly for this project I'm
assuming you already have your
programming software and your Arduino
and breadboard ready to go this is our
first project controlling an analog
output in this case a variable position
Servo motor for this project in addition
to your Arduino and a breadboard you'll
need some jumper wires two 100
microfarad capacitors a potentiometer
and a three wire Servo motor as well as
header pins if your Servo has a female
connection Port like mine before we dive
into the circuit build I'm going to tell
you a little bit about what capacitors
do in circuits and you know what that
means
so maybe you're familiar with or you've
heard the fact that a car burns more gas
per distance traveled on Startup than it
does while operating at steady state
speed electrical motors have a very
similar phenomenon when they start
moving they draw more current and that
can cause fluctuations in voltage in
your circuit that can damage electrical
components or just cause them to not
work properly and precisely we need the
electrical equivalent of your car's gas
tank and here's where the capacitor
comes in capacitor is basically composed
of two sheets of conductive metal very
often aluminum separated by a thin
dielectric insulating layer typically
like ceramic it essentially stores up a
small amount of charge like a mini
battery and can be used to smooth out
changes in voltage in your circuit it
charges and discharges quite a bit
faster than a battery it stores less
total energy and its mode of electrical
potential storage is actually an
electric field rather than chemical
storage like a normal battery the most
common and probably best metaphor for
how a capacitor serves your circuit is
the metaphor of a dam that's Dam YouTube
please don't flag my content in this
scenario you have water AKA electricity
flowing in at a somewhat unreliable rate
but averaging enough to power the
circuit the capacitor acts as a dam or
intermediate storage tank where a small
electric Reservoir can be stored and
then a smooth steady and reliable flow
of electricity comes out from the other
side even if the supply decreases for a
little like when you have a large
startup current draw from a motor and
that's it for this
all right so the Arduino folks call this
project mood Q in the Arduino beginner
project manual because they suggest
using the potentiometer to point your
Servo to some text that says what kind
of mood you're currently in it's not a
bad idea but I think I found a more fun
way to use the project be sure to watch
till the end to see how I use it now
let's go ahead and jump into the
physical circuit build first and then
we'll check out the code let me know in
the comments below if you like circuit
build before code or think it should be
the other way around so as always
connect your 5 volt power and ground
pins to the power rails on one side of
your breadboard and now let's look at
the potentiometer piece of the circuit
jumper 5 volts to one pin of the
potentiometer and ground to the other
and then hook up one of our 100
microfarad capacitors across the same
pins make sure if you have a polarized
capacitor and it has a stripe down one
side you hook that side to ground a
capacitor hooked up in Reverse can
explode and I do not recommend trying
that now go ahead and run a long jumper
wire from analog input pin a0 to the
Arduino board now when we rotate the
potentiometer we'll be changing the
voltage transmit to our analog pin
between 0 and 5 volts and that's it for
the potentiometer Loop so now let's
check out our Servo control Loop if you
have female connector ports on the servo
like this pop in header pins so that you
can plug the servo into your breadboard
then on the red wire hook up 5 volts the
black wire hook up ground and just like
the potentiometer Loop add a decoupling
AKA smoothing capacitor across these
pins careful to get the ground strip on
the proper side the white wire gets
hooked up to an output pin that has
pulse width modulation or pwm
capabilities essentially pulse width
modulation allows you to use a discrete
or digital on off signal to control an
analog or variable State device by
flickering the power on and off in
pulses way faster than you can detect
with the naked eye pwm pins on your
Arduino board are indicated with a
little squiggly line and I'll be using
pin 9 but any pwm pin is fine and that's
it for the physical build the servo has
a few little attachments that can go on
it so feel free to pick your favorite
now let's check our program or sketch
for the project start by importing the
servo control module by typing pound
sign include then inside angle brackets
type servo.h it's important to note
include commands don't get a semicolon
at the end like a typical line of
Arduino code then we're going to Define
our Servo by typing Servo and then the
name of our Servo I'll just call mine my
Servo Now define a few variables we'll
use throughout the project first we'll
create a constant to store a
potentiometer's pin identification which
is a0 next Define an integer we'll use
to store the potentiometer value and an
integer we'll use to store our angle
command in the setup piece of the
program which will run once at system
startup tell the code what pin the servo
will use by the command myservo dot
attach and then the pin number which
again for me is 9. then type the command
serial dot begin followed by our baud
rate or bits per second of data transfer
which is going to be 9600 and that's it
for the setup code now let's check out
the loop code which runs repeatedly the
whole time our Arduino is powered up so
we basically want to read the voltage
coming in from our potentiometer and
command the servo to an equivalent
position based on that to do this start
by using the command analog read on the
pot pin and store it in our pot Val
variable then type up a few serial dot
print lines one for the text pot value
and the second for the actual pot value
reading this will let us monitor the
potentiometer's readings once we power
up the circuit and make sure we're
seeing change as we turn it now to
convert that pot Val to a usable angle
we're going to use the command app and
map takes a total of five arguments
we're gonna store the result of map in
our angle variable and give map the
arguments of raw value which is pot Val
and then the raw Min and Max which are 0
and 1 0 2 3 then our scaled Min and Max
which are 0 and 179. now add two more
serial print commands to display the
angle similar to the pot valve and make
sure your final serial dot print command
is a Serial dot println command so that
every Loop that runs will start on a new
line now the final thing to do in the
code is just the command myservo dot
right and then write the angle command
to the actual Servo then just add a
delay of 10 to 15 milliseconds so the
servo actually has a chance to move
where it's commanded and that's the
whole project go ahead and verify it
then upload it and let's take a look at
what we get you should now have a Servo
arm that moves with your potentiometer
and has a full 180 degree range of
motion this is already super fun and the
first time I got this working I was
really just sitting here messing with
this thing forever now again the project
manual suggested you attach like a
cardboard craft labeled with a few moods
to point to the mood that you're in
that's a fun idea but I found this to be
way more fun
[Music]
and so that is going to be as far as we
build out this awesome project today if
you have any questions about anything
you saw here today be sure to let me
know about them in the comments below
this video and I will get back to you as
soon as I can if you guys want to help
me do bigger and more complex projects
in the future consider becoming a Super
supporter of the channel at my patreon
link in the description of this video as
well shout out to Dale my first patreon
supporter making this all possible also
hit the like button subscribe to the
channel ring that little notification
button hit any other buttons you can
find until next time good luck with your
projects and thanks for watching
[Music]