This post may contain affiliate links. See the Affiliate Disclosure for more information.
Use of information on this site is at your own risk. View the Terms of Service.

AD: This post was reviewed by Grammarly, the Best Automated Proofreader.

Recently I started learning Python, and I was amazed by the simplicity and capabilities. So, I thought let’s connect it to Arduino, and see what I can do with it.

Communication between Arduino and Python isn’t a new concept, There are many modules available for Python that help in the process. In this tutorial, I’m going to use ‘pyserial‘ package for the communication. For this you will need a basic understanding of Python and Arduino. I’m using ‘Python 2.7‘ here.

Once you learn how to communicate between these two environments you can use it for multiple projects. Python is a great tool for image processing, object detection, and color recognition which can be used in various robotics projects.

So let’s get started…

What You Need

Check The Video Tutorial

Setup Process

Arduino Python Communication
  • Facebook
  • Twitter
  • Pinterest

  1. Download and install Python 2.7.x compatible to your OS. If you use Linux, Python is probably already installed. I use Windows 10, so I installed Python for Windows.
  2. Once it is properly installed, set environmental variables so it can be accessed from CMD.
  3. Now to communicate between Arduino and Python, we need to install a Python module called “pyserial“. To install modules we use the command “pip install“.
  4. Open up a CMD terminal, and type pip install pyserial this will install the module required for Serial communication.

With these steps done, we are ready to start programming.

First Download the Arduino and Python example codes, below.

Writing the Arduino Sketch to Communicate to Python

Arduino Python Communication
  • Facebook
  • Twitter
  • Pinterest

Open up the Arduino Sketch, LED.ino.

Now select the board & correct COM port and upload the code.

After the sketch is uploaded Goto >> Tools >> Serial Monitor, Enter ‘1’ to turn the LED on and ‘0’ to turn it off.  We used the built-in LED (pin 13) in the code, but you can connect an external LED to pin 13, 220-ohm resistor, and GND.

Arduino Python Communication
  • Facebook
  • Twitter
  • Pinterest

Writing Python Script

Next, let’s write the python code. But first note the COM port to which the Arduino is connected. Mine is “COM3”. You can find it out from Tools >> Port or through Device Manager. We will need it in next step.

Open the example Python script first in Python IDLE. Goto >> Files >> Open.

Now in the script you have to make small change. The COM port to which the Arduino is connected needs to be added in the script.  In the example code replace ‘COM’ with your COM port.

Testing the Build

After writing and editing the script in IDLE, save it as LED.py (Hit Ctrl  + S to save).

Arduino Python Communication
  • Facebook
  • Twitter
  • Pinterest

Now before running the program, close the Arduino IDE. We need the COM port to be available for serial communication, so both programs can’t be running simultaneously.

Finally, you can RUN the code. Go ahead and hit F5 to run the script.

You will see a screen asking for input. Just enter ‘1’ or ‘0’ to turn LED on/off. If the LED does not respond for the first time just close the IDLE and unplug Arduino and plug it again and retry.

Arduino Python Communication
  • Facebook
  • Twitter
  • Pinterest

If you can control the LED, you have successfully established communication between Arduino and Python. Next time we will see how this can be used in different projects .

I hope you enjoyed this tutorial. Let me know what you think in the comments, below!

And, if you’re feeling generous, feel free to Send Me a Coffee.

Become a contributor for Learn Robotics. Apply Here!

How to Interface Python with Arduino
  • Facebook
  • Twitter
  • Pinterest

Find value in this article? Tip the Author!

global robotics competition online

Get Notified of the Next Competition

 

Be the first to know when we launch our next online robotics competition. Plus gain FREE robotics tips to your inbox.

You have Successfully Subscribed!

liz miller learn robotics



GIVE ME 1 WEEK IN YOUR INBOX

Gain Access to FREE robotics projects.

You have Successfully Subscribed!

Arduino eBook Free Download

GET YOUR FREE PROJECT

Enter your email below!

Get ready to build with Arduino! Check your email for the download!

Black Friday Sale Learn Robotics

Get Notified when our Black Friday Sale Drops!

Plus, Get 20% off Your First Order.

Check your email for Black Friday Specials!

Pin It on Pinterest