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

Servo motor control with 4 PIR sensors using Raspberry Pi

Fri Mar 24, 2017 6:10 pm

I need to control a servo motor with 4 PIR sensors. The servo motor should move to a particular direction (change duty cycle) when each sensor gets activated.

the code is as follows :

import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM)
PIR_PIN1 = 12
PIR_PIN2 = 16
PIR_PIN3 = 20
PIR_PIN4 = 21
GPIO.setup(PIR_PIN1, GPIO.IN)
GPIO.setup(PIR_PIN2, GPIO.IN)
GPIO.setup(PIR_PIN3, GPIO.IN)
GPIO.setup(PIR_PIN4, GPIO.IN)

GPIO.setup(18,GPIO.OUT)

p=GPIO.PWM(18,50)

p.start(2.5)

def MOTION1(PIR_PIN1):
p.ChangeDutyCycle(7.5)
print "Motion 1 Detected!"
servo_movement()

def MOTION2(PIR_PIN2):
p.ChangeDutyCycle(2.5)
print "Motion 2 Detected!"
servo_movement()

def MOTION3(PIR_PIN3):
p.ChangeDutyCycle(2.5)
print "Motion 3 Detected!"
servo_movement()

def MOTION4(PIR_PIN4):
p.ChangeDutyCycle(7.5)
print "Motion 4 Detected!"
servo_movement()


print "PIR Module Test (CTRL+C to exit)"
time.sleep(2)
print "Ready"

def servo_movement():
try:
GPIO.add_event_detect(PIR_PIN1, GPIO.RISING, callback=MOTION1)
while 1:

time.sleep(100)



GPIO.add_event_detect(PIR_PIN2, GPIO.RISING, callback=MOTION2)
while 1:

time.sleep(100)

GPIO.add_event_detect(PIR_PIN3, GPIO.RISING, callback=MOTION3)
while 1:

time.sleep(100)

GPIO.add_event_detect(PIR_PIN4, GPIO.RISING, callback=MOTION4)
while 1:

time.sleep(100)




except KeyboardInterrupt:
print "Quit"
p.stop()
GPIO.cleanup()

servo_movement()


Problem with the code is that, when the 1st sensor is activated, it detects and prints and the servo motor changes the duty cycle. But other sensor inputs are not processed and hence the servo motor does not change the duty cycle. Instead an error shows : "Conflicting edge detection already enabled for this GPIO channel".

Someone please help to modify this code so that i can change the duty cycle of servo motor when each of the sensor gets activated.

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

Re: Servo motor control with 4 PIR sensors using Raspberry P

Fri Mar 24, 2017 8:15 pm

Hello,
you should place python code in

Code: Select all

-tags. See https://www.raspberrypi.org/forums/viewtopic.php?f=32&t=84477
Regards,
Gerhard

pcmanbob
Posts: 6450
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Servo motor control with 4 PIR sensors using Raspberry P

Fri Mar 24, 2017 9:46 pm

Hi.
please next time post using code tags, only added to the problem trying re-indent your code.
gave up in the end and did it over.
ended up with this, I had it working but as I was using switches in place of the PIRs did get some switch bounce, but apart from that it works.
did not do anything with the servo pwm bit left it as it was in your original program. just changed how it was called.

Code: Select all

import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM)
PIR_PIN1 = 12
PIR_PIN2 = 16
PIR_PIN3 = 20
PIR_PIN4 = 21
GPIO.setup(PIR_PIN1, GPIO.IN)
GPIO.setup(PIR_PIN2, GPIO.IN)
GPIO.setup(PIR_PIN3, GPIO.IN)
GPIO.setup(PIR_PIN4, GPIO.IN)

GPIO.add_event_detect(PIR_PIN1, GPIO.RISING)
GPIO.add_event_detect(PIR_PIN2, GPIO.RISING)
GPIO.add_event_detect(PIR_PIN3, GPIO.RISING)
GPIO.add_event_detect(PIR_PIN4, GPIO.RISING)

GPIO.setup(18,GPIO.OUT)

p=GPIO.PWM(18,50)

p.start(2.5)

def MOTION1():
	p.ChangeDutyCycle(7.5)
	print "Motion 1 Detected!"
	servo_movement()

def MOTION2():
	p.ChangeDutyCycle(2.5)
	print "Motion 2 Detected!"
	servo_movement()

def MOTION3():
	p.ChangeDutyCycle(2.5)
	print "Motion 3 Detected!"
	servo_movement()

def MOTION4():
	p.ChangeDutyCycle(7.5)
	print "Motion 4 Detected!"
	servo_movement()


print "PIR Module Test (CTRL+C to exit)"
time.sleep(2)
print "Ready"

def servo_movement():

	try:
		while True:
			if GPIO.event_detected(PIR_PIN1):
				MOTION1()
				time.sleep(0.2)
			if GPIO.event_detected(PIR_PIN2):
				MOTION2()
				time.sleep(0.2)
			if GPIO.event_detected(PIR_PIN3):
				MOTION3()
				time.sleep(0.2)
			if GPIO.event_detected(PIR_PIN4):
				MOTION4()
				time.sleep(0.2)
			time.sleep(0.1)	
	

	except KeyboardInterrupt:
		print "Quit"
		GPIO.cleanup()
		exit()

servo_movement()
let us know if it works ok with the PIRs
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

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

Re: Servo motor control with 4 PIR sensors using Raspberry P

Sat Mar 25, 2017 4:12 am

Thanks mate ! The code worked fine. Only one problem is that till now I was using a 180 degree servo motor. I want to use a 360 servo motor for ease of operation.

Can you tell me the duty cycle values if the servo needs to more in 4 directions according to the sensors, if I use a 360 degree servo ???

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

Re: Servo motor control with 4 PIR sensors using Raspberry P

Sat Mar 25, 2017 6:32 am

You cannot get a 360 servo as a standard hobby servo.
Most have a feedback potentiometer that can only rotate about 300 degrees.

You need a servo gearbox, these are reduction type
https://www.servocity.com/servos/servo-gearboxes
But some have the pot on the big gear and are 400 degree

You need the opposite, 1/2 turn = 1 turn on the output ie 1 to 2 ratio.
360 degree servo mods are really just continuous rotation mods.
http://www.diyrc.com/servo-rotate.htm

You could also do belt and pulley drive.
Big pulley on the servo.
I'm dancing on Rainbows.
Raspberries are not Apples or Oranges

pcmanbob
Posts: 6450
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Servo motor control with 4 PIR sensors using Raspberry P

Sat Mar 25, 2017 9:41 am

Hi RGhosh99

Glad the code worked ok for you. there is one error in it, on all the MOTION subroutines I left in servo_movement(), you don't need this. so you need to edit the program so they look like this

Code: Select all

def MOTION1():
	p.ChangeDutyCycle(7.5)
	print "Motion 1 Detected!"
	

def MOTION2():
	p.ChangeDutyCycle(2.5)
	print "Motion 2 Detected!"
	

def MOTION3():
	p.ChangeDutyCycle(2.5)
	print "Motion 3 Detected!"
	

def MOTION4():
	p.ChangeDutyCycle(7.5)
	print "Motion 4 Detected!"
	
sorry about that I just forgot to remove them when I copied them from you original program.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

madscientist007
Posts: 12
Joined: Fri Jan 26, 2018 5:45 pm

Re: Servo motor control with 4 PIR sensors using Raspberry Pi

Wed Feb 14, 2018 11:29 am

Hello can this be applicable if put a picamera on one of the servo and get to take a picture each time the pir detects movement .

pcmanbob
Posts: 6450
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Servo motor control with 4 PIR sensors using Raspberry Pi

Wed Feb 14, 2018 11:36 am

madscientist007 wrote:
Wed Feb 14, 2018 11:29 am
Hello can this be applicable if put a picamera on one of the servo and get to take a picture each time the pir detects movement .
That is certainly do able you just need to add the picture taking routine to motion functions.
Last edited by pcmanbob on Wed Feb 14, 2018 12:17 pm, edited 1 time in total.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

madscientist007
Posts: 12
Joined: Fri Jan 26, 2018 5:45 pm

Re: Servo motor control with 4 PIR sensors using Raspberry Pi

Wed Feb 14, 2018 12:06 pm

Hello please i am in a great need of help on a project that i am working on. Basically i am trying make a raspberry pi motion detection system using PIR sensors, picamera, a 3x4 matrix keypad for reseting the system when triggered and servo motors turn to the triggered position of the sensor. Please if you can help me i would so much appreciate it. thank you.

pcmanbob
Posts: 6450
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Servo motor control with 4 PIR sensors using Raspberry Pi

Wed Feb 14, 2018 12:31 pm

madscientist007 wrote:
Wed Feb 14, 2018 12:06 pm
Hello please i am in a great need of help on a project that i am working on. Basically i am trying make a raspberry pi motion detection system using PIR sensors, picamera, a 3x4 matrix keypad for reseting the system when triggered and servo motors turn to the triggered position of the sensor. Please if you can help me i would so much appreciate it. thank you.
And your question is ?

If you don't tell us what the problem is how can we help.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

madscientist007
Posts: 12
Joined: Fri Jan 26, 2018 5:45 pm

Re: Servo motor control with 4 PIR sensors using Raspberry Pi

Wed Feb 14, 2018 12:36 pm

Sorry for my lack of clarity. my first question is is this example applicable to a pan and tilt servo.

pcmanbob
Posts: 6450
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Servo motor control with 4 PIR sensors using Raspberry Pi

Wed Feb 14, 2018 12:42 pm

The example code in this tread deals with panning a servo in response to inputs from 4 pir sensors there is no tilt function because it was not required or asked for.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

madscientist007
Posts: 12
Joined: Fri Jan 26, 2018 5:45 pm

Re: Servo motor control with 4 PIR sensors using Raspberry Pi

Wed Feb 14, 2018 12:54 pm

can you help me to apply this principle to a pan and tilt model

pcmanbob
Posts: 6450
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Servo motor control with 4 PIR sensors using Raspberry Pi

Wed Feb 14, 2018 1:46 pm

The original program has 4 PIR's spread out to detect which direction the target is approaching from.

How do you intend to do the detection for the tilt function as a PIR has a cone shaped detection zone that only detects an object within the zone not it actual position.

What are you want to do with regards the tilt are you wanting to look at a specific part of the target or what ?

you need to define what it is you actually want to do that's the first part of your problem.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

madscientist007
Posts: 12
Joined: Fri Jan 26, 2018 5:45 pm

Re: Servo motor control with 4 PIR sensors using Raspberry Pi

Wed Feb 14, 2018 3:06 pm

Thank you i am guessing there is no need for the tilt function. i was given different components to complete a task that was why i was asking.

jarnelramiscal
Posts: 9
Joined: Sat Mar 03, 2018 8:07 pm

Re: Servo motor control with 4 PIR sensors using Raspberry Pi

Mon Mar 05, 2018 5:09 pm

you have a diagram for that

pcmanbob
Posts: 6450
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Servo motor control with 4 PIR sensors using Raspberry Pi

Mon Mar 05, 2018 5:37 pm

jarnelramiscal wrote:
Mon Mar 05, 2018 5:09 pm
you have a diagram for that
? diagram for what mind reading and the use of crystal balls is not supported on this forum.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

jarnelramiscal
Posts: 9
Joined: Sat Mar 03, 2018 8:07 pm

that project sir

Mon Mar 05, 2018 7:24 pm

that project diagram

pcmanbob
Posts: 6450
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Servo motor control with 4 PIR sensors using Raspberry Pi

Mon Mar 05, 2018 7:42 pm

I doubt there was ever a diagram for the project, it's fairly simple in that there are 4 inputs from the PIR's and one output to the servo the pins used are indicated by the gpio setup part of the code.

What is it you need help with.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

jarnelramiscal
Posts: 9
Joined: Sat Mar 03, 2018 8:07 pm

Re: Servo motor control with 4 PIR sensors using Raspberry Pi

Mon Mar 05, 2018 10:28 pm

Can i copy the program
your program is running in one servo and one PIR MOTION?

pcmanbob
Posts: 6450
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Servo motor control with 4 PIR sensors using Raspberry Pi

Tue Mar 06, 2018 12:09 am

The program is there for all to see and anyone to use

Code: Select all

import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM)

# 4 X PIR inputs
PIR_PIN1 = 12
PIR_PIN2 = 16
PIR_PIN3 = 20
PIR_PIN4 = 21
GPIO.setup(PIR_PIN1, GPIO.IN)
GPIO.setup(PIR_PIN2, GPIO.IN)
GPIO.setup(PIR_PIN3, GPIO.IN)
GPIO.setup(PIR_PIN4, GPIO.IN)

GPIO.add_event_detect(PIR_PIN1, GPIO.RISING)
GPIO.add_event_detect(PIR_PIN2, GPIO.RISING)
GPIO.add_event_detect(PIR_PIN3, GPIO.RISING)
GPIO.add_event_detect(PIR_PIN4, GPIO.RISING)

# 1 X Servo output
GPIO.setup(18,GPIO.OUT)

p=GPIO.PWM(18,50)

p.start(2.5)

def MOTION1():
	p.ChangeDutyCycle(7.5)
	print "Motion 1 Detected!"
	

def MOTION2():
	p.ChangeDutyCycle(2.5)
	print "Motion 2 Detected!"
	

def MOTION3():
	p.ChangeDutyCycle(2.5)
	print "Motion 3 Detected!"
	

def MOTION4():
	p.ChangeDutyCycle(7.5)
	print "Motion 4 Detected!"
	


print "PIR Module Test (CTRL+C to exit)"
time.sleep(2)
print "Ready"

def servo_movement():

	try:
		while True:
			if GPIO.event_detected(PIR_PIN1):
				MOTION1()
				time.sleep(0.2)
			if GPIO.event_detected(PIR_PIN2):
				MOTION2()
				time.sleep(0.2)
			if GPIO.event_detected(PIR_PIN3):
				MOTION3()
				time.sleep(0.2)
			if GPIO.event_detected(PIR_PIN4):
				MOTION4()
				time.sleep(0.2)
			time.sleep(0.1)	
	

	except KeyboardInterrupt:
		print "Quit"
		GPIO.cleanup()
		exit()

servo_movement()
and it uses 4 PIR inputs as specified at the start of the program

Code: Select all

# 4 X PIR inputs
PIR_PIN1 = 12
PIR_PIN2 = 16
PIR_PIN3 = 20
PIR_PIN4 = 21
and 1 servo output

Code: Select all

# 1 X Servo output
GPIO.setup(18,GPIO.OUT)
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

jarnelramiscal
Posts: 9
Joined: Sat Mar 03, 2018 8:07 pm

Re: Servo motor control with 4 PIR sensors using Raspberry Pi

Tue Mar 06, 2018 6:58 am

how to connect servo motor to PIR MOTION SENSOR SIR THAT HE CAN DRIVE THE MOVEMENT OF THE SERVO MOTOR

pcmanbob
Posts: 6450
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Servo motor control with 4 PIR sensors using Raspberry Pi

Tue Mar 06, 2018 9:59 am

jarnelramiscal wrote:
Tue Mar 06, 2018 6:58 am
how to connect servo motor to PIR MOTION SENSOR SIR THAT HE CAN DRIVE THE MOVEMENT OF THE SERVO MOTOR
First off posting the same question 3 times wont get you the answer any quicker. especially at 6AM UK time I tend to be asleep.

So diagram for connecting pir's and servo to gpio.

Image

this information could have been found by simple searching the web.

example how to connect PIR
https://projects.raspberrypi.org/en/pro ... mputing/13
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

jarnelramiscal
Posts: 9
Joined: Sat Mar 03, 2018 8:07 pm

Re: Servo motor control with 4 PIR sensors using Raspberry Pi

Wed Mar 07, 2018 2:20 am

tnx sir for the information

jarnelramiscal
Posts: 9
Joined: Sat Mar 03, 2018 8:07 pm

Re: Servo motor control with 4 PIR sensors using Raspberry Pi

Wed Mar 07, 2018 2:21 am

sorry for the disturbing you

Return to “Python”