How to Connect and Control an Arduino From Python!

LeMaster Tech
183 followers
5 Video Views·Apr 20, 2025
Check out my follow up video 'How to Make GUIs in Python for Arduino' Here! https://www.youtube.com/watch?v=Z4eUlQnh3sc
Controlling an Arduino from Python could be super useful for a wide variety of applications and using the serial control method we can easily do this in just a few lines of code! All code in both python and the Arduino IDE needed are here: https://github.com/plemaster01/PyDuino
And don't forget to leave a like, subscribe to the channel, and comment what you want to see next!
Consider becoming a super supporter of the channel at my Patreon as well!
https://www.patreon.com/LeMasterTech
Thanks for watching and goodluck with your projects!!
what's up you guys welcome back to the
channel in today's video I'm going to
show you how to control an Arduino board
from a Python
[Music]
program so if you've been following the
channel for a while you know that uh the
two most common things I make videos on
are making projects with Arduino and
home electronics and then I do a lot of
Python Programming as well and Arduino
projects natively are programmed from
the Arduino ID provided by Arduino and
the language most closely resembles C++
which honestly is fine and it's not that
hard to use some basic inputs and
outputs to do basic things but when you
get to the point where you want like
really robust goys that you've designed
yourself or even more complicated
programs where if you're like me and you
find python a much easier language to
work in than C++ then you might find
yourself oh this would be so much easier
if I could just do it in Python and so
in today's video I'm going to show you
the basics of how to set up a basic
Arduino board and then control it from a
Python program okay and just one thing
to get super clear here is that there
will always be some configuration you
need to do on the Arduino IDE for your
board even with the end goal being to
control that board's inputs and outputs
from Python and for the sake of this
demonstration I'm just going to make a
Arduino project that has two LEDs on it
a green one and a red one and we're
going to control the states of those
LEDs from python using serial messaging
okay so the physical setup of this
project couldn't be simpler you just use
two digital out puts of your choice from
the Arduino board put them into two LEDs
and then make sure they're going through
220 ohm resistors and back to ground and
that's it for the physical setup other
than that we just want to take a quick
look at the Arduino sketch for this
project also now feels like a good time
to mention that all of the code you'll
see today both in the Arduino IDE as
well as python will be available in a
GitHub link below this video if you like
the concepts we're doing here but you
want to see a deeper dive and bigger
more robust projects built both with
Arduino and python be sure to let me
know about in the comments below all
right let's just dive into the Arduino
sketch setup uh with the basic two Loop
setups um it already in here void setup
which is going to run just once when we
boot this thing up and then void Loop
which by default is going to run
continuously while the thing's on and
really the first step is we just need to
define a constant integer um for our red
pin and our green pin because we have
the red light and the green light in
these different digital outputs I have
my red pin uh my red LED on pin two and
I have my green LED on pin 4 so we'll
just Define their pins right there and
that's all we need to do ahead of time
for this specific project okay and then
in setup we only have to do we have to
do three things in total we want to do
serial. begin at a B rate of 9600 and
we're going to use that same B rate when
we connect to it from python so uh
serial. begin at a B rate of 9,600 and
then we just need to set both pin modes
uh on green pin and red pin to Output
okay because we're controlling them um
from programs so uh we are controlling
them from the Arduino even though that
seems a little counterintuitive we're
controlling them from the Arduino from
python uh and we'll see how when we get
into this Loop section so that's all we
need in the setup
section okay and inside of loop really
the first thing we want to do every time
is check if serial. available and syntax
is pretty important here uh you got have
these in parentheses um and then this
needs to have parentheses as well and it
needs to be greater than zero okay so
this means basically there is data at
the serial Port waiting for us that is
what serial. available is checking here
the length of the data being sent and
after that if statement let's open up
some new curly braces because basically
everything else we want to do to this
board is just contingent on their being
data available okay and the easiest
thing to do first is just to define a
string for our message that is going to
be equal to serial Dot and I think it's
just read string like that um and this
is going to get the data that's waiting
for us at kind of the the end of the
serial Port uh on the other side on our
Python program and it's going to read
everything in and now we just need to go
through what do we want it to do um and
actually make sure to stay in the curly
braces still because we're only doing
this if there's a message but what we'll
do is we'll say if the message is equal
to on okay so here's where your specific
app is going to be super important
because if you want something other than
just onoff control which obviously
eventually you will um you want to go
through this kind of if else if else if
else if or do like a match case
statement thing to determine what to do
with different messages and in our case
if the message is on I think we should
just tell the green light to turn on
which is a super easy and
straightforward command it's just
digital WR and then green pin and then
hi uh and then we'll do we'll click down
one level of curly brackets and we'll
say else if and now if message is equal
to off then we'll go ahead and we'll
digital digital right right my goodness
having a hard day green pin low okay and
basically now we have a red pin too we
haven't told to do anything yet so we
should right we should do an else here
that's just basically saying okay there
hasn't been uh any command of the red
pin yet well what if I send a command
accidentally that's not on and it's not
off well then what I want to do is I
want to Blink the red light indicating
that's an error message it's not a
regular status and so to set up what I
want it to do is to Blink five times so
what I'll do is I'll set up a for Loop
and in a for Loop you have to set uh
kind of three things they are
technically all optional sort of but you
need basically these three things you
need the variable and in this case I'm
going to initialize an index variable uh
and call it I then you need the
condition which for me will be I less
than 5 and then you need what happens at
the end of the loop and for me I'm just
going to add one to it so this is some
pretty specific C++ syntax Arduino IDE
syntax this is kind of uh not super
intuitive to look at which is why a lot
of people prefer python but I'm going to
do this for Loop that makes it blink
five times and so what I'm going to do
is I'm just going to use the digital
right uh commands we've already seen but
this time I'm going to say uh red pin
and I'm going to tell it to turn turn on
and then I'm going to use a little 100
millisecond delay uh and then I'm going
to tell it to turn back off so digital
right red pin low and then another delay
so that it actually turns off cuz
remember we're inside a for Loop so if
you don't put this second delay you'll
never actually see it turn off because
it's going to go low and then
immediately go back to high okay but
this is all we need to do on the Arduino
IDE side we're just saying hey if I get
a message telling the green pin to turn
on turn on if I get a message telling
the green pin to turn off turn off if I
get some other message that I wasn't
expecting to see just flash the red
light letting me know that there's an
error um okay so we are just going to
verify that it
works and I spelled digital wrong which
is why we do the verification you know
and there it's money so let's upload it
to our board and that is it from the
Arduino sketch side of things okay so
now the bulk of this project is going to
involve python setup and you can really
think of computer and python in this
case as a totally separate device just
communicating with the Arduino via
serial sort of like the Bluetooth
modules in the recent video that we did
on how to control your Arduino via
Bluetooth the basic thing we want this
project to do is scan our currently
inuse comp ports from our computer tell
us which one looks like it has the
Arduino board on it so that we can
select the Arduino and then we can send
some basic commands to the Arduino from
our Python program okay so let's take a
quick look together at how to make that
most basic version of the project all
right so from the python side of things
to connect to and control in Arduino
board kind of the first module that you
need to know about that you need to
install is you need to run pip install P
serial I already have it installed so I
don't need to run that um but that's
going to allow us to make this import
statement that will let us connect to
serial
devices okay and that import statement
is import
serial.
tools do list
ports uh and then what we're going to do
is we're going to make a variable called
ports and we're going to set equal to
serial do tools. list ports and then a
command built in called com ports okay
and this is going to get all of the
communications ports on our computer and
store those in a list inside our python
script all right now we are going to go
ahead and just set an instance of our
serial so serial instance equal to
serial. serial all right so this is just
us basically telling our program we're
using serial Communications and now
let's make an empty list for the ports
list because the format they're coming
in from this command is not the format
we want to use them as in our program
all right so the thing that we're going
to do is we're just going to say for
each one in our ports list um what we
want to do is we want to add the string
conversion of that port list of that
port to our ports list and then we're
also going to print it in the console so
to do that it's going to be ports list
that we just made. append and then just
a string version of that one list and
then let's go ahead and print the same
thing let's print the string of that one
okay so this is us going through and
it's displaying all of the comp ports we
just detected on our computer now that
we've shown the user all of the ports we
want to actually get the comport that
the user wants to use which is the user
us picking the comp Port that we see the
Arduino on and so we're going to make it
an input and just in here we're going to
say select comp port for Arduino by
number okay so we're just telling them
hey we want to see the number for the
comport for your Arduino and we're going
to bring that in as an input okay so now
just to protect our program a little bit
we'll say for I in range length of our
what did we call it ports list yeah
ports list and and what we're going to
do is we're just going to check and make
sure that the number or honestly it
could be any string of numbers and
letters right like all we asked for was
a user input so we need to make sure
that that's an actual com Port so we're
going to check the com that was just
entered from the user and verify that
it's a valid Port okay and the command
for that is really simple it's just
going to be if ports list at I and then
we're going to use Python's built-in
kind of string tool to check that it
starts with it you could just use like
the typical box bracket ETS um but
there's this built-in tool that's going
to automatically check the beginning of
your string so we might as well use it
and what we're checking for is to see if
it starts with the word com literally
coom all caps and then the string
conversion of the comp Port that we just
specified that we want to use okay so
again um inputs do come in as strings so
this Str Str not necessarily required
but we're trying to build a robust uh
project here and then we'll say okay if
it is good then the port we want to use
will be equal to and now we will just
include Comm in it because um that is uh
what we need to do to address a Serial
Port is have it C whatever and then
we'll also just print out now um we'll
also just print out that we got it so uh
we'll kind of print I'll just print use
there we go so that once we get a
healthy comp Port we have this running
list of statuses in the terminal there
telling us what's going on and now it's
time to actually set up the serial port
and that's really just
uh three easy steps it's serial inst. um
B rate and we want to set it equal to
9600 and then it's our serial instance
uh.
Port Port and that's going to be equal
to use which we just defined and then
serial dot uh serial do sorry serial
inst
dot
inst. openen okay so we're just opening
the comport and saying hey we want to
talk and now basically I don't want to
exit unless I the user actually enter
clicks types in exit or indicates that
they want to terminate the program so
we'll make a while loop that's while
true and the only way to get out of this
while loop will be if you type in the
command exit okay and so the whole time
it's true we'll be continually getting
the command which will be an input from
the user and we'll say Arduino command
and then we'll just tell them we're
looking for on or off uh uh not with a
question mark with a slash there we go
so this is us saying hey enter your
command but that we recommend on off and
then actually I'll tell them lowercase
exit so just like that um and those are
going to be the valid commands so now
the only other thing we have to do is
say that serial instance we have well
now we want to write and the data we
want to write is just the command but
it's not this simple we have to do
something called encoding and that's
because there's a standard string
transfer protocol called utf8 that we
want to send this as so it's actually
command. and code which again is just a
nice string uh conversion tool
transformation tool that python offers
but we need to say command. and code at
utf-8 which is sort of a universal text
format that might be what UTF stands for
I don't know someone look that up um but
anyways the last thing we want to do is
we want to say if the command is equal
to exit right that third option that we
gave then we want to quit the program
pram okay so there's a lot of different
ways you could uh you could go about
skinning this cat but I'm just going to
say if you typed in exit terminate the
program and actually I think even Beyond
quit we'll do exit Okay um and I think
that's all we have to do but before I
run this now uh I want to get my phone
camera going so that I can show you what
the Arduino board there we go so that I
can show you what the Arduino board is
doing when we run this program all right
so let's Boot It Up Hit
play hit play hit
run it will let me run there we go okay
and you can see I ran it and I got these
let me make that a little bigger maybe
you can't see um I got these options for
the comp ports there's serial over
Bluetooth on com 5 and Comm 6 which is
from the last Arduino project I did with
Bluetooth but the Arduino Uno currently
plugged in is an 11 so I'll type in 11
and it's going to say okay you're hooked
up to Comm 11 now give it a command and
if this is working properly then typing
on will get the green light to go on
there we go and typing off we'll get it
to turn back off which is awesome if I
type in a bad command the red light
blinks five times which is great if I
type in even if I type in on in its
lowercase it blinks cuz it's not picking
up the commands it's expecting to see so
this is great I can turn on I can turn
it off and now if I type in exit the red
light's going to Blink because it thinks
that's a bad command but also my python
function has stopped so there you go
that is the basics of the Python program
we need to write obviously you can
extrapolate this out a lot a lot a lot
into more complicated programs and more
complicated projects I definitely think
the next step for me to do as a tutorial
for you guys is to show how you can make
a slightly more complicated Arduino
project but also add a guey um in the
python side of things so this was a
basic tutorial to get your feet wet kind
of dip you into the world of controlling
our uino from Python and in future
videos we'll make more complicated
physical builds and more complicated pro
programs okay you guys so I hope you
have enjoyed this basic tutorial again
this is just sort of in the direction I
want the channel to be going building
real things doing big software projects
doing physical Hardware builds as well
and just finding fun ways to combine
them all together thanks for watching
don't forget to leave any comments or
questions uh you have below and consider
leaving a like on the video subscribing
to the channel to stay up to on all the
great content coming out and if you
would like to consider becoming a Super
supporter of the channel like my friends
Philip and Dale then check out the
patreon link down there below with the
GitHub and all the other links below
thanks so much for watching good luck
with your projects until next time
bye
