I am developing a robot with raspberry pi. To detect obstacles I am using FC-51 IR sensor. This is my current code.

import RPi.GPIO as GPIO
import time

ObstaclePin = 18
tServoPin = 15
GPIO.setup(tServoPin, GPIO.OUT)

GPIO.setup(ObstaclePin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(tServoPin, GPIO.OUT)
tPwm = GPIO.PWM(tServoPin, 50)

def loop():
    while True:
        if (1 == GPIO.input(ObstaclePin)):
            print ("Barrier is detected !")
            print ("No barrier found !")

def turn_right(tPwm):
     for i in range(90, 180):
          positiont = 1./18.*(i)+2
     for i in range(180, 90, -1):
          positiont = 1./18.*(i)+2

def destroy():

if __name__ == '__main__':
    except KeyboardInterrupt:  

This above code works fine when I comment turn_right line in loop() function. That is it print "Barrier is detected !" whenever I close a object to it and when I take object away from it the obstacle indicator turn off. But when I uncomment that code line IR sensor's obstacle indicator keeps blinking after obstacle detection. But there is no object close to it. While blinking it is also not fully illuminated. And servo does not stop. I think IR sensor keep blinking it's indicator as a reason for this


I have connected IR sensor to pin 16 and servo to pin 15 in Raspberry pi. and both VCC and GND connect to a bread board. And an Arduino supply 5v for the bread board.

I can not understand the logic behind this. What am i doing wrong here??

Any help would be great Thank you very much in advance!!!

  • The code has nothing to light a bulb. Could you explain what you think the code does and explain the "bulb"? – joan Jul 12 '18 at 20:12
  • I don't see any code to turn on an LED, but turnRight(tPWM) appears to block, so if an LED was turned on, it won't be turned off until turnRight() is finished. Robot code needs to be asynchronous in order to not have these problems. you can do this with threads or with interrupts. Also it would be nice to say you're not only developing this on the Pi, but also that it's written in Python and which version. – NomadMaker Jul 13 '18 at 0:20
  • @joan I mean, when code is not running IR sensor works as expected. that is when ever i put objects close it will turn on green light and off when i take object away. but while running code with turn_right() it does off the green lights even their is no object. And also turn_right() does not stop. it keep running – iuhettiarachchi Jul 13 '18 at 3:30
  • @NomadMaker Thank you for your ideas. I am using python 3.5.3 for this. Problem is turn_right() function keep running. Can you explain it more, i mean do to this with threads – iuhettiarachchi Jul 13 '18 at 3:35
  • You really need to clean up the code. It isn't indented properly in your sample. Which wouldn't be as big a deal, but python uses indentation as a part of the syntax. What I was thinking, was that turn_right takes time to complete. This means that if you have a new obstacle before then that it won't be detected. If I knew what you meant by "lighting a bulb" I'd be able to make a possible better suggestion. I don't believe it's part of your code. – NomadMaker Jul 13 '18 at 4:03

Your Answer

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

Browse other questions tagged or ask your own question.