RGhosh99
Posts: 25
Joined: Tue Mar 21, 2017 6:57 am

Servo motor control with IR sensors using Raspberry Pi

Tue Mar 21, 2017 7:00 am

Can anyone tell me the Python code required for running a servo motor using IR sensors with Raspberry Pi ?

ghp
Posts: 1498
Joined: Wed Jun 12, 2013 12:41 pm
Location: Stuttgart Germany
Contact: Website

Re: Servo motor control with IR sensors using Raspberry Pi

Tue Mar 21, 2017 4:26 pm

Hello,

perhaps you ask a moderator to move this question to the 'python' thread ?

For servo control, there are many solutions. One example:
http://raspberrypi-aa.github.io/session2/pwm-servo.html

The IR-Sensors have a 3.3V compatible output ? Then look for https://sourceforge.net/p/raspberry-gpi ... /Examples/

Hope this helps,
Gerhard

RGhosh99
Posts: 25
Joined: Tue Mar 21, 2017 6:57 am

Re: Servo motor control with IR sensors using Raspberry Pi

Tue Mar 21, 2017 4:42 pm

Actually i need a python code such that the servo motor moves according to the IR sensor input. There will be 4 IR sensors in four directions and the motor needs to move in those directions depending on which IR is activated. I need python code for this. Please help.

User avatar
Gavinmc42
Posts: 4526
Joined: Wed Aug 28, 2013 3:31 am

Re: Servo motor control with IR sensors using Raspberry Pi

Wed Mar 22, 2017 4:18 am

A servo motor might only have 160-180degrees of movement.
A 360 degree servo can be made by using gears 1:2 ratio.

0 degree = 1ms pulse every 20ms, 180 degree 1.5ms, 360 degree = 2ms

GPIOZERO python library example has servo code?
http://gpiozero.readthedocs.io/en/v1.3. ... html#servo
I'm dancing on Rainbows.
Raspberries are not Apples or Oranges

RGhosh99
Posts: 25
Joined: Tue Mar 21, 2017 6:57 am

Re: Servo motor control with IR sensors using Raspberry Pi

Wed Mar 22, 2017 4:24 am

I am getting a problem like the motor is running automatically without sensor input and the loop of code runs automatically. Anyone with the proper python code for my requirement ? It will be of great help. Thanks

User avatar
Gavinmc42
Posts: 4526
Joined: Wed Aug 28, 2013 3:31 am

Re: Servo motor control with IR sensors using Raspberry Pi

Wed Mar 22, 2017 4:51 am

Well a proper description of your setup might help.
What servo, what IR sensors, how they are wired and what code have you done?

Only a few here can read minds ( see snarky reply post)
I'm dancing on Rainbows.
Raspberries are not Apples or Oranges

RGhosh99
Posts: 25
Joined: Tue Mar 21, 2017 6:57 am

Re: Servo motor control with IR sensors using Raspberry Pi

Wed Mar 22, 2017 5:18 am

I am explaining the setup :
1. Suppose there are 4 IR sensor modules (with 3 pins Vcc Gnd and Output) in four directions East West North South
2. A servo motor SG90 (turns 180 degree, but i may use a 360 degree servo also) is there.
3. The output of sensors are provided as input to the servo through raspberry pi and a PWM input is generated which is connected to Servo input pin.
4. The requirement is suppose the East sensor is activated. The servo should turn in that direction and stop. Again if the North sensor activates it should turn in that direction. This thing should happen with all the sensors.

Can you write me a Python code with this setup ??

User avatar
Gavinmc42
Posts: 4526
Joined: Wed Aug 28, 2013 3:31 am

Re: Servo motor control with IR sensors using Raspberry Pi

Wed Mar 22, 2017 6:37 am

That's very simple code.

But you need to know what PCM pulse width will get the servo pointing in 4 directions
Assuming you have a servo that does +/- 90 degrees, some may only do +-/60 degrees.
Assume 0 degrees is north, pulse width for the zero position is 1.5ms.
So if sensor north has a normally low signal you send 1.5ms to the servo every 20ms.
Use 4 variables and assuming the GPIOZERO library and +/- 90 degree servo

ServoNorth = 15/10000
ServoWest = 125/100000
ServoEast = 175/100000
ServoSouth = 1/1000 or 2/1000

if SensorNorth == 0:
servo.ServoNorth
if SensorSouth == 0:
servo.ServoSouth

I've never used the gpiozero library so you may need to figure the exact code out.
Using gears and tuning the values you can get it pointing anywhere.
I'm dancing on Rainbows.
Raspberries are not Apples or Oranges

RGhosh99
Posts: 25
Joined: Tue Mar 21, 2017 6:57 am

Re: Servo motor control with IR sensors using Raspberry Pi

Wed Mar 22, 2017 6:39 am

Thanks. I will try to use this logic and check. Will post later if there is any issue.

texy
Forum Moderator
Forum Moderator
Posts: 5161
Joined: Sat Mar 03, 2012 10:59 am
Location: Berkshire, England

Re: Servo motor control with IR sensors using Raspberry Pi

Wed Mar 22, 2017 7:16 am

RGhosh99 wrote:Actually i need a python code such that the servo motor moves according to the IR sensor input. There will be 4 IR sensors in four directions and the motor needs to move in those directions depending on which IR is activated. I need python code for this. Please help.
Hi,
the point of gph's post was that you have asked for python help, but posted in the Java forum.
Now moved to python forum.......
Various male/female 40- and 26-way GPIO header for sale here ( IDEAL FOR YOUR PiZero ):
https://www.raspberrypi.org/forums/viewtopic.php?f=93&t=147682#p971555

Return to “Python”