Home IoT & Hardware Arduino Electronics Blueprints

Arduino Electronics Blueprints

books-svg-icon Book
eBook $39.99 $27.98
Print $48.99
Subscription $15.99 $10 p/m for three months
$10 p/m for first 3 months. $15.99 p/m after that. Cancel Anytime!
What do you get with a Packt Subscription?
This book & 7000+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with a Packt Subscription?
This book & 6500+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with eBook + Subscription?
Download this book in EPUB and PDF formats, plus a monthly download credit
This book & 6500+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with a Packt Subscription?
This book & 6500+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with eBook?
Download this book in EPUB and PDF formats
Access this title in our online reader
DRM FREE - Read whenever, wherever and however you want
Online reader with customised display settings for better reading experience
What do you get with video?
Download this video in MP4 format
Access this title in our online reader
DRM FREE - Watch whenever, wherever and however you want
Online reader with customised display settings for better learning experience
What do you get with video?
Stream this video
Access this title in our online reader
DRM FREE - Watch whenever, wherever and however you want
Online reader with customised display settings for better learning experience
What do you get with Audiobook?
Download a zip folder consisting of audio files (in MP3 Format) along with supplementary PDF
What do you get with Exam Trainer?
Flashcards, Mock exams, Exam Tips, Practice Questions
Access these resources with our interactive certification platform
Mobile compatible-Practice whenever, wherever, however you want
BUY NOW $10 p/m for first 3 months. $15.99 p/m after that. Cancel Anytime!
eBook $39.99 $27.98
Print $48.99
Subscription $15.99 $10 p/m for three months
What do you get with a Packt Subscription?
This book & 7000+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with a Packt Subscription?
This book & 6500+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with eBook + Subscription?
Download this book in EPUB and PDF formats, plus a monthly download credit
This book & 6500+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with a Packt Subscription?
This book & 6500+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with eBook?
Download this book in EPUB and PDF formats
Access this title in our online reader
DRM FREE - Read whenever, wherever and however you want
Online reader with customised display settings for better reading experience
What do you get with video?
Download this video in MP4 format
Access this title in our online reader
DRM FREE - Watch whenever, wherever and however you want
Online reader with customised display settings for better learning experience
What do you get with video?
Stream this video
Access this title in our online reader
DRM FREE - Watch whenever, wherever and however you want
Online reader with customised display settings for better learning experience
What do you get with Audiobook?
Download a zip folder consisting of audio files (in MP3 Format) along with supplementary PDF
What do you get with Exam Trainer?
Flashcards, Mock exams, Exam Tips, Practice Questions
Access these resources with our interactive certification platform
Mobile compatible-Practice whenever, wherever, however you want
  1. Free Chapter
    A Sound Effects Machine
About this book
Publication date:
July 2015
Publisher
Packt
Pages
252
ISBN
9781784393601

 

Chapter 1. A Sound Effects Machine

Arduino is a wonderful rapid prototyping platform capable of making a variety of electronic tools, gadgets, and instruments. The fascination with Arduino has grown to the point where makers are creating devices to educate, entertain, and provide new creative tools for individuals interested in science or technology. Some of the electronic devices that can be built with Arduino include DC motor controllers, musical instruments, robots, and smart toys.

In addition to the devices listed, Arduino can be used to create unique sounds as well. There are several programming techniques used to create sounds using Arduino. For example, the Arduino tone library can be used to create a variety of unusual sounds by varying the pitch, frequency, and duration of a pulse width modulated signal. Another approach is to use the recorded sound WAV files stored on an SD card. Arduino can select these files using an SD card software library, thereby allowing the recorded sounds to be heard through a small signal transistor amplifier. In this chapter, we will explore how to build a sound effects machine capable of playing a variety of sounds recorded on an SD card. Also, a discussion on small signal transistor amplifiers and Serial Peripheral Interface (SPI) communication will be reviewed with the project. A modification project allowing the sounds to be played randomly will be discussed in this chapter as well.

 

Parts list


The following is the list of parts required for a sound effects machine:

  • Arduino Uno (one unit)

  • 2N3904 NPN transistor (one unit)

  • 8 ohm speaker (one unit)

  • 470 ohm resistor (yellow, violet, brown, and gold) (one unit)

  • 4.7 kilo ohm resistors (yellow, violet, red, and gold) (four units)

  • Tactile pushbutton or toggle switches (four units)

  • SD module (DFRobot [PN:DFR0071]) or Arduino compatible (one unit)

  • Breadboard

  • Wires

 

A sound effects machine block diagram


Building a sound effects machine is relatively easy as this device only requires four electronic subcircuits, as shown in the following block diagram:

The blocks in the diagram represent the electronic subcircuits.The arrows are the electrical wires connecting to the target electronic subcircuit. The numbers shown with the diagonal lines attached to the arrows tell the number of wires connected to each electronic subcircuit.

The sound effects machine block diagram is an engineering tool used to convey a complete product design using simple graphics. The block diagram also makes it easier to plan the breadboard for prototyping and testing of the sound effects machine in a workshop or laboratory bench. One final observation of the sound effects machine block diagram is that the basic computer convention of inputs is on the left, the processor is located in the middle, and the outputs are placed on the right-hand side of the design layout. As shown, the tactile switches are on the left-hand side, the Arduino is located in the middle, and the small signal transistor amplifier with 8 ohm speaker is on the right-hand side of the block diagram. This left to right design method makes it easier to build the sound effects machine and troubleshoot the errors during the testing phase of project development.

Building the sound effects machine

The sound effects machine is quite simple in design and construction. There are a variety of ways to build this electronic device, such as on a Printed Circuit Board (PCB) or an experimenter board. The method I found to rapidly build this electronic device is to use a solderless breadboard, as shown in this diagram:

In the preceding wiring diagram, the electrical components to build the sound effects machine circuit are placed on the solderless breadboard for easy attachment to the Arduino, SD card module, and speaker. The transistor shown is a 2N3904 NPN type with pin-out arrangement consisting of emitter, base, and collector, in that order. If the transistor pins are wired incorrectly, the WAV file sounds will not be heard through the 8 ohm speaker. The SD module is a DFRobot component (shown next) but any compatible part can be used in this project. If a compatible SD module component is being used, wire the device to the Arduino using the manufacturer's datasheet to ensure proper circuit operation.

Another wiring tool to be used in building the sound effects machine, either on a solderless breadboard or an experimenter board, is a circuit schematic diagram shown next:

As illustrated, the tactile pushbutton switches are used to select the various sounds stored on the SD card. Along with the tactile switches, there are four 2.2 kilo ohm pull-up resistors wired to them. The pull-up resistors allow a binary 0 to be read by the Arduino for selecting the stored WAV files on the SD card.

There are a total of five sound files stored on the SD card. Four of these sound files are easily activated by pressing a tactile pushbutton switch. The fifth sound file turns on when the Arduino is powered on or manually reset. To play the stored sounds on the SD card, the Arduino digital pins D5-D8 require a binary 0 to select the WAV files. More sounds can be added to the SD card and activated by wiring additional tactile pushbutton switches with 2.2 kilo ohm pull-up resistors.

Introducing SPI communication

The SPI communication is used to connect electronic devices such as computers, scanners, and printers together with four wires. The communication concept behind SPI is based on a master-slave configuration. The communication between the master and slave units are full duplex. In full duplex mode, the signals are transmitted simultaneously between the master and slave devices.

An example of a full duplex communication network is a desktop computer connected to a laser printer. The desktop computer will send a message to the laser printer, alerting it that a document is ready for printing. The laser printer will acknowledge the desktop computer by sending a request to print message. Upon receiving the request to print message, the desktop computer will then send the document to the laser printer for printing.

Inside the master and slave units, there are 8-bit shift registers that send binary data between them. A digital clock is used to provide proper timing to the shift registers while data moves between the two connected devices. The binary data movement used by both the shift registers is a Shift Left-Rotate Right sequence.

The data line names for the SPI 4-wire communication network are:

  • MOSI (Master Out, Slave In)

  • MISO (Master In, Slave Out)

  • SCLK (Serial Clock)

  • SS (Slave Select)

Most slave Integrated Circuit(IC) manufacturers (for example, DFRobot) use different pin names for the SPI 4-wire communication configuration. Some of these pin names are listed here:

  • DI (Digital-In)

  • DO (Digital-Out)

  • SCK (Serial Clock)

  • CS (Chip Select)

Additional slaves can be attached to the master SPI device by wiring them in parallel. The slave select signal will be connected from the master to each of the slaves. The slave IC's output is enabled when a corresponding slave signal is turned on or set. The data output from the slave IC is disconnected from the MISO line when the device is deselected by the slave select line.

The wiring for the Arduino to the SD module uses the techniques just discussed to select the individual files on the card. The circuit schematic diagram provides another way to build the microcontroller to SD module interface for sound WAV file selection:

A circuit schematic showing the electrical wiring connections between the Arduino Uno and SD module is shown here:

Adding digital logic switches for WAV file selection

The sound effects machine can produce a multitude of strange noises stored on an SD card. The eerie sounds can be recorded using a free audio editor and recorder called Audacity. Audacity can be found on the Web at http://soundbible.com/tags-strange.html.

Once the desired files have been recorded and saved on the SD card, accessing them requires using digital logic switches wired to the Arduino, as shown here:

The four tactile pushbutton switches along with the 2.7 kilo ohm resistors are wired as Active Low digital inputs. An Active Low operation is a binary 0 value that exists when a trigger, such as a pushbutton switch, has been initiated. The Arduino digital inputs D5-D8 are wired to the four tactile pushbutton switches for receiving the binary 0 digital data. The Arduino sketch will select the appropriate WAV file based on its pushbutton switch being pressed. The following is a small line of the Arduino code used to read an Active Low trigger from a pushbutton tactile switch:

if (SW1 == LOW) { //if SW1 pressed then play file "6.wav"
    tmrpcm.play("6.wav");
}

The line of code will play the WAV file 6.wav only when the pushbutton tactile switch is pressed, triggering an Active Low input signal to the Arduino.

 

Adding SD and WAV file libraries to your Arduino sketch


To allow the sound effects machine to play WAV files, additional software resources are needed. The two important software resources required for the sound effects machine to work properly are the <SD.h> and <TMRpcm.h> libraries. The SD (San-Disk) library is required for read / write operations from an SD part. The Arduino Integrated Development Environment (IDE) has the SD resource as part of its software library.

With the SD library included in the Arduino sketch, as shown next, the available software resource allows the microcontroller to talk to the actual SD card using a module or breakout board. The pin allowing the Arduino to talk to the SD card module or breakout board is the SS or CS pin:

#include <SD.h>   // need to include the SD library

#define SD_ChipSelectPin 10  //using digital pin 10 on Arduino 328

if (!SD.begin(SD_ChipSelectPin)) {  // see if the card is present and can be initialized:
  return;   // don't do anything more if not
}

Depending on the SD module or breakout board manufacturer, the SS or CS pin assignment on the Arduino may vary. Therefore, consult the manufacturer's datasheet for the appropriate Arduino pin assignment. With the appropriate Arduino pin numbers assigned, another file to include in the Arduino sketch is the TMRpcm library.

The TMRpcm library installation

The TMRpcm is a software library used for playing PCM/WAV files on Arduino. The Arduino SD library is used to communicate with an SD card to read data, such as WAV files. The WAV files can be accessed using the TMRpcm library so that they may be heard through a speaker wired to the Arduino. Perform the following steps to install the TMRpcm library:

  1. To obtain the TMRpcm library to use it in the sound effects machine project, there is a GitHub website—https://github.com/TMRh20/TMRpcm/wiki.

  2. Scrolling down on the web page will provide a link to the Arduino.cc website for the library installation instructions. Also, the TMRpcm zipped file is located above this link and can be downloaded on your desktop, pc, or notebook computer's hard drive.

  3. After the installation has been completed, the TMRpcm file will be included in the Arduino's IDE library.

  4. With the TMRpcm file included in the Arduino IDE library, the following line of code can be added to the sound effects machine sketch:

    #include <SD.h>           // need to include the SD library
    #define SD_ChipSelectPin 10  //using digital pin 4 on Arduino nano 328
    #include <TMRpcm.h>           //  also need to include this library...
    #include <SPI.h>
  5. Also included with the sketch is the <SPI.h> file for connecting the SD module or breakout board to the Arduino. With the TMRpcm library added to the Arduino sketch, the WAV file audio content can be heard using a simple transistor amplifier, as shown in the following image:

  6. The circuit schematic can be used to wire the transistor amplifier to the Arduino, as shown in the next diagram:

    The 2N3904 NPN transistor has sufficient gain or volume potential (hfe 100 typical) to drive an 8 ohm speaker. The hfe is an electrical symbol parameter found on transistor or audio amplifier integrated circuit datasheets. The unitless number refers to the current gain for audio amplifiers and transistors to sufficiently drive an electromechanical load wired to them. For amplifiers, in particular, sometimes it refers to the amplification factor of increasing an input signal applied to the transistor or IC amplifier. If a higher audio volume is desired, an LM386 power amplifier IC can easily be substituted for the simple transistor circuit.

  7. With the <TMRpcm.h> library included in the Arduino sketch, the following lines of code allow the WAV files to be heard through the speaker driven by the transistor amplifier:

    TMRpcm tmrpcm;   // create an object for use in this sketch
      int SW1;
      int SW2;
      int SW3;
      int SW4;
      void setup(){
        pinMode(5,INPUT);  //Define A0 as digital input.
        pinMode(6,INPUT);  //Define A1 as digital input.
        pinMode(7,INPUT);  //Define A2 as digital input.
        pinMode(8,INPUT);  //Define A3 as digital input.
    
        tmrpcm.speakerPin = 9; //11 on Mega, 9 on Uno, Nano, etc
    
        if (!SD.begin(SD_ChipSelectPin)) {  // see if the card is present and can be initialized:
          return;   // don't do anything more if not
        }
        tmrpcm.volume(7);
        tmrpcm.play("1.wav"); //the sound file "1" will play each time the Arduino powers up, or is reset
      }
    
      void loop(){
        SW1=digitalRead(5);
        SW2=digitalRead(6);
        SW3=digitalRead(7);
        SW4=digitalRead(8);
    
        if (SW1 == LOW) { //if SW1 pressed then play file "6.wav"
          tmrpcm.play("6.wav");
        } else if(SW2 == LOW){ //if SW2 pressed then play file "4.wav"
          tmrpcm.play("4.wav");
        } else if(SW3 == LOW){ //if SW3 pressed then play file "5.wav"
          tmrpcm.play("5.wav");
        } else if(SW4 == LOW){  //if SW4 pressed then play file "3.wav"
          tmrpcm.play("3.wav");
        }
    
      }

As you can see, this code follows the traditional coding format for programming an Arduino. After uploading the sketch to the Arduino, 1.wav will be heard through the speaker for a few seconds. This initial tone heard upon resetting the Arduino alerts you that the sound effects machine is ready to use. By pressing each of the pushbutton tactile switches, a unique sound will be heard through speaker. If an automatic approach is desired, a random function can easily be programmed for the sound effects machine.

Adding a random function to play sounds automatically

To allow the sound effects machine to select various sounds automatically, the random function can be used. The description for the random function is given on the Arduino.cc website:

The key concept behind this programming instruction is to set a maximum number for the random function generator. This maximum number will prevent the random function from generating values not exceeding the set maximum limit. For example, random(300) will allow the generation of numbers ranging from 0 to 300 to be produced in a non-sequential matter. With this function, the sound effects machine will select WAV files automatically to play, and they can be heard through the simple transistor amplifier-speaker circuit.

To test this random generator function, type the following sketch onto the text editor of the Arduino IDE:

long randNumber;
void setup(){
  Serial.begin(9600);
}

void loop(){
  randNumber = random(5); // set max random number
  Serial.println(randNumber); //print random number on Serial Monitor
  delay(1000); //wait 1 sec between printing random numbers
}

Attach an Arduino to the desktop pc or notebook computer and upload the sketch to it. Next, open the serial monitor and notice the numbers being displayed on the screen. The numbers generated will range between 0 to 5 on the serial monitor. Modeling this function on the Arduino IDE allows a better understanding of how the concept of selecting sounds from the SD card randomly is feasible. Try out different values for the random function parameter and watch the results on the serial monitor.

The next phase of the sound effects machine modification project will consist of taking sections of the random function and adding them to the original sketch. The technique used in this modification method is called software remixing. This software remixing method allows Proof of Concept-embedded devices to be rapidly developed and tested. Again, our concept is to provide a hands-free sound effects machine for selecting WAV files from an SD card.

Type the following sketch onto the IDE text editor and upload the code to the Arduino:

#include <SD.h>                 // need to include the SD library
#define SD_ChipSelectPin 11b  //using digital pin 4 on Arduino nano 328
#include <TMRpcm.h>           //  also need to include this library...
#include <SPI.h>
long randNumber;

TMRpcm audio;   // create an object for use in this sketch

void setup(){
  audio.speakerPin = 9; //11 on Mega, 9 on Uno, Nano, etc

  if (!SD.begin(SD_ChipSelectPin)) {  // see if the card is present and can be initialized:
    return;   // don't do anything more if not
  }
  audio.volume(7);
  audio.play("1.wav"); //the sound file "1" will play each time the Arduino powers up, or is reset
}

void loop(){
  randNumber = random(5); // set max random number 

  if (randNumber == 5){ // if number is 5 play file ""6.wav"
    audio.play("6.wav");
    delay(5000);

  } else if(randNumber == 4){ // if number is 4 play file "4.wav"
      audio.play("4.wav");
      delay(5000);

  } else if(randNumber == 3){ // if number is 3 play file "5.wav"
    audio.play("5.wav");
    delay(5000);

  } else if(randNumber == 2){ // if number is 2 play file "3.wav"
    audio.play("3.wav");
    delay(5000);

  } else if (randNumber == 0){ // if number is 0 play "1.wav"
    audio.play("1.wav");
    delay(5000);
  } else if (randNumber == 1){ // if number is 1 play "2.wav"
    audio.play("2.wav");
    delay(5000);
  }

}

The WAV files will play randomly, based on the number stored in the randNumber variable. The delay(5000) instruction will allow the selected sound to play for 5 sec. An added feature of this sketch is that new files can be added to the SD card and can also be selected on the basis of the assigned number chosen by the else if statement. If additional sounds are added with the random (n) instruction, they will be adjusted to accommodate the number of new WAV files stored on the SD card. Also, to see which WAV files have been selected, the serial monitor can be modified to include the new lines of code.

In addition, the actual WAV file name can be displayed as the machine plays the SD card audio content using the serial monitor. A few additional lines of code can be included to the sketch shown earlier. The new changes to the Random Function WAV code are given here:

#include <SD.h>                  // need to include the SD library
#define SD_ChipSelectPin 11  //using digital pin 4 on Arduino nano 328
#include <TMRpcm.h>           //  also need to include this library...
#include <SPI.h>
long randNumber;

TMRpcm audio;   // create an object for use in this sketch

void setup(){
  Serial.begin(9600);
  audio.speakerPin = 9; //11 on Mega, 9 on Uno, Nano, etc

  if (!SD.begin(SD_ChipSelectPin)) {  // see if the card is present and can be initialized:
    return;   // don't do anything more if not
  }
  audio.volume(7);
  audio.play("1.wav"); //the sound file "1" will play each time the Arduino powers up, or is reset
}

void loop(){
  randNumber = random(5); // set max random number

  if (randNumber == 5){ // if number is 5 play file "6.wav"
    audio.play("6.wav");
    Serial.println("Playing 6.wav");
    delay(5000);

  } else if(randNumber == 4){ // if number is 4 play file "4.wav"
    audio.play("4.wav");
    Serial.println("Playing 4.wav");
    delay(5000);

  } else if(randNumber == 3){ // if number is 3 play file "5.wav"
    audio.play("5.wav");
    Serial.println("Playing 5.wav");
    delay(5000);

  } else if(randNumber == 2){ // if number is 2 play file "3.wav"
    audio.play("3.wav");
    Serial.println("Playing 3.wav");
    delay(5000);

  } else if (randNumber == 0){ // if number is 0 play "1.wav"
    audio.play("1.wav");
    Serial.println("Playing 1.wav");
    delay(5000);
  } else if (randNumber == 1){ // if number is 1 play "2.wav"
    audio.play("2.wav");
    Serial.println("Playing 2.wav");
    delay(5000);
  }

}

Tip

Downloading the example code

You can download the example code files from your account at http://www.packtpub.com for all the Packt Publishing books you have purchased. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.

Type the new sketch onto the text editor of the Arduino's IDE. Upload the sketch to the USB connected Arduino and open the serial monitor. As sounds are playing through the speaker, the WAV file names will be displayed on the serial monitor:

The serial monitor provides a user friendly visual display to watch the WAV sound files as they are being played. Also, this monitor can serve as a software debugging tool by providing WAV file data as the code is being executed. If a particular file is not providing sound, the serial monitor will validate the absence of the data on the SD card quite easily.

 

Adding an LED bar graph display for selected sound


Besides displaying the names of the sound files on a serial monitor, a lighting indicator can be used for positive visual feedback. The idea behind this new feature is to provide a unique visual effect for the random function. The random patterns generated within the software can be seen using solid state indicators like LEDs. The visual random patterns using LEDs will look best when viewed in a darkroom. The Proof of Concept (POC) that is being presented in this section is more of a design challenge to take the knowledge obtained from the previous hardware and software changes for building a unique and visually appealing device.

To help develop the LED bar graph display POC, a possible breadboard layout is shown in the following figure:

As shown in the breadboard, five single LEDs along with 330 ohm series resistors are used to monitor the associate sound files stored on the SD card. The idea behind the breadboard wiring is to show that digital output pins are needed to operate (drive) the LEDs. The digital pins D3, D4, D5, D7, and D8 are used as output drivers for operating the five LEDs.

A circuit schematic diagram shows a better wiring perspective of the LEDs, series resistors, and their attachment to the Arduino:

Although the wiring diagram shows red LEDs, other colors maybe used for the random function visual effect of the code. Also, the five LEDs can be replaced with an LED bar graph display, such as Sparkfun Electronics' catalog number COM-09937. Although the LED bar graph display has 10 individual LEDs in one package, only five are required for this project.

Once the circuit breadboard wiring is complete, a new code is required to operate the LED bar graph display. The following code blocks are required for the Random Function WAV sketch. These example code blocks are templates to help modify the sound effects machine for visual display of the sounds randomly being selected by the code. The digital pins shown in the code will need to be declared, as shown in the code block; the constant names chosen reflect the optoelectronic component wired to the designated digital pins on the Arduino:

int LEDBar1 = 3; // segment 1 of the LED Bar Graph display
int LEDBar2 = 4; // segment 2 of the LED Bar Graph display
int LEDBar3 = 5; // segment 3 of the LED Bar Graph display
int LEDBar4 = 7; // segment 4 of the LED Bar Graph display
int LEDBar5 = 8; // segment 5 of the LED Bar Graph display

Note that the declaration of constant names and variables are placed above the void setup() section of the code. Next, the constant names need to be configured as digital outputs for the Arduino:

void setup(){
 pinMode(LEDBar1,OUTPUT);  //Define LEDBar1 as digital output.
 pinMode(LEDBar2,OUTPUT);  //Define LEDBar2 as digital output
 pinMode(LEDBar3,OUTPUT);  //Define LEDBar3 as digital output
 pinMode(LEDBar4,OUTPUT);  //Define LEDBar4 as digital output
 pinMode(LEDBar5,OUTPUT);  // Define LEDBar5 as digital output
}

The final step of adding the LED bar graph display to the random function WAV code is to operate the LEDs when the assigned sound file is selected:

void loop(){
  randNumber = random(5); // set max random number

  if (randNumber == 5){ // if number is 5 play file "6.wav"
    audio.play("6.wav");
    Serial.println("Playing 6.wav");
    digitalWrite(LEDBar1, HIGH);
    delay(5000);

  } else if(randNumber == 4){ // if number is 4 play file "4.wav"
    audio.play("4.wav");
    Serial.println("Playing 4.wav");
    digitalWrite(LEDBar1, LOW);
    digitalWrite(LEDBar2, HIGH);
    delay(5000);

  } else if(randNumber == 3){ // if number is 3 play file "5.wav"
    audio.play("5.wav");
    Serial.println("Playing 5.wav");
    digitalWrite(LEDBar2, LOW);
    digitalWrite(LEDBar3, HIGH);
    delay(5000);

  } else if(randNumber == 2){ // if number is 2 play file "3.wav"
    audio.play("3.wav");
    Serial.println("Playing 3.wav");
    digitalWrite(LEDBar3, LOW);
    digitalWrite(LEDBar4, HIGH);
    delay(5000);

  } else if (randNumber == 0){ // if number is 0 play "1.wav"
    audio.play("1.wav");
    Serial.println("Playing 1.wav");
    digitalWrite(LEDBar4, LOW);
    digitalWrite(LEDBar5, HIGH);
    delay(5000);

  } else if (randNumber == 1){ // if number is 1 play "2.wav"
    audio.play("2.wav");
    Serial.println("Playing 2.wav");
    digitalWrite(LEDBar5, LOW);
    delay(5000);
  }

}

With these suggested changes to the Random Function WAV code, the sound effects machine will have a visually appealing look as WAV files are being played randomly.

 

Summary


In this chapter, a sound effects machine was built using an Arduino, SD module, and few off-the-shelf electronic components. The fundamentals of SPI and full duplex communications were explained in the chapter. Adding digital logic switches to the Arduino and WAV files to the SD card were also explained. The installation of the TMRpcm library was provided using GitHub website images and step by step instructions. The operation of the Arduino code was explained aided by the comment statements in the applications software. Finally, the sound effects machine was tested along with the additional information to provide a random file selection feature. Adding an LED bar graph display for selected sounds was explained.

In Chapter 2, Programmable DC Motor Controller with LCD, a discussion on how to build a small electronic controller to operate small power, low voltage DC rotating machines will be presented. Learning objectives in the chapter include interfacing discrete digital logic to an Arduino, wiring a small DC motor to a digital logic gate, and programming a dc motor controller function selection cursor for an LCD. Mini lab testing procedures, Arduino code explanation, and the final project assembly will be provided and discussed in this chapter.

Arduino Electronics Blueprints
Unlock this book and the full library FREE for 7 days
Start now