Posts

Showing posts with the label Python

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

serial.serialutil.SerialException: could not open port /dev/ttyACM0

Image
If you try the steps in the post " Talk with Arduino Due in Python Shell " in Linux (Ubuntu in my setup). May be you will be complained with the error of: serial.serialutil.SerialException: could not open port /dev/ttyACM0: [Errno 13] Permission denied: '/dev/ttyACM0' Because your user account have no right to access the /dev/ttyACM0 port. You have to add your user account to the dialout group. Enter the command in Terminal: $ sudo usermod -a -G dialout <username> After you plug in the usb cable, run the command (it need to be run every time plug-in): $ sudo chmod a+rw /dev/ttyACM0 serial.serialutil.SerialException

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