A Halloween Sound Trigger with Raspberry Pi and Arduino

Arduino Home Raspberry Pi
A Halloween Sound Trigger with Raspberry Pi and Arduino
[vimeo:https://vimeo.com/51950656]

People have been asking me about interesting applications for the Raspberry Pi, and whether Raspberry Pi is an Arduino killer of some sort. The answer to the second question is no; in fact it is an Arduino augmenter. This blog post answers the first question with another question: how about a Haunted House sound effects machine?

A new revision of the Early Release of Getting Started with Raspberry Pi came out last Friday. I read Matt Richardson’s chapter on using Pygame with the GPIO pins on the Pi, which included a simple Sound Sample player. I adapted his example to work with an Arduino that talks to the Pi over a serial connection; this skeletal (ahem) hookup could easily be incorporated into some sort of Halloween installation. I decided to use Arduino for reading the inputs because out of the box it is more robust and can handle a wider variety of inputs. Also, there are many existing Haunted House triggering demos out in the wild that use Arduino.

First, you’ll need to prepare the trigger circuit. The following example uses three toggle switches, but you can replace those with any kind of on/off input. In a haunted house (or porch-based trick-or-treater installation), a PIR sensor would be handy for triggering based on proximity.

Here’s the basic schematic:

The 10k resistors can be replaced by the Arduino’s own internal pull-up resistors, if you know how to do that.

I connected the Arduino to the Raspberry Pi using a USB cable. While I had the Pi hooked into a monitor, I found that I could just plug the Arduino through my Mac keyboard USB connector and it got enough power from that to work. If you have an older Raspberry Pi with polyfuses limiting the power on the USB port (check to see if you have 2 little green fuses marked “1104” next to the USB ports), you may need an external hub, or run the Pi headless to free up a USB port.

Next, upload the following sketch to the Arduino:

// PiTalk.ino
// Reads three digital inputs. These could be any kind of 
// switch or (for Halloween) PIR sensors.

byte onState[3] = { 0, 0, 0 }; // save the state of each pin

void setup() {
 Serial.begin(9600);   // Open serial connection
 pinMode(2, INPUT);    // Set these three pins for reading
 pinMode(3, INPUT);    // Each have a 10k pullup externally
 pinMode(4, INPUT);    // so a trigger is LOW
}

void loop() {
 for (int i=0; i<3; i++) {         // Iterate over pins
   if (digitalRead(i+2) == LOW) {  // Check if triggered
     if (onState[i] == 0) {        // Just triggered?
       Serial.write(i+2);          // Send the pin number
       onState[i] = 1;             // but just once
     } 
   } else {
     onState[i] = 0;               // Not triggered
   }
 }
 delay(20);
}

You can use your computer to do upload the program, or you can download and install the Arduino IDE directly on the Raspberry Pi (as described in the next release of Getting Started with Raspberry Pi).

Once the Arduino is programmed, open up the Leafpad text editor on the Raspberry Pi and enter this Python program, adapted from Matt’s Sound Sample Player:

# playSounds.py

import pygame.mixer
from time import sleep
from sys import exit
import serial

pygame.mixer.init(44000, -16, 1, 1024) 

soundA = pygame.mixer.Sound("Scream.wav") 
soundB = pygame.mixer.Sound("WilhelmScream.wav")
soundC = pygame.mixer.Sound("CastleThunder.wav")

soundChannelA = pygame.mixer.Channel(1) 
soundChannelB = pygame.mixer.Channel(2)
soundChannelC = pygame.mixer.Channel(3)

print "Sampler Ready."
serialFromArduino = serial.Serial("/dev/ttyACM0",9600)
serialFromArduino.flush()

while True:
    try:
        val = ord(serialFromArduino.read())
        print(val)
        if (val == 2): 
            soundChannelA.play(soundA) 
        if (val == 3):
            soundChannelB.play(soundB)
        if (val == 4):
            soundChannelC.play(soundC)
        val = 0
        sleep(.01)
    except KeyboardInterrupt: 
        exit()

Save it as playSounds.py. Before you run the script you’ll probably need to install the Python serial module. To do that, type:

sudo apt-get install python-serial

If you’re running the latest Raspbian, you probably have everything you need to get this running. Open the LXTerminal and type:

python playSounds.py

If you get an error that Pygame is not installed, type:

sudo apt-get update
sudo apt-get install python-pygame

You’ll also need some sound files to play. I chose three from the Internet Archive: a generic scream, a Wilhelm Scream, and the classic Castle Thunder sample.

When you press the buttons each sound will play once; Pygame’s mixer will even play all three at the same time if you have multiple trick-or-treaters invading your porch.

51 thoughts on “A Halloween Sound Trigger with Raspberry Pi and Arduino

  1. Tom says:

    Or you could just hook the switches up to GPIO pins, use RPi.GPIO (which makes internal pull-up really easy) and do away with the Arduino…

    1. Derek says:

      Could you elaborate on this Tom?

  2. Bosstiger says:

    Reblogged this on Gigable – Tech Blog.

  3. NUTesla | The Informant » Raspberry Pi utilizzato per realizzare delle decorazioni di Halloween says:

    […] inquietante. Infine, Shawn Wallace ha associato a Raspberry Pi una Arduino Uno per trasformare una breadboard in una pulsantiera che riproduce delle grida o altri suoni a tema: non è troppo tardi per osservare questi progetti e […]

  4. A Halloween Sound Trigger with Raspberry Pi and Arduino #piday #raspberrypi @Raspberry_Pi « adafruit industries blog says:

    […] out this great Raspberry Pi Haunted House sound effects machine from MAKE’s Shawn […]

  5. Denis says:

    Thanks for this guide. But in which directory do you place the files?

    1. Shawn Wallace says:

      The WAV files should be in the same directory as the Python script. You could also use an absolute path name in the pygame.mixer.Sound() constructor.

      1. Ashley Brown (@arctic_sunrise) says:

        Hi i cant get this to work

        *** glibc detected *** python: double free or corruption (top) : 0x0231eed0 ***

        can you help / advise please

        1. Shawn Wallace says:

          How big are your WAV files? Try it with a sound file under 500k or so. If you need to play larger sound files, you’ll probably need to use something other than Pygame to play them back.

          1. Ashley Brown (@arctic_sunrise) says:

            ah thank you ill try that. how would you suggest us to playback multiple audio files (potentially) on teh raspi ? Id be very grateful indeed for help.

          2. Shawn Wallace says:

            Hmmm…I’d like to see that as well. It looks like you could use PyAudio, though I haven’t tried it. It looks it has to be installed from source, but someone posted these instructions in the forums:

            $ sudo apt-get install git $ git clone http://people.csail.mit.edu/hubert/git/pyaudio.git $ sudo apt-get install libportaudio0 libportaudio2 libportaudiocpp0 portaudio19-dev $ sudo apt-get python-dev $ sudo python pyaudio/setup.py install

          3. Ashley Brown (@arctic_sunrise) says:

            i still get the same error – even using the files in your tutorial :-(

            i might have to try a picobbler and gpio

  6. William says:

    I’m working on an arduino project where I want to be able to play some sound effects so I’ll be using this. Thank you very much for it. Can you answer a question for me though. I didn’t quite follow your description of the hookup (between arduino, Pi, monitors, etc) and there is no diagram so I can’t tell. which Pi board do you have? I ONLY need it to play the sounds so I would like to get the cheaper Model A if possible.

    1. Shawn Wallace says:

      The Arduino is connected to the Pi via USB. If the Ethernet cable is hooked up in this example it was just because I was using that to connect over VNC so I didn’t need a monitor keyboard. So, sure you can use the Model A.

  7. Dere says:

    I got everything working like you desribed, but every so often when i hit a button is playing it multiple times in one sec. Any ideas on how to fix this?

    1. Shawn Wallace says:

      Sure; there’s a noisy mechanical connection in the switch that is being read as multiple presses. You probably need to debounce the switch. The easiest way is to do this in code, like this Arduino example:

      http://www.arduino.cc/en/Tutorial/Debounce

  8. William says:

    I got this working as described above but I’m having an issue. I want the script to execute on startup with no user input. In the process of getting there I logged out of the GUI and tried to run the script from the command line. The issue is that the sounds don’t work from the command line like they do in the GUI. I can tell the script is working though because the “Print” lines are showing up. All I get for audio is a momentary click. Any idea how to fix that?

    1. William says:

      I figured this out. I added the script to rc.local

  9. Twin Cities Maker » Blog Archive » Building a classic Star Trek control panel says:

    […] sound loop, plus plays sounds as the switches are flipped. (The code was freely adapted from an article on the Make: blog.) A pair of amplified computer speakers was cannibalized and installed inside the panel. […]

  10. Eibhlin says:

    I am running this program as python playSounds.py but it reports an ImportError: No module named serial. I have installed python-serial and I can import serial within the Python Shell. Do you have any idea why this happening?

    1. Eibhlin says:

      For some reason sudo apt-get install python-serial was not sufficient. I had to install it by downloading from pypi, extracting the file and then install it. (I had this problem for the best part of 2 days!)

  11. MAKE | Building a Classic Star Trek Control Panel says:

    […] sound loop, plus plays sounds as the switches are flipped. (The code was freely adapted from an article on the Make: blog.) A pair of amplified computer speakers was cannibalized and installed inside the […]

  12. Building a Classic Star Trek Control Panel - IT Clips says:

    […] sound loop, plus plays sounds as the switches are flipped. (The code was freely adapted from an article on the Make: blog.) A pair of amplified computer speakers was cannibalized and installed inside the […]

  13. Smartsenses says:

    Reblogged this on SainSmart.

  14. computer repair services washington says:

    I’m working on an arduino project where I want to be able to play some sound effects so I’ll be using this. Thank you very much for it

  15. Diogo Serra says:

    Sorry I understand the exploration of the integration of the two devices , but why not use just the GPIO of the RPi to control the play of the samples? It doesn’t sound implossible to me , does it ?

    1. Mark Smith says:

      well, you’re not WRONG… I’d just think there are a few people out there that wouldn’t want to re-write their entire arduino sketch to accommodate the sound… A raspi at $20-35 that can play multiple channels at once is a better deal than getting an mp3 shield or trigger board for $50 that only does that one thing and is only one channel. A minimum of code-redo on the arduino and this simple-ish setup on the pi, and you’re good to go.

Comments are closed.

Discuss this article with the rest of the community on our Discord server!
Tagged
Shawn Wallace

Shawn Wallace is a MAKE contributor, artist, programmer, and editor living in Providence, R.I. He designs open hardware kits at Modern Device and organized the Fab Academy at the Providence Fab Lab. He makes iPhone synthesizers with the Fluxama collective and is a member of the SMT Computing Society.

View more articles by Shawn Wallace

ADVERTISEMENT

Maker Faire Bay Area 2023 - Mare Island, CA

Escape to an island of imagination + innovation as Maker Faire Bay Area returns for its 15th iteration!

Buy Tickets today! SAVE 15% and lock-in your preferred date(s).

FEEDBACK