Posted on September 20, 2017 by electromaniaweb
The most effective and easy to learn programming language in today’s world is python. With millions of applications and the most user friendly interface we can almost do about anything with it.
Here I have tried to club Arduino environment with that of a python user.
First, you will need to download python on your PC to continue. Follow the following steps :-
The steps mentioned below are applicable only for windows users running either 32-bit or 64-bit OS. The installation procedure for MAC and Linux is different.
- Click on 32-bit Python-2.7.9 and this will install the 32-bit Python IDLE on your Computer. Do not download the 64-bit version or updated versions since they do not provide support for our Arduino Libraries. Even if your Computer is operating on 64-bit you can use 32-bit Python itself.
- Open the downloaded exe file and follow through the instruction. Do not change the directory in which the python is getting installed. It will be C:\Python27 by default and leave it as such.
- While the installation takes place you might get a warning from your anti-virus (if any) in that case click on allow.
Now you will need to import the Pyserial module in your IDLE.
PySerial is a Python API module which is used to read and write serial data to Arduino or any other Microcontroller.
Click on Pyserial Windows to download PySerial. The resulting download will be a exe file which can be directly installed. Do not change any setting while installing. Leave it to the default directory and default settings.
Now, let us check if PySerial is installed properly. To do this, open Python Shell again and type in
import serial. If the library was successfully installed then you should not get any error messages as shown in the picture below. If you get any errors post them on the comment section and we will try resolving it.
Inside the setup function we initialize the serial communication at 9600 baud rate and declare that we will be using the built in led as output and turn it low during program start. We have also sent a welcome message to python via serial print as shown in code "Py12":
Inside the loop function, we read whatever the data that is coming in serially and assigning the value to the variable “data”. Now based on the value of this variable (“data”) we toggle the built in led as shown in code "Py23"
The above mentioned pieces of code will be written in the Arduino IDE. The above mentioned code must be written in the python idle for windows. Next you simply have to upload the code named "Ard123" onto your python shell for controlling the on-board LED on your arduino.
For any queries on understanding the code or any step seeming unclear, drop a comment.
Comments