Arduino Serial Control Tutorial

I’ve had a number of requests recently for help with using the WAV Trigger Serial Control Library for Arduino, so here’s a basic “how-to” using the example Uno sketch that’s now included in the library. For those of you unfamiliar with GitHub, fear not; Just use the “Download Zip” button on the library’s GitHub repo page (above link.)


Step 1: Install some Arduino libraries.

At a minimum, you’ll need to know how to install third party libraries into your Arduino development environment. There are plenty of tutorials available on that subject, so check ’em out and come back here when you’re up to speed. The example sketch requires 3 libraries be installed into your Arduino “libraries” folder.

Some Arduinos, such as the Uno, only have a single hardware serial port. I find it cumbersome to have to share with the USB connection when you need a serial port for your sketch. The AltSoftSerial library effectively creates another serial port out of normal digital pins. The specific pins it uses for RX and TX vary with the Arduino board type, and you can find out which ones by looking in the “known_boards.h” file in that library. All we need to know for now is that for the Uno, it makes Pin 9 be the TX pin.

Update: If you’d rather use a hardware serial port (UART) instead, the library now also supports Serial, Serial1, Serial2 or Serial3. The caveat is that you have to make one small edit in the wavTrigger.h file. See the library readme file for the details of how to do this.

Metro is a great interval timer class when you need to schedule events that you can’t use delay() for, such as blinking lights. The nice thing is that it doesn’t use any hardware timer resources. I use it a lot, and you should too.


Step 2: Prep your WAV Trigger.

I strongly advise that you install post headers or receptacles in any WAV Trigger connector that you’re planning to use. PCB connector pads are not meant for direct soldering and I’ve seen too many pads ripped off boards because people solder directly to them and don’t provide strain relief.

The example Arduino sketch is written to use a specific set of audio tracks. Download these tracks, unzip and copy them to the WAV Trigger’s microSD card. They already have the correct track names. You do not need an init file for this example unless you’re using the on-board amplifier and a speaker instead of the line out audio jack.


Step 3: Connect the WAV Trigger to the Uno.

The library currently only implements commands to the WAV Trigger, so you only need to connect 2 wires: GND, and the Uno TX pin to the WAV Trigger RX pin.



Step 4: Power up and run the sketch.

Apply power to both boards and connect your USB cable to the Uno. If the libraries have been installed properly, you should be able to open the WTriggerUno sketch: File->Sketchbook->libraries->WavTrigger->wTriggerUno.

This example sketch demonstrates many of the library’s functions for starting, looping, fading, cross-fading, stopping tracks and groups of tracks and sample-rate (playback speed) control. It makes use of the sample tracks from Step 2 above, and probably won’t make much sense if you try to use your own sounds as is. You can of course, modify the example sketch to work with any sounds you wish.

The example sketch also assumes you have recent firmware in your WAV Trigger. Some of the features demonstrated by the sketch, such as pitch bending, have been added since the last SparkFun release firmware, so in order to get the full effect, you may need to update your WAV Trigger with the latest version from the download page.


Extra Credit: Powering the WAV Trigger from the Uno.

It’s possible to power the WAV Trigger from the Arduino 5V, although you probably should not do this if you’re planning to drive a speaker continuously at high-levels using the on-board amplifier, due to potentially high 5V current draw (even though I do.)

First you’ll need close the solder jumper on the WAV Trigger that connects the on-board 5V to the serial connector. The WAV Trigger doesn’t ship from SparkFun this way, because people might connect an FTDI Basic at the same time that they’re powering the WAV Trigger from the barrel connector. I always close this jumper because it allows me to power the WAV Trigger from the FTDI basic.


Now just connect a 3rd wire from the Uno 5V pin to the WAV Trigger pin labeled “5Vin” next to the serial RX pin. Make sure not to power the WAV Trigger from the barrel connector at the same time!




  • reply Mike Dodds ,


    What would you recommend for a strategy for releveling gain on playing tracks on-the-fly?

    • reply ,

      Use the trackFade() command, if I understand your question properly.

    • reply jérémy Morelle ,

      is there any chance you add the function to get the track name (or at least the track number)?
      Thanks for your module, it’s a great work.

      • reply f4ckturd ,

        hey if you want to use wav trigger with arduino mega 2560 r3 , what the “pin 9” on that one ?
        thatnks! great product!

        • reply djsoulmachine ,

          Hi Jamie!
          I would like to control the wav trigger with MIDI and an Arduino (the MIDI will be received by the Arduino and re-send to the Wav trigger by the Arduino).

          I assume that :
          – I need to modify the wavtrigger.h to set the serial output to 31250
          – I need to set the INITmaker to “MIDI”

          Could you please confirm ?
          Thanksa lot Jamie!

          • reply ,

            The WAV Trigger Arduino Serial Library supports the WAV Trigger serial protocol, which is not MIDI. Just changing the baud rate in the library does not make it MIDI. It’s a different protocol. If you want to transmit MIDI from the Arduino, I’d suggest using an existing Arduino MIDI library. And yes, you’ll need to set the WAV Trigger to MIDI mode using the init file.

            • reply djsoulmachine ,

              Yes, I forgot to mention that I do use a MIDI library to make the MIDI datas going through the Arduino to the WAV trigger.
              My only question was about the Baudrate since, correct me if I’m wrong, your wavtrigger.h set the serial output at 51700kbps. But maybe the init file overcome this parameter?

              • reply djsoulmachine ,

                Appart from the MIDI I’ll use the Arduino to control the SampleRate

                • reply ,

                  Setting the serial port to MIDI mode in the init file will automatically set the baud rate to 31.25kbps. In MIDI mode, you’ll need to use MIDI Pitch Bend messages to control sample rate.

                  • djsoulmachine ,

                    OK, so the setSampleRate() function is no longer available in MIDI mode? 🙁

                    • ,

                      It never was. I think you’re confusing MIDI with the WAV Trigger’s native serial protocol supported in the Arduino library. The WAV Trigger’s serial port can only be set for one or the other – not both at the same time. The default serial mode supports the native serial protocol messages described in the on-line user’s guide and is compatible with the Wav Trigger Arduino library. It includes a sample-rate offset command.

                      When the serial port is put into MIDI mode via the init file, the WAV Trigger only recognizes standard MIDI messages. MIDI has no “sample-rate offset” message, but it does have MIDI Pitch Bend, which effectively does the same thing.

                      It’s always been this way.

                  • reply djsoulmachine ,

                    Does this mean that I cannot go further than 5 semitones in MIDI mode ?
                    I actually need to reach 0.5x speed…
                    Thanks a lot Jamie, this will me my last question 🙂

                    • reply ,

                      The latest firmware version, v1.18, supports a MIDI Pitch Bend range of +/- 7 semitones. If you need the full range, you have to use the serial protocol, not MIDI. I will point out here that if you’re already using an Arduino to read MIDI, using the WAV Trigger’s native serial protocol (and the Arduino library) gives you a lot more flexibility. Because you’re starting the tracks yourself, you have independent volume control of each track, and you can create any MIDI note to track mapping you want.

                      • djsoulmachine ,

                        Thanks a lot for all you answers Jamie!
                        My purpose is actually to build a musical instrument, this is why I use the Arduino MIDI library because it is really powerfull in terms of low latency and functionality. If I use the serial protocol, I’m not sure I am capable enought to write a sketch able to receive MIDI and trigger the tracks with the same efficiency… I studied a lot the wav trigger sketch you provided and I didn’t see a easy way (I mean easy for me !) way to do it…

                        Maybe I’ll have to forget this “Half Speed” feature for my project :'(
                        If you plan to extend the range of the MIDI pitch bend, please let me know!
                        Or even just a “Half Speed” trigger, I actually don’t need all the semitones between 🙂

                        Thanks again Jamie, you’re the best!

                • reply diandeRocker ,

                  Hello, i like the Wav-trigger board a lot, the Mellotron is so fantastic.
                  And this board is great, so many on this little thing, and send it different Midi channels
                  and it is multi-timbral, Great work, thanks.
                  My Question is about the samples put on the Micro-sd card, i put some on it converted and numbered
                  right and not all samples work, some do and some don’t, any clue for how this is happening,
                  I use a Kingston 8 gb card, and fill it with WinXP or Win7,
                  When i try to access the card sometimes the computer says it’s write protected.
                  So i try again after a reboot and then it’s accessable.
                  Thanks in advance, Dian from the Netherlands

                  • reply ,

                    You must be sure that your samples are saved as 16-bit, stereo, 44.1kHz wav files with no meta-data stored in the file. Audacity, a free open-source audio editor, can be used to read and then re-save a .wav file in the correct format.

                  • reply Jeremie ,

                    In your Github documentation it is written than the track number t can only be between 1 and 999, is it still true?

                    I actually didn’t see this statement before and I loaded my SD card with more than 1000 wav files and, surprisely, when I made some tests with the wTrig.playTrackPoly(int t) function, the files above 1000 are playing and the files below 1000 are not… That does not make sense, does it?

                    Thanks in advance!

                    • reply ,

                      The latest version of the WAV Trigger firmware supports up to 2048 tracks and the library doesn’t limit the number sent. I’ll change github readme – thanks for pointing that out. Not sure why tracks under 1000 aren’t playing for you. Make sure you include the leading 0’s in the track filename, i.e. “0016_xxx.wav”. Try playing one of the low number tracks with a trigger input.

                    • reply Tom ,

                      I am trying to get the examples running and and getting the error ‘class AltSoftSerial’ has no member named ‘clear”. This same error (HardwareSerial rather than AltSoftSerial) shows up if I try to use the Hardware Serial port. What am I missing?

                      • reply ,

                        I recently incorporated changes to add two-way serial communications done by hwestbrook – see the Jul 7 commit. Until I can have a look, I suggest using the Apr 26, 2015 commit which does not have this issue.

                        If you would please post this issue on the github site, perhaps he’ll have time to fix it before I will.

                        • reply Mark ,

                          Another user noted on github that commenting out the offending line in wavTrigger.cpp [line 51: WTSerial.clear(); ] fixed the problem. This appears to work for me as well, though I haven’t done much experimentation with the wavTrigger yet.

                      • reply AlexW ,

                        I followed the tutorial along with the example program (+ example wav files) on an Uno, but could not get the wav trigger playing any file through the arduino. Only by pressing the button on the wav trigger the first sound played successfully. Rx is wired to pin 9 and the LED at 13 is blinking as programmed in the example program. I currently do not have a device to update the firmware.
                        Any suggestions?

                        • reply ,

                          Serial communication is notoriously difficult to troubleshoot without an oscilloscope to see where the problem is. It sounds like you have a wiring problem or even a hardware issue with your WAV Trigger or Uno.

                        • reply Darin ,

                          I’m using the WT alongside a SparkFun MIDI shield (on an Uno). The MIDI shield has a hard requirement of using pins 8 and 9 for soft serial. Is there any way to use another pin besides 9 with the WT? I looked through the library code but didn’t see anything obvious.

                          • reply ,

                            If I understand your question correctly, AltSoftSerial requires the use of pin 9 for its TX on the Uno. This is a limitation of AltSoftSerial and the Uno, which has limited resources and only the 1 hardware serial port used for programming. If you need 2 serial ports (WAV Trigger plus MIDI) I’d recommend the Teensy, which has 3 hardware serial ports. I’ve used this to handle both MIDI and the WAV Trigger’s serial protocol.

                          • reply Russ ,

                            Hi Jamie, I’m new to the fascinating world of electronics and could use a little guidance in getting the WTriggerResponse working in my particular environment. I’m building a customized bird clock using the WAV Trigger and the Arduino Pro Mini. I’ve successfully compiled and uploaded the WTriggerResponse sketch to the Pro Mini (had to comment out line 51: WTSerial.clear(); in the wavTrigger.cpp to get it to compile) so I can test serial communication from the Arduino to the WAV Trigger.

                            I’m powering the Pro Mini from the WAV Trigger. To do this I’ve powered the WAV Trigger using the barrel jack (9vdc) and connected wires from the VIN and GND pins next to the barrel jack to the VCC and GND pins on the Pro Mini. For the serial communication I’ve connected the TXO pin on the Pro Mini to the RX pin on the WAV Trigger.

                            When I power up the WAV Trigger I see a red light flashing (every 2-3 seconds or so) and there’s a solid red light on the Pro Mini. I was thinking I could use the Arduino Serial Monitor (under the Tools menu) to provide input. When I enter ‘p’ and click “Send” nothing happens (I was expecting to see output on the Serial Monitor). I’ve tried sending ‘p’ and ‘s’ values using the various sending options (no line ending, newline, carriage return, both NL & CR) but that made no difference … any ideas?

                            • reply ,

                              First, you should be aware that you are not actually “powering the Pro Mini from the WAV Trigger”. You’re simply tapping into the output of your 9V supply, which is powering both boards.

                              Second, if you don’t connect the WAV Trigger’s TX to the Pro Mini’s RX, then you won’t get responses in any event. And unless your Pro Mini code is echoing characters from the debug serial to/and from the WAV Trigger serial port, the serial monitor is in no way connected the WAV Trigger.

                              Third, I think you’re confusing the WAV Trigger’s serial protocol with the MP3 Trigger’s. They are completely different, and the WAV Trigger does not support typing ASCII commands from a terminal program.

                              I suggest you start by following the original “WTriggerUno” example in the library. Make sure you understand the code in the example sketch.

                              Unless you need responses from the WAV Trigger, you might also consider using the previous library commit. The recent addition, which introduced the compile problem was not done by me, and I just haven’t had a chance to clean this up.

                            • reply Russ ,

                              …meant to say when ‘p’ was entered I expected to hear sounds through the headphones and to see text output on the Serial Monitor when ‘s’ was entered.

                              • reply Russ ,

                                Thank you Jamie for your quick response, explanations, and guidance … I will try the WTriggerUno tutorial this afternoon and see how that goes.

                                • reply Russ ,

                                  Great news! I was finally able to get the wTriggerUNO sketch working. The problem I was having was connecting the RX pin on the WAV Trigger to the TXO (D1) pin on the Pro Mini. Once I connected the RX pin on the WAV Trigger to pin 9 (D9) on the Pro Mini everything worked. Thanks again for all your help!

                                  Leave a comment