Control Arduino using Python

Arduino is the most popular thing in those days and you can do so many things using arduino, but using arduino with combination of python gonna blow up i mean you can almost do anything you want using arduino with python and in this blog we are gonna control our led using Arduino UNO, Python and pyfirmata package for arduino.

This is arduino uno make sure to connect arduino uno with your computer via arduino cable. i guess you mught might have some basic knowledge of arduino and python!

After connecting arduino with python open main arduino software.

And then select COM port where you have connect your arduino mine is COM4 you should check yours!

Then you gotta click on Tools and then Manage Libraries, or press ctrl + shift + i

It will open tab something like this,

Then click on search bar and search for firmata, select the latest version and press enter

nice now close that tab and go to file tab, then goto examples then firmata then standardFirmata.

You will get some code in your arduino IDE

just hit Upload but make sure that your arduino is connected to your computer perfectly and main COM port is selected!

work of arduino ide is done now open your favourite code editor!

and create a new python file automation.py

now get an LED yourself and put small rode of led inside gnd port in arduino and pur bigger rode inside digital pin 5.

here resistor is optional!

now it's time to code in our automation.py file!

here is the code

# import modules
from pyfirmata import Arduino, pyfirmata
import time

port = 'COM4' #define COM port which you choose in Arduino IDE
pin = 5 # Put the number of digital pin where black wire is connected in circuit
board = Arduino(port) # define board

board.digital[pin].mode = pyfirmata.OUTPUT # define output mode

# create function to turn on and off led
def turn_on_led():
    board.digital[pin].write(1) # 1 means power on
def turn_off_led():
    board.digital[pin].write(0) # 0 means power off

while True:
    turn_on_led() # call the function
    time.sleep(2) # wait for 2 seconds and turn off led! 
    turn_off_led() # call the function

So yeah that was the whole code and procedure to control LED light using python and arduino make sure to follow!

21