Blinking LED python program for Arduino

Explanation of Blinking LED python program for Arduino UNO

In the previous programming Arduino using Python we explained the process of installing necessary programs and libraries and illustrated with an example of a simple LED blinking python program. We did not explain the code however. So in this tutorial the code for that blink led python program will be explained.

The python program code is shown again below.

Here the first import line imports the pyfirmata library or module. This is required for communication of python program to the Arduino using the Firmata protocol. But before we can use it, the Arduino must first be installed with Firmata protocol. Also the pyfirmata module is a class which has defined different attributes and methods for working with Arduino like board, port, pin assignment, read and write etc. We will use these attributes and methods in down the program.

The next line is also an import statement where we have imported the time library or module. This was imported just for creating time delay of specified seconds between the LED ON and LED OFF state. We could optionally have created a python function that implements the required delay. But we used the inbuilt time module.

The next statement is pin = 6. This statement just creates a variable called pin and assign to pin 6. Similarly the next statement port = ‘com3’ is another variable declaration where we have assigned com port 3 to variable called port. This is needed when creating Arduino object in the next line.

The next statement is the board = pyfirmata.Arduino(port). This statement is creating an object called board using the pyfirmata function Arduino() where port variable created earlier is passed as argument. Now the Arduino() function accepts other argument parameters also. Other parameters inlcude layout, baudrate, name and timeout. See the pyFirmata documentation for details.

Once we have the board object we can use various methods defined in the class pyfirmata for accessing arduino pins, read and write etc. For example, in our case we have used the digital[pin].write(1) to write HIGH value to the arduino pin 6 using digital pin 6. So this specific statement is like using digitalWrite(HIGH) in Arduino IDE program(see digitalRead and digitalWrite). There are other method to write high value to the Arduino pins which we will show in next tutorials.

Then we have the while loop. This loop continuously executes the statement within with it. In python we write such continuous loop using while True: followed by the statements we want to execute continuously. Within the loop we have used the board.digital[pin].write(1) and then used the delay of 0.2 second using the time.sleep(0.2) method. Similarly after writing high we wrote low using the board.digital[pin].write(0) and again using the time delay time.sleep(0.2).

The last line board.exit() is used to exit the program and deleting the board object created. This flushes the variables stored in the memory of the computer and releasing any variables, ports that are still connected to the board object.

What do you think?

0 points
Upvote Downvote

Total votes: 0

Upvotes: 0

Upvotes percentage: 0.000000%

Downvotes: 0

Downvotes percentage: 0.000000%

Leave a Reply

Your email address will not be published. Required fields are marked *

Programming Arduino using Python

Programming Arduino using Python

How to read and write to Arduino using Python

How to read and write to Arduino using Python