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

The first thing is to Download the Python IDE from the official website, version of your need. This project is relevant for python versions greater than 3.0.

At the time of implementing this project, the latest version of Python is Python 3.9.5 . You can download the 64-bit version of Python 3.9.5 from this link.

Downloading pySerial

To use Python to access the computer's Serial Port, we'll need the pySerial library. pySerial is a Python Serial Port Extension that allows Python to access the Serial Port on a variety of operating systems, including Windows, Linux, and Mac OS (OSX).

We will use pySerial to read and write data to Arduino using Python in our project. To get the file, click here  pySerial.

NOTE: Location is very important, as we will navigate this location and install the pySerial.

Extract the contents of the file to any folder like “D:\pyserial-3.5”.

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 ON




LED OFF



The LED will turn ON and OFF correspondingly by entering 1 and 0 in the python shell.







Comments

Popular posts from this blog

ESP32 Error: A fatal error occurred: Timed out waiting for packet content.

How to calculate Mean, Median and Mode in python

Detecting Obstacles and Warnings with ultrasonic sensor