Home TI-Nspire Scripting HQ TI-Nspire STEM HQ Getting Started with TI LaunchPads TI LaunchPad Lesson 4

     
     

Lesson 4: From Serial to Sound - Connecting a Speaker

Energia Reference page

     
           
     

Connecting to the World Beyond

Time to go beyond the simple lights and buttons of the LaunchPad board.

Right now is a good time to have a quick look at the MSP-432 board in more detail. The board communicates with the world outside via connector pins. There are 40 pins available, numbered from the top left (1 - 10) - so the pin labelled 3V3 corresponds to pin 1, while P6_4 is pin #10. The opposite outer row is next - so pin 11 is found at the bottom far right (P3_4) up to pin 20 (GND). Pins 21 - 40 are numbered on the two inner rows, as shown.

Look around the board for pins labelled GND. These are "ground" pins and are used to complete a circuit when power is required to be sent to an external device. The power comes as either 3.3V (volts) or 5V. Some devices could be damaged if the higher voltage is used, so be careful that you check on the requirements. You will see pins labelled 3V3 and 5V and GND pins at several places around the button half of the board, and generally these may be used interchangeably.

  
  

Making Some Noise - Connecting a Simple Speaker

For many peripherals (such as the simple speaker used here) the power is sent to the peripheral via the pin that is allocated to it. In the example below, we have used pin 6 (see red jumper on the diagram), but any numbered pin could have served. Pin 22 was used as the GND (black jumper). So power is sent through pin 6 to the speaker, then back to the GND pin, completing the circuit. Simple!

If you have access to the Grove sensors, you might find a buzzer in your collection. This activity works for a simple buzzer, as well as for a proper speaker (although it will not have quite the same pleasant sound!). Just make sure that you nominate the right pin number. For many of the Grove Sensors, the white lead is not used: black is GND, red is the voltage (usually 3V3) and the yellow is the actual "SIG" pin which you can set to any of the 40 options that suits.

int notes[] = {131, 147, 165, 175, 196, 220, 247, 262};
String inString = "";
int pin = 6;

void setup()
{

Serial.begin(9600);
}

void loop()
{

while(Serial.available()){

char recvChar = Serial.read();
if (isDigit(recvChar)) { // check if the input is a digit

inString += (char)recvChar; // add that character to a string
int input = inString.toInt(); // convert the string to an integer
tone(pin, notes[input], 200); // play the tone for that integer
Serial.println(input); // print the input
inString = ""; // clear the string for new input:
}
}
}

Look closely at the sketch now, line by line. Some useful and interesting things are happening here.

First we define three types of variables. "notes" is a list of integer values, corresponding to the frequencies of scale from C2 to C3 (note that the standard value is 440 MHz for A4, the A below middle C. These are approximate values only so they may not always sound exactly as you might expect.)

Next, we define a string ("inString") and, for convenience, the pin value we will use. Try different values, moving your jumper to other pin locations.

We are using a "while" loop here, an alternative to the "if" statement used previously. Both are suitable options for tests such as this. As previously, we define a character called "recvChar" as the result of reading input from the Serial monitor.

For this sketch, we want the serial input to be an integer value from 0 to 7, corresponding to the 8 elements of the "notes" list (so notes[0] = 131, notes[7] = 262).

Once a character has been read from the Serial Monitor, it is checked to see if it is numeric (isDigit). If so, then it is added to the waiting string, inString. This is converted from string to integer by the in-built function, string.toInt().

The tone command takes three parameters - the pin to be used, the frequency of the note, and the duration in milliseconds. Note that if the input is 0, then the tone played will be note[0], the first entry in the note list. Print the inptu to the Serial Monitor just to see what is happening, and then clear the string to start over for the next input.

Note: the opposite of the tone() command is noTone() - case sensitive and taking no arguments.

  
  

int notes[] = {131, 147, 165, 175, 196, 220, 247, 262};
String inString = "";
int pin = 6;

void setup()
{

Serial.begin(9600);
}

void loop()
{

while(Serial.available()){

char recvChar = Serial.read();
if (isDigit(recvChar)) { // check if the input is a digit

inString += (char)recvChar; // add that character to a string
}

if (recvChar == '\n') { // if you get a newline, play the string

int input = inString.toInt(); // convert the string to an integer

if (input > 7) { // Test the length of the input

tone(pin, input, 200); // if greater than 7 play that frequency eg 440
}
else {
tone(pin, notes[input], 200); // else play that note from the list
}
Serial.println(inString.toInt());
inString = ""; // clear the string for new input:
}

}
}

Building a String

So far, we have just used single characters to write to the board. This works well for simple things, but very quickly we will find that we need a better system. For example, suppose we wanted to be able to type notes such as A4, and C3, or just build a scale from one note (say, "C") to that same note one octave higher.

The sketch displayed here is very similar to the previous one with a small but very important exception.

The line inString += (char)recvChar is designed to add characters to a string. So it will work just as well for a value such as "440" as it does for "4". All we need is a method to tell the sketch where our value ends, and for that, we use a simple newline (\n) which occurs when you press the enter key in the Serial Monitor. It is an "invisible" character so you don't see it, but it is there and can be used as a test.

So in this sketch, we wait until we sense an enter key to register our input and to play the tone. I added another test - if the value is between 0 and 7, then it can serve to just play a note from the octave list defined previously. If larger, then it cannot play a tone from that list, so it will just play whatever value it carries. In this way, you can now type 440 and the speaker will play A4.

Take note of this technique - it will become very important later when we are sending information back and forth using, not just the Serial Monitor, but Bluetooth Low Energy (BLE).

Challenges

Instead of typing numbers, wouldn't it be good to be able to type actualy note names and have these played? Think about the switch function from the last lesson, with cases such as "C4" and "B3", etc.

And what about actually playing tunes? For that we might need lists for both note frequency and duration...

  
  

Back to Top

  

Home TI-Nspire Scripting HQ TI-Nspire STEM HQ Getting Started with TI LaunchPads TI LaunchPad Lesson 4