Arduino Squarewave Generation

Yesterday I came across this: Secrets of Arduino PWM by Ken Shirriff and Paul Badger. It has a brilliant explanation of some of the different modes and functions of the Pulse Width Modulation timers on some popular ATMega / Arduino chips. I think anyone having struggled with the datasheet will appreciate this article immensely.

My main interest was in generating a variable audio-frequency square wave, for obvious reasons: to make noise! This is something I’ve not been able to figure out before. The Auduino approach is great, and makes great sounds, but uses a different approach altogether – granular synthesis with a fixed frequency PWM timer interrupt.

Ken explains how PWM frequency can be controlled by two means: the timer prescaler and the counter top limit. The prescaler divides the clock frequency by a fixed value of 1, 8, 32, 64, 128, 256, or 1024. The top limit sets a value which the counter counts up to before restarting. Anyhow, no need to restate what’s already in the article – the upshot is that we can have coarse and fine control over PWM frequency.

The above table shows the frequency (y axis, log scale) versus top limit (x axis) for different prescaler values, for Timer 2 phase-correct PWM Output A (pin 11). Output A has a fixed duty cycle of 50%. Output B (pin 3) has 2x the frequency, and has adjustable pulse width.

To try it out I hooked up an arduino to some pots and an audio jack.

The code is super simple:

/*
  Wire up pots to analog inputs 5, 4, and 3.
  Output on pins 3 (pulse width modulated)
  and 11 (1/2 frequency at 50% duty cycle).
  Wire output pins and gnd to a pot for volume control.

  Frequency calculation
  pin 11 = 16MHz / prescaler / limit / 4
  pin 3  =  16MHz / prescaler / limit / 2

  see http://arduino.cc/en/Tutorial/SecretsOfArduinoPWM
*/

#define DIV_PIN   5
#define FREQ_PIN  4
#define DUTY_PIN  3

void setup(){
  pinMode(3, OUTPUT);
  pinMode(11, OUTPUT);
  TCCR2A = _BV(COM2A0) | _BV(COM2B1) | _BV(WGM20);
}

// accepts values 1-7 to set prescalers 1/8/32/64/128/256/1024
// 0 turns timer off
void setPrescaler(uint8_t divisor){
  if(divisor > 7)
    return;
  TCCR2B = _BV(WGM22) | divisor;
}

// expects a value 0.0-1.0
void setDutyCycle(float value){
  OCR2B = OCR2A * value;
}

void setTopLimit(uint8_t limit){
  OCR2A = limit;
}

void loop(){
  setPrescaler(analogRead(DIV_PIN)>>7);
  setTopLimit(analogRead(FREQ_PIN)>>2);
  setDutyCycle((float)analogRead(DUTY_PIN)/1023.0);
}

Updated!

I couldn’t help myself, I went ahead and coded up a MIDI handler which turns an unsuspecting Arduino into a squarewave hardware synthesizer. The code is available on github. You can use MidiSerial to enable a MIDI interface communicating over the Arduino serial line.

The SquareWaveSynth responds to all MIDI notes from 0-127, but has troubles rendering some of them. It would no doubt be better to use the 16 bit Timer 1 instead of Timer 2. It also responds to mod-wheel changes (cc 1) which changes the duty cycle, from 50% to 0.

Here it is playing the theme to Ghouls and Ghosts (well, one voice of it!) with the help of Ableton Live.

Posted in Hacking, Hardware | Tagged , , , , , | 2 Comments

Lasercuts


I spent an hour or two today getting my head around the downstairs laser cutter. After a couple of test cuts on card, and figuring out that everything needed to be scaled down by about 1.5%, I managed to cut some BlipBox end plates. And it was actually remarkably easy. Materials used were 4mm transparent perspex and 5mm black ABS.

Previously I’ve manually milled the holes for connectors et c in the stock end plates, which are moulded ABS and wrap snugly around the ends. I wasn’t too sure how sheet plastic would turn out. Turns out just fine! All that is needed after layz0ring is a quick countersink of the screw holes, and it all fits perfectly. After that 1.5% re-scaling.

Posted in BlipBox, News and Updates | 2 Comments

Blip of Life

Today’s lunch break project was this: implement Conway’s Game of Life on the BlipBox using Processing.

The code is very simple, and since the game area is small it doesn’t have to be terribly efficient.

Functionality: At any time new cells can be drawn on the screen.

The iteration speed is controlled by dragging over the first dot column. Press bottom left corner to stop/start. When stopped, the display is inverted.

Posted in BlipBox, Software | Tagged , , , | Leave a comment

It’s Alive!

After a long period of testing and refactoring firmware and hardware, it’s now official: it works!
The latest version of the PCB, v9, uses transistors to switch the LED current. This leads to a more stable operation, brighter display, and less strain on the microprocessor’s output pins since they don’t have to drive the LEDs directly.
And the problem with flicker? Fixed!

 

100nF capacitor across GSCLK and IREF pins on TLC5940 ic

 

The solution, as previously mentioned, is a capacitor across two pins on the LED driver. Why exactly this works is anyone’s guess. But it does, and after much testing we’re satisfied it is a robust solution.

Posted in BlipBox, News and Updates | Tagged , , | Leave a comment

MidiSerial Updates

As it turns out the initial release of MidiSerial had some problems sending messages to the Arduino. It seems the FTDI virtual comm port drivers are a bit particular about the termios settings they expect – at least my simplistic approach to configuring a raw serial connection was not good enough.
Having come across this excellent article on todbot I have now managed to sort the problems out.
Up-to-date source code is available on our git server and github.
You can also download a Mac OS X Universal binary from here: MidiSerial.

Posted in MidiSerial | Leave a comment

New PCBs!

The new PCBs have arrived, hurrah! and I’ve obviously soldered one up to try it out.

First results were a bit mixed: the transistor design works, as would be expected, but the LEDs are definitely flickering even with this configuration.

This is all a bit weird since the exact same circuit design when breadboarded has no flicker at all.

What I found was that adding a capacitor (100nF between GSCLK and IREF on the TLC5940) completely stops the flicker, and increases the intensity of the display. With the extra cap, the board works beautifully and seems completely stable.

Why and how the capacitor fixes the flicker is a bit of a mystery.

I’m in the process of refactoring and improving the firmware, including the communications protocol, in order to have more low-level control over the device. This should help in figuring out exactly what is happening.

Posted in BlipBox | Leave a comment

Happy Birthday BlipBox!

And happy Women’s Day!

While migrating the BlipBox codebase from CVS to git I realised that it is 2 years to the day that the repository was created. So it’s our birthday!

The new git repository is available here though there will still be some configuration going on while we settle in.

Posted in BlipBox | Leave a comment

New Blog Home

Welcome to the new home of the BlipBox Blog, now transmitted to you direct from Rebel HQ in Hackney, London.

Along with the hosting change we are planning a number (indeed; a host) of updates and improvements. Over the next couple of weeks we will publish more content, schematics, documentation and source code. And we are hoping you will find it, to the greater part, nicely presented and easy to navigate.

Posted in News and Updates | Leave a comment

MidiSerial Utility

Since a long time I’ve been somewhat frustrated with how difficult it is to use MIDI with Arduino-based devices when connecting them to a computer by USB. If the ‘duino implements the MIDI protocol, basically I want to be able to set it up as a MIDI device to communicate more or less directly with other MIDI based software on my laptop. But because the FTDI USB drivers present the Arduino as a serial device, and there seem to be no utilities available to convert serial to and from MIDI, this has been quite impossible.

Well, no longer so. I’ve written a small command-line utility which connects a MIDI device to a serial port. Furthermore it lets you connect using non-standard serial speeds such as the MIDI standard of 31250 baud.

MidiSerial v1
usage:
-p FILE set serial device
-s NUM set serial speed
-v print messages sent/received over serial
-l list MIDI input/output devices
-i NUM set MIDI input device
-o NUM set MIDI output device
-c NAME create MIDI input/output device
-h or --help print this usage information

This is actually extremely useful for MIDI DIY-ing. If you wire up a DIN plug to the Arduino you’ve got an instant MIDI out port (see for example Todbot’s Spooky project), but since MIDI runs on 31250 baud you are out of luck with USB – most applications, including the Arduino IDE and Processing, only accept standard serial speeds, and MIDI applications still don’t do serial.

The utility is written using POSIX termios for serial comms, and the cross-platform JUCE library for MIDI, and should compile on Linux and Windows as well as Mac OS X. I’ll try to make a Linux build available soon. It should work fine with any serial connection, not just Arduinos.

Ah, so while writing this I’ve made the inevitable discovery that someone’s beaten me to it – in fact there seem to be several utilities doing pretty much the same thing listed at arduino.cc. Well there doesn’t seem to be a Mac version around so maybe there’s room for another.

Another discovery I’ve just made is that cvs.pingdynasty.com is, um, not showing up the project, or anything else for that matter. I’d better get on that, then. In the meantime, get the source code from here, and a Mac OS X 10.6 i386 binary here.

Posted in Software | 1 Comment

New year, new PCBs

Okay so it’s February already, but things are finally moving again with the project and a few new PCB’s are on order.

The previous design that we tried used a MIC5891 serial-input latched source driver. This seemed perfect on paper, but turned out not to work at all with 5v USB power because the IC drops too much voltage to drive the LEDs. Dang! And having at least the option to power by USB only is something we don’t want to sacrifice.

The new design by contrast uses discrete transistors to drive the LEDs, which are multiplexed by the microcontroller. This design can take a separate, higher input voltage, but also works fine with 5v. At least it does on the breadboard, we’ll have to wait and see how the PCB behaves!

Delivery is due in early March, news to follow shortly thereafter.

Posted in BlipBox | 2 Comments