Wednesday, July 22, 2015

Motion Triggered Music Player


Turn on music as soon as you walk into a room! This tutorial covers a minimalistic entrance sensor that triggers music of your choice. The design can easily be adapted to suit a variety of needs (e.g. alarm system, motion-triggered lights, etc.).

The system is controlled by a Raspberry Pi, and aside from the RPi and speakers, the materials cost is extremely low (~ $20). Build time is approximately 1 - 2 hours.

For an inexpensive set of speakers, check thrift stores and/or friends' garages.

Materials




1. Raspberry Pi + Accessories
This tutorial assumes that the RPi has been set up and includes all necessary accessories (e.g. power cord, SD card, etc.). It is also recommended to use an RPi that has been set up for wireless and with a GPIO breakout cable.
Here's a tutorial on how to set up the RPi.

2. Infrared (IR) Breakbeam Sensor(s)

3. 10 kOhm Resistor

4. Speakers w/ 1/8" audio jack (standard headphone jack)

5. 22 or 24 gauge wire (min. 15 feet)
Recommended to get at least two to three colors (e.g. red, black & yellow).

6. Optional: PCB board and/or Connectors
Although not necessary, a small PCB is recommended to make the system more robust and longer lasting.
Add connectors between the sensor and RPi GPIO for simpler installation, transportation and to more easily allow for future customization.

7. Recommended: LED + 10kOhm Resistor (for testing)


Tools


1. Soldering Iron*, Solder & Solder Wick

2. Wire Strippers

3. Epoxy

4. Multimeter

5. Recommended: Breadboard + Breadboard Wires (for setup and testing)
*Note: If you do not have access to a soldering iron, you can twist wires together and coat in hot glue.

Step 1: Setup Raspberry Pi to Play Music



This project provides an overview for how to play a music file saved on the RPi SD card. Possible modifications include setting up the RPi to play music from an external USB drive or streaming music from a website.
1. Install updates w/ this command:
sudo apt-get update
2. Download example music file for testing:
wget   http://goo.gl/MOXGX3   -O example.mp3 --no-check-certificates
Note: This saves the file in whatever directory you are currently in.

3. Open the Terminal window and go into the folder with the music file
Use following command (switch “FolderName” for location of music file):
Cd /home/pi/FolderName
4. Type following command to play file over the audio output:
omxplayer example.mp3

Step 2: Build it! Pt. 1



Recommended to build the circuit on a breadboard and test prior to soldering any of the components onto the PCB board.

 
1. Extend the wire leads of the IR beam (2 wires: red & black) and IR receiver (3 wires: red, black & yellow).
Plan out and measure the route of the wires from where the IR beam and receiver will be installed to the speaker system. At a minimum, make each wire lead 3 - 5 ft.
Recommended to add connectors to the IR beam and/or receiver to make it easier to disconnect them and, if necessary in the future, modify the system.



If the schematic makes sense, skip the rest of the Build it! steps.

2. Connect the positive side of the IR sensor beam to Raspberry Pi 5V pin.

3. Connect the negative side of the IR beam to RPi ground pin.









4. Connect the positive side of the IR receiver to RPi 5V pin.

5. Connect the negative side of the IR received to the RPi ground pin. 








Step 3: Build it! Pt. 2


1. Connect the data pin (yellow wire) of the IR receiver to RPi GPIO pin 23.
 
2. Connect the 10kOhm resistor between the data pin/RPi GPIO pin 23 and the RPi 5V pin.








3. Recommended: Connect an LED + resistor across the IR receiver data pin (yellow wire).
Connect the positive LED leg to the IR receiver data pin. Connect the negative LED leg to the resistor. Connect the other side of the resistor to ground.








4. Plug in RPi and log in; check that IR beam turns on.
If you've connected the LED, it should turn on when the IR receiver gets the signal from the IR beam. Check that the LED turns off if you interrupt the beam.



Step 4: Code it! Pt. 1

The following is a basic program to play a song when the IR beam signal is broken (AKA when you walk between the IR beam and IR receiver). Here's the GitHub page.

Save the song of your choice to an easy-to-remember-and-type folder, in this example I used "FolderName" to designate the title of the folder in which the mp3 is saved.

Note: This program prints a message to the screen each time the song is played. This worked for my needs, as I wanted to have a log of each time music is played, but of course this code is just an outline of what is possible. Modify it to suit your needs :)
import time
from threading import Thread

import RPi.GPIO as GPIO
import subprocess


class AutoTrigger():
    def call_omxplayer(self):
        print ("playing " + self.file_path)
        pid = subprocess.call(['omxplayer', self.file_path], stderr=subprocess.PIPE, stdout=subprocess.PIPE)
        self.is_running = False

    def play_song(self):
        if not self.is_running:
            self.song_thread = Thread(target=self.call_
omxplayer, args=())


            self.song_thread.start()
            self.is_running = True

    def __init__(self, pin, file_path):
        self.pin = pin
        self.file_path = file_path
        self.is_running = False
        GPIO.setup(pin, GPIO.IN)
        '''
            This is a hack (the callback) thanks for python closures!
        '''
        GPIO.add_event_detect(self.
pin, GPIO.FALLING, callback=lambda x: self.play_song(), bouncetime=10)

   
def main():
    GPIO.setmode(GPIO.BCM)
    AutoTrigger(25, '/home/pi/FolderName/SongName.
wav')
    AutoTrigger(24, '/home/pi/FolderName/SongName2.mp3
')

    print ("Ready: !")
    try:
        while True:
            pass
    except KeyboardInterrupt:
        GPIO.cleanup()       


if __name__ == '__main__':
    main()

Step 5: Install it!



1. Check system for functionality. If system works as expected, coat all exposed electrical connections (except for RPi plugs) in hot glue or epoxy.
Be aware that epoxy is essentially permanent; use hot glue if you want to modify the system in the future.



2. Attach the IR beam to one side of your front door frame, and the IR receiver on the other side of the door frame, directly in line with the beam. Run the wires up and over the door frame.
It is somewhat tricky to align the IR beam and receiver; highly recommended to use the LED + resistor to quickly see when the beam and receiver are aligned. Use sticky tack or tape to help w/ alignment.
Secure the IR sensor via thumbtacks, nails, screws, etc. Suggested to run the wires up and over the door frame (instead of on the floor).

3. Place the RPi in a convenient and out-of-the-way location.
Consider where you want to place the speakers. If you need longer wires, measure the needed distance and solder in additional wires to the RPi power, ground and GPIO pins.






4. Set up the speakers.

5. Test!
Plug in the RPi and trigger the system to be sure that the music plays when you walk through the IR beam.

Step 6: Activate and Enjoy!


The only issue w/ this design is that the IR receiver range is shorter than I originally wanted, only about 2 feet. Some solutions to this problem would be to use a lens to magnify the IR beam or increase the power input.

Possible modifications of the project include using WiFi to stream music or podcasts, triggering lights, and/or using it to detect motion in and out of your home for a basic security system.. or to see how many times your cat leaves the house while you're gone.

Thanks for reading! Please check out and subscribe to my YouTube channel for more projects and other cool science stuff.

Happy building!

Sunday, May 24, 2015

DIY Money Blower Machine


A fun & whimsical way to collect donations for a variety of causes. Depending on the materials you have lying around your house, this machine can be built for free! Even if you have to buy all the materials, maximum cost is less than $15.
This is a minimalistic and easy design for makers of all experience levels! The whole machine takes about an hour to build.
------
Materials

1. Cardboard Box (not pictured)
Medium to small, somewhere around 12 in. x 12 in. x 12 in.
2. DC Computer/Electronics Fan
Recommended to harvest one from broken electronics equipment. Otherwise, these can be purchased online from a variety of manufacturers for ~ $5. Here's one website.
3. Power adapter cord (120 VAC to ~ 12VDC)
Recommended to repurpose an unused power cord lying around; old charging cords (e.g. phones, cameras, etc.) are perfect.
4. Saran Wrap
------
Tools

1. Wire strippers
2. Scissors
3. Soldering Iron*
4. Hot glue gun
5. Duct Tape (or other heavy duty tape)
*If soldering iron isn't available, tightly twist wires together and coat in hot glue.
------
Build it! Pt. 1



Optional: Spray paint, or paint, cardboard box.
1. Cut a 3 in. x 1 in. slot in the top of the cardboard box.
2. Cut windows into the 3 of the sides of the cardboard box.
Be sure to leave at least 1 - 2 in. of cardboard along the edges of each side.
3. Cut the remaining side into a door.
Cut along 3 of the edges (bottom, one side, and top) so that this side opens and closes, with one of the edges as the hinge.
4. Tape saran wrap on the inside of the cardboard box to cover the window holes.
Tape saran wrap onto the farthest most edge. Pull one side of the saran wrap tight and tape to box. Repeat with remaining sides. If there are loose areas, gently pull up tape and re-adhere to box.
------
Build it! Pt. 2



1. Cut connector ends off of the computer fan and the power adapter and strip wires.
For the computer fan, strip the red and black wires.
For the power adapter, separate the two wires and strip.
2. Connect the red computer fan wire to one lead of the power strip, and the black computer fan wire to the other power strip lead. Check that the fan turns on when the power adapter is plugged in.
3. If fan turns on, unplug the power adapter and solder the connections together. Coat in hot glue.
------
Build it! Pt. 3



1. Glue computer fan on the inside of the cardboard box, in the center of the side with the door.
2. Cut a hole in the center of the cardboard box door to expose the computer fan.
3. Cut a top vent in the cardboard box door.
Recommended to cut small slits, or cover larger vents with skewers or toothpicks.
4. Adhere the cardboard box door to the side.
I found strips of velcro lying around. Other ways to adhere are rubber bands, string, or just hot glue the sides together.
------
Test & Impress!

Add a few dollar bills to check that there is good airflow. If it works as expected, use at parties as a fun way to collect money for food + drink, or incorporate into your next fundraiser to collect donations!
If you notice that the dollar bills are getting stuck in the corners, add some angled cardboard rectangles or increase the size of the vent.

Monday, March 30, 2015

DIY Portable Amplifier




As an avid climber, a full day, or weekend, break from my cellphone and e-mail is one of the best perks. Still, like nearly any activity, good music makes it more enjoyable. This desire to merge two of my passions, climbing and music, resulted in this small, yet robust, portable amplifier.

Here's a video of the final system in action.












Materials


  •  3 Capacitors:
C1 = C3 = 220 microF
C2 = 10 microF
  • 4 Resistors: 
R1 = 10 Ohm
R2 = 1 kOhm
R3 = 10 Ohm 
R4 = variable 10 kOhm (w/ an SPST or SPDT switch)
  • 9 V battery + battery clip
  • Speaker (4 - 8 Ohm)*
*Have a broken sound system? Harvest the speaker! Look for one that can fit in the palm of your hand.
  • 22 Gauge stranded wire ( ~ 1 ft)
  •  Case**
**There are many options! My case is a mini-lunchbox type thing found at a thrift store for $1. Look around the house, or peruse a thrift store for vintage tins, cigar boxes or metal piggy banks. Look for containers made from metal, wood, or rigid plastics.
  • Recommended: Piece of wood for speaker mount
This improves audio output by providing a solid resonant surface. This step was somewhat involved (tool heavy), can be accomplished in many different ways, and is entirely optional, so I will just briefly cover this part of the process.


Tools


  • Soldering iron, solder + solder sucker. 
  • Epoxy or other bonding agent. 
  • Wire strippers 
  • Highly recommended: Multimeter. 
  • For the case, you also need a drill.
  • To mount the speaker in wood: a dremel or other wood cutting tool (+ clamps + eye protection)

Build it! Pt. 1


1. Solder wire leads to the audio input.


2. Solder wire leads to the speaker (recommended to use at least 6" leads).











3. Solder wire leads onto the variable resistor (potentiometer) pins.
Check the resistance between the difference potentiometer pins w/ a multimeter before soldering to determine the functionality of each pin.
Aside: Wire color, while entirely optional, helps distinguish the different pin functions. To wire the potentiometer so that the volume increases from the "off" position: Pin 1 goes to ground, pin 2 goes to LM386 pin 3, and pin 3 goes to the positive audio input.



4. Solder wire leads to the switch pins on the bottom of the potentiometer.
Check resistance across switch pins w/ multimeter. This potentiometer switch has a front pin that goes to ground and the left front pin to positive input.






Build it! Pt. 2



If schematic makes sense, skip next two steps (still suggested to build on breadboard first).

1. Build circuit on a breadboard. Recommended to draw out how it will transfer to the PCB board pads.
Connect a battery and check audio input w/ either a multimeter or an actual source (will need a 1/4" or 1/8" headphone cable).

2. Solder LM386 amplifier to PCB board.











3. Solder the battery clip leads and potentiometer switch leads to the PCB board.
The potentiometer leads are inserted between the positive battery lead and the 10 ohm resistor going to the LM386 voltage source pin.
Note: If your variable resistor does not have a switch, it is highly recommended to include a power switch for the battery.


Build it! Pt. 3

1. Solder the remaining components to the PCB board using the PCB pads and/or wires (breadboard pictured for clarity).


A. Connect LM386 pins 2 and 4 to ground.

B. Connect 1kOhm resistor and 10 microF capacitor from LM386 pin 1 to pin 8 (shorter leg (negative pin) of the capacitor connects to LM386 pin 8).











 C. Connect the long leg (positive pin) of the 220 microF capacitor to LM386 pin 5. Connect one side of the 10 ohm resistor to the short leg of the capacitor, and the other side of the resistor to ground. Connect the positive speaker lead to the short leg of the capacitor, and the negative lead to ground.




D. Connect one side of 10 ohm resistor to LM386 pin 6 and other side to positive battery (w/ switch). Connect long leg of 220 microF capacitor to LM386 pin 6 and short leg to ground.












E. Connect the potentiometer pin 1 to ground, pin 2 to LM386 pin 3, and pin 3 to positive audio input.

F. Connect negative audio input to ground.





Case it! Pt. 1


1. Plan + mark location of each of the components.
The potentiometer, or volume knob, power switch, audio input, and speaker are installed on the exterior. The PCB board and battery are on the interior.










2. Drill two holes in case exterior for the potentiometer knob and for the audio input.
If case is not metal, recommended to include a sheet of metal to provide a better ground for the audio input.

3. Remove screw heads from potentiometer knob and audio input. Push potentiometer knob and audio input through the holes and reattach screw heads.

4. Check connections w/ a multimeter and/or an actual audio input (will need a 1/4" or 1/8" audio cable).







5. Dab epoxy onto bottom of case to adhere the PCB board.
To secure the 9V battery: use velcro strips, or make a quick fabric pouch and epoxy the pouch to the case.







Case it! Pt. 2
1. Mount the speaker in wood or in case exterior.
There are different options depending on your chosen case and available tools. It sounds great w/ wood, otherwise you can also mount it directly to the exterior of the case.

One option is to follow the design featured in this tutorial:

A. Cut a piece of wood to fit inside the case (bottom slab). Cut a hole in the middle large enough to pull the speaker through.
 



B. Take a thin piece of wood and cut a notch out. The notch should be sized so that the screw holes of the speaker frame sit on top of the wood.










 C. Optional: Screw speaker into top wood ring. Screw top ring into bottom slab.













Test & Go Adventuring!





Test the electrical connections by plugging in an audio source (e.g. electric guitar or mp3 player). Check that the potentiometer effectively controls the volume and that the switch turns the system on/off. Once everything is working, coat electrical connections in epoxy.

Take your new portable amplifier to bring music to your next outdoor adventure!
Creative Commons License
This work by Jennifer Fox is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License