webwinkelkeur logo

4.7 avg.

5101+ reviews
webwinkelkeur logoView all

5101+ reviews

5101+ reviews

Order by 16:00 for same day shipping

14 days return

EN

Individual

Business

GPIO Project 8 - Sound Controlled Motor

Beginner
30 Minuten
114,90

In this project you will learn how to use a sound sensor to control a motor. The sound sensor detects sounds (for example a clap) and sends a signal to the Raspberry Pi, which activates a motor.

Connection diagram

Connect the sound sensor:

VCC: Connect to 3.3V (pin 1).
GND: Connect to GND (pin 6).
DO (digital output): Connect to GPIO 17 (pin 11).

Connect the ULN2003 driver to the stepper motor:

Stepper motor: Connect the connector of the stepper motor to the header of the ULN2003 driver.
ULN2003 driver:

IN1: Connect to GPIO 27 (pin 13).
IN2: Connect to GPIO 22 (pin 15).
IN3: Connect to GPIO 23 (pin 16).
IN4: Connect to GPIO 24 (pin 18).
VCC: Connect to 5V (pin 2).
GND: Connect to GND (pin 6).

Pinout Reference

GPIO

Pin #

Function

Connection

GPIO 17Pin 11Digital inputSound sensor DO
GPIO 27Pin 13Stepper motorULN2003 IN1
GPIO 22Pin 15Stepper motorULN2003 IN2
GPIO 23Pin 16Stepper motorULN2003 IN3
GPIO 24Pin 18Stepper motorULN2003 IN4
3.3VPin 1NutritionSound sensor VCC
5VPin 2NutritionULN2003 VCC
GNDPin 6Earth (Ground)Sound sensor and ULN2003

Python-code in Thonny

Step 1: Write your code

Open the Thonny Python IDE and enter the following code:

import RPi.GPIO as GPIO
from time import sleep
from collections import deque

# Geluidssensor en motor-pinnen
IN1 = 17
IN2 = 27
IN3 = 22
IN4 = 23
SOUND_SENSOR_PIN = 24

# GPIO instellen
GPIO.setmode(GPIO.BCM)
GPIO.setup(SOUND_SENSOR_PIN, GPIO.IN)  # Geluidssensor als input
GPIO.setup(IN1, GPIO.OUT)
GPIO.setup(IN2, GPIO.OUT)
GPIO.setup(IN3, GPIO.OUT)
GPIO.setup(IN4, GPIO.OUT)

# Stepper motor sequentie (4-fase stappen)
step_sequence = [
    [1, 0, 0, 0],
    [1, 1, 0, 0],
    [0, 1, 0, 0],
    [0, 1, 1, 0],
    [0, 0, 1, 0],
    [0, 0, 1, 1],
    [0, 0, 0, 1],
    [1, 0, 0, 1]
]

# Instellingen voor piekdetectie
HISTORY_SIZE = 10	# Hoeveel metingen we bijhouden
THRESHOLD = 7		# Aantal HIGH's in HISTORY_SIZE om als piek te zien
MINIMUM_ACTIVE_TIME = 1	# Minimal actief blijven (seconden)

# Historie van geluidmetingen
sound_history = deque([1] * HISTORY_SIZE, maxlen=HISTORYSIZE)

def set_step(w1, w2, w3, w4):
    """Stel de status van de motorpinnen in."""
    GPIO.output(IN1, w1)
    GPIO.output(IN2, w2)
    GPIO.output(IN3, w3)
    GPIO.output(IN4, w4)

def step_motor(steps, delay):
    """Draai de motor een aantal stappen."""
    for _ in range(steps):
        for step in step_sequence:
            set_step(*step)
            sleep(delay)

try:
    while True:
       # Lees de huidige status van de geluidssensor
	current_state = GPIO.INPUT(SOUND_SENSOR_PIN)

	# Voeg de huidige meting toe aan de historie
	sound_history.append(current_state)

	# Tel hoeveel keer HIGH (1) in historie
	low_count = sound_history.count(0)

	# Controleer of er een piek is
	if low_count >= THRESHOLD:
		print("Piek in geluid gedetecteerd! Steppermotor draait.")
		step_motor(512, 0.002)	# Draai 512 stappen vooruit
		sound_hitory.clear()	# Reset de historie na een plek
		sound_history.extend([1] * HISTORY_SIZE)	# Vermijd snelle heractivatie	
		sleep(MINIMUM_ACTIVE_TIME)	# Zorg dat de motor niet constant triggert
	sleep(0.01)

except KeyboardInterrupt:
    print("\nProgramma gestopt.")
finally:
    GPIO.cleanup()  # Reset de GPIO-instellingen

Step 2: Save the file

Click File > Save As and name the file sound_controlled_motor.py.

Step 3: Run the script

Click the greenRun button (▶) at the top the Thonny interface.

How does it work?

  1. Sound sensor:
    • The sensor detects sounds such as clapping and sends a high signal (HIGH) to GPIO 17.
  2. Stepper motor:
    • Once sound is detected, the motor will rotate 512 steps (forward).
  3. Adjust speed and steps:
    • Adjust the value of delay and steps in step_motor()function to change the speed and distance of the motor rotation.

Result

  • Sound detected: The stepper motor makes one full rotation (512 steps).
  • No sound detected: The motor remains silent.

Experimenting

  1. Adjust direction:
    • Run the motor in reverse by reversing the order of the step_sequencelist.
  2. Use light or other sensors:
    • Replace the sound sensor with another input, such as an LDR or a push button.
  3. Add a flashing LED:
    • Add a LED that flashes when the engine is running.