May 20, 2014

Day & Night Sampler - a raspberry pi mpd mini boombox Overview

This project had its origin a couple of years ago. I received a nice tin box with a plastic window on the front. The window just screamed out for a special display project of some kind. The box had "Day and Night Sampler" (D&NS) embossed on the front. Thus, the name.

I decided this would be a good box to hold a small prototype of a boombox that I wanted to build, complete with Vacuum Fluorescent Display (VFD), a super-nice glowy character display. You could use a character LCD or OLED display instead of the VFD, but you'll have to figure out how to adapt the python scripts for your particular display.





Features:
  • VFD character display 20 chars x 2 lines
  • Raspberry Pi attached to small amp through USB Sound card
  • IR Remote Control: Volume +/-, Playlist up/down/change, etc.
  • Internet WiFi Streaming of stations or play from stored playlists
  • View Now Playing Song Info, Date/Time, or Weather

Before Starting This Project:
This is not a good learning project for people brand new to the raspberry pi or hardware in general. You should have experience with:
  • Basic soldering and electronics
  • ssh/scp
  • Debian Linux
  • Software installation
  • nano or vi editor
  • a Dremel or hole cutter
  • a drill
This is a long build, but if you have the required skills, not a particularly difficult one. Working straight through these steps, you should be able to get a boombox like this built over a weekend.
Disclaimer & Warning
This project may involve potentially dangerous or hazardous activities. You undertake these activities at your own risk. The author is not responsible for any harm, injury or damage you may incur. If you do not think you have the skills to safely work on this project, DON'T!


Getting Started
I started with the tin enclosure and gathered enough materials to see if my project idea was feasible. After measuring and testing a rough fit for the speakers, VFD and raspberry pi, I had enough confidence that the project would work and things would fit.


Continue to Part 1: Parts List

Day & Night Sampler - a raspberry pi mpd mini boombox Part 1: Parts List


Bill of Materials/Parts List:
  • raspberry pi; I got mine from mcmelectronics.com, $35 plus shipping
  • two small speakers [*1] mcmelectronics has these for $10.99 each plus ship
  • Patriot WiFi USB dongle, model PCWUSB1150, on sale at Fry's, $7.99; 
  • adafruit stereo 3.7W Class D Audio Amplifier, part ID 987  $14.95
  • adafruit perma-prototype board, part ID 723 $5.95
  • adafruit Vacuum Fluorescent  Display 20 Char x 2 Lines, part ID 347 , $29.95 (Since Discontinued!)
  • adafruit 5V/2A power supply, part ID 276  $9.95
  • adafruit panel mount 2.1 mm DC barrel jack, part ID 610  $2.95
  • Syba (C-Media) USB sound adapter, available at amazon.com $7.99
  • 3.5mm headphone extension, male-to-male, under $5 on ebay
  • USB Short Extension cable, Type A Male to Female, about $3-5 from ebay; use with the C-Media USB sound adapter
  • adafruit 8 Bit (TTL) Logic Level Converter for interfacing the VFD to the pi, product ID 735, $1.50
  • adafruit mini remote control, product ID 389 , $5; Or, any surplus remote control you have
  • IR Sensor, TSOP38238, available at adafruit, product ID 157 , $1.95; also Mouser and others
  • SDHC Card, available at adafruit and others, adafruit part ID 102, $7.95
  • Lighted push button switch, about $4.00
  • Misc jumpers and wire
  • An enclosure of some kind, made or found. I used the tin described above. Wood box is a better choice.
  • "Spoon Handle", a carrying handle for the enclosure, about $4.95 at Ace Hardware
  • Misc 4/40 screws, 8/32 screws, brass standoffs and brass acorn nuts, also from Ace, under $5
 Note [*1]: The speaker sizes I used were predetermined by the tin I was using, small tin, small speakers. I used 2 inch speakers, and they are pleasant sounding but have little bass. Frequency range 150Hz to 20KHz. If you want to build a project like this with better sound, get speakers that are full-range, probably about 4" or more, preferably shielded.

The physical build, fitting all the hardware into the enclosure, will be discussed later. We'll start with making the software work.


Continue on to Part 2, Software Installation

Day & Night Sampler - a raspberry pi mpd mini boombox: Part 2 Software Installation

See Part 1, Parts List

To get started building the boombox software from scratch, complete the following steps:
  • Prepare your SD card and raspberry pi using the tutorial from the excellent Adafruit Learning System by Dr. Simon Monk. We will use the Raspbian 2014-01-07 Wheezy Debian distribution.
  • You need some software not covered in Lessons 1 -6 in the Adafruit Learning System:
    • sudo apt-get install python-pip      [easy install for other modules]
    • sudo pip install pyserial
    • sudo pip install feedparser     [for parsing weather info and other RSS feeds]
    • sudo apt-get install python-dev
    • sudo pip install spidev
  • To get mpd music player and sound working, and allow python to work as an mpd client, you'll need to install:
    • sudo apt-get install alsa-base alsa-tools alsa-oss alsa-utils
    • sudo apt-get install mpd mpc
    • sudo apt-get install python-mpd
  • For the python LIRC client pylirc to work :
    • sudo apt-get install liblircclient-dev
    • sudo pip install pylirc2
  • Install the IR Remote Control software by following Dr. Monk's guide on the Adafruit Learning System: http://learn.adafruit.com/using-an-ir-remote-with-a-raspberry-pi-media-center/lirc
    • We're using stock Raspbian Wheezy 2014-01-07 for our boombox; Dr. Monk is using XBMC. These changes are necessary:
      • skip the link about setting up your raspberry pi as a media center, in the Overview section
      • in the step labelled "LIRC", you can skip the "disable" of Enable GPIO TSOP IR Receiver as this is for the XBMC distribution
      NOTE! I am using pin 23 for lirc_rpi, Dr. Monk is using pin 18; if you want to use pin 18, make the following change in /etc/modules:
     
    # lirc_rpi gpio_in_pin=23
       lirc_rpi gpio_in_pin=18

Fix WiFi Dropout Issues
If you are using a WiFi adapter based that uses the 8192cu kernel module (Realtek chipsets) see the fix for occasional dropouts documented on Adafruit.
To install my code, download it from here: thisoldgeek rpi-boombox
  • Install the python scripts in /home/pi
  • Save the rpi_boombox_init.sh script in a directory of your choice; instructions for installing this script are on this page
  • To configure the weather display in rpi_boombox.py:
    • change the following for your locality
def weather_info():
    d = feedparser.parse('http://www.wunderground.com/auto/rss_full/CA/Pleasant_Hill.xml')    # Change to your local area

NOTE: This is not using the API for wunderground, for which you need to get an API key
  
Contiune to Part 3, Configure MPD Player

Day & Night Sampler - a raspberry pi mpd mini boombox: Part 3 Configure MPD Player

See Part 2, Software Installation


With the raspberry pi off, plug your USB Sound Card/Dongle into a raspberry pi USB port. If you want your boombox to work with WiFi and no additonal USB hub, you'll probably need a USB extension of some kind. The USB Dongle is too thick to fit into a pi USB port and also use the second USB port for a WiFi dongle.

The USB extension would look something like this:
Now, power up your raspberry pi.

To install the mpd music player-

  • sudo apt-get install mpd mpc  (should have already been done if you completed Part 2) and configure:
  • Raspian by default stores its mpd files in /var/lib/mpd. You'll need to add:
    • your own music to /var/lib/mpd/music
    • your own playlists to /var/lib/mpd/playlists
    • select your music/create your playlists and copy to the appropriate folder with "scp ...." scp username@192.168.0.x/playlists/classical.m3u /var/lib/mpd/playlists/classical.m3         (on the console of the rpi and copying from your source computer to the rpi)
    • Note: you will need to change permissions on the /var/lib/mpd/.. folders and files in order to copy to them
    • Alternatively, you can create the music and playlists under a /home/pi/.mpd directory, but you will have to change the locations of these in mpd.conf
    • If you do put these folders in the home directory, in /etc/mpd.conf you must comment out the line, under General Music Daemon Options, that says
       #user           "mpd"
    • in /etc/mpd.conf you need to change the following lines to look like this:
      • #bind_to_address        "localhost"
      • zeroconf_enabled                "yes"            [used in Avahi service]
      • in my case, when I entered mpc {to get an mpd status], I saw this:
          • [playing] #1/7   9:21/0:00 (0%)
            volume: n/a   repeat: off   random: off   single: off   consume: off
      • so, I had to change the "audio_output" section in mpd.conf to this:audio_output {
                type            "alsa"
                name            "My ALSA Device"
                device          "hw:0,0"        # optional
                format          "44100:16:2"    # optional
        #       mixer_device    "default"       # optional
        #       mixer_control   "PCM"           # optional
                mixer_control   "Headphone"       # added for CMedia USB from alsamixer panel volume name
                mixer_index     "0"             # optional
        }

The entry for mixer_control in /etc/mpd.conf, for my USB sound card, had to be changed to match the name below the main volume control on the alsamixer panel.
To find this, run alsamixer from the command line.
 

At this point, you should have a working mpd installation. You can test mpd out by adding a music stream and starting it. Enter "mpd" on the command line. Mpd should start up; you may get an error about port 6600 already in use. That's harmless, meaning mpd was already started.

 Then run mpc. You'll see something like:

 volume: 100%    repeat: off    random: off

First thing to do is set the volume waaaaay down, perhaps to 20%, or it will be painfully LOUD!

mpc volume 20%

You can test running a web radio stream by:

 mpc add http://66.162.107.142/cpr2_lo

followed by:

mpc play

...and you should hear some music playing.



Day & Night Sampler - a raspberry pi mpd mini boombox Part 4: Starting the Boombox Automatically

See Part 3 of this project, Configure MPD Player.

Let's start making the software work together. First, copy code from github to your target raspberry pi. You should have already completed this in Part 2 of this project. You'll need the VFD.py and rpi_boombox.py scripts for the basic boombox functions. You can copy these to /home/pi.


Now we'll add software changes that make the raspberry pi act more like consumer electronics. It won't be "instant on", but it will start up in a minute or so and the rpi_boombox.py script will start shortly after that. The following script can be copied from github, it's name is rpi_boombox-init.sh. You should have already copied this in Part 2.


We need to start the main rpi_boombox.py python script at power on.
Here is the script:
### BEGIN INIT INFO
# Provides: rpi_boombox - now playing / date time /weather
# Required-Start: $remote_fs $syslog
# Required-Stop: $remote_fs $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Main Menu shown on Vacuum Flourescent Display
# Description: now playing / date time /weather
### END INIT INFO


#! /bin/sh
# /etc/init.d/rpi_boombox


export HOME
case "$1" in
    start)
        echo "Starting rpi_boombox"
        /home/pi/rpi_boombox.py  2>&1 &
    ;;
    stop)
        echo "Stopping rpi_boombox"
    rpi_boombox_PID=`ps auxwww | grep rpi_boombox.py | head -1 | awk '{print $2}'`
    kill -9 $rpi_boombox_PID
    ;;
    *)
        echo "Usage: /etc/init.d/rpi_boombox.py {start|stop}"
        exit 1
    ;;
esac
exit 0

Add this script in /etc/init.d/rpi_boombox.

Reboot and the python script rpi_boombox.py in /home/pi/ should start up auto-magically!

Continue to Part 5: Hardware Configuration

Day & Night Sampler - a raspberry pi mpd mini boombox Part 5: Hardware Configuration

See Part 4, Starting the Boombox Automatically

In this section we'll discuss connecting the VFD, the IR remote receiver, the amp and testing with a power supply.

Hook up the raspberry pi to the VFD wires according to the following diagram:

NOTE: The attached cable on the VFD I purchased routed +5V to a Yellow wire and GND  to a GREEN wire, instead of the more common Red for +5V and Black for GND.

UPDATE March 11, 2015: Tim had a question on how the audio was fed to the amp. A USB Sound Card/dongle is attached to the Pi. A 3.5mm audio cable is plugged into the USB Sound dongle. On the other end of the cable (red arrow in picture), the cable is stripped into separate right/left/ground wires and attached to the 3.7W amplifier.


 From the VFD datasheet, I could not tell if the display was 3.3V signal-level (TTL) tolerant. So, I used a level shifter to change the output of the Rpi SPI signals from 3.3V to 5V as shown in the above diagram. 

Wall power, at +5V, is being fed directly to the board shown above. The VFD takes its +5V and GND from this board, not the Rpi. GND from the Rpi is tied to the board shown above, to supply a common GND.

To connect the IR receiver, here's a diagram from adafruit learning system:



Above is a diagram of the rpi pin connections. NOTE: I used pin #23 for the lirc_rpi (IR) input pin. If you follow Dr. Monk's tutorial, he used pin #18. This can be changed in /etc/modules.



To set up the audio output, attach a 3.5mm headphone extension cable to the USB sound dongle and the other end to some speakers.

You might be able to run the raspberry pi with the VFD attached from a computer USB port. However, it's safest to power the pi from a powered USB hub capable of 2 Amps output.

Power the raspberry pi up and wait a minute or two. If all went well, you should hear sound through your speakers and, shortly after that, the currently playing song or stream will show up on your VFD.

If things aren't going well, you have some troubleshooting to do. Most often, there's a problem with a connection: jumper wires are loose or in the wrong header, for example. So, check your connections first. After checking out possible hardware issues and eliminating them, and you still have a problem, you may have something slightly different in your software setup than what I've documented here, or something has changed, say in the Debian image you're using.





Day & Night Sampler - a raspberry pi mpd mini boombox Part 6: Using the Remote

See Part 5, Hardware Configuration.

At this point, your software and hardware should be working together well.

Here's a diagram of the functions programmed into the remote control:

I've found you have to press the keys on this remote very firmly to get a good response. It's natural to think that holding down a volume up or down key will continuously change the volume - it won't on this remote. You will have to press the up/down button each time you want to change the volume by 5%, as programmed in the code.

To switch the VFD display between displaying mpd current song, current time and weather:
  • Press the Setup button
    • the VFD will display mpd   time   weather with a blinking cursor under the first letter of mpd
    • use the Left/Right arrows to move the cursor between the choices
    • to make a selection, press the Enter/Save button

To turn off the boombox, press the Stop/Mode button. This will start a shutdown sequence on the pi and take about 30-60 seconds to complete. The VFD will display a message: " Shutdown Started/ Off at red light out". This may sound cryptic. I used a lighted power on/off pushbutton to power the pi, with the red LED powered by the pi. When it's safe to remove power from the raspberry pi by using this pushbutton, the red LED will be off, thus the message.


Continue to Part 7: Building the Box

Day & Night Sampler - a raspberry pi mpd mini boombox Part 7: Building the Box

See Part 6, Using the Remote


The tin box I wanted to use as a case for the mini boombox had "issues" that I thought I could get around. I knew that the metal of the box, unless insulated, could short out pin connections easily. My idea to insulate the box was to coat the inside in vinyl adhesive contact paper.

That worked OK for awhile - until I started moving the pi and other components around inside the box. The sharp pins of the components pierced the contact paper and started to short out. My solution to that was to case up the raspberry pi (a box within a box) and put insulating tape around connections and other potential trouble spots.


To create a pattern for cutting speaker holes, I placed a piece of paper over the back circular cone of the speaker. I dragged a pencil over the circumference of the speaker, which left a dark line where the edge was. I cut this out, taped it to the box, drew a line around the pattern with Sharpie. Remove the pattern, and Dremel the circle out. Though rough, it's good enough - inserting the speakers through the front of the box covers the ugly hole.

I used acorn nuts for the speaker holding screws because they matched the color family and metallic style of the box. I added a metal "spoon handle" available at the hardware store, also in similar color scheme, screwed through the lid as a carry handle.





 The DC in is straight 5V. I soldered it to a piece of perf board (adafruit prema proto board) and then the switch in line to a USB micro cable with the USB A end cut off. The micro end I kept and used to power the raspberry pi. The LED part of the pushbutton switch I connected to Tx on the pi. This will supply power as long as the raspberry pi is up, and drop power when the pi has safely shut down. When the light goes out, the pi is down, so it's safe to push the button to turn off power completely.

Continue to Part 8 -Controlling your Music







Day & Night Sampler - a raspberry pi mpd mini boombox Part 8: Controlling your Music

See Step 7, Building the Box


One way to control mpd music on a network is with the MPoD app, available for IOS. There are similar mpd clients on Android.
MPod for I0S is nice if you have multiple players (boombox, desktop, laptop). If you have avahi/zeroconf configured properly,
MPod can discover your players. You can also add a player manually to MPoD.
To configure on a raspberry pi:
  • Make sure that the line in /etc/mpd.conf that says "zeroconf_enabled' is set to "yes"
The following steps are from: (http://richddean.com/post/36677503186/raspberry-pi-setup)

  • - Install Avahi
      sudo apt-get install avahi-daemon
      sudo insserv avahi-daemon
    - Create a config file for Avahi
      sudo nano /etc/avahi/services/multiple.service
    Add the following lines in the nano editor.
     
     
     
          %h
         
              _device-info._tcp
              0
              model=RackMac
         

         
              _ssh._tcp
              22
         

     
  • Apply the new configuration:
    • sudo    /etc/init.d/avahi-daemon restart
  • restart mpd:
    • sudo /etc/init.d/mpd restart
Personally, I prefer the instant gratification of an IR remote, which is included in these build instructions. The remote will change a station quickly, allow fairly quick volume up/down and other behaviours on your boombox or music player.

Let me know in the comments if you build one of these. I hope you do, and use your own unique creativity to mod it up! 
   

May 18, 2014

Simple Dead Bug Computer Volume Control

Here's a follow-on to my post about using the LeoStick as a volume control. You must make the changes referred to in the LeoStick post if you want to try what I describe here. The Arduino micro also uses the ATMEGA32u4 chip - it's a Leonardo in a tiny package. So, it too can function as a computer volume control.

I was brainstorming how to use the Arduino micro with a minimum number of wired connections to a rotary encoder. It struck me that the micro is breadboard-compatible, so had pin spacing that could work with the rotary encoders that Adafruit sells. In fact, you  can solder the rotary encoder right on top of the micro,  "dead bug " style.


 

The first trick here is to align the pins from the encoder to micro pins that are also contiguous to each other.  One side of the encoder, used for volume up/down, uses digital pins.  The other uses Analog pins as digital pins. 

Also, there are two required GND connections to make the encoder work. The second trick is to  make a GND connection with analog and digital pins. You can do this by setting pins LOW:

pinMode(A3, OUTPUT);
digitalWrite(A3, LOW);


Like the LeoStick, this works on Linux, Windows, Mac and Android.

Click for code on github