How to Use Python to Make Your Arduino Blink¶
In this tutorial, I will show you how to control your Arduino through Python. I will use the LattePanda with the built in Arduino chip, and show you the basics of how to make the LED light blink. Once you understand this technique, you can play around with the code to control your Arduino with python.
What you need¶
- 7-inch 1024 x 600 IPS Display for LattePanda
- Mouse and Keyboard
The latest software is available on the Arduino website. IDE (integrated development environment) allows programmers to develop software. Make sure to download the most updated Arduino IDE. I'm using Arduino 1.6.5.
Available on python website. I've used python 2.7.15 found here. I've used the following installer to install my python IDE:
Please make sure to select "add python.exe to Path" when installing 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.
Similar to the C# tutorial, we will need to upload the StandardFirmata code into 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 they have been selected, upload the code to your Arduino by clicking the arrow shown below.
Wait for the code to be uploaded.
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
It should appear like this
Type the following command in your command prompt window:
pip install pyFirmata
The following message will be shown if the installation is successful.
Open IDLE Python to check whether or not the pyfirmata package has been installed correctly.
Enter the following command:
If no error message appears, it means that firmata has been successfully installed. Please refer to the picture below.
Create a new python file (File -> New FIle) in the IDLE program, then enter the following code. Change 'COM3' into the port that was shown on your device previously on Arduino. Pin 13 is selected as it is the default pin with the built in light.
from pyfirmata import Arduino, util import time board = Arduino('COM3') while True: board.digital.write(0) time.sleep(1) print("on") board.digital.write(1) time.sleep(1) print("off")
The IDLE window should display on/off messages corresponding to the light. Feel free to play around with other pins to explore your options. Here is the GIF again on how your Arduino should blink. Enjoy!