Posts

Showing posts with the label pySerial

Control Arduino LED using Python with GUI

Image
This example demonstrate how to control Arduino LED using Python with GUI, using tkinter library. #tkinter for Python 3.x #Tkinter for Python 2.x import serial import time import tkinter def quit(): global tkTop tkTop.destroy() def setCheckButtonText(): if varCheckButton.get(): varLabel.set("LED ON") ser.write(bytes('H', 'UTF-8')) else: varLabel.set("LED OFF") ser.write(bytes('L', 'UTF-8')) ser = serial.Serial('/dev/ttyACM1', 9600) print("Reset Arduino") time.sleep(3) ser.write(bytes('L', 'UTF-8')) tkTop = tkinter.Tk() tkTop.geometry('300x200') varLabel = tkinter.StringVar() tkLabel = tkinter.Label(textvariable=varLabel) tkLabel.pack() varCheckButton = tkinter.IntVar() tkCheckButton = tkinter.Checkbutton( tkTop, text="Control Arduino LED", variable=varCheckButton, command=setCheckButtonText) tkCheckButton.pack(anchor=tki...

Talk with Arduino Due in Python Shell

Image
With pySerial installed, it's easy to communicate with Arduino board in Python Shell. Connect Arduino Due with sketch of former post downloaded. For Python 2.x, enter the commands in Python Shell to turn ON/OFF LED on Arduino Due Board >>> import serial >>> ser = serial.Serial('/dev/ttyACM0', 115200) >>> ser.write('H') >>> ser.write('L') For Python 3.x, the String in write() have to be casted to bytes. >>> import serial >>> ser = serial.Serial('/dev/ttyACM0', 115200) >>> ser.write(bytes('H', 'UTF-8')) >>> ser.write(bytes('L', 'UTF-8')) Related: serial.serialutil.SerialException: could not open port /dev/ttyACM0 Communicate with Arduino in Python programmatically

Install pySerial on Ubuntu

Image
To check if pySerial installed, open Python Shell, type the command: >>> import serial if error message returned, means pySerial not installed. pySerial not installed with error - To install pySerial on Ubuntu, download and unpack  pyserial , open Terminal and change to the unpacked folder. - To install for Python 2.x, type the command: $ sudo python setup.py install - To install for Python 3.x, type the command: $ sudo python3 setup.py install (To install the module for all users on the system, administrator rights (root) is required, run with sudo.) import serial with pySerial installed