How to output 8-bit music with Arduino


I'm making a music player with Arduino. I chose to make 8-bit music (square, triangle and noise waveforms), that coded like "turn on square at frequency 262". However, when it comes to playing music, all I have found were tone() examples, playing PCM samples or some complex code with no explanation.

So, if you know how to output 8-bit music, please reply to this post.

Thanks in advance,

Hal Chamberlin wrote Byte magazine articles in the 1980s for generating multi-tonal music using 8-bit processors and DACs.

of course you need to output the samples at a constant rate

Chamberlin described creating a sine table in RAM which was fractionally incremented thru to sinusoidal waveforms. multiple sinsoids can be combined.

an integer and fraction bytes were used to generate a 16-bit index into the table, only the integer part used to read a table value. the accumulated fraction occasionally skipped an integer step thru the sine table

there would be separate tables describing what tone(s) to generate. that table would be indexed thru at a much lower rate than the samples to change tones. of course, you may need several tables to generate music phrases and yet another mechanism to determine which/when phrases are played

Well, there’s this: Arduino Chiptunes : 8 Steps (with Pictures) - Instructables

I don’t know how much to trust the author. They put in a “main()” and left out setup() and loop(). Their main() didn’t call ‘init()’ or ‘init_varient()’ so the Arduino library won’t be properly initialized.

They also have this comment:

  TCCR0B = 0x02; // clkIO/8, so 1/8 MHz

Since the normal clock rate on an UNO is 16 MHz, a prescale of 8 will give 2 MHz, not 1/8th MHz! Maybe they were using a factory-fresh ATmega328P?!? They run at 1 MHz until you burn the Arduino bootloader which enables the 16 MHz crystal oscillator.

Anyway, it might be a reasonable place to start. I cleaned it up to make it a little more Arduino Friendly and it seems to produce music on Pin 3.

I cleaned it up to make it a little more Arduino Friendly and attached it below.

Chiptune.ino (23.6 KB)