Introduction: Raspberry Pi Communicates With Pi Zero and Amp Zero, Triggering Playing of Audio

About: Hi, I like making things for raspberry pi, and Arduino. Especially to do with machine vision and animals! But also for signals such as EEG and EMG. I'm a Neuroscience graduate. I'm interested in solar system …

Hi, I made this as a component for my elephant detection system. The aim was to use it to play sounds of bees in order to scare away elephants when they try to steal crops! But, the general idea might be useful to adapt to many other applications!

You can see the entire thing working in the video above!

My elephant detection and deter project is here:

* please let me know if you get stuck, I've dealt with various errors, so should be able to help!

Step 1: Parts Required

The parts required are:

  • Raspberry Pi 3
  • JustBoom Amp Zero pHAT for the Raspberry Pi Zero (£24)
  • Raspberry Pi Zero W with Soldered Header (£14)
  • of course you can solder them on yourself, which would mean a cost of £9 + headers e.g. £1
  • micro SD card with operating system installed. I've used 16GB with NOOBS pre-installed (£9.99)
  • Power supply for Amp Zero
  • Speakers x2, I'm using two waterproof 5V 4Ohm speakers from ebay (£11 each)

Step 2: Attach the Pi Zero and the Amp Zero Together

First we need to attach the amp zero HAT to the pi zero! This is really easy and you just need a tiny phillips screwdriver to do it. There are 4x metal spacers (well they look like nuts really), and 8x metal bolts.

1. Put you SD card into the pi zero card reader slot!

2. Fit your nuts/spacers and secure them to the pi zero with bolts

3. Gently attach the Amp Zero female header to the Pi Zero male header. Push down to do this.

4. Screw in your last 4x nuts to the holes in amp zero - so securing the two devices together

* The power connector on the Amp Zero is really delicate!

Step 3: Configure Raspian on the Pi Zero to Use the Amp Zero for Audio

Now we need to configure Raspian OS to use the Amp Zero for sound output. First open the config.txt file with the command on your raspberry pi:

sudo nano /boot/config.txt

Now scroll down the file until you reach the line:

dtparam=audio=on You should comment this out with #, so you will have:

#dtparam=audio=on Now, go ahead and add these new config instructions:




Ok, all done!

Step 4: Connect Your Speakers to the AmpZero

I just purchased some cheap waterproof speakers from ebay for £11 each. My speakers are 5V 4Ohm 'Weatherproof Communication Extension Speaker B185' .

First of all I cut off the 3.5mm jack that came on the end of the speaker input cables, and stripped the wires to attach to the speaker output terminals on the Amp Zero.

Then I attached the wires to the speaker output terminals of the Amp Zero. There are four output blocks. From the left: outputs 1 and 2 for the first speaker, outputs 3 and 4 for the second speaker. The -ve output is first, then the +ve output, same again for the second speaker.

Step 5: Test Out Playing Some Audio!

I downloaded wav files from The audio files here are licensed under Creative Commons Attribution 3.0 Unported (CC BY 3.0)

Probably you won't want to download the sounds of bees like I did! Anyway, I saved them in a directory called deter_elephants on the PiZero. That's what the code later will reference.

Now, we can play them with aplay. That's a linux command line player for the ALSA soundcard driver

The following python code can be used to play the wav files you downloaded:

import time<br>import randomimport os
scare_sounds = ['aplay bees1.wav', 'aplay bees2.wav', 'aplay bees3.wav']
i = 0
while i < 4:
    i = i+1
    to_play = random.choice(scare_sounds)
    print(to_play) #debug

We import time, random and os. We execute aplay use os.system(aplay filename). The command to execute on command line (i.e. "aplay") and the file to pass to aplay (e.g. "bees1.wav") are all stored in the scare_sounds list.

So random.choice will pseudo-randomly pick a list item and put it into to_play variable (e.g. "aplay bees3.wav") and then os.system will execute this.

The while loop is set to run while i is less than 4. You can adjust this to how long you want to continue playing the scare_sounds. It depends on the length of your scare_sound audio files and how long you think it will take to deter the elephants! So that needs some field research to determine!

So we've got that python code saved as into deter_elephants directory, and the bee%.wav files saved into the deter_elephants directory!

* yes I know it's unlikely you want to scare elephants as I did! But it can easily be adapted for something else!

Step 6: Getting Started to Communicate From Our Raspberry Pi 3 to the Pi Zero and Amp Zero Via Bluetooth

First let's make sure serial profile is loaded on both Raspberry Pi 3 and Pi Zero:

sudo sdptool add SP

Then we need to get a MAC address for the Bluetooth adaptors on the Pi Zero and the Pi 3.

We can use the following command to get the MAC address:


And we should get back something like 43:43:A1:12:1F:AC (which is what I got). You shouldn't get something like AA:AA:AA:AA:AA:AA. That is not good!

Step 7: Pair the Raspberry Pi 3 and Pi Zero

Now is the time to pair your raspberry pi 3 with the pi zero so they can communicate via Bluetooth! It's easy in the GUI. Just head to the Bluetooth icon on the top right of screen for your pi zero and select add device (the other device i.e. raspberry pi 3 should be set via its Bluetooth GUI menu to discoverable when you do this).

Step 8: Let's Deploy Code for the Pi Zero (and Amp Zero)

Now, the Pi Zero is going to act as the server in this communications scenario. It is waiting for a Bluetooth message from the Raspberry Pi 3 (client) and when it gets one, it will play audio! N.B. This is Python 3 code, because socket library doesn't allow Bluetooth in Python 2.7.x. You can get this on my GitHub here:

import socket
import time
import os
import random
hostMACaddress = 'xxx'
port = 9
backlog = 1
size = 1024
s = socket.socket(socket.AF_BLUETOOTH, socket.SOCK_STREAM, socket.BTPROTO_RFCOMM)
s.bind((hostMACaddress, port))
print("We are waiting for a message from the Raspberry Pi 3 to arrive via bluetooth!")
	client, address = s.accept()
	data = client.recv(size)
	if data:
		#echo back
	print("closing the socket")
message = str(data)
#convert the data received to a string
if message == "b'yes_audio'":
	print("play scare sounds now")
	scare_sounds = ['aplay bees1.wav', 'aplay bees2.wav', aplay bees3.wav']
	i = 0
	while i <10:
		i = i+1
		to_play = random.choice(scare_sounds)
print("Finished playing audio. ")

Step 9: Let's Deploy Code for the Raspberry Pi 3

Now, the Raspberry Pi 3 is going to act as client in this communications scenario. So it will send a message to the server (Pi Zero) via Bluetooth! N.B. It is Python 3 code.

import socket<br>message = "yes_audio"
serverMACAddress = '00:1f:e1:dd:08:3d'
port = 9
s = socket.socket(socket.AF_BLUETOOTH, socket.SOCK_STREAM, socket.BTPROTO_RFCOMM)
while 1:
    s.send(bytes(message, 'UTF-8'))
Wireless Contest

Participated in the
Wireless Contest