Arduino “Make Your Uno” Synth – 1 – Simple Tones

Having now built my synth board, I’m going to start to talk about some of the projects that I can do with it.

The full index of projects and my personal build notes can be found here: Arduino “Make Your Uno” Synth.

This post contains the following experiments:

  • Controlling the frequency of a tone with a potentiometer.
  • Choosing a note to play with a potentiometer.
  • Playing a tone melody.
  • A simple four-step tone sequencer.

Warning! I strongly recommend using old or second hand equipment for your experiments.  I am not responsible for any damage to expensive instruments!

These are the key Arduino tutorials for the main concepts used in this project:

If you are new to Arduino, see the Getting Started pages.

Parts list

  • Arduino “Make Your Uno” Kit
  • USB-C programming lead

The Circuit

We’re going to initially make use of A0 (circled below) and the speaker (so the volume pot too of course).  The last experiment will use all 5 potentiometers connected to the Arduino and the volume control.

Arduino Synth Shield - Tones

Notice that I’ve labelled my potentiometers.  It is worth doing as a reminder as the ordering is a little odd on first glance!

The first experiments using A0 are basically the equivalent of the following circuit.  Note that for the purposes of illustration, I’ve replaced the amplifier with a simple connection to a loudspeaker via a resistor – which would work fine too if you don’t have a Synth Shield yourself.

MakeYourUno-Tones_bb

Experiment 1 – Tone Generator

The first experiment will use the A0 pot to control the frequency of a tone produced using the Arduino tone() function on the speaker pin (connected to D9).

The basic code is as follows:

int potPin = A0;
int SPEAKER = 9;

void setup() {
}

void loop() {
  int potReading = analogRead (potPin);

  if (potReading == 0) {
    noTone (SPEAKER);
  } else {
    int pitch = map(potReading, 0, 1023, 120, 1500);
    tone (SPEAKER, pitch);
  }
}

Now if you turn up the volume slightly (not too loud) and fiddle around with the pot on A0 you should start to hear a continuous tone with its frequency dependent on the setting off the pot.  Turn clockwise to increase frequency and anti-clockwise to decrease. The frequency range is 120Hz to 1500Hz.  Fully anti-clockwise (reading 0) with turn the tone off completely.

There is a detailed discussion about how it works here: Arduino Tone Generator.

Download the code from GitHub here.

Experiment 2 – Note Generator

This is a variation on the above experiment but the synth will only play frequencies that correspond to notes in the typical Western chromatic scale.  Again turn clockwise for higher notes and anti-clockwise for lower.

This sketch includes a list of note frequencies and now uses the potentiometer to choose one of the notes from the table.

The main code is as follows (abridged):

int potPin = A0;
int SPEAKER = 9;

#define NOTE_B0 31
#define NOTE_C1 33
//... and so on

int notes[] = {
  NOTE_B0, NOTE_C1, //... and so on
};

void setup() {
}

void loop() {
  int numNotes = sizeof(notes)/sizeof(notes[0]);
  int potReading = analogRead (potPin);

  if (potReading == 0) {
    noTone (SPEAKER);
  } else {
    int pitch = map(potReading, 0, 1023, 0, numNotes-1);
    tone (SPEAKER, notes[pitch]);
  }
  delay(1);
}

Once again turn up the volume slightly and fiddle about with the first potentiometer.  The full code will cover the notes from B0 (~30Hz) up to D#8 (~5kHz) which is over 7 octaves.

There is a detailed discussion about how it works here: Arduino Note Generator.

Download the code from GitHub here.

Experiment 3 – Tone Melody

This is a variation of the standard Arduino “Tone Melody” example (which can be found here).  I’ve based this on the melody I used for my Raspberry Pi Pico “hello world” (with full apologies to the great J.S.B!).

This uses A4 as a tempo control, but otherwise everything is “hard coded” into the sketch. Note that this can only play sequences where each note is the same step, and two notes in consecutive steps will sound like a single note.  A step can contain 0 though for “no note”.

int tempoPin = A4;
int SPEAKER = 9;

#define TEMPO_MIN 30 // 1 every two seconds
#define TEMPO_MAX 480 // 8 a second

// Frequencies for each note are taken from toneMelody
#define NOTE_B0 31
#define NOTE_C1 33
//... and so on

// Provide a list of the notes we want to play in the sequence.
// All notes are the same length.
//
int notes[] = {
NOTE_C4, NOTE_E4, NOTE_G4, NOTE_C5, NOTE_E5, NOTE_G4, NOTE_C5, NOTE_E5,
//... and so on
};

int numNotes;
int playingNote;

void setup() {
  numNotes = sizeof(notes)/sizeof(notes[0]);
  playingNote = 0;
}

void loop() {
  int tempo = map (analogRead(tempoPin), 0, 1023, TEMPO_MIN, TEMPO_MAX);

  if (playingNote >= numNotes) {
    noTone (SPEAKER);
    delay (5000);
    playingNote = 0;
  }

  if (notes[playingNote] == 0) {
    noTone (SPEAKER);
  } else {
    tone (SPEAKER, notes[playingNote]);
  }

  delay (60000UL/tempo);
  playingNote++;
}

The tempo is expressed in beats per minute (as is usual in music).  To convert to milli-seconds, means dividing 60,000 (60 seconds in milliseconds) by the required tempo. This is then fed straight into a delay() call to provide the required pause on that note.

Download the code from GitHub here.

Experiment 4 – Note Sequencer

This is a slightly simplified version of the sample application suggested in the Synth Shield tutorial.  It is a basic 4-step tone sequencer playing the notes from experiment 2.  The tones for each step are set by the potentiometers A0 to A3 and the tempo is set by A4.

Features:

  • Set the bpm from 30 to 480.
  • Uses discrete notes from B0 to D#8 for the tones.
  • Maps the potentiometers to steps to fit the order on the shield – left to right top row, then left to right bottom row. (A0 -> A2 -> A1 -> A3 -> A0).

There is a detailed discussion of how it works here: Arduino Tone Step Sequencer.

Download the code from GitHub here.

Closing Thoughts

This is just getting started, but for this first set of experiments I wanted to include some basic tone() experiments that could be performed just using the components in the kit itself.

Kevin

Leave a comment