Bit crushing, rate reducing, weird noises: DIY 10-bit effects/guitar pedal with an Arduino for lo-fi DSP.
Check out the demo video on Vimeo.
Step 1: Materials
- Arduino (Diecimila, or with auto-reset)
- Soldering iron
- Hot glue gun
- Wire cutters
- Drill press or dremel
- Hot glue
- Perf board
- Audio jacks (I used 1/8") (x2)
- Interface inputs, e.g.: 3 potentiometers
- Interface outputs, e.g.: 3 LEDs and 3 150 ohm resistors
- Resistors: 1 k, 10 k, 1.2 k (x2), 1.5 k, 390 k
- Capacitors: 2.2 uF (x2)
Step 2: Preparing the Enclosure
I used a "fast ethernet media converter" for my enclosure. It's just a box that happens to fit the Arduino, some interface elements, and two audio jacks. It's pretty sturdy metal, which is important for a pedal. As an added bonus: it has a hinge on the back, which makes it easy to open and close.
The only modifications I had to make to this enclosure were some holes for the three pots (using a drill press) and cutting out some plastic for the USB connector.
Step 3: Connecting the Components
- Situate the Arduino
- Install any of your interface components, like pots or LEDs
- Install your input and output jacks
We'll be modifying the analog reference value, so if you want to use any pots you have to connect them to the AREF pin instead of the usual 5V pin.
For the 1/8" connectors (or anything that isn't on the same panel of the enclosure as the Arduino) be sure to use flexible wires. Otherwise it will be hard to close the case and the joints might break or other connections might come loose.
Step 4: Normalize the Input and Output
This is the only real "trick" when it comes to the hardware for this system. Audio happens as an AC signal from -1 V to +1 V, but the analog inputs on the Arduino run from 0 V (Ground) to some positive voltage called the analog reference (5 V by default). You can specify this positive voltage in code or with an external reference.
Since -1 V to +1 V is a 2 V range, we'll choose something smaller than 2 V for our analog reference value. It turns out 1.1 V is specified as a built in internal reference, which works out nicely.
From here we have to normalize the -1 V to +1 V as 0 V to 1.1 V. I did this using a resistor in series followed by a voltage divider circuit. A guitar cannot directly drive this circuit, it needs a preamp (like another pedal) to drive it. I'm sure you could add a transistor or op-amp preamp to the perf board so you could plug into the pedal directly.
For the output, we're going to be using PWM. With some low level hacks in software, you can get an 8-bit PWM running at 62kHz = 16 MHz / 28
There are some other methods for audio output on the Arduino. A good overview can be found at uC hobby. I got some great results from an R2R DAC, but considering it needs around 40 resistors for 10-bit stereo output, I decided against it. Instead I went with the weighted pins technique, which is kind of like a cross between standard PWM and a resistor ladder.
Building the Circuit
I built two of each circuit on one perf board. I had a ground strip down the center that helped for arranging things as neatly as possible. The first time I built the circuit, it was too tall and didn't fit in the enclosure, so I had to build it again.
When you have capacitors in series like this, they will cut off some of your low frequencies. WIth a 2.2 uF capacitor, it's low enough so it doesn't really affect sounds in our hearing range. The larger the value, the better; but capacitors tend to get physically larger as their value increases.
Step 5: Connect Remaining Components
- The audio input goes into the input circuit, then into the Arduino's analog inputs
- The pot taps go to the Arduino's analog inputs
- Two LEDs go to PWM outputs, one goes to a digital output
- Four PWM outputs go into the 8/2-bit weighted DAC circuit
- The output of the weighted DAC circuit goes to the audio output
Step 6: Upload DSP Code
The includes in ArduinoDSP are useful for setting the proper PWM prescaling and analog input prescaling values. They turn pins 3 and 11 into the left outputs (8 and 2 bits respectively) and 5 and 6 into the right outputs, using Fast PWM settings with no prescaling so the PWM is as fast as possible. The ADC is also set to a low prescale value, 32, and the analog reference is set to 1.1 V (the internal reference).
To modify the basic ArduinoDSP code, just insert your own code to modify the variable "input" between the line "short input = analogRead(left);" and "output(left, input);".
The code I've already written does a few things. The LEDs provide visual feedback about the knob positions and input level, and the pots control settings for the DSP happening inside the microcontroller. The first pot controls the mode, the second controls a parameter of that mode, and the third controls an effective sample rate. The modes include:
- Bitcrush: bit shift the input to the right and then to the left, chopping off N bits.
- Bitshift : shift the input to the left, leading to a weird effect for the first few values and then eventually noise (i.e., the "dithering bits").
- Overdrive: Multiply the input by a float from 1-20.
- Binary impulse response: do various binary operations on the input and the last result (XOR, NOR, XNOR, NAND...)
Step 7: Variations and Notes
- Add an RC low pass filter on the output with selectable corner frequency
- Weirder modes: bit remapping? bit rotation?
- Repeat last N samples? This is heavily limited by the ATmega's RAM.
- Run off 9V wall adapter instead of USB power
- 6 8-bit outputs running into a 5.1 speaker system?
- Use an Arduino mini for a super small pedal
- Patch bay as an interface?
- Input volume knob
Because the ADC is really the primary bottleneck in this setup, any alternative ADC methods could be really helpful (there's a great reference of alternatives here, though ultimately it'd be easiest to use a dedicated ADC chip via SPI instead of implementing these manually). As the system is now, it's best to stick to mono inputs if you want to retain fairly accurate output.
Thanks to Andrew Armenia for help with input normalization, Dane Kouttron for explaining a few things about PWM on ATmega168s, James Miglietta for assuring me that guitar pedals run at normal audio voltages, and Blair Neal for wanting a bitcrusher/sample rate reducer in the first place.
Another great technique using an audio buffer and doing "real" effects has been demoed by Martin Nawrath.
I think one of the biggest benefits of Martin's approach is that he has an interrupt for the ADC sampling. Normally, the ADC is called in a blocking way using analogRead() (i.e., the code doesn't go past analogRead() until the conversion is made). Martin's technique frees up the code to do other things while the ADC is being done.