This trick teaches you how to use Micro SD/SD card for Arduino and speaker.
Devices and components
Arduino Uno Rev3
Speaker: 0.25 W, 8 ohms
Male/female connection wires
MicroSD Module (Generic)
Materials and tools
Hot glue gun (generic)
Software and tools
Arduino IDE
Project description
secure code
arduino
1#include <SD.h> //include SD module library
2#include <TMRpcm.h> //include speaker control library
3
4#define SD_ChipSelectPin 4 //define CS pin
5
6TMRpcm tmrpcm; //crete an object for speaker library
7
8void setup(){
9
10 tmrpcm.speakerPin = 9; //define speaker pin.
11 //you must use pin 9 of the Arduino Uno and Nano
12 //the library is using this pin
13
14 if (!SD.begin(SD_ChipSelectPin)) { //see if the card is present and can be initialized
15
16 return; //don't do anything more if not
17 }
18
19 tmrpcm.setVolume(6); //0 to 7. Set volume level
20 tmrpcm.play("1.wav"); //the sound file "1" will play each time the arduino powers up, or is reset
21}
22
23void loop(){}
Note: Content and images are from: https://projecthub.arduino.cc/, with some modifications.
If you want it removed due to copyright reasons, please leave a comment. Thank you.
I want to share this article more widely so that everyone knows about Arduino and your project.