How to use a potentiometer to change the behavior of the GlowSaber

GlowSaber Switch assembly

11999092_1652403508350401_7697716071713430521_n
GlowSaber switch assembly and protection guard

The GlowSaber has a switch assembly, that controls the on/off functions. It also has a LED to show that the GlowSaber is ready to start, and finally has a small 1 kΩ potentiometer. The following is a schematic of the switch assembly:

GlowSaber-switch-assembly-schematic_schem
GlowSaber switch assembly schematic

The connector at the bottom is used with a cable to connect to the GlowSaber main PCB. The pins are, from left to right:

  • gnd: Ground
  • off: This is the input from the battery.
  • on: When the hard switch is closed this routes the input voltage back to the main PCB
  • led: This is connected to a digital output port in the GlowSaber processor. It is turned on when the GlowSaber is ready to start.
  • pgm: This is the output of the center pin of the potentiometer, and is connected to port A0 (analog zero) in the processor.
  • ssw: Soft switch. This is the switch that actually turns the GlowSaber RGB LED and sound on.

The processor inside the GlowSaber is very small and only has about 2 kbytes of memory. The program is loaded from a flash memory and it takes some time to load, usually about 3~4 seconds. Leaving the battery always connected is a bad idea since although in its “Off” state the saber does not use too much power, still it uses some and the battery will be dead after only a few days of storage.

To save battery the GlowSaber design uses two switches, the hard switch in the schematic above, used to load the program and have the saber ready. The soft switch uses a temporary switch to let the processor to turn the lights on or off.

What is a voltage divider

The potentiometer acts as a voltage divider. In the switch assembly there is a Vref voltage coming from the main board. This is used to light the switch assembly LED, to complete the circuit for the Soft Switch and to provide a reference voltage for the potentiometer middle pin.

The understand how a voltage divider works we need some math and physics concepts. The first concept that we need to apply is Ohm’s Law:

The current through a conductor between two points is directly proportional to the potential difference across the two points.

In mathematical terms this can be written as:

Current∝ Voltage

and it is read the Current is proportional to the Voltage (potential difference). To make this principle useful we need to move from a proportion to an equality. To do this we need to introduce a constant of proportionality:

Current ∗ Resistance = Voltage

or as more commonly stated:

Voltage = Current * Resistance

The common symbol for voltage is V, measured in Volts(V), for current is I, measured in Amperes (A) and for resistance is R, measured in Ohms(Ω), and we arrive to the famous Ohm’s Law in mathematical terms:

V = I*R

This expression can be written to compute each of the terms in it:

I = V / R

and

R = V / I

Ohm’s Law help to understand how current is related to potential differential, but does not explain what happens when electricity flows through a resistor. For that we need to introduce the law of energy conservation:

In a closed system (like an electric circuit), no energy is ever lost. The total amount of energy in the system is always the same.

For instance, in a circuit with a light bulb, some energy is released from a battery, moves to the light bulb, lights the bulb filament, and returns to the battery.

Using the law of conservation of energy it can be stated that the sum of changes of potential in a circuit is zero. In the light bulb circuit the battery increases the potential, and the light bulb decreases it,  and, in this case the increase is the same as the decrease.

Let’s consider the following circuit:

Resistors in Series Circuit
Resistors in Series Circuit

It has a 9 Volt battery and two resistors, one of 220Ω and one of 470Ω. According with the conservation of energy law, the increase of potential in the battery is the same as the decrease of potential in the resistors:

Vbattery – Vresistor1 – Vresistor2 = 0

or

Vbattery = Vresistor1 + Vresistor2

To simplify the expression we can write it as

V = V1 + V2

And we can read it as the drop of voltage in the resistor 1 and the resistor 2 is equal to the voltage provided by the battery.

In this circuit the amount of current remains constant as there is only one source of energy and all of it has to pass through the only path in the circuit. The only element that provides energy is the battery. Based on this observation we can rewrite the expression above as:

I*Rtotal = I*R1 + I*R2

I*Rtotal = I*(R1 + R2)

and we can remove I from the expression to get

Rtotal = R1 + R2

This is a very important consequence of the conservation of energy law: the total resistance of circuit with resistors in series (one after the other) is equal to the sum of the individual resistance of all the resistors.

If we measure the difference of potential between points A and C of the circuit we will be measuring the voltage of the battery. But what is the voltage between A and B? And between B and C?

The potential drop between A and B is given by the equation:

V1 = I * R1

since we know that

I = V / Rtotal

we can rewrite V1 to get:

V1 = R1 * (V / Rtotal)

V1 = V * R1/(R1+R2)

To get the potential drop between B and we can use a similar expression:

V2 = R2 * (V / Rtotal)

V2 = V * R2/(R1+R2)

The important thing about V1 and V2 is that the actual value of the resistance is not enough to determine the potential drop. To determine the voltage drop we need to find the ratio of the particular resistance to the total resistance in the circuit.

In the specific circuit above we have a battery voltage of 9V and two resistors, one of 470Ω and one of 220Ω. Thus

R = 470 + 220 = 690Ω

V1 = 9 * 470 / 690 = 6.1304 V (potential drop between A and B)

V2 = 9 * 220/690 = 2.8696V (potential drop between B and C)

V = 6.1304 + 2.8696 = 9V

I = V / R = 9 / 690 = 0.01304A

If both resistors had the same value, then the values for V1 and V2 will be:

R = R1 + R2 = 2R1 or 2R2 (since the value is the same)

V1 = V * R1/2R1 = V/2

V2 = V * R2/2R2 = V/2

And using two identical value resistors we are dividing the voltage exactly in two.

Let’s now consider a more general case:

resistorsInSeries3

V = V1 + V2 + V3

I * R = I * R1 + I * R2 + I * R3

I * R = I * (R1 + R2 + R3)

R = R1 + R2 +R3

Potential drop between A and B

V1 = I * R1 = (V / R ) * R1

V1 = V * R1 / (R1 + R2 + R3)

Drop between B and C

V2 = V * R2 / (R1 + R2 + R3)

Drop between C and D:

V3 = V * R3 / (R1 + R2 + R3)

and we can extrapolate that for a system with n resistors, the voltage drop for one of them will be

Vi = V * Ri / (R1 + R2 + … + Ri + … + Rn)

What is the potential drop between B and D? This involves two resistors, R2 and R3 so the answer is:

VBD = V2 + V3 = V * R2/ Rtotal + V * R3/ Rtotal = V * (R2 + R3) /  Rtotal

And generalizing to resistors in series the voltage drop from resistor i to will be

Vi-n = V * (Ri + … + Rn) / Rtotal

A potentiometer is a voltage divider

resistorsinseries-pot

Consider the diagram of the two circuits above. The resistance with an arrow across is the symbol for a potentiometer. At any given point the slider, represented by the arrow is in a position in between the two extremes. If all the way to the left, then the resistance is zero and no voltage drop happens. If it is all the way to the right then the resistance is 10KΩ and the voltage drop is 9 Volts. Imagine that the slider is somewhere in between, in such a position were there is a resistance of 3.3kΩ to the left and about 6.7kΩ to the right. That is equivalent to the lower circuit in the figure. The voltage drop from B to C is:

VBC = V * 6800/10000 = 6.12 Volts

A potentiometer usually has three terminals, as suggested by its symbol. When connected to a reference Voltage on one of them and to ground on the other, the slider will have a voltage value that ranges from zero (0V) to the reference voltage.

Getting the voltage value in an Arduino board

The Arduino family of processors have several analog input ports, that are in effect a Digital to Analog Converter, that takes a given voltage, compare it to its own reference voltage, and convert the value to a number between 0 to 1023. This is what is called a 10bit DAC.

The following figure shows a potentiometer connected to a ProTrinket 3V3 board. This processor is the heart of the GlowSaber.

pot-with-trinket

This code will read the voltage reported in the port A0 and will communicate the value to the host computer:

#define LS_BATTERY_VOLTAGE A0
void setup() {
 Serial.begin(9600);
 analogReference(DEFAULT);
 pinMode(LS_BATTERY_VOLTAGE, INPUT);
}

void loop() {
 float voltage = 3.3 * analogRead(LS_BATTERY_VOLTAGE) / 1024;
 Serial.print('Voltage now is: '); Serial.println(voltage);
 delay(500);
}

Of course that this is a very trivial example, but based on this very simple circuit and code you can perform very complex operations. In theory you have a value between 0 and 1023 and you can make decisions based on that value. In practice I don’t think that you should use more than 10 different values, and for that you could use the map Arduino function:

void loop() {
  int aValue = analogRead(A0);
  int anotherValue = map(aValue, 0, 1023, 0, 10);
  ...
  switch(anotherValue) {
  case 0:
    doSomething();
    break;
  case 1:
    doSomethingElse();
    break;
  ...
  }
}