azimshauki
Posts: 5
Joined: Tue Apr 17, 2018 8:28 pm

Voice Sensor Operated Servo Motor SG90

Tue Apr 17, 2018 8:52 pm

Hi guys. I'm facing some issue that i'm need to make automatic baby cradle. Once baby cry detect, the cradle will move. However, when i test my code, the servo motor not running. Thank you for helping.

This is my code:

Code: Select all

import RPi.GPIO as GPIO
import time

GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)

# 1 X Mic Inputs
Mic = 17
GPIO.setup(Mic, GPIO.IN)
GPIO.add_event_detect(Mic, GPIO.RISING)

# 1 X Servo Output
GPIO.setup(18,GPIO.OUT)
p=GPIO.PWM(18,50)
p.start(7.5)

def sound():
	p.ChangeDutyCycle(7.5)
	print ("Sound Detected!")
	
	p.ChangeDutyCycle(12.5)
	print ("Sound Detected!")
	
	p.ChangeDutyCycle(2.5)
	print ("Sound Detected!")

print ("Baby Cry (CTRL+C to exit)")
time.sleep(1)
print ("Ready")

def servo_movement():

	try:
		while 1:
			if GPIO.event_detected(Mic):
				sound()
				time.sleep(1)
			
			if GPIO.event_detected(Mic):
				sound()
				time.sleep(1)
			
			if GPIO.event_detected(Mic):
				sound()
				time.sleep(0.1)
			time.sleep(1)	

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

servo_movement()

User avatar
mahjongg
Forum Moderator
Forum Moderator
Posts: 12416
Joined: Sun Mar 11, 2012 12:19 am
Location: South Holland, The Netherlands

Re: Voice Sensor Operated Servo Motor SG90

Tue Apr 17, 2018 9:16 pm

Added code tags, without which Python code becomes obfuscated.

azimshauki
Posts: 5
Joined: Tue Apr 17, 2018 8:28 pm

Re: Voice Sensor Operated Servo Motor SG90

Tue Apr 17, 2018 9:39 pm

mahjongg wrote:
Tue Apr 17, 2018 9:16 pm
Added code tags, without which Python code becomes obfuscated.
Sorry. i don't understand. Can you explain again?

User avatar
mahjongg
Forum Moderator
Forum Moderator
Posts: 12416
Joined: Sun Mar 11, 2012 12:19 am
Location: South Holland, The Netherlands

Re: Voice Sensor Operated Servo Motor SG90

Wed Apr 18, 2018 11:26 am

Without code tags all the indentations (which are essential for python) will be lost. So I have added the code tags for you.

without them your code would have looked like this:

import RPi.GPIO as GPIO
import time

GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)

# 1 X Mic Inputs
Mic = 17
GPIO.setup(Mic, GPIO.IN)
GPIO.add_event_detect(Mic, GPIO.RISING)

# 1 X Servo Output
GPIO.setup(18,GPIO.OUT)
p=GPIO.PWM(18,50)
p.start(7.5)

def sound():
p.ChangeDutyCycle(7.5)
print ("Sound Detected!")

p.ChangeDutyCycle(12.5)
print ("Sound Detected!")

p.ChangeDutyCycle(2.5)
print ("Sound Detected!")

print ("Baby Cry (CTRL+C to exit)")
time.sleep(1)
print ("Ready")

def servo_movement():

try:
while 1:
if GPIO.event_detected(Mic):
sound()
time.sleep(1)

if GPIO.event_detected(Mic):
sound()
time.sleep(1)

if GPIO.event_detected(Mic):
sound()
time.sleep(0.1)
time.sleep(1)

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

servo_movement()

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

Re: Voice Sensor Operated Servo Motor SG90

Wed Apr 18, 2018 1:50 pm

Code tags look look this

Add [code] at the top

code goes here

and [/code] at the bottom.

the actual code tag is the bit in bold but when you add them to your post you don't need to make them bold that's just so they show up her.

it changes your code from this

def sound():
p.ChangeDutyCycle(7.5)
print ("Sound Detected!")

p.ChangeDutyCycle(12.5)
print ("Sound Detected!")

to this

Code: Select all

def sound():
	p.ChangeDutyCycle(7.5)
	print ("Sound Detected!")
	
	p.ChangeDutyCycle(12.5)
	print ("Sound Detected!")
	
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

Idahowalker
Posts: 445
Joined: Wed Jan 03, 2018 5:43 pm

Re: Voice Sensor Operated Servo Motor SG90

Wed Apr 18, 2018 4:01 pm

Is the mic just an audio mic hooked to a GPIO pin?

Is the mic signal amplified and sent to a comparator where there is a threshold adjust made to cause the mic o/p to render a state of either 1 or 0? Have you confirmed that the threshold adjustment will trigger the comparator, when the infant cries?
Without knowing why you are deleting my postings, I will not know how...

azimshauki
Posts: 5
Joined: Tue Apr 17, 2018 8:28 pm

Re: Voice Sensor Operated Servo Motor SG90

Wed Apr 18, 2018 7:25 pm

Idahowalker wrote:
Wed Apr 18, 2018 4:01 pm
Is the mic just an audio mic hooked to a GPIO pin?

Is the mic signal amplified and sent to a comparator where there is a threshold adjust made to cause the mic o/p to render a state of either 1 or 0? Have you confirmed that the threshold adjustment will trigger the comparator, when the infant cries?
yes.
mic at gpio17
servo at gpio18.

i want to filter voice. when infant cry, the servo will move. another noise will not affected the servo. i only want baby cry to move the servo.

Idahowalker
Posts: 445
Joined: Wed Jan 03, 2018 5:43 pm

Re: Voice Sensor Operated Servo Motor SG90

Wed Apr 18, 2018 9:22 pm

azimshauki wrote:
Wed Apr 18, 2018 7:25 pm
Idahowalker wrote:
Wed Apr 18, 2018 4:01 pm
Is the mic just an audio mic hooked to a GPIO pin?

Is the mic signal amplified and sent to a comparator where there is a threshold adjust made to cause the mic o/p to render a state of either 1 or 0? Have you confirmed that the threshold adjustment will trigger the comparator, when the infant cries?
yes.
mic at gpio17
servo at gpio18.

i want to filter voice. when infant cry, the servo will move. another noise will not affected the servo. i only want baby cry to move the servo.
Yes, mic is directly connected to the GPIO pin?

To make a circuit that will only respond to the baby's cries will be an interesting task. You'd have to record the baby's cries, digitize the baby's cries, filter out all other sounds with a discriminator circuit, and compare the baby's cries with the incoming digitized cries. Cool project.
Without knowing why you are deleting my postings, I will not know how...

azimshauki
Posts: 5
Joined: Tue Apr 17, 2018 8:28 pm

Re: Voice Sensor Operated Servo Motor SG90

Fri Apr 20, 2018 8:39 pm

Idahowalker wrote:
Wed Apr 18, 2018 9:22 pm
azimshauki wrote:
Wed Apr 18, 2018 7:25 pm
Idahowalker wrote:
Wed Apr 18, 2018 4:01 pm
Is the mic just an audio mic hooked to a GPIO pin?

Is the mic signal amplified and sent to a comparator where there is a threshold adjust made to cause the mic o/p to render a state of either 1 or 0? Have you confirmed that the threshold adjustment will trigger the comparator, when the infant cries?
yes.
mic at gpio17
servo at gpio18.

i want to filter voice. when infant cry, the servo will move. another noise will not affected the servo. i only want baby cry to move the servo.
Yes, mic is directly connected to the GPIO pin?

To make a circuit that will only respond to the baby's cries will be an interesting task. You'd have to record the baby's cries, digitize the baby's cries, filter out all other sounds with a discriminator circuit, and compare the baby's cries with the incoming digitized cries. Cool project.
Thanks for response. Can you give more ideas? I dont have idea how to do this. How to digitize? and how to do steps that you explain above?

Idahowalker
Posts: 445
Joined: Wed Jan 03, 2018 5:43 pm

Re: Voice Sensor Operated Servo Motor SG90

Fri Apr 20, 2018 8:50 pm

Learning how to use an A to D (Analog to Digital Converter), voice quality, would be a good starting point. Typing "audio analog to digital converter IC' will give several possibilities.
Without knowing why you are deleting my postings, I will not know how...

User avatar
mahjongg
Forum Moderator
Forum Moderator
Posts: 12416
Joined: Sun Mar 11, 2012 12:19 am
Location: South Holland, The Netherlands

Re: Voice Sensor Operated Servo Motor SG90

Sat Apr 21, 2018 10:52 pm

Thanks for response. Can you give more ideas? I dont have idea how to do this. How to digitize? and how to do steps that you explain above?
he's being ,cynical, there is simply no way to distinguish a human baby's cries from any other sound, especially not with the extremely primitive "audio input" you are using, and a bit of python code.

Just to get sound input of sufficient quality to use artificial hearing you would need much better quality hardware, and the best programmers in the world would need ten man years to write the software.

nios2
Posts: 38
Joined: Mon Mar 12, 2018 3:22 am

Re: Voice Sensor Operated Servo Motor SG90

Sun Apr 22, 2018 6:11 am

As other posters have pointed out, the difficult part of this project is detecting the cry of the baby. You could pass over that part of the task to a dedicated sound detection module; I have used the below which works well:-

https://www.mikroe.com/speakup-2-click

User avatar
elParaguayo
Posts: 1943
Joined: Wed May 16, 2012 12:46 pm
Location: London, UK

Re: Voice Sensor Operated Servo Motor SG90

Sun Apr 22, 2018 8:56 am

I'm not experienced in this sort of project in any way shape or form but if I were doing it, I'd try find a way of monitoring audio levels and triggering an action when a threshold level is exceeded. Distinguishing the cry from other noises will be far too hard. Assuming your baby is in a quiet place already, the volume test should be enough.

I believe there are hardware modules for this sort of thing.
RPi Information Screen: plugin based system for displaying weather, travel information, football scores etc.

Return to “Python”