A Jedi quest: How did I build my lightsaber


IMG_0829

Everything has a beginning

In the summer of 2013 I decided to get an Obi Wan Kenobi costume for Halloween. I looked online and found a place in Hong Kong that will make the costume to size. I order it and although it arrived too late for that Halloween I could always use it in next years Halloween. As part of the costume, I needed a lightsaber. I looked for them in the internet and found several sites that offer pretty good ones, some following the specs of the movie ones with minute detail. They were very expensive though, and somehow I did not felt like buying one already made.

Little by little the idea started growing in me about building one. If you are a Star Wars fan you know that every Jedi Knight had to build her or his own lightsaber. Besides I was always tempted to do a project with one the inexpensive processor boards available, especially around the Arduino.

Lightsaber specifications

My goals in making a lightsaber where very simple:

  1. I have to make it from scratch, well almost. Of course I did not want to design and build a microprocessor, or LED’s or other components, but I would not buy a part that was specially made for a lightsaber, like the handle or the light emitter. By the way both are easily available in the internet. Just type lightsaber in Google and you will get many entries, from very cheap toys to very expensive ones.
  2. I did not want a one color light saber. I wanted one where I could choose the color, and even change colors as it moved.
  3. The lightsaber should be able to generate its own sound. Not from an mp3 file, but generated by the processor and able to detect motion and change pitches according to that.
  4. I did not want to replicate any of the movie characters handles. I wanted to create my very own handle. Ideally something that I could fabricate in my workshop, but if not, at least my very own design.
  5. Not consciously at the beginning, but now it is very clear that the lightsaber software could be modified and or replaced at any time

Basic concepts

Setting my goals, I started to play with the basic concepts and its implementation. To begin with, what Arduino® board should I use? The answer was more or less provided by what is available at your local RadioShack. I visited mine and notice that they have Arduino® Uno in three different formats, the processor by itself, or the “Arduino® Starter Kit”, or the “Make Ultimate Microcontroller Pack with Arduino® Kit”.

So, how to decide which one to buy? I am assuming two scenarios:

  1. You have a bunch of electronic components like resistors, capacitors, jumper cables, transistors and most importantly a breadboard to assemble your project. Then the only thing you need is the processor. You can get it at your local RadioShack for about $25 dollars. Some search in the internet can get you down to about 15~17 plus shipping and handling. That will put you in the $20~25 price range.
  2. You are completely new to this hobby. Then get either the “Arduino® Starter Kit” or the “Make Ultimate Microcontroller Pack with Arduino® Kit”. I choose the later because it has many more components than the starter kit and the only thing missing from it was the “Arduino Project Book”, that can be found online in the Arduino site.

Having acquired my Arduino board and all the extra components that I might need I started with goal #2: how to make a multicolored light emitter. This is a twofold question: first you need a source for the light, and a way to change its color; second you need a media for the light.

Since I already have my processor and required electronics I decided to tackle first the source of light. The first Arduino sketch was about connecting a RGB LED to my processor board and somehow control the color change. First of all you need what is called a RGB LED. That is a device that is able to light a Red or a Green or a Blue LED. Alternatively you can also create an arrangement of three individual LED’s each of Red, Green or Blue. Lucky for me, my “Ultimate Microcontroller Pack” had a single device RGB LED.

Chapter 7 of the Arduino cookbook is devoted to Visual Output, and the first example is about controlling an RGB LED. I put together a circuit like the one below

The operation is very simple: a LED is a special kind of diode that happens to emit light when a current pass through it. A diode is a semiconductor that only allows current to pass in one direction. When one of the ports 3, 5 or 6 in the Arduino is set to HIGH, then the current pass through the corresponding LED and it will emit light. A small resistor is added to prevent too much current to pass through the led and burn it. The figure below shows the circuit

rgbledcircuit

A program can be written to set the values of ports 3, 5 and 6 to HIGH or LOW. My first program looked like this:

/**
* Create an alias for pins 3, 5 and 6
**/
#define RED 3
#define BLUE 5
#define GREEN 6
int ledPin[] = { RED, GREEN, BLUE };
/**
* Will initialize the pins 3, 5 and 6 as outputs
**/
void setup() {
   for (int i = 0; i < 3; i += 1) {
       pinMode(ledPin[i], OUTPUT);
   }
}
/**
* Will light each of the leds for one second
**/
void loop() {
   for (int i = 0; i < 3; i += 1) {
       digitalWrite(ledPin[i], HIGH);
       delay(1000);
       digitalWrite(ledPin[i], LOW);
   }
}

Although this code is pretty boring, as it only turns on the LED red, green and blue lights, it was all I needed to prove that I can control the color, and more important, it was good enough to test the material that I had in mind for the media.

I use a small cylinder of acrylic, about ¾ of an inch in diameter and positioned on top of the led. The acrylic piece shone with red, green and blue light. It was very dim, but certainly the acrylic was transmitting the light. I sanded the cylinder with a 100 grit piece of sandpaper and the colors show more vividly. So based on this experiment I had the fundaments of my lightsaber emitter: an RGB LED and a piece of acrylic.

One of the problems of the code above is that it shows the colors at full intensity and each color in order. What happens if I want to get a yellow color in the acrylic? Well using color theory, if you turn red and green at the same time you will get yellow. The second program show many more colors:

#define RED 3
#define BLUE 5
#define GREEN 6
int ledPin[] = { RED, GREEN, BLUE };
int black[]  = { 0, 0, 0 };
int blue[]   = { 0, 0, 1 };
int green[]  = { 0, 1, 0 };
int cyan[]   = { 0, 1, 1 };
int red[]    = { 1, 0, 0 };
int purple[] = { 1, 0, 1 };
int yellow[] = { 1, 1, 0 };
int white[]  = { 1, 1, 1 };
int* colors[] = { black, blue, green, cyan, red, purple, yellow, white };
void setColor(int colorIndex) {
   for (int i = 0; i < 3; i += 1) {
       digitalWrite(ledPin[i], colors[colorIndex][i]);
   }
}
void setup() {
   for (int i = 0; i < 3; i += 1) {
       pinMode(ledPin[i], OUTPUT);
   }
}
void loop() {
   for (int i = 0; i < 8; i += 1) {
       setColor(i);
       delay(1000);
   }
}

But still each color is an all or nothing proposition. And the problem is that a LED does only have two possible values: ON or OFF. That means light or dark. There is not in between. How can you fade the colors to for instance change green into yellow through a period of time? Or show sudden flashes of red when the light is blue?

The answer to that is to use a technique called PWM or Pulse Width Modulation. PWM works by changing the state of an output port on and off over a period of time. If there is no change and the state is off the led will be dark. If there is no change and the state is on, the led will be light. But if the state switches on and off our perception is that the value is in between dark and light. This is the way to achieve dimming or fading in a LED. Arduino UNO supports six ports with PWM: 3, 5, 6, 9, 10 and 11. That is why I used ports 3, 5 and 6 for my circuit.

Controlling the color change

Once I knew how to change the colors of the RGB LED the problem become how to trigger the changes. First I thought of using the tilt sensors that come with the Arduino kit. This sensor is a small switch that has a tiny metallic ball inside. When you move it, it opens or closes the switch. The only problem with this solution is that is an all or nothing. On or off. I wanted a more fine control. Then I looked for accelerometers for the Arduino. There are many in the market, and at the time I found one in Amazon that looked like it will solve the problem nicely:

5175GD8JbHL._AA160_

I connected the accelerometer to my Arduino and was able to detect motion. An accelerometer basically will report a voltage in its output, and the Arduino analog to digital converter will change the voltage to a value between 0 and 1023. Zero is the maximum negative acceleration and 1023 is maximum positive acceleration. The accelerometer needs to be calibrated, that is find what the minimum and maximum values are. The program need to constantly monitor the values and set the limits accordingly:

int minX = 9999;
int maxX = -9999;
void loop() {
    int xAcc = analogRead(A0);
 
    if (minX > xAcc) {
        minX = xAcc;
    }
 
    if (maxX < xAcc) {
        maxX = xAcc;
    }
}

Now with an accelerometer I had the means to control the colors. The accelerometers can produce three values, one for each of the axis, X, Y and Z. I assigned each axis a color: X to red, Y to blue and Z to green. A change in the value of the accelerometer would produce a change in the PWM value for the particular color. Moving the saber will change its color.

Sound

For the original saber I used a sound synthesizer for Arduino, called Auduino. This program uses five inputs to control the tone generated. I noticed that I really only need one input to change the frequency of the sound and what I did was to combine the changes in acceleration in all axis to modify the frequency of the sound. All the other four values needed for the Auduino were found using a set of potentiometer and reading their values. Once I got to satisfying values I hard coded them in the saber code.

Another problem was that Arduino could produce the sound, but could not drive a loud enough speaker. I needed to add an amplifier circuit. Searching in the web I found a one watt amplifier using an LM386 chip. After some tinkering and several failures and burned chips I arrived to this configuration

One Watt AmpLight

7RGBLED

Seven segment RGB LED

3WattRGBLed

Super bright 3 Watt RGB LED

The only ingredient missing was the RGB LED. Again, using a search in Google I found this beautiful seven RGB LED assembly. This assembly had several advantages, one of them that it is ready to connect to an Arduino. It uses three inputs that can be PWM ports, and has one common ground pin. The colors are very bright, and when tested with my piece of acrylic it shone really nice. Later when I put the first saber together I will find that the light was not bright enough to illuminate the whole length of the saber. That is when I researched for another solution to saber lightning. And then I found a 3 Watt RGB LED. This led was exactly what I needed, although I needed to add more logic to the circuit, as the Arduino is not capable to deliver 3 Watts of power. I ended using a 9 Volt battery to power the LED and using a chip with an array of 7 Darlington transistors to amplify the signal from the Arduino and delivering it to the LED.

Choosing the right Arduino

With all the components the only thing missing was to select the Arduino board for the work. An Arduino Uno was perfect to write and test the circuits and code, but it is very big, and there is no way it can fit inside of an small diameter pipe. Browsing through the Arduino site I found two possible candidates, the Arduino Micro and the Arduino Nano. Both were more than enough for the job, with the Nano being smaller than the Micro. I choose the Nano because it was smaller and because I found a source for it in China for about $10, compared with the $35 for the Micro.

Arduino Nano

However, after putting together the first two light sabers I decided that the source in China was not reliable. It took 6 weeks for the product to arrive through the mail and the inexplicably some of them failed. After I had three processors failed without apparent reason I decided to look for another source. Ada Fruit is a hacker and electronic supply store from New York, and they have all sorts of great products. One day visiting their site I found that they just released what they called the “Pro Trinket” a very small Arduino compatible processor. And the best news was that it is made here in the USA, Ada Fruit is behind it and it is even smaller than the Nano, without sacrificing any functionality with the exception of being able to have serial communication with the board using the micro USB port. And it comes in two flavors, 3V3 and 5Volts.

ADA Fruit Pro Trinket 3V3

Ada Fruit Pro Trinket 3V3

With the only exception of having to use Ada Fruit libraries for the processor, the software for my saber runs in this little board without modification. Here is a picture with all the hardware in place. The long board has the processor, the accelerometer, a 5V voltage regulator and three resistors to control the amount of current going into the RGB LED. The small board in the bottom is the switch assembly. It has two switches, a small slider switch close to the wires and the yellow round one.

Why two switches? Because this processor has a very small memory and it takes some time for the boot loader to start the process. I decided to have two switches, one to power the processor and launch the boot loader, and the second to actually start the loop that lights the saber and produces the sound on the tiny little speaker at the bottom. The alternative was to have the processor always on as soon as you connect the battery and have only one switch.
And to make sure that there is enough power to drive the lights (3 Watts), the speaker (2.5 Watts) and the processor I am using a battery holder that can have 6 AA batteries.

LightSaberOff

Light Saber assembled

Once the slider switch is on and the processor ready to execute the program, there is an audio signal and the led in the switch assembly is turned on:

LightSaberOnFinally, when the yellow button is pressed the RGB LED is turned on and the speaker sounds

LightSaberOn-LightOnThe hilt

The hilt design proved to be a challenge all by itself. My original idea was to turn one out of an aluminum bar. Problem is that I do not know how to turn, and I don’t have a lathe. Then I settle for a material easy to obtain and easy to machine with the tools I have in my garage: Several hand saws, a drill press and a Dremel rotary tool. PVC proved to be a satisfactory material. Easily available at your local hardware store, it has many accessories and connectors that can be used in building the hilt. After a couple of visits to my local Home Depot I got a piece of 1-1/4 internal diameter PVC. External diameter is about 1.66″. With my first sable I just connected the different boards with cables and stuffed everything into a piece of PVC pipe. I don’t have that saber any more because I used the components to build saber number two:

LS2I am afraid to open this saber and replace the batteries as I don’t know if I will be able to put it together again. As you can see it is mainly a piece of 1-1/4″ PCV, with an end cap at the bottom and a couple at the top. The upper piece has a 1″ internal diameter enough to hold a 1″ rod of acrylic or polycarbonate. My first three sabers used a solid acrylic rod for the blade. I changed that to a 1″ tube of polycarbonate with 1/8″ walls. It was pretty rustic but it served its purpose.

LS5This one has the prototype for the switch assembly, although it is saber #5. Still plumbing grade PVC pipe, and the ribs where made with balsa wood and bamboo. Is is shorter that the other ones as I made it with a left over piece of PVC.

LS4Saber #3 I gave as a birthday present to my son Juan Pablo. I made this saber #4 immediately after and I used some of his ideas on how to decorate it.

LS6I found a great source of furniture grade PVC. Saber #6 and #7 are made with this PVC tubing. Still 1-1/4″ internal diameter. I used a piece of aluminum channel profile to made a housing for the switch assembly, that gives a nice touch to the saber. Color electric tape provides many opportunities to decorate the saber.