Hello, this is Ivan, and welcome back to another complete project from Artekit.

This time we bring you a board that simulates the sounds from a lightsaber, using low cost components. This project is a little more complex than the projects we have done before for the blog, and it will need a few external components. But don’t worry, it’s nothing hard to get.

Side note: If you are looking for a ready-to-go board for a lighsaber project, check out the new PropBoard!

We wanted to do another project before the end of the summer. So as usual, we filled the table with every little piece of hardware we found around the lab. And nothing came out. After a few days, we received the breakout boards batch, and that changed the game. So, between one earthquake and the other, the project took shape.

As you can see in the video, we are using a desktop PC speaker, but just for the plastic case around it (there is no amp inside the case). So you can use any 8 ohm, 4W speaker. If you are using a hacked-from-somewhere speaker, we recommend you to find a proper enclosure, until you find a nice pitch. Having the speaker lying on the table only makes low volume noises.

How does this works?

We have the STM32 running the program (source code here), that senses the buttons and processes the accelerometer indications, reads the waveforms from the serial flash and sends them to the DAC, then to the audio amplifier.

Basically we need to detect two events: when the blade hits something and when the blade moves. The MMA8452Q has two interrupt pins that can be set to signal pre-configured events. At startup we tell the accelerometer to detect a “pulse” (a tap) and a transient (that is a certain acceleration in a given window of time). With this two events we can detect when the blade hits something (a pulse) or moves (a transient). We have the transient detection on pin PB1 and the pulse detection on PB2, of the STM32.

Maths are done inside the accelerometer (by Freescale), so the I2C channel is free for whatever you want after the startup. This is a nice feature of the MMA8452Q, that frees resources by processing the most common accelerometer operations inside the IC. Optionally you can query the accelerometer to know on which axis a given event was detected, current acceleration, etc.

Where is the sound coming from?

The waveforms are stored inside the SST25VF080B serial flash. In this case we are using 16-Bit WAV files at 22050 KHz sample rate. We have found the sound files at freesound.org, from an user called joe93barlow (thanks to him).

At first we have tried to use PWM and delta-sigma PWM to generate the sound. But at 16-Bit 22050 Hz it needs a very high PWM frequency. It might work with 8000 Hz audio, but we wanted something with a little bit more quality. So we opted for the external DAC (if you are using a high-density or XL-density STM32, those have an integrated DAC).

The DAC we are using is a 12-Bit DAC. So we have to shift the 16-Bit samples down to 12-Bit. This is a limitation by the fact that the MCP4921 was the only non-SMD DAC we’ve found in our lab. A 16-Bit DAC can be used in place of the MCP4921, and they are more or less the same to use.

Here we enter more in detail on the source code: these waveform are logically divided into two sound groups. The HUM sound, and the ALT sounds: the HUM is the background sound that is always on. The ALT are sporadic sounds that are mixed with the HUM sound. These ALT sounds are divided in the following way: the ON sound, the OFF sound, the HIT sound (a laser from a pistol hits the blade), the STRIKE sound (two saber blades that meet) and the SWING (sort of a doppler simulation when the blade is moved quickly). These are the kind of sound we are playing when an event (pulse, transient and buttons) is detected.

TIM1 generates an interrupt at every sample we have to send (at 22050 Hz). The interrupt code fetches a sample and enables TIM2 to send the sample to the DAC.

If you are quick on maths you have noticed we have to send a sample to the DAC every 0.045 ms, and we have to achieve a fluid, constant output without interruptions. Sometimes we have to mix the HUM sound with the ALT sound so we need to read two waveforms instead of one. All this while leaving to the STM32 enough room to do some processing.

Interesting. Tell me more.

The solution was to read chunks of audio samples using a double buffer. While we play a chunk of samples, in the meantime, we quickly read (at 36Mhz from the serial flash) the next chunk of samples. Since we are dealing only with 20KB of RAM, the secret of this project is how fast we can read from the serial flash. If you are not using a SST25VF080B, you will need an equally fast serial flash.

When the first chunk of data is done playing, we quickly switch buffers and start playing the next chunk. The first buffer is free to load another chunk of samples. The cycle repeats. Since we are playing 22050 samples per second, the chunk size has to be long enough to keep playing while we read another chunk.

The said method is done for two kinds of sounds (ALT and HUM), that have to be loaded and mixed in real time. So instead of two switching-buffers, we have four. This sums to the time the chunk has to be playing while we grab (possibly) another two chunks (one for HUM, one for ALT when it’s playing).

So we have TIM1 running at aprox. 22050 Hz, mixing samples. And TIM2 sending the last read (and mixed) sample to the DAC. Since we don’t want to be busy all the time inside a timer interrupt, we use SPI with DMA. There is a “DMA request queue” mechanism defined in dma.c that appends DMA requests for sound chunks and executes them one after another, and flags the chuncks as “ready” when it’s done reading from the serial flash. The serial flash is so fast that I believe we are wasting more time setting the DMA stuff than actually reading.

The samples are mixed simply by adding the PCM values of the HUM and ALT waveforms. The PCM values for a (signed) 16-Bit audio file go from -32768 up to 32767. If the sum goes beyond that range we clip it at the maximum positive or negative value. There is a lot of discussion out there of how the mixing should be done. We picked that because it sounds decent with our waveforms. If you want to change the algorithm, it is in the MixSamples() function in main.c.

Buttons are simple: one for turning the thing on and off, and one for the HIT sound since we didn’t find a simpler way to simulate a laser pistol shooting at a laser blade we don’t have. 🙂

You can find more insights on the implementation by browsing the source code.

The schematics here above uses the AK-MMA8452 V1 board. We actually ship the AK-MMA8452 V2 board and it is necessary to enable the on-board I2C pull-ups and join the ADDR solder jumper in order to make the project compatible with the AK-MMA8452 V1 version.

For uploading the sound files (included in the source code) you have to connect the serial port to the PC (indicated in the schematics, through screw terminals, and it is TTL, so make sure you put a level translator). Connect the serial port and open up a terminal (like TeraTerm) at 115200 bps, 8, N, 1, and press any key. The sound output will stop and you will see the message “Start sending the file. Reset the board when finished.”. Send the “sounds.bin” file, and reset the board when finished. If you are using TeraTerm make sure you click on the “binary” checkbox in the “Open file” dialog before sending the file.

The “sounds.bin” file can be generated using a program included in the package (VS2010 source code available). This is a very (very) simple program that assembles the sound file. Press the “Add file” button and pick a 16-Bit WAV PCM file at 22050 KHz (yes, only in that format) of a given type: HUM, ON, OFF, HIT, STRIKE, SWING. Then select the right type in the combo-box. Just make sure to include only one HUM sound, one ON sound, one OFF sound, and try to add the HIT/STRIKE/SWING types all together (add all the HIT sounds, then all the STRIKE sounds, and so on). With the waveforms ordered that way, the program can select them pseudo-randomly, in order to not repeat the same sounds.

Conclusion

We hope you have enjoyed reading (and implementing) this project as much as we have enjoyed building it. This project also settles the base for all kind of audio projects you can do, like an audio recorder or a generic music player. The code size is quite small (7200 bytes with O3 optimization) so there is plenty of room for you to play and improve the project.

It is known that every initiated into the force has to build his own lightsaber. Well… you can start with this.

hi thanks for the reply i have just one more question i would like to ask, can you provide me with a full schematic, i have seen the one you have but i would like one of all the parts connected together in one big diagram. i am sorry if i have pushed you to much thanks shahin

The schematic is there. I don’t think it would be clearer by not separating individual components and connecting them all together. Just follow net’s names and ask any questions you may have, here or in the forum.

Thank you.
Volume can be adjusted with the potentiometer (it’s on the back of the board). In the schematics it is named “R3”. By setting it too loud you may distort the sound. It depends also on the speaker you use.
You can use an SD by removing the serial flash and connecting it to the SPI1. You will need a fat file system to access the files (like FatFS http://elm-chan.org/fsw/ff/00index_e.html). There is flash room for that, and examples on implementation can be found for STM32.

I am trying to do something similar with an arduino Uno, and I am having trouble finding the “source code” in the format that I am used to using in the zip file. can you point me in the right direction or am I missing something entirely

Long time since Anyone posted somehing here,
I’m building à binch of different lightsabers.
It started with me buying à cheap fake one and now i’m filming à lightsaber battle scène and building all kinds of Sabers.

I’m used to gettin my soundboard’s out of older lightsabers, because The soundboardS on THE sabershop are just to expensive.

But than i found this website and after looking up THE parts i could Find that you use, i was suprised how cheap it is.

Zo my questipn is; Can you five me à list of all THE parts jat i meed to buld à proper light, soundboard?

hi, I did see that the AK-STM32-DIP36 is able to produce pwm signals. I was wondering if any of the pins that can be configured for wmp signals are still available after building this project? and if so, how many?
i’m hoping to use them to drive a LED signal or 2.
Thank you and great job on this project!

I’m hoping to build this project and take it a bit further. are there any free pins available with this build that are capable of producing a PWM signal?

i’m hoping there are at least 2 PWM pins i can use to drive a NPN transistor, which will drive high power LEDs. one PWM pin would be the main LED and will have a flicker in sync with the hum sound. the 2nd signal will flash with the hit and strike sounds. if there are 3 pins available for pwm i can incorporate RGB color mixing.

ive been doing some more research into the cortex-m3’s capabilities and looking at more possible breakout boards. i came across a breakout board that can control 12 channels of 16bit pwm. it uses a SPI interface, and there arent any SPI interfaces available. how difficult would it be (for the coding) to substitute the SPI DAC for a I2C-interface DAC? that would free up a SPI channel for using the PWM module i found, allowing this build to drive main blade LEDs, and also have plenty of hookups for additional accent LEDs. additionally, can the cortex-M3 handle the added PWM channels without losing the flawless audio playback you designed in this project?

i also want to add that i am still taking baby steps with coding. i took an into to C programming course while getting my electronics degree, but that was over 2 years ago. where would be a good place to start on learning how to configure the I2C DAC and adjust the code to send the right signal to it?

Hi,
The DAC is driven by a bit-banged (simulated) SPI, from TIM2. The real available SPI is used to read chunks of audio from the memory. And I wouldn’t change the reading SPI configuration, in order to avoid interruptions in the audio.
A back-of-the-envelope calculation tells me that for a 12-bit audio at 22050 Hz the DAC should process ~44000 bytes/s (16 bits per sample, leaving out the address and other settings from the calculation). A full-speed I2C at 400Kbits can transmit ~50000 bytes/s leaving very little margin. Perhaps you can use a PWM LED controller through I2C (instead of a DAC).
In any case, I cannot quantify how much code intervention it should take.

Hi. Anything between 8V and 30V DC can be used as the power supply, connected to the AK-POWER2500 as in the original schematics. Or you can remove the AK-POWER2500 board and supply 3.3V directly.
What voltage is this ‘recharge pack’?

This is all Chinese for me but I have a friend who is into electronics and he said he can program these as well. My only problem is that I need to order the parts and give them to him. I found a lot of components enlisted but I’m not quite sure I covered everything or that I need all these. I read that you do not sell this built together and programmed, but is there a full list what to order for complete build? Thank you for your help.

And how can I connect a LED to this audio board. The LED just has to go on when everything else goes on and it has to go off when everything else goes of. It does not need to flicker or flash or anything.

Hi,
I doesn’t come to mind a signal that switches on and off that you can use to turn the LED on/off .
There are some free pins (PA0, PA11 and PA8 pins) you can use to enable the LED, but you’ll have to modify the code. Power supply for the LED, depends on what you want to use. On the board there is 9V from the battery and 3.3V after the power supply.

Hello. I’ve used the 3mm LED to know when the board was powered up, so I think it’s not the best place to put your LED.
I think the best way could be to modify the code to produce a signal in any of the remaining free pins to turn the LED on. You’ll need the circuitry to drive the LED too, if it is a high-power one.

the program hangs in i2c.c line 72 in the while loop:
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));
I2C_CheckEvent always returns ERROR, so execution is trapped in this while loop.
The rest works – if I comment out the accelerometer initialization accInit(); in main.c everything works: I hear the hum sound and the hit sounds works too.
I just cannot get the accelerometer running. I am using the V2 version. Any ideas?
What about the bridge between pins 7 and 8 on the schematics? There are no such pins on the V2 board …

The V2 accelerometer board has a pull-up on the SA0 pin (address selection). This makes the accelerometer addressable as 0x1D. Try joining the ADDR solder jumper on the accelerometer board or change the #define MMA8452_ADDR 0x1C line in mma8452.c file to #define MMA8452_ADDR 0x1D.

Remember also to enable the pull-ups by soldering the solder jumpers on the back of the accelerometer board.

We can keep this conversation in the forum if you want. There we don’t have the posts nesting as with the comment sections, and it should be clearer.

I have tried to build this project exactly to your schematic. I am using the AK-LINK JTAG, however, I do not know how to connect to it in uVision. Do you have any instructions on how to upload the code?

This explanation did not fully understand . How can I install programs on this project and audio files using the JTAG bus with S Link V2. I ‘m so glad you detailed explanation. keil.v5 installed on the PC . Are Keil explains with adjustments ? Please. I do not know the exact English. I use Google translation. I’m sorry if the translation error . Greetings.

Hi!
“For uploading the sound files (included in the source code) you have to connect the serial port to the PC (indicated in the schematics, through screw terminals, and it is TTL, so make sure you put a level translator). Connect the serial port and open up a terminal (like TeraTerm) at 115200 bps, 8, N, 1, and press any key. The sound output will stop and you will see the message “Start sending the file. Reset the board when finished.”. Send the “sounds.bin” file, and reset the board when finished. If you are using TeraTerm make sure you click on the “binary” checkbox in the “Open file” dialog before sending the file.”

I’m not sure this is mentioned ones. I need more descriptive information. UART RX and TX bus TTL RS232 converter using a lightsaber could transfer your project SMT32F103 motherboard. I need to RS232 converter and a PC cable connection pin number.
I’m having difficulty in a statement. Please try to understand and help me.
I’ll try this if JTAG. I will use ST-Link V2 Debug.
Greetings.

Hi, I’ve built the board and I’ve tried to upload the sound file. I used Tera Term with a USB connection on port COM7. I configured the port at 115200 bps, 8, N, 1 and when I send the file I see the file get charged but only if the checkbox Binary is not checked, otherwise nothing happen. In any case the board don’t work. I have to change some configuration because I use a USB port? Please help me. Many thanks.

I was not able to make the supplied Eclipse project work with the latest version of the GNU ARM Eclipse Toolchain. I builds without errors, but no ELF file is produced (last few steps in the tool chain was not run). I had to create a new empty STM32F103T8 project and then add your code files manually to make it build.

I also had some problems with the code immediately entering the sound file download/flash program procedure when I did not have the serial/UART cable connected to the PC (noise on RX pin?). After uploading the sounds.bin and then removing the configPoll() call in the main loop it works OK.

Hi frodesto, Thank you for commenting, and sorry for the delayed answer.
Yes, a noisy UART RX pin can be interpreted as the user sending a character. A weak pull-up or pull-down resistor (external or internal) can solve the problem.

Hi, I try to use your SndPack application to pack some new sounds. However some files just can’t be included, though they have the same attributes (.wav 352kbps). Even a same wav file re-saved by Adobe Audition can’t be included.
Do you know why? Many thanks

Many thanks for your reply. yes I am sure these parameters are the same (sample rate 22050 Hz and 16-bit per sample). The problem is that I can choose these new files to add into the bin, yet some of them is ok, while the rest just don’t have any response. My OS is win7 home edition. I use this software in XP compatible mode

Hey, It seems this project is still supported, is there any chance that links will be added to the other parts? I’m having trouble finding some of the parts written here, I would really appreciate it!
Also, I’m a software developer but I did not study any electrical engineering, would this kind of project be too hard for a novice EE learner?
I saw boards that do similar things that price about 70$, but I thought making one on my own might be cheaper and more fun =]

Hello. Sorry for the late answer.
Most of the missing parts can be found on big distributors like Mouser or Digikey.
This is not the easiest project but it’s not that hard. My advise: to have the right instrumentation (oscilloscope, multimeter) to diagnose if something doesn’t work, and some code debug capabilities and tools (JTAG for example).

Hi is there any chance I can purchase a completed board from you guys so all I have to do is fit it to my hilt? I’m building a Lord Revan sabre from scratch and I have never done any electronics work. Plus I’m in the U.K. How much will it cost all together with delivery thanks.

This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.

You can adjust all of your cookie settings by navigating the tabs on the left hand side.