Motion triggered music player using Raspberry Pi

Project by
Mepits

Home automation is an area many are interested in. There are several home automation solutions available in market but we have a habbit of doing it ourselves. In this tutorial we shall see how we could make Raspberry Pi start a music player when we enter the room. To make this possible we need to make the Pi interact with the environment using sensors and control the music player.

Raspberry Pi

Raspberry Pi is an ARM-based computer that is widely used in DIY and engineering projects. It is a single board computer that has its  processing, memory and I/O units integrated in a single circuit board. If you are unfamiliar with Raspberry Pi you may go through the tutorial below first and then come back.

https://www.mepits.com/project/183/Latest-Project-Articles/What-is-Raspberry-Pi-?-Raspberry-Pi-Tutorial-Part-1

IR Break Beam Sensor

IR break beam sensor is an infrared sensor that detects the entry of a person into a room when he breaks a beam of infrared rays emitted by its transmitter. The transmitter and the receiver are to be placed facing each other at the desired location. When powered on the transmitter emits a beam of IR rays which is received by the receiver. As soon as someone enters the room the beam is broken and the entry is detected.

Materials Required

The project requires the following materials :-

  • Raspberry Pi
  • IR Break Beam Sensor
  • LED
  • 10 kΩ, 1 kΩ resistors
  • Speakers with standard headphone jack
  • Wires
  • PCB Board and Connectors

Steps to create a motion triggered music player using RPi

Step 1 : - Testing music player

To play an audio file we need a music player. If you have successfully installed Raspbian as given in the tutorial you need not install any new software. Raspbian includes a package called omxplayer by default which is a command line music player built specifically for Raspberry Pi. If you are using a different distro install omxplayer or find a similar package from its repository.

To start testing connect your speakers to the audio jack and load a music file to your Pi's SD card. Open terminal and change the directory to the one containing the music file.

Command :- cd /path/of/folder

Now, invoke omxplayer to play the file.

Command :- omxplayer filename.extension

Step 2 :- Interfacing the break beam sensor

You need to interface the break beam sensor to the Raspberry Pi to detect your entry into the room. For this purpose we shall make use of the GPIO pins of the Pi. It is a good practice to test the circuit on breadboard and then build it on the PCB. The circuit diagram is given below. In the circuit diagram you can see that VCC and GND pins of IR transmitter and receiver are connected to the respective pins of the Raspberry Pi. If you are new into building circuits make sure you do not make any mistakes here. Connecting them wrongly can damage your devices. The third pin of the receiver is the output pin connected to a GPIO pin of Raspberry Pi.

Step 3 :- Writing the code

The code to play music when your entry is detected is given below. It is recommended that you run it on your PC first so that errors in spacing can be avoided.

#DIY Motion Triggered Music Playerimport 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=subpr

ocess.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: sel

f.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:while True:

pass

except KeyboardInterrupt:

GPIO.cleanup()

if __name__ == '__main__':

main()

Installation

Once you are done coding test if the setup is performing as needed. Find out the practical separation possible between the transmitter and receiver. The receiver must not be kept so far from the transmitter that the beams die out before reaching the receiver. Now you have to place them accordingly near the door. Fix them and the Pi properly. You are ready to try out the motion triggered music player.

Modifications

Some people may find that they are not able to maintain as much separation between the receiver and transmitter as they want. If you encounter this problem you can look for alternative ways and make modifications in your project. One alternative is to use the modules for obstacle sensors. They have both the receiver and transmitter on the same chip and uses the inverse of the logic used in the break beam sensor. Here, your entry causes the transmitted IR beam to reflect back and fall on the receiver. It is not the absence but the presence of IR beams that is detected here.

The circuit has to be modified accordingly. Since we are using a module the connections between VCC and GND of receiver and transmitter is not required any more. Now we have to only connect the VCC and GND of the module to the corresponding pins of the Pi and connect the output pin of the module to one of the GPIO pins of  the Raspberry Pi. The modified circuit diagram is given below.

 

User Review (0)
Related Items