Engineers Garage

  • Projects and Tutorials
    • Electronic Projects
      • 8051
      • Arduino
      • ARM
      • AVR
      • PIC
      • Raspberry pi
      • STM32
    • Tutorials
    • Circuit Design
    • Project Videos
    • Components
  • Articles
    • Tech Articles
    • Insight
    • Invention Stories
    • How to
    • What Is
  • News
    • Electronic Products News
    • DIY Reviews
    • Guest Post
  • Forums
    • EDABoard.com
    • Electro-Tech-Online
    • EG Forum Archive
  • Digi-Key Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • EE Resources
    • DesignFast
    • LEAP Awards
    • Oscilloscope Product Finder
    • White Papers
    • Webinars
  • EE Learning Center
  • Women in Engineering

How to Add Sound to Raspberry Pi Game Pad- (Part 37/38)

By Ajish Alfred

The Raspberry pi is a single board computer that is designed with an aim of providing help in the computer education to students at school. This is a nice platform to try different programming techniques and learn software tools also. The board is provided with a RCA connector which can be used to connect it directly to a TV screen which is based on PAL and NTSC standard. The board also has a HDMI connector output which can be used to connect the board to a HD TV. One can also use remote login to access the Raspberry pi and view the GUI (Text User Interface) on the PC screen. The Raspberry pi board is also very easy to interface with external devices or circuits through its pin outs. This board is also provided with an audio socket also. All these makes the Raspberry pi a suitable platform for playing and developing interesting games.

This article focuses on how to add sound to a game that is coded to be played with a Raspberry pi gamepad. This simple graphical game is coded using the HTML5 and JavaScript language and interfaced it with a simple game pad hardware connected to the pins of Raspberry pi board. This game is added with a background music and keypad tones which can be enjoyed while playing with the help of a headset or sound system.
[[wysiwyg_imageupload:11067:]]

 


 

The gamepad is a simple circuit built on a breadboard with few buttons, LEDs and a resistor array. There are eight general purpose IO pins on the 13*2 pin connectors of the Raspberrypi board and among them four pins has been selected as input and then remaining four pins as output. The input pins are connected to push button and are pulled down using 1K resistors. The output pins are connected to the LEDs through another set of 1K resistors. For this project the Raspberry pi board is loaded with Ubuntu and is remotely accessed using VNC. To access the pins that coming out of the Broadcom controller the C library “bcm2835” has been downloaded and installed.

This game uses two MP3 files bgm.mp3 and kpm.mp3, one for the background music and the other for playing the keypad tone. There are so many command line media player software available that can be downloaded and installed on the Ubuntu of the Raspberry pi. The MPG321 is one such command line audio player that can be installed easily using the following command;
sudo apt-get -y install mpg321

 

This MPG321 is an MP3 player application and any MP3 file can be played with the help of this application by typing the following command format
mpg321 <filename.mp3>

 

This software has lot of options like volume control, file seeking, remote mode of operation which makes it perfect to use with any kind of front end applications, here the HTML game as an example. More details about the options can be found at the man pages;
man mpg321
The game runs in a browser window and it communicates with the game pad through a Named Pipe or FIFO. There are multiple processes running which can read from the game pad and write the required commands to the FIFO for controlling the game. The JavaScript written in the game code simply reads from the FIFO for the input control data.
The NAMED PIPE or FIFO needs to be created using the following command in the same directory where the executable for the gamepad code written in C and the HTML game files written in HTML5 and JavaScript exist.

mkfifo xyz

The FIFO can also be created using the code itself by adding the function mkfifo(), open() as explained in a previous documentation.
The parent process in the C code creates five child processes, among which four of them are dedicated for each of the keys in the gamepad and the fifth one is for playing the background music. Whenever a key is pressed or released, they will generate a signal and send it to the parent process with a value. This method of sending a signal with value  helps the parent process to identify which key has been pressed or released. 
Block Diagram of Raspberry pi gamepad
Fig. 2: Block Diagram Of Raspberry Pi Gamepad
As soon as the parent process detects a key press from the value it received along with the signal, it simply writes a particular character corresponding to that key to the NAMED PIPE or FIFO. The HTML game will be reading continuously from the FIFO every 500ms to get the input. Hence both the C code and the HTML file needs to be in the state of execution. The gamepad hardware communicates with the C code only and the C code communicates with the JavaScript using the NAMED PIPE.
The background music will keep on playing as a separate child process from the time the game starts and the keypad tones will be played by the Parent function whenever it detects a signal from the child processes which are dedicated to read the gamepad keys.  The Parent process uses the “system()” function to play the keypad tone as shown in the following statement.
system ( “mpg321 kpm.mp3” );

Circuit Diagrams

Circuit-Diagram-Add-Sound-Raspberry-Pi-Gamepad

Project Video


Filed Under: Raspberry pi
Tagged With: game pad, Raspberry Pi, sound
 

Questions related to this article?
👉Ask and discuss on Electro-Tech-Online.com and EDAboard.com forums.



Tell Us What You Think!! Cancel reply

You must be logged in to post a comment.

HAVE A QUESTION?

Have a technical question about an article or other engineering questions? Check out our engineering forums EDABoard.com and Electro-Tech-Online.com where you can get those questions asked and answered by your peers!


Featured Tutorials

  • Designing Gate Driver Circuit and Switching Mechanism for Modified Sine Wave Inverter – (Part 9/17)
  • Completing Modified Sine Wave Inverter Design with Full Bridge Circuit and Step Up Transformer – (Part 10/17)
  • Designing an Offline UPS – Part (12 /17)
  • How to reduce Switching Time of a Relay – (Part 15/17)
  • Testing MOSFET – (Part 16/17)
  • Driving High Side MOSFET using Bootstrap Circuitry – (Part 17/17)

Stay Up To Date

Newsletter Signup

Sign up and receive our weekly newsletter for latest Tech articles, Electronics Projects, Tutorial series and other insightful tech content.

EE Training Center Classrooms

EE Classrooms

Recent Articles

  • Renesas to demonstrate first working silicon based on the Arm Cortex-M85 processor
  • STMicroelectronics releases first automotive IMU with embedded machine learning
  • Infineon offers DC-DC controller for full LED headlamps without a microcontroller
  • Vishay launches new high-precision, thin-film wraparound chip resistor
  • STMicroelectronics’ common-mode filters ensure signal integrity in serial interfaces

Most Popular

5G 555 timer circuit 8051 ai Arduino atmega16 automotive avr dc motor display Electronic Part Electronic Parts Fujitsu ic infineontechnologies integratedcircuit Intel IoT ir lcd ldr led maximintegratedproducts microchip microchiptechnology Microchip Technology microcontroller microcontrollers mosfet motor powermanagement Raspberry Pi remote renesaselectronics renesaselectronicscorporation Research samsung semiconductor sensor software STMicroelectronics switch Technology vishayintertechnology wireless

RSS EDABOARD.com Discussions

  • Capacitive Switches
  • CSI-2 FIFO or Line buffer?
  • Temperature sensor readout
  • What is the function of the long stub?
  • Circuit Performance Exploration

RSS Electro-Tech-Online.com Discussions

  • Opamp ciruit
  • Audio equalizer
  • Background of Members Here
  • software PWM
  • SPI Questions
Engineers Garage
  • Analog IC TIps
  • Connector Tips
  • DesignFast
  • EDABoard Forums
  • EE World Online
  • Electro-Tech-Online Forums
  • Microcontroller Tips
  • Power Electronic Tips
  • Sensor Tips
  • Test and Measurement Tips
  • 5G Technology World
  • About Us
  • Contact Us
  • Advertise

Copyright © 2022 WTWH Media LLC. All Rights Reserved. The material on this site may not be reproduced, distributed, transmitted, cached or otherwise used, except with the prior written permission of WTWH Media
Privacy Policy | Advertising | About Us

Search Engineers Garage

  • Projects and Tutorials
    • Electronic Projects
      • 8051
      • Arduino
      • ARM
      • AVR
      • PIC
      • Raspberry pi
      • STM32
    • Tutorials
    • Circuit Design
    • Project Videos
    • Components
  • Articles
    • Tech Articles
    • Insight
    • Invention Stories
    • How to
    • What Is
  • News
    • Electronic Products News
    • DIY Reviews
    • Guest Post
  • Forums
    • EDABoard.com
    • Electro-Tech-Online
    • EG Forum Archive
  • Digi-Key Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • EE Resources
    • DesignFast
    • LEAP Awards
    • Oscilloscope Product Finder
    • White Papers
    • Webinars
  • EE Learning Center
  • Women in Engineering