Playing sounds and using buttons with Raspberry Pi



Similar documents
Adafruit's Raspberry Pi Lesson 9. Controlling a DC Motor

Adafruit's Raspberry Pi Lesson 11. DS18B20 Temperature Sensing

Drive a 16x2 LCD with the Raspberry Pi

Adding a Real Time Clock to Raspberry Pi

Arduino Lesson 13. DC Motors. Created by Simon Monk

Adafruit's Raspberry Pi Lesson 5. Using a Console Cable

Adafruit's Raspberry Pi Lesson 6. Using SSH

SSH to BeagleBone Black over USB

PiFace Control & Display

Adafruit's Raspberry Pi Lesson 7. Remote Control with VNC

Raspberry Pi radio player with touchscreen

Adafruit's Raspberry Pi Lesson 5. Using a Console Cable

Arduino Lesson 9. Sensing Light

Monitor Your Home With the Raspberry Pi B+

Adafruit NFC/RFID on Raspberry Pi

Setting up IO Python Library on BeagleBone Black

Arduino Lesson 5. The Serial Monitor

Adafruit's Raspberry Pi Lesson 1. Preparing an SD Card for your Raspberry Pi

Arduino Lesson 14. Servo Motors

Arduino Lesson 4. Eight LEDs and a Shift Register

5inch HDMI LCD User Manual

Adafruit's Raspberry Pi Lesson 5. Using a Console Cable. Created by Simon Monk

Arduino Lesson 17. Sending Movement Detector

7inch HDMI LCD (B) User Manual

PiFace Real Time Clock user guide

WebIOPi. Installation Walk-through Macros

Raspberry Pi Kernel-o-Matic

Setting up a Raspberry Pi as a WiFi access point

Raspberry Pi Adding a Real Time Clock (RTC)

Set up and Blink - Simulink with Arduino

Install bluez on the Raspberry Pi

Adafruit's Raspberry Pi Lesson 3. Network Setup

Arduino Lesson 0. Getting Started

Matrix and 7-Segment LED Backpack with the Raspberry Pi

7/Basic Input and Output

Adafruit MCP9808 Precision I2C Temperature Sensor Guide

How To Connect A Raspberry Pi To The Internet On A Microsoft Moonstone 2 (Microsoft) Microsoft Powerbook 2 (Powerbook 2) (Powerboard 2) And Powerbook 3 (Powerstation 2)

Adafruit NFC/RFID on Raspberry Pi. Created by Kevin Townsend

SETTING UP RASPBERRY PI FOR TOPPY FTP ACCESS. (Draft 5)

Using an IR Remote with a Raspberry Pi Media Center

Recording audio from your computer.

AR1100 Resistive Touch Screen Controller Guide

Adafruit Proto Shield for Arduino

secucam User Manual Version 1.1.x Darhon Software

Case Closed Installation and Setup

In-System Programmer USER MANUAL RN-ISP-UM RN-WIFLYCR-UM

COBRA. Audio Box Detailed User Guide

Linux Development Environment Description Based on VirtualBox Structure

Strato Pi Hardware Guide

BeagleBone Black: Installing Operating Systems

DS1307 Real Time Clock Breakout Board Kit

Cloud Cam: Internet-Connected Security Camera

Preparing Raspberry Pi

Intro to Intel Galileo - IoT Apps GERARDO CARMONA

2.2" TFT Display. Created by Ladyada. Last updated on :15:09 PM EDT

Arduino Lesson 1. Blink

Introduction. Created by Richard Bell 10/29/2014

Computer Audio Design 1543 DAC Windows Recommended Playback Software Installation

mypro Installation and Handling Manual Version: 7

Creating a DUO MFA Service in AWS

Bluefruit LE Python Library

2. Boot using the Debian Net Install cd and when prompted to continue type "linux26", this will load the 2.6 kernel

USER GUIDE. Diagnostic Web Server FW ver BrightSign, LLC Lark Ave., Suite B Los Gatos, CA

Beginner s Guide to the PI MATRIX. by Bruce E. Hall, W8BH 1) INTRODUCTION

Accessing I2C devices with Digi Embedded Linux 5.2 example on Digi Connect ME 9210

TSL2561 Luminosity Sensor

Embedded Based Web Server for CMS and Automation System

MP3-Digital Audio Player. MP55BK User Manual

PART 1. Using USB Mixer with a Computer

Character LCDs. Created by Ladyada. Last updated on :45:29 PM EDT

The Coast to Coast AM Podcast for itunes 11

Setting up WiFi with BeagleBone Black

SSH and Basic Commands

Raspberry Pi Android Projects. Raspberry Pi Android Projects. Gökhan Kurt. Create exciting projects by connecting Raspberry Pi to your Android phone

Raspberry Pi Setup Tutorial

Bob Rathbone Computer Consultancy

Using Red Hat Enterprise Linux with Georgia Tech's RHN Satellite Server Installing Red Hat Enterprise Linux

UPS PIco. to be used with. Raspberry Pi B+, A+, B, and A. HAT Compliant. Raspberry Pi is a trademark of the Raspberry Pi Foundation

Chapter 1: Getting Started

CPSC 226 Lab Nine Fall 2015

Adafruit SHT31-D Temperature & Humidity Sensor Breakout

How to Connect a Computer to Your TV Set Overview Many people already know that you can connect a computer to a plasma TV, but it is not as well

The Rush 24/7 Podcast for itunes 11

Wireless Security Camera with the Arduino Yun

Standalone Attendance Monitoring and Projector System

Bob Rathbone Computer Consultancy

A REST API for Arduino & the CC3000 WiFi Chip

How to install SVXLink on Raspberry Pi and other Linux based Single Board Computers (SBC)

Creating an ESS instance on the Amazon Cloud

How to read Temperature and Humidity from Am2302 sensor using Thingworx Edge java SKD for Raspberry Pi

How to use the VMware Workstation / Player to create an ISaGRAF (Ver. 3.55) development environment?

Building a Basic Communication Network using XBee DigiMesh. Keywords: XBee, Networking, Zigbee, Digimesh, Mesh, Python, Smart Home

Copyright 2012 by Fen Logic Ltd. All rights reserved.

Raspberry Pi Webserver

SAS Installation via the Client-Server Image (CAHNRS Site License)

Universal Mobile Print Server On the Cheap, and Cloud-free. What You Will Need. Configuring your Pi as a Print Server

Guide for Updating Firmware and Troubleshooting Connection Issues

RN-52 Bluetooth Hookup Guide

Using pcduino's WiFi Dongle With the Pi a

Honeywell Internet Connection Module

Transcription:

Playing sounds and using buttons with Raspberry Pi Created by Mikey Sklar Last updated on 2015-04-15 01:30:08 PM EDT

Guide Contents Guide Contents Overview Install Audio Install Python Module RPi.GPIO Bread Board Setup for Input Buttons Code Fancier Code: A Very Simple Jukebox 2 3 4 6 7 10 12 Page 2 of 14

Overview One of the great things about the Raspberry Pi is how everyone starts with the same piece of gear. Since the audio hardware is identical on every unit, it is trivial to load the drivers and play mp3 files. This guide describes how to connect input buttons and play audio files using a Raspberry Pi running Raspbian. We make use of Adafruit's Pi Cobbler and the Python module RPi.GPIO. If you have not already used the Raspberry Pi as a input device this guide will show you how to wire the pull-down resistors, GPIO pins, and buttons. Page 3 of 14

Install Audio With the Pi connected to the Internet and SSHed in (see our previous tutorial (http://adafru.it/aj5)), install the Alsa audio drivers and the mpg123 MP3 Player: sudo apt-get update sudo apt-get install alsa-utils mpg123 You'll probably be prompted to continue. Press y for yes. Page 4 of 14

Next, reboot the Pi by typing sudo reboot. If you're using SSH, you'll have to reconnect after it boots. Once you're logged back in, load the sound drivers and do some setup for the 3.5mm jack output: sudo modprobe snd_bcm2835 sudo amixer cset numid=3 1 Page 5 of 14

Install Python Module RPi.GPIO The RPi.GPIO python module offers easy access to the general purpose IO pins on the Raspberry Pi. To get the latest version of this, you can take a little diversion to follow the instructions in Adafruit's Raspberry Pi Lesson 4. GPIO Setup (http://adafru.it/exn). If you're comfortable, you can probably skip the lesson above and issue the following command in a terminal window: sudo apt-get install python-dev python-rpi.gpio Page 6 of 14

Bread Board Setup for Input Buttons Important things to note: The Adafruit Pi Cobbler (http://adafru.it/914) is being used; for the A+/B+/Pi 2, the Pi Cobbler Plus (http://adafru.it/2029) should be used instead The Adafruit Clear Full sized breadboard (http://adafru.it/239) is being used (3) 10k pull-up resistors (3) Momentary push-button switches (http://adafru.it/367) GPIO pins 23, 24, and 25 Cobbler 3.3v rail is tied to positive breadboard Cobbler gnd rail is tied to negative breadboard Each button connection looks like: 3.3v --> 10k Pull-up Resistor --> GPIO --> Button --> GND Page 7 of 14

Here's a Fritzing (http://adafru.it/exu) sketch of the layout for the Cobbler Plus for a Pi 1 Model B+ or Pi 2 (click for a bigger image): If you've got Fritzing installed, you can download the sketch here: Page 8 of 14

Download Sketch http://adafru.it/exv Page 9 of 14

Code Now for a bit of Python: Use a text editor like Nano to paste this code into a file named raspi-audio-button.py Download or copy several mp3 files to the Pi and place them into the same directory as the raspi-audio-button.py script Change the filenames in the code to match the files you've just copied Make the file executable with chmod nano raspi-audio-button.py #!/usr/bin/env python import os from time import sleep import RPi.GPIO as GPIO GPIO.setmode(GPIO.BCM) GPIO.setup(23, GPIO.IN) GPIO.setup(24, GPIO.IN) GPIO.setup(25, GPIO.IN) while True: if (GPIO.input(23) == False): os.system('mpg123 -q binary-language-moisture-evaporators.mp3 &') if (GPIO.input(24) == False): os.system('mpg123 -q power-converters.mp3 &') if (GPIO.input(25)== False): os.system('mpg123 -q vader.mp3 &') sleep(0.1); $ chmod +x raspi-audio-button.py Make sure you have speakers or headphones hooked up to the 3.5mm jack, and run the Python program as an administrator using sudo : Page 10 of 14

$ sudo python raspi-audio-button.py Now you should be able to trigger each mp3 by hitting the corresponding button. A handful of things worth noticing here: os.system('command') will run command just like if you typed it at the prompt. The & tells the shell to run the command in the background. This way you can actually play more than one file at once. The -q option to mpg123 suppresses diagnostic messages. You can remove it if you'd like to see song titles. The sleep(0.1) call is necessary to avoid spawning tons of mpg123 calls from a single button press. Page 11 of 14

Fancier Code: A Very Simple Jukebox Our first Python script uses every button to do pretty much the same thing: Each runs a command to play a different mp3. What if you want to use the 3-button scheme to control lots of mp3s? Try the following script: Page 12 of 14

#!/usr/bin/env python from os import listdir import subprocess from time import sleep import RPi.GPIO as GPIO GPIO.setmode(GPIO.BCM) GPIO.setup(23, GPIO.IN) GPIO.setup(24, GPIO.IN) GPIO.setup(25, GPIO.IN) mp3_files = [ f for f in listdir('.') if f[-4:] == '.mp3' ] if not (len(mp3_files) > 0): print "No mp3 files found!" print '--- Available mp3 files ---' print mp3_files print '--- Press button #1 to select mp3, button #2 to play current. ---' index = 0 while True: if (GPIO.input(23) == False): index += 1 if index >= len(mp3_files): index = 0 print "--- " + mp3_files[index] + " ---" if (GPIO.input(24) == False): subprocess.popen(['mpg123', mp3_files[index]]) print '--- Playing ' + mp3_files[index] + ' ---' print '--- Press button #3 to clear playing mp3s. ---' sleep(1) if (GPIO.input(25) == False): subprocess.call(['killall', 'mpg123']) print '--- Cleared all existing mp3s. ---' sleep(0.1); Here, we get a list of mp3 files in the current directory, and then set up the following control scheme: Page 13 of 14

button #1 (GPIO 23) clicks through the list of available mp3s button #2 (GPIO 24) plays the currently selected mp3 button #3 (GPIO 25) runs a command that kills any existing mp3 player processes, stopping whatever mp3s are playing right now Last Updated: 2015-04-15 01:30:09 PM EDT Page 14 of 14