Introduction: How to Communicate With Arduino From a Python Script
Welcome you to Being Engineers. If you are reading this then you must have worked with Arduino. You may know python language and if you don't, well after reading this Instructable, you will be interested in learning about it. How about sending and receiving data from Arduino inside the Python shell? Sounds cool!
In this tutorial, we will show you how to do it. It is quite helpful when you process some data in Python and want a visual or physical output. Just for an example - If there is someone in the room and your script detects that through image processing, then it sends the data to an Arduino Uno so that the Arduino can send a message to your mobile via a GSM module. These types of applications require this method of communication.
Without making much delay, let's start this process.
- Python should be installed in your system.
- Arduino IDE should be installed previously.
If you want to learn Python, then do take a look at our Python Tutorial series.
Step 1: Install the Module
The communication between your system and Arduino will be taken care by a very useful python module named "Pyserial". To check if the module is installed or not, just open your Python Shell and type in the command ' import serial '. If it doesn't give any error, then it's already installed and you can go to step 4 directly.
If not then follow the steps:
- Open MY PC.
- Go to C Directory. (i.e where the python is installed)
- Open the PythonXX folder. (XX denotes the python version)
- Then go to Scripts. You should see the application file named pip2.7. If not then you might not have installed python properly. ( for python 2.7 version this is pip2.7 )
- Right click on that folder window with shift key pressed. A menu will appear. There you can find the option Open command window here. ( OR You can go to file > open command prompt > open command prompt as administrator )
- Type in the command pip2.7.exe install pyserial and press enter.
Now, the module will be installed on its own. Wait until it says Successfully Installed. When done, close that window.
You can follow the tutorial in our video if you are having trouble following this Instructable.
Step 2: Setup the Arduino
When the pyserial module is installed, plug in your Arduino Uno and do the following:
- Open Device Manager to find out in which port the Arduino is assigned.
- Open ArdionoIDE.
- Now we will be writing a simple code just to check if it works or not. So just go through the code that has been attached and upload that code to your Arduino with the proper port.
In this code, the Arduino will see if there is anything available in serial communication. If yes then it will read that data. If the data is equal to 's' then the led at pin 13 will be high for 2 sec and then it will be off again.
Step 3: Send Data to Arduino!
Once the previous step is done and the code is uploaded, keep the Arduino plugged in the same port.
Open Python Shell. Type import serial. It should not give and error. If yes go back and install the module again.
Then give a variable name, I will be giving ser1. It can be anything. Then type the command:
ser1 = serial.Serial('COMx', 9600) [ x is the com port number ]
Now we will send some data - ser1.write('s'.encode())
's' is the data, and this data have to be a string. Now after writing this, if you press enter, then the LED in PIN13 of Arduino will glow for 2 seconds. Then it will be off. If you give the same command, the same thing will happen.
So in this way you can communicate with your Arduino from a python script. Hope this instructable was helpful and if yes then please do subscribe us to our youtube channel. It motivates us to make more contents.
Channel link - www.youtube.com/c/being_engineers1
Website link - www.being-engineers.com