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

Comments & issues
josh.williams
Fresh meat
Posts: 4
Joined: Fri Mar 27, 2020 7:23 pm
Contact:

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

Postby josh.williams » Wed May 06, 2020 10:55 am

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

User avatar
mparadis
Site Admin
Posts: 733
Joined: Fri Oct 28, 2011 12:17 pm
Contact:

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

Postby mparadis » Fri May 08, 2020 10:33 am

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.

josh.williams
Fresh meat
Posts: 4
Joined: Fri Mar 27, 2020 7:23 pm
Contact:

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

Postby josh.williams » Tue May 19, 2020 1:21 pm

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!


Return to “General”

Who is online

Users browsing this forum: No registered users and 2 guests