Pyduino

Library allowing users to control an Arduino either programatically or interactively without coding C/C++ and instead using only Python
CodeHardware

Pyduino project

Check out the project on jhviggo/pyduino. This project was using at Aarhacks 2019 Christmas Hackathon by the winners of the Interactive Christmas category.

This project is about controlling an Arduino from your computer, without compiling the code over and over again. It can be used both interactively through a python terminal or programmatically with python. It allows you to set pins on the Arduino as OUTPUT or INPUT and then read or write to either digital or analog pins. It is a way to work with the Arduino without having to code C++ and compile it every time. With this library you can interactively set, read from and write to pins.

Installing the project

The project uses Python setuptools to install the package on your system, allowing it to be imported from anywhere.

# Clone project
git clone git@github.com:jhviggo/pyduino.git
cd pyduino

# Install pyduino package
pip install -r requirements.txt
pip install . # Installs pyduino package

Arduino setup

Compile the file src/arduino/pyduino.ino to your arduino, it contains the protocol used to communicate with it. Make sure it compiles correctly and try running:

python pyduino/test/e2e.py

You should see output somewhat like this:

Connecting to port COM3...
Setting pin mode
         setting pin 5 as OUTPUT
         saving as {"pin": 5, "mode": "OUTPUT"} to local storage
Setting pin mode
         setting pin 7 as INPUT
         saving as {"pin": 7, "mode": "INPUT"} to local storage
Setting pin mode
         setting pin 8 as OUTPUT
         saving as {"pin": 8, "mode": "OUTPUT"} to local storage
Writing to digital pin
         writing 1 to digital pin 5
Reading from digital pin
         reading from digital pin 5
        [-] not enough values to unpack (expected 2, got 1)
Writing to analog pin
         writing 15 to analog pin 1
Reading from analog pin
         reading from analog pin 1
        [-] not enough values to unpack (expected 2, got 1)
Closing connection...
         connection closed

run

Writing a program to interact with the Arduino is simple, the library has helpful classes to establish a connection and controlling pins. There is also a HelperFunctions class, which allows you to enable functionality such as verbose error messages and localization. You can look through the script below to get an idea of how to use it.

# import Connector and controller
from pyduino.HelperFunctions import HelperFunctions
from pyduino.controllers.Pin import *
import time

# create an instance with serial_port and if you want verbose prints
connector = Connector(serial_port='COM3', read_timeout=1, verbose=True)
conn = connector.get_serial_connection()

# sets global verbose and language
options = {
    'verbose': True,
    'language': 'en'
}
HelperFunctions(options=options)

# pass the connection to the controller
controller = Controller(conn, verbose=True)

# set pin_mode and write
pin: PinInterface = DigitalPin(conn, 7)
pin.write(1)

# set pin_mode and read
pin: PinInterface = DigitalPin(conn, 8, mode='INPUT')
pin.read()

# close connection
connector.close_connection()