I want to use the photoelectronic with the Raspberry Pi for a micromouse maze.

enter image description here

Using sensors to detect the presence of micromouse at specific locations. I was given all this from my father since he's very into robotics but doesn't have time to help me since he's always~ busy but I do find it interesting. My friends and I usually do all kinds of things with the Raspberry Pi but first time with sensors and we're stuck.

enter image description here

The sensor will be placed under the white plastic,overall i'm expecting it to look like this:

enter image description here

Which is not working, it's just an example. I want to know how to use this sensor with the raspberry and the programming codes. I believe it should be python. If I can do this I would want to make other fun application with the sensor.So when the micromouse goes through this thing it will detect and starts the timer and the next time it goes through it detects again that something went past and stops the timer. Running on raspbian and ssh using putty.

I am now following the this website to use the sensor to detect something and display "motion detected". All I did was the same only that my sensor is PZ2-42 keyence so I connect it like this:

enter image description here

as my sensor need 12-24V which the raspberry doesn't have enough power to power it up. So I connect the Raspberry Pi to ground but not using the raspberry pi"s power supply. The codes are from the website above:

# pir_1.py
# Detect movement using a PIR module
# Author : Matt Hawkins
# Date   : 21/01/2013

# Import required Python libraries
import RPi.GPIO as GPIO
import time

# Use BCM GPIO references
# instead of physical pin numbers

# Define GPIO to use on Pi

print "PIR Module Test (CTRL-C to exit)"

# Set pin as input
GPIO.setup(GPIO_PIR,GPIO.IN)      # Echo

Current_State  = 0
Previous_State = 0


  print "Waiting for PIR to settle ..."

  # Loop until PIR output is 0
  while GPIO.input(GPIO_PIR)==1:
    Current_State  = 0

  print "  Ready"

  # Loop until users quits with CTRL-C
  while True :

    # Read PIR state
    Current_State = GPIO.input(GPIO_PIR)

    if Current_State==1 and Previous_State==0:
      # PIR is triggered
      print "  Motion detected!"
      # Record previous state
    elif Current_State==0 and Previous_State==1:
      # PIR has returned to ready state
      print "  Ready"

    # Wait for 10 milliseconds

except KeyboardInterrupt:
  print "  Quit"
  # Reset GPIO settings

Once, I connect it up the code stuck at :

Waiting for PIR to settle ... 

Eventhough my sensor was turn green red as it detects my hand,the code should have display motion detected but it didn't. I don't know what is the problem. I check the output voltage of the sensor and it was 0.735 V when nothing is detected but when it detects it goes to 42.4 mV. So I think that when nothing its detected it is a close circuit but when it detects it opens the circuit so the input change state by seeing the code above then it displays motion detected. I don't know whether the problem is that the input voltage when has'nt been detected is not enough for the Raspberry Pi to recognized it or receive as the voltage is too low which concludes the reason why it stays at ready even though the sensor changes from green to red when it detects my hand.

  • The sensor you are using requires at least 12 volt to work. What exactly is your question?
    – Gerben
    Feb 19, 2014 at 19:23
  • i want to know how to use the sensor with the raspberry pi as input so when they sense something they sent input to the raspberry pi then the raspberry pi starts the timer then when the sensor sense something again the input is then sent then the timer would stop. i need the circuitry diagram and the python codes on how they send as input. Feb 20, 2014 at 1:33
  • Please add link to more information about this sensor. Also maybe just have sensor on the maze, with pi attached by a long wire. Then pi could handle multiple sensors.
    – francis
    Feb 20, 2014 at 13:32
  • I have solved my problem: I didn't read the specification of the sensor, the assume the black wire to be ground but it was the output while the blue was suppose to be ground.TY guys for taking the time to solve my problem Feb 21, 2014 at 4:40

1 Answer 1


Problem solved: I didn't read the specification of the sensor, the assume the black wire to be ground but it was the output while the blue was supposed to be ground.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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