EasyVolts and Python. Read button, control PWM

Hi, here is the new demo of EasyVolts usage. In this demo python script controls EasyVolts to read external button and control RC servo without any complicated schematics or external power supply.

Here is the connection schematic:

Servo_Button_EasyVolts

Rx pin of the EasyVolts is configured as 50Hz pwm output to control servo, Tx pin – as input with weak pullUP to read the button press without external components (the button shortens Tx to Gnd at press). Power pin provides power for the servo.

The setup is controlled by following python 3 script:

import serial
import time

#main program starts here
print("Open control serial port. Use CTRL+C for exit\r\n")
serialPort = serial.Serial('COM18')#set your port name here
print('Set voltage:4800mV. Set current protection limit: 400mA.')
serialPort.write(b'u4800\r') #set voltage value
serialPort.write(b'i400\r') #set current limit value
print('Set Modbus mode.')
serialPort.write(b'M\r') #set modbus mode to make UART RxTx pins free to use them as GPIO/PWM
print('Set Tx pin to Z+pullUp mode.')
serialPort.write(b'zTh\r') #set TX pin in Z+pullUp mode
print('Set Rx pin to PWM mode with 50Hz frequency (F_divider=4800) and dutyCycle 3%.')
serialPort.write(b'f4800\r') #set PWM frequency divider 4800 (to generate 50Hz frequency)
serialPort.write(b'pR30\r') #set Rx pin to PWM output mode with dutyCycle = 3%

#read TX input (button) in a loop
button = 0
oldButton = 0
direction = -1
pwm = 30

try:
    while(1):
        time.sleep(0.03)
        serialPort.flushInput()
        serialPort.write(b'rT\r') #request Tx pin READ
        #read response with Tx pin input state (button state)
        line = str(serialPort.readline(), 'utf-8')
        if("T0\r\n" == line):
            button = 0
        if("T1\r\n" == line):
            button = 1
        #change servo movement direction after each button press
        if(oldButton != button):
            if(button == 0):
                direction = -1*direction
            oldButton = button
        #turn servo until button is pressed and max angle not reached. Each step is 0.5% (1 unit is 0.1%)
        if(button == 0):
            pwm += direction * 5
            if(pwm>100):
                pwm = 100
            if(pwm<30):
                pwm = 30
        print('     Button='+str(button) + ', direction=' + str(direction) + ', pwm=' + str(pwm/10), end='%\r')
        serialPort.write(b'pR'+ bytes(str(int(pwm)),'utf-8') + b'\r')

except KeyboardInterrupt:
    pass

#before exit set output power to 0 and TxRx pins to Z state
serialPort.write(b'u0\r')
serialPort.write(b'zT\r')
serialPort.write(b'zR\r')
serialPort.close()
print("Exit!")

Here is the video with the demonstration of the project in action.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: