Page 1 of 1

Voice Sensor Operated Servo Motor SG90

Posted: Tue Apr 17, 2018 8:52 pm
by azimshauki
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()

Re: Voice Sensor Operated Servo Motor SG90

Posted: Tue Apr 17, 2018 9:16 pm
by mahjongg
Added code tags, without which Python code becomes obfuscated.

Re: Voice Sensor Operated Servo Motor SG90

Posted: Tue Apr 17, 2018 9:39 pm
by azimshauki
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?

Re: Voice Sensor Operated Servo Motor SG90

Posted: Wed Apr 18, 2018 11:26 am
by mahjongg
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()

Re: Voice Sensor Operated Servo Motor SG90

Posted: Wed Apr 18, 2018 1:50 pm
by pcmanbob
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!")
	

Re: Voice Sensor Operated Servo Motor SG90

Posted: Wed Apr 18, 2018 4:01 pm
by Idahowalker
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?

Re: Voice Sensor Operated Servo Motor SG90

Posted: Wed Apr 18, 2018 7:25 pm
by azimshauki
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.

Re: Voice Sensor Operated Servo Motor SG90

Posted: Wed Apr 18, 2018 9:22 pm
by Idahowalker
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.

Re: Voice Sensor Operated Servo Motor SG90

Posted: Fri Apr 20, 2018 8:39 pm
by azimshauki
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?

Re: Voice Sensor Operated Servo Motor SG90

Posted: Fri Apr 20, 2018 8:50 pm
by Idahowalker
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.

Re: Voice Sensor Operated Servo Motor SG90

Posted: Sat Apr 21, 2018 10:52 pm
by mahjongg
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.

Re: Voice Sensor Operated Servo Motor SG90

Posted: Sun Apr 22, 2018 6:11 am
by nios2
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

Re: Voice Sensor Operated Servo Motor SG90

Posted: Sun Apr 22, 2018 8:56 am
by elParaguayo
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.