Project tutorial
Amazon Alexa-Powered Wireless Speakers

Amazon Alexa-Powered Wireless Speakers © GPL3+

This project shows you how to build your own Amazon Alexa-powered wireless speakers. "Alexa, tell me a joke."

  • 2,658 views
  • 1 comment
  • 13 respects

Components and supplies

Necessary tools and machines

09507 01
Soldering iron (generic)

Apps and online services

About this project

Alexa is a smart assistant by Amazon and it comes in various devices like Echo, Dot, etc. Alexa is fun to use and comes with thousands of skills that makes it possible to interact with smart home projects and other interactive skills. I wanted to build a wireless speaker and rather than just building another plain old wireless speaker, I decided to spice things up by adding the Amazon Voice Kit.

The goal of this project is to make a portable wireless speaker that connects to the internet via WiFi and runs amazon Alexa Voice Kit. You can pair it with your Alexa app on your smart phone and ask, "Alexa, what is the distance to the moon?"

Tools and Components

Here is a complete list of what you will need to get started with this project.

  • Raspberry Pi Zero W
  • Arduino Nano
  • USB Sound Card
  • NeoPixel Ring
  • SD Card 8GB or Higher
  • Old Stereo Speaker or two 8Ohm Speakers
  • 5V USB microphone or Cheap LM358 based Microphone
  • 18650 Li-ion Battery
  • TDA2822M (2)
  • 4.7 Ohm Resistor
  • 10k Resistor
  • 1.5k Resistor
  • 10K Pot
  • 0.01uF Capacitors
  • 100uF Capacitor
  • 0.1uF Capacitor
  • 220uF Capacitor
  • 20nF Capacitor
  • Male Header pins
  • PCB wire connectors
  • PCB
  • Boost Converter
  • Micro USB OTG Cable
  • Micro USB Cable
  • Vinyl sheet (Optional)
  • Soldering Iron
  • Soldering Lead
  • Connecting Wires

Raspberry Pi Zero Installing Raspbian

Before we dive right into building the speakers, let's setup the raspberry pi so you could connect it to WiFi and enclose it in the speaker box later. The raspberry pi runs Raspbian OS, which can be downloaded from the raspberry pi website, make sure you download the lite version of the OS as you will be running this headless (No monitor or keyboard).

After Downloading Raspbian Lite OS, you will need to create a Bootable SD card for the raspberry pi to boot from, for this you can use Ether or Rufus. Make sure you select the right image file while writing to the SD Card.

After you have completed writing to the SD Card, now we need to add a few files to make sure your raspberry pi connects to the internet and enables ssh. To connect to the internet, create a file wpa_supplicant.conf and add the following lines to it.

country=US
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1
network={
    ssid="ssid"
    scan_ssid=1
    psk="password"
    key_mgmt=WPA-PSK
}

Make sure you enter your WiFi SSID and password in the file where it mentioned "SSID" and "Passphrase".

Next, to enable ssh access you will need to create a file named ssh (empty file) in the root folder of your SD Card.

Next, you should plug the micro SD card into your raspberry pi and you should see the led on the raspberry pi blink randomly indicating successful boot. After few minutes go to your WiFi router page and in the list of wireless clients you should find the IP address, copy that for later.

To ssh to your pi you will need putty, once you have downloaded putty paste the IP address in the field where it says host name and hit connect. This should ask you for a login username and password which is pi and raspberry. Once you have got ssh working we can proceed to building the speakers.

Building the Speakers

If you like DIY hardware now comes the fun part, lets start with building the speakers. I had an old stereo speaker which I decided to recycle, you could use any speaker of purchase a pair of 8 Ohm speakers.

I rapped the speaker with some vinyl sheets to give it a clean look, this covered the wear and tear of the old speaker and also made it look better.

Now that you have selected the speaker you are going to use we can proceed to the next step which is building the amplifier.

Building the amplifier

Now it is time to build a stereo amplifier which is small enough carry around and should be powered by a pair of 3.7V Li-ion batteries. For this I used a TDA2822M which is a low voltage dual amplifier, this amplifier IC is cable of operating at voltages down to 1.8V. I'm using 2 ICs in Bridge mode to get max power output from both the ICs.

You should follow the circuit diagram as seen above, you can try it out on a breadboard if you want to make changes, but make sure to solder all the components onto a PCB once you are done with the testing.

The two 10K pots are used to balance the amplitude of the sound output, make sure you adjust these in such a way that the amplitude from both the speakers need to match to get a perfect sound balance.

Once you have done creating the circuit you should test it by connecting the speakers and powering it up using the Li-ion battery. You can test it by playing some music from your phone or for better test results you could check by supplying a sine wave of different frequencies, using a signal generator.

Sound Card

The pi zero doesn't have a 3.5mm jack like the other Raspberry pi series, one option would be to use the GPIO pins to give out audio in the form of PWM waves. I did not go with the GPIO pins as there was too much noise generated when working with WiFi.

So I decided to go with a USB sound card, I purchased one from Ebay and this worked great with minimal noise. The sound card comes with 2 3.5mm jacks, one for the audio output and the other for the microphone input.

I desoldered both the jacks and soldered some wires which I connected to the amplifier. I also added a 10k pot to the microphone terminals to adjust the signal amplitude.

For the microphone I used a LM358 based microphone module which I bought at a local hardware store. You could also go with a USB microphone. Now that the individual modules are done, it is time to assemble it all together, to complete the wireless speakers.

Completing the Wireless Speakers

There was enough unused space inside my speakers that I used to add the amplifier and the remaining modules. Before you add all the components into the case make sure to test it out once.

To Power the raspberry pi you will need a 5V source, so you will need to add a boost converter to convert the 3.7V from the li-ion batteries to 5V. For this I used the XL6009 Boost converter, you will have to adjust the voltage to 5V by varying the 503 Pot.

Plot out where you are going to place all the modules in the case, and then use screws to hold the components in place.

For the microphone, power switch and charging port you need to add additional holes I used a rotary tool for this.

I used a bit of plastic sheet to hold the battery, with the help of two screws on either sided of the battery, the remaining modules were held in place with the help of some screws.

Make sure to label the wires or use different colored wires so you don not mix them up. Also make sure there is sufficient space between the speakers and the circuits as the speakers metallic contacts may short the circuits.

The raspberry Pi can be powered via the GPIO pins, connect the 5V pin of the pi to the +Vout Pin of the Boost Converter and Gnd pin of the pi to -Vout of the Boost Converter, you should do the power connections only after you have set the boost converter to 5V.

The amplifier circuit can either be powered via the boost converter or by connecting it directly to the battery. Add a switch between the battery and the boost converter to turn off the wireless speaker.

Once everything is in place you can test it out before closing up the entire case, try playing some songs by ssh into the raspberry pi.

Once that is done, you have completed all the hardware connections, next we need to work on the software to install the amazon voice kit and to get Amazon Alexa up and running.

Software Time

The main software that we need to set up is the amazon voice kit, before we can start with the installation, you will need to create an amazon developer account, you could also use your amazon shopping account to login.

Once you are logged in to your developer account, navigate to the Alexa tab and select Alexa voice service. On the next page select create product.

Select Product Name and Product ID (example: raspberry_pi), select product type as Alexa enabled device and fill in the rest as appropriate.

Next, you need to create a Security Profile,

  • From the select a security profile - Select Create New Profile
  • In the profile name - Enter raspberrypi
  • In the security description - raspberry pi and hit next
  • Make sure you note all the details in this page for later

In the allowed origins field enter the following urls -

http://localhost:5050
http://<raspberry-pi-ip>:5050

and in the return urls field enter

http://localhost:5050/code
http://<raspberry-pi-ip>:5050/code

and then click finish, you will need all the details in the past few pages for later, you can visit the developer page and view the details of your device if you need them later.

Once you have got all the developer related IDs set up, now it is time to install the kit on the raspberry pi, open up a ssh terminal in the raspberry pi and run the following commands to install git.

sudo apt-get update
sudo apt-get install git

To make things easier to install we will be using an automated script created by xtools-at, this will also give us the option to side load the google assistant also if you need it.

Let's start with cloning the repository -

cd /opt
sudo git clone https://github.com/xtools-at/AssistantPi.git AlexaPi

Once that is done it is time to run the automated script, this will install all the dependencies and software required to run Alexa on the wireless speakers.

sudo /opt/AlexaPi/src/scripts/setup.sh

If you prefer to install everything yourself, you can visit the Alexa raspberry pi tutorial, although this will require to install the desktop version of raspbian.

Once you have installed everything it is now time to setup the audio input and output devices on the rapsberry pi zero. For this, make sure the USB sound card is plugged in and connected to the amplifier.

To set the sound devices you will need to edit a configuration file to do that run -

cd ~
nano .asoundrc

Next edit the fields where it says card number and device number below, usually for USB sound card, the card number is "1" and the device number is "0".

pcm.!default {
  type asym
  capture.pcm "mic"
  playback.pcm "speaker"
}
pcm.mic {
  type plug
  slave {
    pcm "hw:<card number>,<device number>"
  }
}
pcm.speaker {
  type plug
  slave {
    pcm "hw:<card number>,<device number>"
  }
}

You can check your card and device number by running -

aplay -l

Once you are done editing the file reboot the raspberry pi with -

sudo reboot

Once the pi has rebooted, you should have the sound input and output devices working, now it is time to run Alexa on your wireless speakers.

python /opt/AlexaPi/src/main.py

And you should hear Alexa respond back in a couple of seconds indicating that everything went fine, now you can ask the Alexa powered wireless speakers questions just as you would do the amazon echo device.

Upgrade

Adding a music visualizer, here is how to spice things up and add some NeoPixel LEDs to dance with the music you play.

Circuit

The circuit is simple all you need to do is connect the Arduino Nano as shown in the image bellow. I neopixel ring I used has 11 LEDs and a few buttons that I will use as volume controls in the future upgrade.

Software

To program the arduino Nano you will need the Arduino IDE, you can download this from the Arduino official website. After you download the IDE copy the code from below and paste it in the IDE.

You will also need to add the NeoPixel Library, that can be imported in Sketch => Include Library => Manage Libraries. Search for NeoPixel library and install it.

Make changes to the number of LEDs to the number of LEDs on your neopixel. Make sure you select the right board from the boards menu and the right serial port and hit upload.

#include <Adafruit_NeoPixel.h>
#define MIC_PIN A1 // Microphone Input
#define LED_PIN 5 
#define N_PIXELS 11 // Number of LEDs on the ring
#define N 200 
#define fadeDelay 10 
#define noiseLevel 2 
Adafruit_NeoPixel strip = Adafruit_NeoPixel(N_PIXELS, LED_PIN, NEO_GRB + NEO_KHZ800);
int samples[N]; 
int periodFactor = 0; 
int t1 = -1; 
int T; 
int slope; 
byte periodChanged = 0;
void setup() {
 strip.begin();
 ledsOff();
 delay(500);
 displayColor(Wheel(100));
 strip.show();
 delay(500);
}
void loop() {
 readSamples();
}
void readSamples() {
 for(int i=0; i<N; i++) {
   samples[i] = analogRead(0);
   if(i>0) {
     slope = samples[i] - samples[i-1];
   }
   else {
     slope = samples[i] - samples[N-1];
   }
   if(abs(slope) > noiseLevel) {
     if(slope < 0) {
       calculatePeriod(i);
       if(periodChanged == 1) {
         displayColor(getColor(T));
       }
     }
   }
   else {
     ledsOff();
   }
   periodFactor += 1;
   delay(1);
 }
}
void calculatePeriod(int i) {
 if(t1 == -1) {
   t1 = i;
 }
 else {
   int period = periodFactor*(i - t1);
   periodChanged = T==period ? 0 : 1;
   T = period;
   t1 = i;
   periodFactor = 0;
 }
}
uint32_t getColor(int period) {
 if(period == -1)
   return Wheel(0);
 else if(period > 400)
   return Wheel(5);
 else
   return Wheel(map(-1*period, -400, -1, 50, 255));
}
void fadeOut()
{
 for(int i=0; i<5; i++) {
   strip.setBrightness(110 - i*20);
   strip.show();
   delay(fadeDelay);
   periodFactor +=fadeDelay;
 }
}
void fadeIn() {
 strip.setBrightness(100);
 strip.show();
 // fade color in
 for(int i=0; i<5; i++) {
   delay(fadeDelay);
   periodFactor+=fadeDelay;
 }
}
void ledsOff() {
 fadeOut();
 for(int i=0; i<N_PIXELS; i++) {
   strip.setPixelColor(i, 0, 0, 0);
 }
}
void displayColor(uint32_t color) {
 for(int i=0; i<N_PIXELS; i++) {
   strip.setPixelColor(i, color);
 }
 fadeIn();
}
uint32_t Wheel(byte WheelPos) {
 if(WheelPos < 85) {
   return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
 } 
 else if(WheelPos < 170) {
   WheelPos -= 85;
   return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
 } 
 else {
   WheelPos -= 170;
   return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
 }
}

After the code is uploaded now it time to play some tunes in the speaker and watch the LEDs dance to the music that you play.

Code

wpa_supplicant.conf SH
country=US
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1

network={
    ssid="your_real_wifi_ssid"
    scan_ssid=1
    psk="your_real_password"
    key_mgmt=WPA-PSK
}
Arduino NeoPixel CodeArduino
#include <Adafruit_NeoPixel.h>
#define MIC_PIN A1 // Microphone Input
#define LED_PIN 5 
#define N_PIXELS 11 // Number of LEDs on the ring
#define N 200 
#define fadeDelay 10 
#define noiseLevel 2 
Adafruit_NeoPixel strip = Adafruit_NeoPixel(N_PIXELS, LED_PIN, NEO_GRB + NEO_KHZ800);
int samples[N]; 
int periodFactor = 0; 
int t1 = -1; 
int T; 
int slope; 
byte periodChanged = 0;
void setup() {
 strip.begin();
 ledsOff();
 delay(500);
 displayColor(Wheel(100));
 strip.show();
 delay(500);
}
void loop() {
 readSamples();
}
void readSamples() {
 for(int i=0; i<N; i++) {
   samples[i] = analogRead(0);
   if(i>0) {
     slope = samples[i] - samples[i-1];
   }
   else {
     slope = samples[i] - samples[N-1];
   }
   if(abs(slope) > noiseLevel) {
     if(slope < 0) {
       calculatePeriod(i);
       if(periodChanged == 1) {
         displayColor(getColor(T));
       }
     }
   }
   else {
     ledsOff();
   }
   periodFactor += 1;
   delay(1);
 }
}
void calculatePeriod(int i) {
 if(t1 == -1) {
   t1 = i;
 }
 else {
   int period = periodFactor*(i - t1);
   periodChanged = T==period ? 0 : 1;
   T = period;
   t1 = i;
   periodFactor = 0;
 }
}
uint32_t getColor(int period) {
 if(period == -1)
   return Wheel(0);
 else if(period > 400)
   return Wheel(5);
 else
   return Wheel(map(-1*period, -400, -1, 50, 255));
}
void fadeOut()
{
 for(int i=0; i<5; i++) {
   strip.setBrightness(110 - i*20);
   strip.show();
   delay(fadeDelay);
   periodFactor +=fadeDelay;
 }
}
void fadeIn() {
 strip.setBrightness(100);
 strip.show();
 // fade color in
 for(int i=0; i<5; i++) {
   delay(fadeDelay);
   periodFactor+=fadeDelay;
 }
}
void ledsOff() {
 fadeOut();
 for(int i=0; i<N_PIXELS; i++) {
   strip.setPixelColor(i, 0, 0, 0);
 }
}
void displayColor(uint32_t color) {
 for(int i=0; i<N_PIXELS; i++) {
   strip.setPixelColor(i, color);
 }
 fadeIn();
}
uint32_t Wheel(byte WheelPos) {
 if(WheelPos < 85) {
   return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
 } 
 else if(WheelPos < 170) {
   WheelPos -= 85;
   return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
 } 
 else {
   WheelPos -= 170;
   return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
 }
}

Schematics

Circuit
Circuit skmmehjhcd sdi9erc1ex
Arduino NeoPixel Circuit
Neopixel vkfrxzex9c

Comments

Similar projects you might like

Wireless Stunts

by Alexander

  • 3,040 views
  • 4 comments
  • 10 respects

Alexa Powered Arduino Kitchen Assistant

Project tutorial by TheParticleGuy

  • 2,865 views
  • 1 comment
  • 14 respects

WW2 Tank Laser Tag Sherman & Panther

Project tutorial by Arduino “having11” Guy

  • 16,308 views
  • 1 comment
  • 57 respects

Arduino-Powered Smart Light (Works with Amazon Echo)

Project tutorial by Tinker Project

  • 17,627 views
  • 8 comments
  • 34 respects

Light Control Using Arduino and Amazon Alexa

Project tutorial by Rajesh

  • 14,225 views
  • 1 comment
  • 16 respects
Add projectSign up / Login