Arduino Step Sequencer: How to program a step sequencer with Arduino

Reading Time: 4 minutes


In this tutorial we show how to program an Arduino Uno board to realize a step sequencer.

The circuit used for this project is available on the web pageĀ of our Step Sequencer prototype.

/*
  ++ 6 STEP SEQUENCER ++
 
  DESCRIPTION:
  Reads values from 6 potentiometers to change the pitch of any step of the sequencer tone generator.
  Change the pitch of each step with a potentiometer.
    
  CIRCUIT:
  Connect a speaker or piezo to pin number 13 and to ground;
  Connect the first lead of a potentiometer to 5v and the last to ground, the one in the middle to Analog Input pin 0;
  (Repeat for all the four potentiometers connecting the middle lead to Analog Input pins 0, 1, 2, 3, 4, 5);
  Connect a switch from digital port 3 and ground, put a 220 Ohm Resistor between the digital port 3 and 5V;

  For more information follow this link: https://www.artislab.it/it/step-sequencer/
 
  This example code is for educational purpose and it is in the public domain.
  Author: Costantino Rizzuti 2016
  Derived by a previous sketch made by Alessandro Contini & Paolo Cavagnolo
  
*/

// Tones table definition
#define NOTE_B0  31
#define NOTE_C1  33
#define NOTE_CS1 35
#define NOTE_D1  37
#define NOTE_DS1 39
#define NOTE_E1  41
#define NOTE_F1  44
#define NOTE_FS1 46
#define NOTE_G1  49
#define NOTE_GS1 52
#define NOTE_A1  55
#define NOTE_AS1 58
#define NOTE_B1  62
#define NOTE_C2  65
#define NOTE_CS2 69
#define NOTE_D2  73
#define NOTE_DS2 78
#define NOTE_E2  82
#define NOTE_F2  87
#define NOTE_FS2 93
#define NOTE_G2  98
#define NOTE_GS2 104
#define NOTE_A2  110
#define NOTE_AS2 117
#define NOTE_B2  123
#define NOTE_C3  131
#define NOTE_CS3 139
#define NOTE_D3  147
#define NOTE_DS3 156
#define NOTE_E3  165
#define NOTE_F3  175
#define NOTE_FS3 185
#define NOTE_G3  196
#define NOTE_GS3 208
#define NOTE_A3  220
#define NOTE_AS3 233
#define NOTE_B3  247
#define NOTE_C4  262
#define NOTE_CS4 277
#define NOTE_D4  294
#define NOTE_DS4 311
#define NOTE_E4  330
#define NOTE_F4  349
#define NOTE_FS4 370
#define NOTE_G4  392
#define NOTE_GS4 415
#define NOTE_A4  440
#define NOTE_AS4 466
#define NOTE_B4  494
#define NOTE_C5  523
#define NOTE_CS5 554
#define NOTE_D5  587
#define NOTE_DS5 622
#define NOTE_E5  659
#define NOTE_F5  698
#define NOTE_FS5 740
#define NOTE_G5  784
#define NOTE_GS5 831
#define NOTE_A5  880
#define NOTE_AS5 932
#define NOTE_B5  988
#define NOTE_C6  1047
#define NOTE_CS6 1109
#define NOTE_D6  1175
#define NOTE_DS6 1245
#define NOTE_E6  1319
#define NOTE_F6  1397
#define NOTE_FS6 1480
#define NOTE_G6  1568
#define NOTE_GS6 1661
#define NOTE_A6  1760
#define NOTE_AS6 1865
#define NOTE_B6  1976
#define NOTE_C7  2093
#define NOTE_CS7 2217
#define NOTE_D7  2349
#define NOTE_DS7 2489
#define NOTE_E7  2637
#define NOTE_F7  2794
#define NOTE_FS7 2960
#define NOTE_G7  3136
#define NOTE_GS7 3322
#define NOTE_A7  3520
#define NOTE_AS7 3729
#define NOTE_B7  3951
#define NOTE_C8  4186
#define NOTE_CS8 4435
#define NOTE_D8  4699
#define NOTE_DS8 4978


</pre>
// Declaration of tones used in the sequencer
// Define here the note you want to play.
// The first notes are mapped to the lowest position of the pots
int pitch[] = {
               NOTE_C6,
               NOTE_E6,
               NOTE_G6,
               NOTE_C7,
               NOTE_E7,
               NOTE_G7,
               NOTE_C8};

// Declaration of variables
int speaker = 9; // Speaker output pin
// Variables used to calculate tempo
int bpm=60; // set BPM
// set Subdivision 1=quarter note; 0.5 ->eight note, ....
float subdivision=1.5;
int NumStep=4; // The Number of Steps (Max 6)
int StepCounter=0;
int potValue=0; // variable to store the value read from the pot
unsigned int note=0; // variable to store the frequency of the note
unsigned long IOI; // Inter-Onset Interval (time between two steps)
unsigned long Duration; // notes duration

void setup() {
    pinMode(4, INPUT); // Switch input
    pinMode(5, OUTPUT); // LED Output
    pinMode(speaker, OUTPUT); // Speaker Output
    //Inter-Onset Interval computed according bpm and subdivision
    IOI = subdivision*60000/(bpm);
    Duration=0.666*IOI;
}

void loop() {
    digitalWrite(5,HIGH); // Turn on the LED
    potValue = analogRead(StepCounter); // Read pot value
    // Discretization of the pot intervals - in order to assign the note
    if ((potValue>=0) && (potValue<128))
         note = 0; // No Sound!!!
    if ((potValue>=128) && (potValue<256))
         note = pitch[0];
    if ((potValue>=256) && (potValue<384))
         note = pitch[1];
    if ((potValue>=384) && (potValue<512))
         note = pitch[2];
    if ((potValue>=512) && (potValue<640))
         note = pitch[3];
    if ((potValue>=640) && (potValue<768))
         note = pitch[4];
    if ((potValue>=768) && (potValue<896))
         note = pitch[5];
    if ((potValue>=896) && (potValue<1024))
         note = pitch[6];

    StepCounter++; // Increment the step
    if(StepCounter>=NumStep)StepCounter=0; // If StepCounter greater than NumStep back to 0
    // If switch is on and pot in not full on the left
    if(note!=0&&digitalRead(4)==HIGH){
        tone(speaker, note,Duration); // Play the note
    }
    delay(0.5*IOI); // Wait half period
    digitalWrite(5,LOW); // Turn off the LED
    delay(0.5*IOI); // Wait half period
}

This code was used in the Oracolo sound object created by Costantino Rizzuti (2016). Watch some demonstration videos:

This simple code is very effective to start playing 8bit sounds, but it has a big problem about timing. In fact, the delay() function, used to control the temporal scanning of events, is rather imprecise (it has a resolution of a few thousandths of a second) and does not allow a regular succession of equidistant events over time. For this reason it is very difficult to use this code to create multiple sequences of notes that can be overlapped and mounted together to create tracks with more complex musical materials. To solve this problem, a new code has been created that uses a different timing system based on the use of the micros() function of Arduino which provides greater temporal accuracy of the order of microseconds.

Here some tracks created programming our PDSynth-00 prototype with this last step sequencer code.

Why do not you even take a look at our products on Tindie.com?

I sell on Tindie

If you like this project and you want to try to make it don’t esitate to write us for problems, curiosities and suggestions:

Security Code:
security code
Please enter the security code:

Submit

One thought to “Arduino Step Sequencer: How to program a step sequencer with Arduino”

Leave a Reply

Your email address will not be published. Required fields are marked *