How to Use Python to Make Your Arduino Blink¶
Introduction¶
In this tutorial, we will show you how to control your Arduino using Python. We will use the LattePanda with the built-in Arduino for this example and show you the basics of how to make the LED light blink. Once you master this technique, you can play around with the code yourself to control your Arduino with python code.
What You Need¶
Hardware
- LattePanda
- 7-inch 1024 x 600 IPS Display for LattePanda
- Mouse and Keyboard
Software
- Arduino IDE
The latest software is available on the Arduino website. IDE stands for integrated development environment, and it allows programmers to develop software. Make sure to download the most updated Arduino IDE. This example uses Arduino 1.8.5.
- Python 2.7.15
This software is available on Python's official website. This example uses python 2.7.15, which can be found here. I've used the following installer to install my python IDE:
When installing, please make sure to select "add python.exe to Path" by clicking on the "Will be installed on local hard drive" option. This is shown below.
Once everything is ready, we can start the process.
Step 1¶
Similar to the C# tutorial, we will need to upload the StandardFirmata code onto your local Arduino device. You can access the code by clicking File → Examples → Firmata → StandardFirmata, as shown below.
After the code appears, select the correct Arduino type and port, as shown below.
Finding the correct Arduino type
With the corresponding port
Once these have been selected, upload the code to your Arduino by clicking the arrow shown below.
Wait for the code to be uploaded.
Step 2¶
The next step is to install the firmata package through the command prompt window. Make sure to right-click and run the command prompt as administrator.
After clicking this
This window should appear
Type the following command into your command prompt window:
pip install pyFirmata
The following message will be shown if the installation is successful.
Step 3¶
Open IDLE Python to check whether the pyfirmata package has been installed correctly.
Enter the following command:
import pyfirmata
If no error message appears, this means that pyfirmata has been successfully installed. Please refer to the picture below.
Create a new python file through the IDLE program and enter the following code. Change 'COM3' into the port that was shown on your device previously on Arduino. Pin 13 is selected as the default pin, as it has the built-in light.
Code:
from pyfirmata import Arduino, util import time board = Arduino('COM5') board.digital[13].write(0) time.sleep(1) print("on") board.digital[13].write(1) time.sleep(1) print("off")
The IDLE window should display on/off messages which represent the status of the light. Feel free to play around with the other pins and explore the different options available to you. Here is the GIF again on how your Arduino should blink. Enjoy!