Controlling Arduino’s LED with Python
In this project, we integrate Arduino to Python and give a brief project that allows us to monitor Arduino's LED blinking using the Python programming language. Built around AVR Microcontrollers, Arduino is among the most capable open source electronics prototyping platforms. Python, on the other hand, is one of the most extensively used high-level programming languages in the development community.
Pairing Arduino and Python opens up a world of possibilities for projects, ideas, and blends. The Internet of Things, or IoT, is one such application that necessitates elements such as serial communication interfaces, graphical user interfaces etc.
Python Programming Language has a large collection of open source and simple to use libraries, making it the best language for IoT applications. In this project, we'll learn how to install the Python IDE on your computer and how to operate an Arduino using Python programming.
Downloading Python
Downloading pySerial
After extracting run the Windows Command Prompt with Administrator privileges and navigate to the folder where pySerial is extracted. (In my case, it is to D:\pyserial-3.5).
In order to install pySerial, type the following command in the command prompt and hit enter.
python setup.py install
After successful installation of pySerial, you can check if it is integrated to Python or not by entering the following command in the Python IDLE.
import serial
Installing Arduino with Python
We'll continue with connecting Arduino with Python now that we've successfully installed Python and pySerial. For this, we'll use Python to control the Arduino's on-board LED, i.e., we'll use the Python Programming Language and IDE to turn on the Arduino's on-board LED.
To do so, we'll first develop a simple Arduino application. The program's concept is as follows: The Arduino UNO, which is connected to a computer, checks for Serial Data and turns on or off the LED based on the data received from the Serial Port.
The program is given below:
Arduino code:
The code is pretty straightforward. The Arduino will read the data from the Serial Port and if the Data is 1, it will turn ON the LED and if the Data is 0, it will turn OFF the LED.
Now open the Python IDLE and create a new file by clicking on File – – >New File. This will open an empty text editor.
Python Code
After saving the file, we can run the file by selecting Run – – > Run Module in the editor window.
Controlling Arduino with Python
Upload the Arduino Code to the Arduino UNO. Note the COM Port to which it is connected. The same COM Port number should be given in the Python Code.
After uploading the code to Arduino, launch the Python Program. The Python Shell will be opened with message saying “Connection Established” and “Enter 1 to ON LED and 0 to OFF LED”.
LED OFF









Comments
Post a Comment