the wondrous tales of a software guy in hardwareland


In the last two weeks, I learned a lot. I can proudly say that I’m a good 0.002% less ignorant than before.

So, here my hard-earned life lessons for your advancement on the path to enlightenment.

1. Get your filthy hands off my pins

This is clearly stated in the Arduino documentation, but I overlooked it completely. Digital pins 0 and 1 are off-limits, taboo, verboten, no-go. They are used by Arduino for serial communication. I had a button connected to pin 1, and got all kinds of erratic behaviour until I commented out all the Serial code I had…

But I’m not going to give up Serial: it’s too useful for debugging purposes, and I plan to use it also for the sequencer itself. For dumping and loading patterns to and from USB, and hopefully also as a MIDI in through the computer.

This means 2 digital pins less. Sigh. But don’t worry! Read on and you’ll see that this is less of a tragedy than you may think.

2. A saucerful of SPI

I also totally misunderstood how SPI works. It’s true that the pins to use are hardcoded and can’t be changed. But this doesn’t mean you can’t have more than one SPI chip connected! And you don’t need another full set of pins either, just one more!

Let me try to explain. The pins assignment for the SPI interface is:

13 (SCK): this is the clock signal for the data exchange PWM.
12 (MISO): data from the chip to Arduino.
11 (MOSI): data from Arduino to the chip.
10 (SS): slave select. This pin must be LOW to tell the chip we are reading or writing data.

The trick is that pins 11, 12 and 13 can be shared among different chips! Every chip needs just one dedicated SS pin. You have to remember to set LOW the right SS pin for the chip you are talking to (and set it back HIGH when you’re done). Other chips on the same SCK/MISO/MOSI pins will not notice, unless their SS pin is HIGH. Brilliant!

This means I only need 4 pins for the 2 DAC chips (I don’t need MISO, since I’m not reading anything from a DAC). And I can control both pitch and cutoff of the monotron. Yay!

This is going to be the schematic for the DAC part:

Untested, but my gut feeling is that it will work. Unless I misunderstood something else again :-)

3. Several species of small furry buttons gathered together in a cave and grooving with an analog pin

I also realized that, in the end, my worries about running out of digital pins were basically moot.

I knew already that you can use analog pins as digital pins. And that you can use one analog pin for multiple digital inputs (at least, pushbutton inputs). In fact, I was planning to add 4 pattern select buttons on a spare analog pin anyway.

And then it struck me (the reason will be evident in the next chapter) that with this technique I can have tons of buttons at my disposal! More buttons that would fit on my perfboard, in fact.

It’s just a matter of picking the right resistors, and you can have 10, 15, maybe even 20 buttons for each analog pin. There is one limitation, of course: you can’t read every button state independently as with separate digital pins. In general, only one button per analog pin can be read simultaneously.

But for my project, this is not a problem, no sir. It’s a monophonic sequencer after all, I don’t need to play chords or something.

I did a quick breadboard test with seven buttons on a pin and 1k resistors, and indeed it works flawlessly. I would have tested more, but… guess what? I need to buy more buttons. And more resistors. Good grief, when will I stop buying stuff? :-)

4. The dark side of the prototype

Most important of everything, I realized that after all I don’t like my prototype.

At first I was focused on getting the code to work, which it does. But after some hands-on tests on the prototype, I became painfully aware that the user interface I envisioned is far from usable. Setting up a pattern is painfully slow and tedious and error-prone (I kept turning the wrong knob again and again!). Furthermore, the idea of entering notes with micromillimetric turns of a knob is just flawed.

To make a long story short, I’m radically changing my project. It was very good (and a good amount of fun anyway) to prototype it, because it really showed me how things work. I also learned a lot of small things I didn’t consider. For example, it is very important to have a visual distinction of the 16 steps in blocks of 4 (like the Roland drum machines, where you have 4 groups of 4 buttons in different colors), otherwise you get easily lost.

So, considering also my recent discoveries from the previous chapters of this post, I’m going to build something which will be functionally similar to an actual TB-303. With a full blown octave keyboard (12 buttons), plus some more for editing purposes, and just a few knobs to turn.

This is going to be slightly harder than what I initially planned, and I will need to plan carefully the perfboard layout to optimize the space and avoid ending up with a total mess of cables soldered here and there. For now, I just have laid out the Arduino pin assignment as follows:

D0 Serial
D2 tone()
D3 to monotron's gate
D4 LCD display
D10 SPI SS DAC pitch
D11 SPI MISO (both DACs)
D12 SPI SS DAC cutoff
D13 SPI SCK (both DACs)
A0 BPM knob
A1 cutoff knob
A2 glide knob
A3 length knob
A4 keyboard (12 buttons)
A5 rest button + step inc/dec + octave up/down
A6 start/stop + menu button
A7 patterns 1..8

Phew. I'm basically using all available pins, just more judiciously this time :-)

It buggers me a little that I still need to buy parts, and I will probably not be able to prototype everything on breadboard (it's going to be big, and with way too many cables). But I will test the single sub-circuits (keyboard, DACs, etc.) and I hope that the final "product" will work fine.

That's basically everything. If you find some of this useful, or spot other errors or false assumptions in my reasoning, please leave a comment below. That's what blogs are supposed to be for, isn't it?


, , , ,

Comments are currently closed.