Home ← TI-Nspire Scripting HQ ←STEM HQ ← Getting Started with TI LaunchPads ← TI LaunchPad Lesson 4
Lesson 4: From Serial to Sound - Connecting a Speaker
Lesson 4: From Serial to Sound
Lesson 7 - Real world data at your fingertips: Light, Ultrasonic Motion and more...
Lesson 8 - Build your own BLE ultrasonic motion detector for under $USD30
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 34 (the Innovator Hub speaker pin), but any numbered pin could have served. Pin 22 was used as the GND (black jumper). So power is sent through pin 34 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.
Hacking the Hub™
In order to send data via the serial port to the Hub, some jumper caps need to be added to the pins in the centre of the board as shown. The standard MSP432 comes with 4 of the central 6 pins covered by jumper caps, whereas all jumper caps have been removed from the Innovator Hub central pins, and you will need to source some more to cover four pins (you can leave the two middle pins) if you wish to send commands via the serial monitor. If you are unable to do this, continue to lesson 5, connect a BLE module and use this to communicate with your hub!
Note, in the sketch below the sound pin used is pin 34 - which is the pin allocated to the Innovator Hub speaker!
You might also try using a PUSH button to serve as the trigger for this activity!
Remember: You can always reflash your Innovator Hub to return it to its original state, using the tools that TI has made available (just follow the Resources tab, and then "Keep your Innovator up to date"). So it is possible to explore coding the Hub in new ways!
TI Innovator™ Hub (with MSP432 LaunchPad)int notes[] = {131, 147, 165, 175, 196, 220, 247, 262};
String inString = "";
int pin = 34;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.
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...
int notes[] = {131, 147, 165, 175, 196, 220, 247, 262};
int pin = 34;void setup()
{Serial.begin(9600);}
void loop()
{
while(Serial.available()){}char recvChar = Serial.read();
}switch(recvChar) {
case 'c':
tone(pin, 131, 200);
break;case 'd':
tone(pin, 147, 200);
break;case 'e':
tone(pin, 165, 200);
break;case 'f':
tone(pin, 175, 200);
break;case 'g':
tone(pin, 196, 200);
break;case 'a':
tone(pin, 220, 200);
break;case 'b':
tone(pin, 247, 200);
break;case 'C': // using uppercase for octave
tone(pin, 262, 200);}
break;
int buttonState1 = 0;
int pin = 6;// notes in the melody:
int melody[] = {165, 147, 131, 147, 165, 164, 165,
147, 146, 147, 165, 195, 196};// note durations: 4 = quarter note, 8 = eighth note, etc.:
int durations[] = {4, 4, 4, 4, 4, 4, 2,
4, 4, 2, 4, 4, 2};void setup()
{pinMode(PUSH1, INPUT_PULLUP);}
Serial.begin(9600);
void loop()
{
buttonState1 = digitalRead(PUSH1);}
while(Serial.available()){
}char recvChar = Serial.read();
Serial.println(recvChar);
switch(recvChar) {
case '1':
buttonState1 = LOW;
break;
}if (buttonState1 == LOW) {
for (int thisNote = 0; thisNote < 13; thisNote++) {}
// to calculate the note duration, take one second
// divided by the note type.
//e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
tone(pin, melody[thisNote], 1000/durations[thisNote]);
int pauseBetweenNotes = 1000/durations[thisNote] + 50; //delay between pulse
delay(pauseBetweenNotes);
noTone(pin); // stop the tone playing
}
Home ← TI-Nspire Scripting HQ ← STEM HQ ← Getting Started with TI LaunchPads ← TI LaunchPad Lesson 4