This is basically a followup question to my previous questions (you don't need to read them to answer it). Primary question is, can a Raspberry Pi 2 take more than one inputs from different circuits at the same time (same code running)? I have built a functioning photogate.
Here is my circuit: my circuit for photogate

The code that I am using is:

import RPi.GPIO as GPIO import time     GPIO.setmode(GPIO.BOARD)
GPIO.setup(11,GPIO.IN)     start = time.time() stop = time.time()
gateState = False     try:
        while True:
            if (GPIO.input(11) != gateState):
                gateState = not gateState                    
                if (gateState == True):
                    start = time.time()    
                    stop = time.time()
                    if stop - start > 0.0001:
                        print "Time:  ", stop - start, "s"
                        print "Speed: ", 1.5/(stop-start), "m/s"
                        print " " except KeyboardInterrupt:

So, I was wondering, how I would use another photogate on the same Pi at the same time as the first position to measure speed of an object at two times. (basically use two photogates). How would I manipulate the code to use two photogates at the same time, that is if Pi can do it. Which GPIO would I use, (any random two?)

2 Answers 2


I gave you code in raspberry pi2 photogate which will work with multiple gates and +/- 10 µs timing.

You should be able to see that the code you posted above can not work with multiple gates. In an attempt to gain timing resolution the code you posted is using a busy wait. You can not busy wait in more than one place at the same time.

  • I will try this as well in a new sd card (new raspian). I might ask questions about it later. Thanks Nov 30, 2015 at 8:57
  • @SarthakGarg You can store many programs on one SD card.
    – joan
    Nov 30, 2015 at 9:18
  • Multiple GPIO libraries? Because your method said to remove the older GPIO library. Also, i dont want to mess with the things as at least something is working. And I have useless micro SDs lying around so why not! Nov 30, 2015 at 22:48
  • @SarthakGarg It says to remove any existing directory called PIGPIO as that will be overwritten by a new install of pigpio. No mention of other libraries as far as I can see. There are dozens of Pi GPIO libraries. The can happily co-exist with each other.
    – joan
    Nov 30, 2015 at 23:14
  • Ohh, i thought PIGPIO was the library, never mind. I will probably install it as the more the merrirer (in this case). Could you give me some guidance on the above comment in the answer above. (my comment for the above answer- both the arduino one and the accuracy one) Thanks Dec 1, 2015 at 3:20

Yes, you can use multiple input circuits driving multiple GPIOs, provided that the voltage range is appropriate and all share a common ground with the pi.

No, this is not a particularly good way to do time measurements, as the pi typically runs a multistasking operating system such as Linux (or, if you must, Windows) and that is notorious for resulting in a substantial and variable latency in responding to external stimulus. In theory you could overcome this by doing the timing in the kernel, or better yet using a realtime kernel - but it is a lot of effort for marginal results when the real issue is that the wrong platform has been chosen.

Theorectically, if you wanted to do it in the simplest way anyway you could use anotherif checking the other photogate input, or better yet use something blocking on a read of the interrupt node for that gpio, and hope that this results in the scheduler waking you up as quickly as possible. But it will only approximate the timing.

If you want to build a good photogate timer, use a barebones microcontroller and/or feed your inputs directly into a counter timer block's capture trigger. This could potentially be a $1 class part accessorizing the pi, if you really need the capabilities of that for other purposes. (Or to put it in hobbyist language, this is properly an Arduino problem not a pi problem, but you could potentially use a baby Arduino to accomplish it and report the time difference to a pi).

  • Could explain your comment's third paragraph with theoretical stuff. Also, which parts would I need on top of what I have given here to build a microcontroller photogate that measures accurately but at the same time gives value to pi. Is it possible to just build something with what I have. But at the same time, I am willing to work harder to build a better photogate so do you know how I can find details on how to build it. Thanks Nov 30, 2015 at 8:50
  • Also what is the accuracy of my current photo gate. Thanks Nov 30, 2015 at 8:54

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.