#!/usr/bin/env python3 # Control snowflake with sound sensor! # Give snowflake its own power supply (sharing GND with the Pi) # (c) 2018 Bradley Knockel # Numbering I use for my snowflake: # 1 # 6 2 # 5 3 # 4 # If using hardware SPI, make sure you've enabled SPI using # sudo raspi-config (in Interface Options) # Then reboot the Pi. # How to install the Adafruit modules for ADC... # sudo apt-get install python3-pip # sudo pip3 install adafruit-mcp3008 # sound sensor (KY-037)... # + -> 5V # G -> GND # A0 -> voltage divider -> ADC pin 1 (channel 0) # Sadly, it picks up any breeze over microphone. # Voltage divider # R1 = 1000 ohm + 1000 ohm # R2 = 5100 ohm # The output of KY-037 should never be much more than 3.3 V unless very loud, # but the voltage divider is for my sanity. # ADC (MCP3008) pins... # VDD (16) to 3.3V # VREF (15) to 3.3V # AGND (14) to GND # CLK (13) to GPIO 11 (sclk) (must be at least 10 kHz) # DOUT (12) to GPIO 9 (miso) # DIN (11) to GPIO 10 (mosi) # CS/SHDN (10) to GPIO 8 (ce0) (must be pulled HIGH/off between conversions) # DGND (9) to GND # Without an amplifier, the signal isn't very strong, but # this works great with loud sound! pin1 = 21 pin2 = 20 pin3 = 16 pin4 = 12 pin5 = 16 # same as pin3 pin6 = 20 # same as pin2 # if sharing Pi pins, still use a different resistor for each transistor # set the volume thresholds t1 = 3 # at least 2! t2 = 4 t3 = 5 t4 = 6 import time import RPi.GPIO as IO IO.setmode(IO.BCM) IO.setup(pin1, IO.OUT) IO.setup(pin2, IO.OUT) IO.setup(pin3, IO.OUT) IO.setup(pin4, IO.OUT) IO.setup(pin5, IO.OUT) IO.setup(pin6, IO.OUT) import Adafruit_MCP3008 ## Software SPI configuration: #CLK = 11 #MISO = 9 #MOSI = 10 #CS = 8 #mcp = Adafruit_MCP3008.MCP3008(clk=CLK, cs=CS, miso=MISO, mosi=MOSI) ## Hardware SPI configuration: import Adafruit_GPIO.SPI as SPI # Adafruit's spidev module mcp = Adafruit_MCP3008.MCP3008(spi=SPI.SpiDev(0,0)) def write(a1,a2,a3,a4,a5,a6): IO.output(pin1, a1) IO.output(pin2, a2) IO.output(pin3, a3) IO.output(pin4, a4) IO.output(pin5, a5) IO.output(pin6, a6) try: val2 = mcp.read_adc(0) while 1: # maybe needed to remove noise from a change in LEDs #time.sleep(0.001) val1 = mcp.read_adc(0) time.sleep(0.001) # 1 divided by 4*this gives lowest frequency detected val2 = mcp.read_adc(0) if abs(val1-val2) >= t4: write(1,1,1,1,1,1) elif abs(val1-val2) >= t3: write(0,1,1,1,1,1) elif abs(val1-val2) >= t2: write(0,0,1,1,1,0) elif abs(val1-val2) >= t1: write(0,0,0,1,0,0) else: write(0,0,0,0,0,0) except: pass # cleanup write(0,0,0,0,0,0) IO.cleanup()