Created
February 17, 2021 11:23
-
-
Save smirgol/efa89720365b758c992ec66566529afb to your computer and use it in GitHub Desktop.
Python 3 script to control Arduino PWM
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python3 | |
"""Control an Arduino over the USB port.""" | |
# Based on: | |
# usb.py | |
# Created by John Woolsey on 12/17/2019. | |
# Copyright (c) 2019 Woolsey Workshop. All rights reserved. | |
# Reference: | |
# https://www.woolseyworkshop.com/2020/02/05/controlling-an-arduino-from-a-raspberry-pi/ | |
# Dependencies: | |
# pip3 install serial | |
# pip3 install pyserial | |
# USB_PORT = "/dev/ttyUSB0" # Arduino Uno R3 Compatible | |
USB_PORT = '/dev/ttyACM0' # Arduino Uno WiFi Rev2 | |
# Imports | |
import serial | |
# Functions | |
def print_commands(): | |
"""Prints available commands.""" | |
print("Available commands:") | |
print(" set_fan_1 - Set FAN 1 speed (0-255)") | |
print(" set_fan_2 - Set FAN 2 speed (0-255)") | |
print(" get_fan_1 - Get FAN 1 speed") | |
print(" get_fan_2 - Get FAN 1 speed") | |
print(" x - Exit program") | |
# Main | |
# Connect to USB serial port at 9600 baud | |
try: | |
usb = serial.Serial(USB_PORT, 9600, dsrdtr=False) | |
except: | |
print("ERROR - Could not open USB serial port. Please check your port name and permissions.") | |
print("Exiting program.") | |
exit() | |
# Send commands to Arduino | |
print("Enter a command from the keyboard to send to the Arduino.") | |
print_commands() | |
while True: | |
command = input("Enter command: ") | |
if command == "set_fan_1": # Set speed for FAN 1 | |
speed = 0 | |
while True: | |
speed = input("Fan speed FAN 1 (0-255): ") | |
speed = speed.strip() | |
if speed.isdigit(): | |
break; | |
cmd = 'set_fan_1===' + speed | |
usb.write(cmd.encode()) | |
#usb.write(b'set_fan_1===' + speed) | |
print("Set speed FAN 1 to :" + speed) | |
elif command == "set_fan_2": # Set speed for FAN 2 | |
speed = 0 | |
while True: | |
speed = input("Fan speed FAN 2 (0-255): ") | |
speed = speed.strip() | |
if speed.isdigit(): | |
break; | |
cmd = 'set_fan_2===' + speed | |
usb.write(cmd.encode()) | |
print("Set speed FAN 2 to :" + speed) | |
elif command == "get_fan_1": # turn off Arduino LED | |
usb.write(b'get_fan_1') # send command to Arduino | |
line = usb.readline() # read input from Arduino | |
line = line.decode() # convert type from bytes to string | |
line = line.strip() # strip extra whitespace characters | |
if line.isdigit(): # check if line contains only digits | |
value = int(line) # convert type from string to int | |
else: | |
print("Unknown value for Fan 1: '" + line + "'.") | |
value = 0 | |
print("Fan 1 value:", value) | |
elif command == "get_fan_2": # turn off Arduino LED | |
usb.write(b'get_fan_2') # send command to Arduino | |
line = usb.readline() # read input from Arduino | |
line = line.decode() # convert type from bytes to string | |
line = line.strip() # strip extra whitespace characters | |
if line.isdigit(): # check if line contains only digits | |
value = int(line) # convert type from string to int | |
else: | |
print("Unknown value for Fan 2: '" + line + "'.") | |
value = 0 | |
print("Fan 2 value:", value) | |
elif command == "x": # exit program | |
print("Exiting program.") | |
exit() | |
else: # unknown command | |
print("Unknown command '" + command + "'.") | |
print_commands() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment