Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
December 27, 2021 02:03 am GMT

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.

Arduino UNO Image

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.

Tools

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

Select COM PORT

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

Manage Libraries

It will open tab something like this,

Manage Libraries tab

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

Install firmata

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

Image description

You will get some code in your arduino IDE

Arduino Code

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

HIT Enter

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.

Image description

here resistor is optional!

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

here is the code

# import modulesfrom pyfirmata import Arduino, pyfirmataimport timeport = 'COM4' #define COM port which you choose in Arduino IDEpin = 5 # Put the number of digital pin where black wire is connected in circuitboard = Arduino(port) # define boardboard.digital[pin].mode = pyfirmata.OUTPUT # define output mode# create function to turn on and off leddef turn_on_led():    board.digital[pin].write(1) # 1 means power ondef turn_off_led():    board.digital[pin].write(0) # 0 means power offwhile 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!


Original Link: https://dev.to/pritudev/control-arduino-using-python-4c5g

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To