Page 1 of 1

Using an IR sensor to check if an object is within range.

Posted: Wed May 06, 2020 10:55 am
by josh.williams
Hi all,

I have an IR Reflective Sensor 1-4mm (ID: 1146_0) and I just want to use it to check if an object is within range of the sensor (i.e. within 1-4mm away).

Here is the example code for my device:

Code: Select all

from Phidget22.Phidget import *
from Phidget22.Devices.VoltageRatioInput import *
import time

#Declare any event handlers here. These will be called every time the associated event occurs.

def onSensorChange(self, sensorValue, sensorUnit):
	print("SensorValue: " + str(sensorValue))
	print("SensorUnit: " + str(sensorUnit.symbol))
	print("----------")

def onError(self, code, description):
	print("Code: " + ErrorEventCode.getName(code))
	print("Description: " + str(description))
	print("----------")

def main():
	#Create your Phidget channels
	voltageRatioInput0 = VoltageRatioInput()

	#Set addressing parameters to specify which channel to open (if any)
	voltageRatioInput0.setHubPort(5)

	#Assign any event handlers you need before calling open so that no events are missed.
	voltageRatioInput0.setOnSensorChangeHandler(onSensorChange)
	voltageRatioInput0.setOnErrorHandler(onError)

	#Open your Phidgets and wait for attachment
	voltageRatioInput0.openWaitForAttachment(5000)

	#Do stuff with your Phidgets here or in your event handlers.
	#Set the sensor type to match the analog sensor you are using after opening the Phidget
	voltageRatioInput0.setSensorType(VoltageRatioSensorType.SENSOR_TYPE_1146)

	try:
		input("Press Enter to Stop\n")
	except (Exception, KeyboardInterrupt):
		pass

	#Close your Phidgets once the program is done.
	voltageRatioInput0.close()

main()
I basically want to add something like this pseudo-code to my main

Code: Select all

if out of range:
        print("Out of range.")
else:
        print("Distance = {}".format(voltageRatioInput0.getSensorValue()))
but I can't figure out what the correct syntax to replace "out of range".

Can anyone help?

Thanks,
Josh

Re: Using an IR sensor to check if an object is within range.

Posted: Fri May 08, 2020 10:33 am
by mparadis
Since you've configured the channel using setSensorType, you should be getting an "out of range" error event whenever the sensor doesn't detect anything within 4mm.When you get a valid reading, it will fire a SensorChange event instead.

If you don't want to use events, and just want to check using the if-statement format you listed, you can do this instead:

Code: Select all

val = voltageRatioInput0.getVoltageRatio()
if val < 0.03773 or val > 0.5364 :
        print("Out of range.")
else:
        print("Distance = " + val)
I got these numbers from the calculation our libraries use to convert voltageRatio into sensorValue for the 1146. Those two values correspond to 1.5mm and 4mm.

Re: Using an IR sensor to check if an object is within range.

Posted: Tue May 19, 2020 1:21 pm
by josh.williams
mparadis wrote:Since you've configured the channel using setSensorType, you should be getting an "out of range" error event whenever the sensor doesn't detect anything within 4mm.When you get a valid reading, it will fire a SensorChange event instead.

If you don't want to use events, and just want to check using the if-statement format you listed, you can do this instead:

Code: Select all

val = voltageRatioInput0.getVoltageRatio()
if val < 0.03773 or val > 0.5364 :
        print("Out of range.")
else:
        print("Distance = " + val)
I got these numbers from the calculation our libraries use to convert voltageRatio into sensorValue for the 1146. Those two values correspond to 1.5mm and 4mm.
Thanks!