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 c…


This content originally appeared on DEV Community and was authored by pritudev

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 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!


This content originally appeared on DEV Community and was authored by pritudev


Print Share Comment Cite Upload Translate Updates
APA

pritudev | Sciencx (2021-12-27T02:03:13+00:00) Control Arduino using Python. Retrieved from https://www.scien.cx/2021/12/27/control-arduino-using-python/

MLA
" » Control Arduino using Python." pritudev | Sciencx - Monday December 27, 2021, https://www.scien.cx/2021/12/27/control-arduino-using-python/
HARVARD
pritudev | Sciencx Monday December 27, 2021 » Control Arduino using Python., viewed ,<https://www.scien.cx/2021/12/27/control-arduino-using-python/>
VANCOUVER
pritudev | Sciencx - » Control Arduino using Python. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/12/27/control-arduino-using-python/
CHICAGO
" » Control Arduino using Python." pritudev | Sciencx - Accessed . https://www.scien.cx/2021/12/27/control-arduino-using-python/
IEEE
" » Control Arduino using Python." pritudev | Sciencx [Online]. Available: https://www.scien.cx/2021/12/27/control-arduino-using-python/. [Accessed: ]
rf:citation
» Control Arduino using Python | pritudev | Sciencx | https://www.scien.cx/2021/12/27/control-arduino-using-python/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.