Analog as Digital Help
Posted: 07 Feb 2024, 17:31
Hello,
I have a RevPi Connect S with a MIO module attached.
I was following the steps outlined on this page: https://revolutionpi.com/en/tutorials/a ... gurieren-2 regarding configuring a analog output as a digital output.
I am trying to use the pasted code (code1) to use the pi to control a motor to lock and unlock an inlet. However I don't hear a response from the motor when I use a separate script to call the lockinlet function. I tested the inlet already and know that it is functional. I ran a separate script (code2), and had the pi set AnalogOutput_1 to high, (which as per my pictory config would send a 5V signal), but when I use a multimeter to measure the pin I do not see a change in the voltage.
I am really new to this device and pictory in general and would really appreciate some help. I have pasted my code below and attached my pictory configurations.
Looking forward to your response,
Vickkram
CODE 1: ----------------------------------------------------------------------------------------
import time
import revpimodio2
# Defining analog input channels
ANALOGINPUT_TEMP_PIN = "AnalogInput_1"
ANALOGINPUT_PP_PIN = "AnalogInput_2"
ANALOGINPUT_LCK_PIN = "AnalogInput_3"
# Defining digital output channels
ANALOGOUTPUT_LOCK_PIN_POS = "AnalogOutput_1"
ANALOGOUTPUT_LOCK_PIN_NEG = "AnalogOutput_2"
# Constants
VDD = 5
INLET_UNLOCKED = 0
INLET_LOCKED = 1
rpi = revpimodio2.RevPiModIO(autorefresh=True)
def getVoltage(pin):
return rpi.io[pin].value/1000
def getLCLState():
voltage = getVoltage(ANALOGINPUT_LCK_PIN)
current = (VDD - voltage)/1000
PTC_res = voltage/current
print(PTC_res)
print("Voltage of Lock State:" + str(voltage))
if(PTC_res>100 and PTC_res <2000):
return INLET_UNLOCKED
elif(PTC_res>10000):
return INLET_LOCKED
else:
return -1
def lockInlet(retry=0):
rpi.io[ANALOGOUTPUT_LOCK_PIN_POS].value = 1
time.sleep(.5)
rpi.io[ANALOGOUTPUT_LOCK_PIN_POS].value = 0
state = getLCLState()
if state == INLET_LOCKED:
print("Inlet Locked")
return 1
elif retry == 0:
print("problem with inlet locking - trying one more time")
return lockInlet(retry=1)
else:
print("locking error")
return -1
def unlockInlet(retry = 0):
rpi.io[ANALOGOUTPUT_LOCK_PIN_NEG].value = 1
time.sleep(.5)
rpi.io[ANALOGOUTPUT_LOCK_PIN_NEG].value = 0
state = getLCLState()
if state == INLET_UNLOCKED:
return 1
elif retry == 0:
time.sleep(.5)
return(unlockInlet(retry = 1))
else:
#TODO: ERROR OUT or try one more time then error out
return -1
---------------------------------------------------------------------------------------------
CODE 2: -----------------------------------------------------------------------------------
import revpimodio2
import time
rpi = revpimodio2.RevPiModIO(autorefresh=True)
ANALOG_INPUT = "AnalogInput_1"
DIGITAL_OUTPUT = "AnalogOutput_1"
try:
while True:
value = 1
rpi.io[DIGITAL_OUTPUT].value = value
print(f"Value: {value}")
time.sleep(1)
finally:
rpi.cleanup()
--------------------------------------------------------------------------------------------------------------
I have a RevPi Connect S with a MIO module attached.
I was following the steps outlined on this page: https://revolutionpi.com/en/tutorials/a ... gurieren-2 regarding configuring a analog output as a digital output.
I am trying to use the pasted code (code1) to use the pi to control a motor to lock and unlock an inlet. However I don't hear a response from the motor when I use a separate script to call the lockinlet function. I tested the inlet already and know that it is functional. I ran a separate script (code2), and had the pi set AnalogOutput_1 to high, (which as per my pictory config would send a 5V signal), but when I use a multimeter to measure the pin I do not see a change in the voltage.
I am really new to this device and pictory in general and would really appreciate some help. I have pasted my code below and attached my pictory configurations.
Looking forward to your response,
Vickkram
CODE 1: ----------------------------------------------------------------------------------------
import time
import revpimodio2
# Defining analog input channels
ANALOGINPUT_TEMP_PIN = "AnalogInput_1"
ANALOGINPUT_PP_PIN = "AnalogInput_2"
ANALOGINPUT_LCK_PIN = "AnalogInput_3"
# Defining digital output channels
ANALOGOUTPUT_LOCK_PIN_POS = "AnalogOutput_1"
ANALOGOUTPUT_LOCK_PIN_NEG = "AnalogOutput_2"
# Constants
VDD = 5
INLET_UNLOCKED = 0
INLET_LOCKED = 1
rpi = revpimodio2.RevPiModIO(autorefresh=True)
def getVoltage(pin):
return rpi.io[pin].value/1000
def getLCLState():
voltage = getVoltage(ANALOGINPUT_LCK_PIN)
current = (VDD - voltage)/1000
PTC_res = voltage/current
print(PTC_res)
print("Voltage of Lock State:" + str(voltage))
if(PTC_res>100 and PTC_res <2000):
return INLET_UNLOCKED
elif(PTC_res>10000):
return INLET_LOCKED
else:
return -1
def lockInlet(retry=0):
rpi.io[ANALOGOUTPUT_LOCK_PIN_POS].value = 1
time.sleep(.5)
rpi.io[ANALOGOUTPUT_LOCK_PIN_POS].value = 0
state = getLCLState()
if state == INLET_LOCKED:
print("Inlet Locked")
return 1
elif retry == 0:
print("problem with inlet locking - trying one more time")
return lockInlet(retry=1)
else:
print("locking error")
return -1
def unlockInlet(retry = 0):
rpi.io[ANALOGOUTPUT_LOCK_PIN_NEG].value = 1
time.sleep(.5)
rpi.io[ANALOGOUTPUT_LOCK_PIN_NEG].value = 0
state = getLCLState()
if state == INLET_UNLOCKED:
return 1
elif retry == 0:
time.sleep(.5)
return(unlockInlet(retry = 1))
else:
#TODO: ERROR OUT or try one more time then error out
return -1
---------------------------------------------------------------------------------------------
CODE 2: -----------------------------------------------------------------------------------
import revpimodio2
import time
rpi = revpimodio2.RevPiModIO(autorefresh=True)
ANALOG_INPUT = "AnalogInput_1"
DIGITAL_OUTPUT = "AnalogOutput_1"
try:
while True:
value = 1
rpi.io[DIGITAL_OUTPUT].value = value
print(f"Value: {value}")
time.sleep(1)
finally:
rpi.cleanup()
--------------------------------------------------------------------------------------------------------------