I purchased a bunch of “80cm Infrared Proximity Sensor” devices and I’m unable to make them work as expected.
Once connected to D4, as per the sample instructions, when I run the “grove_infrared_distance_interrupt.py” sample (with and without sudo), I always get “found something” result. No matter that there’s nothing in front of the sensor for way more than 80cm.
Is there anything special that must be done to make it work?
Sorry for my late response. You’re right. I now recall I also tried that script (grove_80cm_infrared_proximity_sensor.py). When I run it, using sudo and making sure I connect the sensor to the A0 port, I always get the following responses ranges, no matter I place my hand in front of the sensor (1cm) or leaving no object in front of it:
All these years later I’ve exactly the same issue as OP and I’m wondering how he managed to solve it. Because no matter what I put in front of it I always get the same read out no matter if it’s 10 cm or 80 cm. I switched pins, switched read outs switched everything I can possibly switch but it’s always ~10cm. Code I’m using:
import time
import grovepi
class ProxySensor:
def __init__(self, sensor):
self.sensor = sensor # an integer
grovepi.pinMode(sensor, "INPUT")
time.sleep(1) # this is a hardware thingy
def read(self):
adc_ref = 5
total = 0
for i in range(20):
sensor_value = grovepi.analogRead(self.sensor)
total += sensor_value
total_value = total / 20
return (
total_value,
round((float)(total_value) * adc_ref / 1024, 2)
)
def main():
proxy_sensor = ProxySensor(1)
while True:
print(proxy_sensor.read())
time.sleep(.5)
if __name__ == '__main__':
main()
As weird as it sounds, I made it work whit the official code… connected the sensor to A0, but used the “sensor = 1” in the code… still trying to understand why this is happening. Same problem whit other sensors (like grove air quality), and it works with A0 but using “sensor = 1”.
Because this was originally designed to be used in a teaching environment, and people naturally start counting with “1” instead of “0” as computers do.