Two Questions: Hello in the video at 1:40, i had replaced the 4.3k resistor with 4.7k resistor, what would be the change in the gpio input when the ir phototransistor is active and unactive (light falls and doesn't fall). Thanks And https://www.youtube.com/watch?v=KLeQTd1Ll_g Here is the video for my current project. I was going to modify the code given after trying it but the code did not work at all or atleast did not return any values. Nothing shows up after I activate the code. Just the main screen for python 3 home and a restart statement. I have built the circuit.(circuit without the extra led and the first given code in pyhton 3 on raspberry pi 2. I have updated and upgraded. What should I do?) The print function does not work. I doubt that the rest of the funcetion in while function works. If i add print before while, it prints that comment like: print "ok" prints ok when run (the rest of the code is the same. only added, didn't remove anything) but the rest of the function does not work. How do I fix it? Thanks

Sorry about the incomplete question. (i'll leave the previous questions for reference) Here is the code:

import RPi.GPIO as GPIO import time    
start = time.time() stop = time.time() gateState = False    
        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:

When I execute this code, it causes false data to appear. This is the image that i see when i runt the code

afterwards. more random digits start appearing as data. I know this is wrong because blocking the ir led has no effect and pushing the ground pin to the side creates rapid data of very small speed. What am i doing wrong. My circuit is: [![Here is the circuit except that the 4300 ohms R1 is 4700 Ohms instead][2]][2]

[2]: image 2[Here is the circuit except that the 4300 ohms R1 is 4700 Ohms instead. as a comment because i cant post more than 1 image. sorry][2]

  • 1
    What is the question? It is far from clear that this is Pi related. You need to include the code and circuitry in your question. Expecting people to look at a video is unrealistic.
    – Milliways
    Nov 23 '15 at 6:37
  • Add your code and related output. Also, add circuit diagram instead of pointing to a youtube video. Nov 23 '15 at 12:13
  • Link to circuit: i.stack.imgur.com/xEpTM.png Nov 24 '15 at 6:54

The following code will time the edge to +/- 10 µs or so.

Change the GATE (Broadcom) GPIO and START_EDGE (0 or 1) to suit.

#!/usr/bin/env python

# photo-gate.py
# 2015-11-26
# Public Domain

import time

import pigpio # http://abyz.me.uk/rpi/pigpio/python.html

GATE = 4

START_EDGE = 0 # Measure low edge

start_edge_tick = None

def cbf(gpio, level, tick):
   global start_edge_tick
   if level == START_EDGE:
      start_edge_tick = tick
   elif level != pigpio.TIMEOUT:
      if start_edge_tick is not None:
         edge_ticks = pigpio.tickDiff(start_edge_tick, tick)
         print (edge_ticks)

pi = pigpio.pi() # Connect to Pi.

if pi.connected:

   pi.set_mode(GATE, pigpio.INPUT)

   cb = pi.callback(GATE, pigpio.EITHER_EDGE, cbf)

   time.sleep(3600) # Run for an hour.


   pi.stop() # Disconnect from Pi.
  • Could you explain more in depth of how to correct it. Sorry, i am extremely new to coding and raspberry. One question, will the gpio input pin stay same, currently i was gpio 17 or 11 board. Also, what is this bit for? #!/usr/bin/env python # photo-gate.py # 2015-11-26 # Public Domain and basically the rest of the code? how do i print the speed and measure time. Is this code a replacement or an addition? Thanks Alot @joan Nov 27 '15 at 1:37
  • The code is a replacement which uses my pigpio library to do the timings. It sets up a callback which waits for changing edges on a GPIO. EITHER_EDGE means get told when a GPIO changes from low to high (rising edge) and from high to low (falling edge). The callback receives a tick parameter. The tick is the microsecond at which the edge change happened. The difference in ticks gives the number of microseconds between the monitored edges. So you have the time in microseconds. DIstance = speed * time, so speed = distance / time. Divide the tick difference by 1000000 for seconds.
    – joan
    Nov 27 '15 at 8:25
  • i copy pasted the code but what i get back is Traceback (most recent call last): File "/home/pi/Desktop/photogate2.py", line 9, in <module> import pigpio # abyz.co.uk/rpi/pigpio/python.html ImportError: No module named pigpio >>> Nov 27 '15 at 23:07
  • You have to download and build the library and then start the daemon (sudo pigpiod). See abyz.co.uk/rpi/pigpio/download.html
    – joan
    Nov 27 '15 at 23:11
  • It started working with my older code after building a new circuit. Thanks for the help! I'll stick with whats working :) Nov 28 '15 at 2:20

If the actual length of your object isn't 1.5 meters long, that should be your answer. You need to convert whatever the length of your object into meters prior to plugging it in.

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.