Raspberry Pi Tutorial¶
Hardware Requirements¶
Raspberry Pi with OS installed on SD Card x 1
Microphone x 1
Audio Card x 1 (Not needed if microphone is connected via USB)
Bread Board x 1
LED x 2
100 ohm Resistor x 2
Jumper wires x 5
Setting up Raspberry Pi¶
Flash a 8GB+ memory card with Rasberry OS Lite
Insert the card in to Raspberry Pi
Connect monitor, keyboard, ethernet and power to Raspberry Pi
Turn on the power and let the OS bootup
Use username:
pi
and password:raspberry
to loginUse
sudo raspi-config
to configureChange password
Enable SSH
Enable any interface options as needed
Expand the filesystem ( Advance optoins > Expand filesystem)
Update system using
sudo apt-get update && sudo apt-get upgrade -y
Now you are updated and ready to use via SSH from different computer. Now you can disconnect monitor and keyboard if you want to use SSH.
Using pi-clap¶
How to wire up for pi-clap?¶
Connect
Ground Pin(6)
to breadboard’s sidelineConnect one Red LED and 100ohm Resistor in serial with LED’s positive ends
Connect negative ends of LEDs to grounded sideline
Connect the Resisters on breadboard to
GPIO 12 Pin
&GPIO 24 Pin
on Raspberry Pi as in the circuit diagramPlugin the USB input audio device(Audio Card or Microphone)
Reboot the OS with sudo reboot (This should load the audio driver automatically in most cases for the device connected)
Install pip & portaudio module
sudo apt-get install -y python3-pip portaudio19-dev
Install pi-clap pip module
pip3 install pi-clap
Now the Raspberry Pi is ready use pi-clap
Use following code for pi-clap to start listening. Make the necessary adjustments in the code and values to match your microphone and pinout. You can also add more methods in the derived classes.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 | #!/usr/bin/python3
from piclap import *
from gpiozero import LED
class PiController:
'''Describes the controller methods which are usually used while connected
to a raspberry pi controller using the module `gpiozero`.
'''
def __init__(self):
self.led12 = LED(12)
self.led24 = LED(24)
def toggleLight(self):
self.led12.toggle()
print("Light toggled on pin 12")
def lightBlinker(self, times=10):
self.led24.blink(n=times)
print("Light blinks", times, "times on pin 24")
class Config(Settings):
'''Describes custom configurations and action methods to be executed based
on the number of claps detected.
'''
def __init__(self):
super().__init__()
self.controller = PiController()
self.chunk_size = 512 # Reduce as power of 2 if pyaudio overflow
self.wait = 0.5 # Adjust wait between claps
self.method.value = 600 # Threshold value adjustment
def on2Claps(self):
'''Custom action for 2 claps'''
self.controller.led12.blink(n=1)
print("Light flashed on pin 4")
def on3Claps(self):
'''Custom action from PiController for 3 claps'''
self.controller.toggleLight()
def on5Claps(self):
'''Custom action from PiController for 5 claps'''
self.controller.lightBlinker(times=5)
def main():
config = Config()
listener = Listener(config)
listener.start()
if __name__ == '__main__':
main()
|
Finally run you code with python3 app.py
Try 2 claps to Light up the connected LED for 1 sec and 3 claps to toggle ON/OFF state of the connected LED. Also, Use 4 claps to exit from the system.