rhoadley.net   music   research   courses   software   blogs

ARU    CMC2a    ACMC
CBHH    Sensor Technology    Sonic Art    Major Project    MA Resources


Resources:    Bioacoustics    Jitter    MaxMSP    OSC    Physical    PD       pTech    sTech    SuperCollider    C/Xcode

sTech Resources:     Home     Blog     Forum     Examples     Projects     Tasks     Tutorials


Sensor Technology Tasks

Arduino Task 2: Basic Sound and MaxMSP

Task 2 Name: Basic Sound with the Arduino Set: w3ii Due: Thursday 16th December 2010 Weighting: 0% Courses: stech
Prev Task: Hello World Next Task: Arduino, MaxMSP, SuperCollider and Breadboards
Task Summary All stech tasks

Making Sounds with an Arduino (including via Max).

To cover:


Using a Piezo disc to make sounds
Give the sounds a duration
Generate and Modulate Pulses
Notes Over the Serial Port with Duration Controlled by a Potentiometer
Use the Arduino Board with Max

 

Use a Piezo to Make Sounds

First, use a Piezo to make sounds via the Arduino, (piezos can be used to create sounds as well as 'receive' them, as in the knock example we've covered.


/* Play notes over serial
 * v1.0b002 February 2010
 * arduino example: arduino.cc
 * mods by rhoadley, rhoadley.net
 * 
 * Arduino Code Example - playing sounds
 * Example 1: just playing sounds
 * The following program is an example that will play notes 
 * out of the characters received over the serial port. 
 * To try it out, upload this file to Arduino, hook up a piezo 
 * or a speaker to digital pin 8 (black to ground) and send characters to play 
 * each note.
 * Do this by opening the Serial Monitor (button in bar above), 
 * typing a letter in the field that appears below and clicking 
 * on the 'Send' button.
 *
 * It's tricky to get two piezos/speakers working together.  Why is this?
 * 
 * 
 * ----------------------
 * 
 * note - half period (uS)
 * ----------------------- 
 * c    -   1,911
 * d    -   1,703
 * e    -   1,517
 * f    -   1,432
 * g    -   1,276
 * a    -   1,136
 * b    -   1,012
 * C    -   956
 */

int val = 0;      // to check if there is data available from the serial port
int val2 = 0;     // stores the data that came from the port
int piezoPin = 8; // pin number where to hook the speaker
int pulso = 0;    // value of half pulse

void setup() {
  Serial.begin(9600);
  pinMode(piezoPin, OUTPUT);
}

void loop() {
  val = Serial.available();
  if (val > 0) {
    val2 = Serial.read();
    if (val2 == 'c') {
      pulso = 1911;
    }
    if (val2 == 'd') {
      pulso = 1703;
    }
    if (val2 == 'e') {
      pulso = 1517;
    }
    if (val2 == 'f') {
      pulso = 1432;
    }
    if (val2 == 'g') {
      pulso = 1276;
    }
    if (val2 == 'a') {
      pulso = 1136;
    }
    if (val2 == 'b') {
      pulso = 1012;
    }
    if (val2 == 'C') {
      pulso = 956;
    }
  }
  digitalWrite(piezoPin, HIGH);
  delayMicroseconds(pulso);
  digitalWrite(piezoPin, LOW);
  delayMicroseconds(pulso);
}


 

Give the Notes Duration


/* Play Notes Over Serial Port with Duration
 * v1.0b001 December 2008
 * arduino example: arduino.cc
 * mods by rhoadley, rhoadley.net
 *
 * Example 2: upgrading to notes
 * Notes have duration in time, this program will play a tone for some time, 
 * and then go silent, instead of keeping it sounding forever, 
 * as the previous program does.
 * This example also plays strings of letters.
 *
 * To try it out, upload this file to Arduino, hook up a piezo 
 * or a speaker to digital pin 8 (black to ground) and send characters to play 
 * each note.
 *
 * Send the Arduino letters by opening the Serial Monitor (button in bar above), 
 * typing a letter in the field that appears below and clicking 
 * on the 'Send' button.
 *
 * ----------------------------
 * 
 * note - half period (uS)
 * ----------------------- 
 * c    -   1,911
 * d    -   1,703
 * e    -   1,517
 * f    -   1,432
 * g    -   1,276
 * a    -   1,136
 * b    -   1,012
 * C    -   956
 *
 * We create a function that will take care of
 * playing a tone for a while and go silent
 * afterwards
 */

int val = 0;      // to check if there is data available from the serial port
int val2 = 0;     // stores the data that came from the port
int piezoPin = 8; // pin number where to hook the speaker
int dur = 150;

void playNote(int note) {
  // in the following line, dur represents the duration of the note in milliseconds
  for( int a=0; a <= dur; a++) {
    digitalWrite(piezoPin, HIGH);
    delayMicroseconds(note);
    digitalWrite(piezoPin, LOW);
    delayMicroseconds(note);
  }
}

void setup() {
  Serial.begin(9600);
  pinMode(piezoPin, OUTPUT);
}

void loop() {
  val = Serial.available();
  if (val > 0) {
    val2 = Serial.read();
    if (val2 == 'c') {
      playNote(1911);
    }
    if (val2 == 'd') {
      playNote(1703);
    }
    if (val2 == 'e') {
      playNote(1517);
    }
    if (val2 == 'f') {
      playNote(1432);
    }
    if (val2 == 'g') {
      playNote(1276);
    }
    if (val2 == 'a') {
      playNote(1136);
    }
    if (val2 == 'b') {
      playNote(1012);
    }
    if (val2 == 'C') {
      playNote(956);
    }
  }
}




 

Generate and Modulate Pulses


/* Play Siren-like Pulses
 * v1.0b001 December 2008
 * arduino example: arduino.cc
 * mods by rhoadley, rhoadley.net
 * 
 * Example 3: Playing Pulses
 * The following program is an example that will play notes 
 * out of the characters received over the serial port. 
 * To try it out, upload this file to Arduino, hook up a piezo 
 * or a speaker to digital pin 8 (black to ground) and send characters to play 
 * each note.
 * Do this by opening the Serial Monitor (button in bar above), 
 * typing a letter in the field that appears below and clicking 
 * on the 'Send' button.
 * 
 * 
 * ----------------------
 * 
 * note - half period (uS)
 * ----------------------- 
 * c    -   1,911
 * d    -   1,703
 * e    -   1,517
 * f    -   1,432
 * g    -   1,276
 * a    -   1,136
 * b    -   1,012
 * C    -   956
 */

int val = 0;      // to check if there is data available from the serial port
int val2 = 0;     // stores the data that came from the port
int piezoPin = 8; // pin number where to hook the speaker
int pulso = 0;    // initialise the pulse
int del1 = 400;   // delay
int pulso2;       // initialise

// so you can send values to the board
void setup() {
  Serial.begin(9600);
  pinMode(piezoPin, OUTPUT);
}

void loop() {
  val = Serial.available();
  if (val > 0) {
    val2 = Serial.read();
    
    if (val2 == 'c') {
      pulso = 1911;
    }
    if (val2 == 'd') {
      pulso = 1703;
    }
    if (val2 == 'e') {
      pulso = 1517;
    }
    if (val2 == 'f') {
      pulso = 1432;
    }
    if (val2 == 'g') {
      pulso = 1276;
    }
    if (val2 == 'a') {
      pulso = 1136;
    }
    if (val2 == 'b') {
      pulso = 1012;
    }
    if (val2 == 'C') {
      pulso = 956;
    }
  }
  
  
  digitalWrite(piezoPin, HIGH);
  delayMicroseconds(pulso2);
  digitalWrite(piezoPin, LOW);
  delayMicroseconds(pulso2);
  
    delayMicroseconds(del1);
      pulso2 = (pulso2-10);
    delayMicroseconds(del1);
        if (pulso2 < 200) 
        {
          pulso2 = pulso;
        }
}



 

Notes Over the Serial Port with Duration Controlled by a Potentiometer


/* Play Notes Over Serial Port with Duration
 * v1.0b002 December 2010
 * arduino example: arduino.cc
 * mods by rhoadley, rhoadley.net
 *
 * Example 2: upgrading to notes
 * Notes have duration in time, this program will play a tone for some time, 
 * and then go silent, instead of keeping it sounding forever, 
 * as the previous program does.
 * This example also plays strings of letters.
 *
 * To try it out, upload this file to Arduino, hook up a piezo 
 * or a speaker to digital pin 8 (black to ground) and send characters to play 
 * each note.
 
 * Then, connect a potentiometer (for example, 470K log or lin) in the following fashion: the outer connections to 5v and ground,
 * the central connection to Analogue In 0.
 *
 * Send the Arduino letters by opening the Serial Monitor (button in bar above), 
 * typing a letter in the field that appears below and clicking 
 * on the 'Send' button.
 *
 * ----------------------------
 * 
 * note - half period (uS)
 * ----------------------- 
 * c    -   1,911
 * d    -   1,703
 * e    -   1,517
 * f    -   1,432
 * g    -   1,276
 * a    -   1,136
 * b    -   1,012
 * C    -   956
 *
 * We create a function that will take care of
 * playing a tone for a while and go silent
 * afterwards
 */

int val = 0;        // to check if there is data available from the serial port
int val2 = 0;       // stores the data that came from the port
int piezoPin = 8;   // pin number where to hook the speaker
int potVal = 150;   // the value of the potentiometer
int potPin = 0;     // select the input pin for the potentiometer

void playNote(int note) {
  // in the following line, dur represents the duration of the note in milliseconds
  for( int a=0; a <= potVal; a++) {
    digitalWrite(piezoPin, HIGH);
    delayMicroseconds(note);
    digitalWrite(piezoPin, LOW);
    delayMicroseconds(note);
  }
}

void setup() {
  Serial.begin(9600);
  pinMode(piezoPin, OUTPUT);
}

void loop() {
  potVal = analogRead(potPin);    // read the value from the sensor
  Serial.println(potVal);
    
  val = Serial.available();
  if (val > 0) {
    val2 = Serial.read();
    if (val2 == 'c') {
      playNote(1911);
    }
    if (val2 == 'd') {
      playNote(1703);
    }
    if (val2 == 'e') {
      playNote(1517);
    }
    if (val2 == 'f') {
      playNote(1432);
    }
    if (val2 == 'g') {
      playNote(1276);
    }
    if (val2 == 'a') {
      playNote(1136);
    }
    if (val2 == 'b') {
      playNote(1012);
    }
    if (val2 == 'C') {
      playNote(956);
    }
  }
}





 

Notes Over the Serial Port with Frequency Controlled by a Potentiometer


/* Play Notes Over the Serial Port with Frequency Controlled by a Potentiometer
 * v1.0b002 February 2010
 * arduino example: arduino.cc
 * mods by rhoadley, rhoadley.net
 *
 * Example 2: upgrading to notes
 * Notes have duration in time, this program will play a tone for some time, 
 * and then go silent, instead of keeping it sounding forever, 
 * as the previous program does.
 * This example also plays strings of letters.
 *
 * To try it out, upload this file to Arduino, hook up a piezo 
 * or a speaker to digital pin 8 (black to ground) and send characters to play 
 * each note.
 
 * Then, connect a potentiometer (for example, 470K log or lin) in the following fashion: the outer connections to 5v and ground,
 * the central connection to Analogue In 0.
 *
 * Send the Arduino letters by opening the Serial Monitor (button in bar above), 
 * typing a letter in the field that appears below and clicking 
 * on the 'Send' button.
 *
 * ----------------------------
 * 
 * note - half period (uS)
 * ----------------------- 
 * c    -   1,911
 * d    -   1,703
 * e    -   1,517
 * f    -   1,432
 * g    -   1,276
 * a    -   1,136
 * b    -   1,012
 * C    -   956
 *
 * We create a function that will take care of
 * playing a tone for a while and go silent
 * afterwards
 */

int val = 0;        // to check if there is data available from the serial port
int val2 = 0;       // stores the data that came from the port
int piezoPin = 8;   // pin number where to hook the speaker
int potVal = 150;   // the value of the potentiometer
int potPin = 0;     // select the input pin for the potentiometer

void playNote(int note) {
  // in the following line, dur represents the duration of the note in milliseconds
  for( int a=0; a <= 20; a++) {
    digitalWrite(piezoPin, HIGH);
    delayMicroseconds(note);
    digitalWrite(piezoPin, LOW);
    delayMicroseconds(note);
  }
}

void setup() {
  Serial.begin(9600);
  pinMode(piezoPin, OUTPUT);
}

void loop() {
  potVal = analogRead(potPin);    // read the value from the sensor
  Serial.println(potVal+1);
    
  val = Serial.available();
  if (val > 0) {
      playNote(potVal+1);
    }
}


 

Connecting Arduino and Max

For those of you eager to get on with connecting to Max, here are some patches to be playing with. Incidentally, you need to be a little careful with properly opening and closing your serial ports, whether USB or Bluetooth. If you don't, you stand the very real risk of crashing one or other requiring a reboot of your computer.





to top of page The Task

  • Make sure you do the above. Not just once, but a number of times through the week. At least twice. You need to do it enough so that you feel reasonably comfortable and confident.

  • Once you're comfortable, create a few examples using one or more piezo discs. While accepting the limitations of the technology, make something as interesting as possible. Investigate ways of improving playback.

  • For this task, no software other than Arduino (or Processing) is necessary.

    Added Value
  • Completing the basics of the task will get you a basic pass mark. Try to develop other ways of implementing or extending the task for bonus marks up to 70 and beyond. In this case, you might consider ways of making the patterns of tones played back more interesting.

    What to submit
  • Clearly, I don't want you to submit your Arduinos. That means you need to submit some documentary evidence that you've completed your task. The most obvious way to do this is to submit some sort of video of your task working. If you can't do this, take photos. Bear in mind that you'll get extra marks for 'added value', so try to adapt and make more interesting your files. Simply repeating what we've already done will get minimal marks.

    Finally
  • Compress your files, demos, photos, videos etc. into one file called your_student_number_"basicsound" (e.g. 0504335_basicsound.zip), include a readme file with your name and student number and, if necessary, how to use or just open your work.
  • Submit a copy of the task to the i-Centre on Thursday 13th May 2010

  • On Mac OS, zip or stuff your patches by selecting the relevant files and/or folders and right-clicking/control-clicking on them, choosing 'Create Archive' or 'Compress...'.

You might also be interested in:


The Projects

The projects and tasks are designed to help you through the various courses and materials that you'll have to deal with, and also to provide an active and practical element to what could otherwise become a rather dry and technical exercise. Tasks are small exercises - you may be asked to complete one or two per week. Projects are larger and carry a higher percentage of the mark. We will undertake two, three, four or more projects and tasks. The final project is usually an individual choice project, and will be worth significantly more than the others in terms of percentages in your portfolio. We will usually try to set aside a time to perform the projects in a public setting.