Write the Python program and Run it
Python
from pyfirmata import Arduino from time import sleep # Connecting to the board board = Arduino( 'COM8' ) # initializing the LEDs led1 = board.get_pin( 'd:13:o' ) led2 = board.get_pin( 'd:12:o' ) led3 = board.get_pin( 'd:11:o' ) led4 = board.get_pin( 'd:10:o' ) # wait for 1s at every count value wait = 1 # initialise all to False (off) val_1 = val_2 = val_3 = val_4 = False # led4 is the least significant bit and led1 is the most significant bit while True : # this is an infinite loop which won't end untill the terminal is killed for ____ in range ( 2 ): for ___ in range ( 2 ): for __ in range ( 2 ): for _ in range ( 2 ): sleep(wait) # Updating the values and printing them led1.write(val_1) led2.write(val_2) led3.write(val_3) led4.write(val_4) print ( int (val_1), int (val_2), int (val_3), int (val_4)) val_4 = not val_4 val_3 = not val_3 val_2 = not val_2 val_1 = not val_1 print ( "\n\n" ) |
C++
// C++ code int led1 = 0; int led2 = 0; int led3 = 0; int led4 = 0; int counter; int counter2; int counter3; int counter4; void setup() { pinMode(13, OUTPUT); pinMode(12, OUTPUT); pinMode(11, OUTPUT); pinMode(10, OUTPUT); Serial.begin(9600); } void loop() { for (counter4 = 0; counter4 < 2; ++counter4) { for (counter3 = 0; counter3 < 2; ++counter3) { for (counter2 = 0; counter2 < 2; ++counter2) { for (counter = 0; counter < 2; ++counter) { delay(1000); digitalWrite(13, led4); digitalWrite(12, led3); digitalWrite(11, led2); digitalWrite(10, led1); Serial.print(led4); Serial.print(led3); Serial.print(led2); Serial.println(led1); led1 = 1 - led1; } led2 = 1 - led2; } led3 = 1 - led3; } led4 = 1 - led4; } Serial.println(); } |
Output:
0 0 0 0 0 0 0 1 0 0 1 0 0 0 1 1 0 1 0 0 0 1 0 1 0 1 1 0 0 1 1 1 1 0 0 0 1 0 0 1 1 0 1 0 1 0 1 1 1 1 0 0 1 1 0 1 1 1 1 0 1 1 1 1 ...
Here is the Simulation Output!
Control Arduino with Python and pyFirmata
In this article, we will learn how to link an Arduino to a Python script in order to operate the Arduino. This example of constructing a 4-bit binary up-counter using Python script to control Arduino helps us understand this better.