Showing posts with label python. Show all posts
Showing posts with label python. Show all posts

Saturday, January 14, 2023

Program Arduino with Python | PyFirmata |

Before we dive deep into this topic it is very important to understand very basic of Arduino- like how to upload a program from Arduino IDE and how to write simple line of codes just to turn ON/OFF Led is good enough. If you want to learn how to do it, please checkout these 2 posts: Post 1 and Post 2.

So, let’s start today's topic:

Connect your Arduino board to your computer and then go to: File-> Examples ->Firmata -> StandardFirmata

It should open the StandardFirmata program on a new IDE. As soon as it opens just click on upload button and wait until it is completely uploaded on your Arduino UNO without errors. If you get any error write it in comment section I will try to help as much as I can.

So, what we did right now is uploaded a sketch on Arduino's memory so from now if we send commands to Arduino from Python, Arduino can understand them. 

Now we can start with the python part. I personally like using PyCharm as my default python IDE, so in this video we will use the same. When you open PyCharm click on Package installer in bottom left side of the IDE which looks like this:

Now type pyFirmata in the search console of Package installer and you can see an available pyFirmata package there:

Once you locate the right package as shown in image above you should be able to install in couple of seconds without any problem.

If you are using any other IDE just write this in your terminal :

pip install pyfirmata


Now as we have installed the package for PyFirmata what we need to do next is import the package in IDE by writing:

import pyfirmata

then we can define the Arduino board port. You can find the COM port number by going to Start-> Control Panel -> Hardware and Sound -> Device manager and after that look for “Ports” and see what port Arduino is connected to for me it is “COM7”, please check for yourself.


Now we need to write our port number in python code by writing: 

Arduino(‘COM7’)

Now we can define pin number we are interested in by writing:  

Pin3 = board.get_pin(‘d:3:p’) 

Where "d" means digital pin and then "3" is pin number and last "p" means PWM control. After this you have configured the pin now the very last step is to try manipulating values on the pin to do so try:

Pin3.write(0.1) 

In the brackets you can write anything between 0 and 1 try couple of things and then see how the Arduino responds to that. 

So that was all you needed to know to start getting hands on with python and Arduino if you want to know more about available commands and stuff pls refer to the documentation in the description. 

(https://pyfirmata.readthedocs.io/en/latest/


Stay tuned 😊


Program Arduino with Python | PyFirmata |

Before we dive deep into this topic it is very important to understand very basic of Arduino- like how to upload a program from Arduino IDE ...