Thursday, December 17, 2015

Teensy USB Wii Classic Controller

I'm a fan of the Wii Classic Controller, but I've never cared for having to keep wireless controllers charged up for an occasional bout of nostalgia.  Wouldn't it be great if I could use my Wii Classic Controller over USB?  This is how I made it.

I opened up my controller with a tri-wing screwdriver and saw that the accessory port mechanism takes up a lot of interior space.  Once removed, the space looked about right to fit a Teensy LC.

I used an old Samsung phone USB cable like this one that is appropriately white to match the controller.  I split open the micro-B side with an x-acto blade.  After removing the outer plastic casing, the metal shield, and the connector parts, I carefully stripped away a tiny amount of insulation from each of the four wires.  The remaining end of the cable fits nicely through the hole left by the accessory port button.  The rectangular block at the end conveniently keeps the cable from pulling on the soldered wire connections.

Next I removed the controller cable.  The five wires shown below (red, yellow, green, white and black) are connected in a plastic jumper casing.  I unscrewed the circuit board from the controller case and flipped it over to desolder all five wires, removing the whole jumper and leaving just the bare circuit board to connect new wires to.

I connected the Teensy to the Wii controller according to the schematic below.  (The white wires are shown as grey.)  The USB cable has four wires:  black, green, white, and red.  Only four of the five wires from the Wii controller are necessary:  white, red, yellow, and green.  (The black wire is left open.)  As recommended I used 4.7K pull-up resistors for the I2C pins.

I threaded the USB cable thru the top of the controller case (thru the accessory port button hole).  I soldered the four USB wires to the side of the Teensy not seen in the picture below.  I soldered the resistors flush to the Teensy board and connected carefully measured wires to reach from the Wii controller port to the designated Teensy pins.

Note that I used a black wire instead of a white wire for the ground connection, but it connects to the Wii controller circuit board where I detached the white wire from.  If I were to do it again I would use thinner wires and/or remove some of the plastic between the analog joysticks to run the wires so that they could be more easily placed out of the way of the right trigger mechanism.  It's a tight squeeze, but I got it to work as shown.

When fully reassembled the edge of the Teensy board sits on the outside of the analog joystick board.  Here you can see the Teensy thru the accessory port slots.

The code can be downloaded here.  It simply reads in the controller status over I2C using a Wire library and maps the status to send out over USB using the Teensy Joystick support.

I found the following sources particularly helpful for this project.

Happy classic gaming!

Thursday, December 10, 2015

Arduino Code for Microtonal MIDI

See previous posts about my microtonal MIDI converter here, here, here, here, and here.

Here is the Arduino code.  Notes are below.

Seven Segment library:

As outlined in this earlier post I wrote a seven segment library with fast writing to digital pins.  It needs to be fast, or else it slows the MIDI processing down noticeably.

Microtonal MIDI library:

I also wrote a microtonal MIDI library that handles the input of MIDI note events and the output of n-TET microtonal note events.  To keep the code fast, this library filters out, i.e., does nothing with, MIDI commands or data that are not related to note events.

The code uses all 16 MIDI channels successively and cyclically to send pitchbend and notes.  This is because pitchbend affects a whole channel, and each microtonal note often requires a different pitchbend value.  It can be adjusted to use less channels, but the number of channels would be the number of available notes of polyphony.

I implemented a monophonic mode that retriggers the previous note when necessary.  When dealing with microtonal scales with more than 12 notes, in some cases the same MIDI note is used for adjacent microtonal notes with different pitchbend values.  If both notes are held on and then one released, when a note off command is sent for one of the microtonal notes on a monophonic synthesizer then both notes are turned off.  When monophonic mode is active the code retriggers the remaining microtonal note and pitchbend value, which is what you would expect when playing a monosynth.  The illuminated button toggles between retrigger being on for a monosynth and being off for a polyphonic synth.
I also implemented Pythagorean tuning in the code--mapped to the "00" steps per octave setting, but it is commented out because the required if statements caused noticeable latency.  Perhaps there is more optimization to do, or perhaps a faster microcontroller like on a Teensy would allow my code to work well enough.  Or, maybe stick with 53-TET for a good enough approximation of 5-limit just intonation. :)

Main code:

The main code includes interrupt handling for the rotary encoder and button.  I found this page and this page helpful for writing the interrupt code. 

The ISR handles all the rotary encoder and illuminated button functionality.  When an encoder is turned, the motion is mapped to a change in the steps per octave or base MIDI note, and those values are updated for the seven segment display.  When the button is pressed, monophonic mode is toggled on or off and the internal LED is toggled on or off to match. 

The main loop consists of essentially a call to handle one byte of incoming MIDI data and then a single flash of the appropriate LEDs in the seven segment display.

Wednesday, December 2, 2015

My Arduino Based Microtonal MIDI Converter

 It's alive!  See earlier posts for more details.

The 12 is the number of steps per octave, and the 60 is the base MIDI note number.  The lit up button means that it is in monophonic mode, which basically means it will retrigger the last note when necessary.  Pushing the button toggles the light off and sets it in 16-voice polyphonic mode and does not retrigger the last note.  More details about retriggering are to come when I post about the code.

Here you can see inside a little better without flash, but the LEDs wash out.  The parts list is below.  I used some LEGO bricks to wedge the main boards in place.

Another angle shows the MIDI out port.  The MIDI in port is on the left side.  I turned the encoder knobs to set it to 10 steps per octave and 62 as the base MIDI note number.  I hope to post video soon to show it in action.

Here is a parts list:

Monday, November 30, 2015

Momentary Push Button Debouncing Circuit and LED with Arduino

Further testing of my Arduino based microtonal MIDI converter revealed that for a monophonic synthesizer extra code is required to perform last note retriggering when more than one key is pressed and the keys pressed map to the same MIDI note number (but different pitchbend values).  So I decided to use a momentary push button to toggle between monophonic and polyphonic modes along with an LED to indicate the two modes.

I acquired one of these buttons with a built-in LEDThis page describes well why debouncing is necessary and provides a possible circuit about 2/3 of the way down.  I implemented it according to the below schematic.  The time for this debouncing circuit to discharge is directly proportional to the (external) resistance and the capacitance.  So I chose the largest capacitor in my box of parts: 100 pF, and I chose an (external) resistor value less than the 20 K internal pull-up resistor but still large:  10 K.  These values worked well, so I kept them.

I connected the LED using the below schematic with a 220 ohm current limiting resistor as recommended by adafruit.

Friday, November 20, 2015

Four Digit Seven Segment Display and Port Manipulation with Arduino

The next enhancement for my Arduino based microtonal MIDI converter is to add a 4-digit seven segment display to show the values to be changed with encoders.  I decided to use the first two digits to display the number of steps per octave and the last two digits the base MIDI note.

I acquired a four digit common anode seven segment display.  According to the datasheet pins 6, 9, 10, and 12 are the digit pins.  These pins are used for selecting each of the four digits.  The other 8 pins are for the seven segments plus a decimal point.

I put current limiting resistors on each of the digit pins because there are only 4.  (I would need 8 resistors if I put them on the segment pins.)  The data sheet specifies a max steady current of 25 mA with a typical 2.2 volts forward.  I used 220 ohm resistors.  Then using Ohm's law the resistors will limit the current to (5 - 2.2) volts / 220 ohms = 12.7 mA, which is within the acceptable range.

I connected the seven segment pins to the 12 digital only Arduino pins 2-13.  I simply connected them in an order that made it easy to wire up.  Then using the pin mapping from the data sheet I specified the Arduino pin mapping in my code with a bunch of #define statements.  I figured it is easier to adapt the code than to have a tangled mess of wires running between the display and Arduino.

// arduino pin numbers for the seven segments and decimal point
#define PIN_A  12
#define PIN_B  8
#define PIN_C  4
#define PIN_D  6
#define PIN_E  7
#define PIN_F  11
#define PIN_G  3
#define PIN_DP 5

// arduino pin numbers for the 4 digits
#define PIN_1 13
#define PIN_2 10
#define PIN_3 9
#define PIN_4 2

I originally started out using the SevSeg library from  It worked well for displaying numbers, but I found that it introduced a significant amount of latency with all the digitalWrite() calls--7 segments plus decimal point times 4 = 32 total.  So I decided to write my own code to write to the pins more efficiently.

For that I needed to learn about port manipulation.  The two data registers PORTD and PORTB map to Arduino pins 0-7 and 8-15, respectively.  When the pin mode is set as OUTPUT, then writing a 0 or 1 to these registers results in setting the pin LOW or HIGH.  So I wrote the following code to quickly set pins HIGH or LOW by writing directly to the corresponding PORT without any error checking.

pinHigh(int pin) {
  if (pin >= 2 && pin <= 7) {
    PORTD |= 1 << pin;
  else if (pin >= 8 && pin <= 13) {
    PORTB |= 1 << (pin - 8);

pinLow(int pin) {
  if (pin >= 2 && pin <= 7) {
    PORTD &= ~(1 << pin);
  else if (pin >= 8 && pin <= 13) {
    PORTB &= ~(1 << (pin - 8));

Thursday, November 19, 2015

Reading Quadrature Code Rotary Encoders with Arduino

Keeping with the theme of enhancing my Arduino based microtonal MIDI converter, I want to add some controls to be able to change values on the fly like the number of steps per octave and the base MIDI note.  So I acquired these Bourns rotary encoders.

I found this Circuits@Home blog post about reading rotary encoders helpful, although I found it a little hard to follow as someone new to the world of Arduino.  I will try to further explain some of the details below.

The post mentions that encoders generate quadrature code.  I copied the quadrature code output pattern below from the data sheet for my encoders.  It shows that when rotating clockwise the states of the AB signal output repeatedly cycle thru 00, 10, 11,  and 01.  (The order is reversed for counterclockwise.)

So then the code from the above referenced post (snippet below), broadly speaking, what it does is store 4 relevant bits in old_AB:  the previous encoder reading (00, 10, 11, or 01) followed by the current encoder reading (also, 00, 10, 11, or 01).  The array enc_states[] contains three possible responses to encoder readings:  0--no change, 1--increment for CW, and -1--decrement for CCW.

/* returns change in encoder state (-1,0,1) */
int8_t read_encoder()
  static int8_t enc_states[] = {0,-1,1,0,1,0,0,-1,-1,0,0,1,0,1,-1,0};
  static uint8_t old_AB = 0;
  old_AB <<= 2;                   //remember previous state
  old_AB |= ( ENC_PORT & 0x03 );  //add current state
  return ( enc_states[( old_AB & 0x0f )]);

So, for example, if the previous reading was 00 and the current reading is 10, then (binary) 0010 maps to enc_state[2] = 1, which means increment because the encoder moved clockwise.  Another example, if the previous reading was 00 and the current reading is 11, then there is a read error or noise so that maps to enc_state[B0011] = 0--no change.  The reader can work out the other 14 possible combinations of previous and current readings to see how they map to the enc_states[].

Wednesday, November 18, 2015

Learning more about MIDI and Arduino

Ever since I made this Arduino based microtonal MIDI converter I have been working on improving it.  This instructable that covers Sending and Receiving on MIDI with an Arduino was a good place to start learning about MIDI, but it only scratches the surface.

One of the problems I encountered was how to handle the MIDI System Real-Time messages that one of my MIDI keyboards sends.  MIDI-OX was useful for monitoring the messages, except that I was not always sure about the messaging it shows.  I later learned that it masks "running status"--more below.

So I wrote code that uses the Arduino to monitor the MIDI input and writes it out to the Serial monitor.  I had to use a software serial port for MIDI since the MIDI 31250 baud rate is not supported by the Serial monitor.  This way each serial port can have a different baud rate.  The code below screens out any MIDI commands other than "note on" or "note off" by only printing out the commands and data below 160, but it is also instructive to build it to not screen out any commands.  See this summary of MIDI messages for all the possible MIDI values.

#include <SoftwareSerial.h>

// receive on pin 7 and transmit on pin 8
SoftwareSerial softSer(7, 8);

int data = 0;

void setup() {

void loop() {
  if (softSer.available())
    data =;
    // screen out commands other than note on or note off
    if (data < 160) {
      Serial.println(data, DEC);

Another piece of the puzzle that I needed was the concept of "running status."  This means that a successive duplicate command, like "note on," can be skipped and just the data can be sent.  So, for example, when playing a 3 note chord, instead of sending 3 "note on" commands with 3 corresponding note and velocity pairs--9 bytes total, instead just 1 "note on" command is sent and then 3 pairs of note and velocity--7 bytes.  Also, "note on" commands with zero velocity can be used instead of "note off."  This means if you're only playing the keys, then a MIDI keyboard could send only 1 "note on" command and pair after pair of note and velocity bytes.  This makes for a more efficient serial communication rate and reduced latency.

My final way to enhance the MIDI portion of my original Arduino device is to build the hardware from components instead of using a Linksprite MIDI shield.  The advantage of doing this is that it can be added onto a proto-board with other functionality like encoders and a display, and panel mounted MIDI ports can be usedBelow is a schematic of the circuit I used.  For reference, here is the official electrical specification.  Note that the it calls for a resistor connected to MIDI out pin 5I found that the resistor reduces the voltage level of the transmitted data enough that the receiving end could not read the it, so I omitted the resistor.