3

I have a Raspberry Pi that has an IR sensor on it, and a Arduino that has IR LEDs hooked up to it. I am sensing when something is put into a box, but not when the item is taken out (callChris() is only called at that point). The only problem is that sometimes the sensors won't sense when the item is taken out of the box. (i.e. The value of GPIO.input(NUM) is still 1 when it should have switched to 0)

Here is an example of this program:

import RPi.GPIO as GPIO
import time
import os
import signal
import sys

IR_SENSORS = [14, 15]
prev_val = [1, 1]

def callChris():
    print("worked")

def checkMail():
    global prev_val
    for index, sensor in enumerate(IR_SENSORS):
        val = GPIO.input(sensor)
        print("[*] {}: prev_val: {} value: {}".format(IR_SENSORS[index], prev_val[index], val))
        if val != prev_val[index] and val == 1:
            prev_val[index] = val 
            callChris()
        prev_val[index] = val

def setup():
    GPIO.setmode(GPIO.BCM)
    print("[*] Set board to GPIO.BCM")
    for sensor in IR_SENSORS:
        print("[*] Setting up sensor on pin {}".format(sensor))
        GPIO.setup(sensor, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)

def main():
    setup()
    while True:
        checkMail()
        #Waits one second before submitting another checkMail request
        time.sleep(1)
 main()

Example output:

[*] Set board to GPIO.BCM
[*] Setting up sensor on pin 14
[*] Setting up sensor on pin 15
[*] 14: prev_val: 1 value: 0
[*] 15: prev_val: 1 value: 0
[*] 15: prev_val: 0 value: 0
[*] 14: prev_val: 0 value: 1
worked
[*] 15: prev_val: 0 value: 1
worked
[*] 14: prev_val: 1 value: 1
/* I took it out here, but they are still 1 */
[*] 15: prev_val: 1 value: 1
[*] 14: prev_val: 1 value: 1

This is my first project with GPIO programming, so I think that there is something hardware related that I am missing. Any help would be great.

Edit: Added imports and call to main() in the code

  • Is it possible that removing the object is also triggering the sensor, thus keeping the value "1"? Is there a different type of sensor that might work better for this application? (NB: I don't have a lot of knowledge about sensors; my thoughts are based on a quick skimming of Wikipedia.) – Pete Hooper Jan 8 '16 at 19:22
  • My impression is that when the sensor sees an IR LED, the value goes to 0, but when the sensor does not see the LED, it goes to 1. IR sensors (when it works correctly, lol) is the best sensor for this project. – T94j0 Jan 8 '16 at 19:36
  • Please post a complete script which exhibits the problem. – joan Jan 8 '16 at 19:45
  • It is complete, the only thing that differs from it and the script on my computer is that this doesn't have the imports and doesn't call main() at the end (which can be inferred as the start) I'll edit those in though. – T94j0 Jan 8 '16 at 19:55
  • Can you include a picture of the hardware - showing the IR bits perhaps? I assume there is some form of modulation of the IR but sensors can get saturated and thus less effective from incident unmodulated IR as well (daylight)! – SlySven Jan 8 '16 at 21:16
2

Since you did not specify what kind of IR sensor you're using, I'll assume it's just a regular IR photodiode.


Add a pull-up resistor. Pin is high when it detects IR, and low if it doesn't. This will not solve the random HIGH/LOW state, but it makes sure that the value comes from detecting IR and not from random electrical noise.

Sources of electrical noise include:

  • Floating pins
  • Fluorescent lighting
  • Basically any unshielded electronics nearby
  • Someone walking into the room (static electricity)

Note:

  • Make sure the inside of your box is non-reflective.
  • You will want to seriously bump up the resistor value. The GPIO gets high at around 1.34 volts according to this YouTube experiment.

.

Pull-up circuit

schematic

simulate this circuit – Schematic created using CircuitLab


Stay away from sunlight. Work at night if possible. A lot of IR sensors are uber sensitive to sunlight (especially the ones from eBay). Also check for stray sources of IR

Sources include:

  • Proximity sensor from your phone (front-side black dots near front camera)
  • No-brand TVs
  • Being near a window during the day, even if you have the blinds/curtains closed
  • CCTV cameras pointed in your general direction
  • Some fire alarms
  • Some motion sensors/home security systems

Source: I've had this problem with Arduino. I was baffled as to why mine was always high.

  • 1
    It worked really well in the dark, thanks for the input! – T94j0 Jan 11 '16 at 22:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.