Create Brain Powered Sounds!

So far we have mastered light control with our brain. Now, let’s control sound using a piezo buzzer.

The different note pitches you hear in this video are controlled by the brain. This is programmed so that high attention values create high pitch notes while low attention values create low pitch notes. As this video plays, you will hear the pitch of each note rise, meaning the attention score of the person here wearing the BCI unit is becoming more focused!

Estimated Time 

Approximately One Hour

Number of People

1- 2 students per materials set

Necessary Supplies

  • One NeuroMaker BCI Unit
  • One Uno Board
  • USB cable 
  • Computer
  • At least two Male to Male Jumper Wires
  • One Passive Piezo buzzer
  • One Breadboard

Necessary Program Files

Please see the code below

Pre Check Items

  • NeuroMaker BCI Unit is fully charged
  • NeuroMaker BCI Connect Software is running properly

Background

We have spent a lot of time so far creating different light patterns using our brain. We at NeuroMaker are sure that by now you can more clearly see that our attention scores can be used to control all kinds of other items.

Sound is an interesting concept to control. One could imagine using our attention score ranges like we did in the last project to select songs we want to hear, organize a playlist or control the choice of some pre-recorded sound. For this project, we are going to actually create sounds by passing electricity to a special device called a piezo buzzer. These are commonly found in greeting cards that play songs, small household alarms or toys that play simple melodies. These are particularly interesting for us because we can control nearly everything about the sounds we want with an UNO controller including volume, pitch, melodies and more.

Using NeuroMaker BCI, we are going to program different notes that will play according to our attention values. In the example provided below, high attention scores will create high pitched sounds and low attention scores will create low pitched sounds. We will be able to tell how focused you are just by the sounds you can hear.

Table of Contents

Project Instructions

Compared to our last few projects, building this circuit is going to be incredibly easy. For the passive piezo buzzer we will be using, all we need to do is connect one side of the buzzer to a digital pin and the other side to ground. No need for resistors or any other electronics.

  • Connect digital pin 4 to one side of the buzzer
  • Connect the other side of the buzzer to ground

That’s it! No more additional wires or resistors required.

The Piezo Buzzer

Piezo buzzers are very simple electrical devices. Inside of the buzzer itself there is a small piece of metal that is designed so that when a certain amount of current is passed through it, it will vibrate and make noise. Thanks to our controller and the code we create, we can control how much electricity we want to give to this device. This control allows us to decide which pitches we want, how often to play them and more.

If we wanted to manually code how much electricity to pass to the sensor to play the musical notes we want, we would probably need a LOT of time to learn this. Thankfully, one of the available Arduino functions is tone() which will give us the control we need for this project. Additionally, our friends at Arduino have also provided us with the information needed to add easy to select notes that any musicians out there may have seen. Scale notes like E, F, G, B flat and everything else can be easily added to our code.

Now it’s time to take a look at our code. For our BCI purposes, we are most interested in how our attention score can control this device. If you wish to understand more about how the tone() function works, how the scaleNotes[ ] array is organized and some of the other details, it is best to work with the source material. You find that all here on the Arduino page: https://www.arduino.cc/en/Tutorial/BuiltInExamples/toneMelody

One incredibly important step to keep in mind is that for this project to work properly, you MUST save the pitches.h file into your Arduino IDE project tab. The information to complete this is located midway through the link provided above and starts at the image provided to the right of this text. This file will allow us to select the musical notes we want to play.

Now onto our BCI component. In this selected piece of the below code you will notice the following text:

				
					while (Serial.available() > 0){
  val = Serial.read();
}
thisNote = val * (.49);
				
			

By now we are quite familiar with the Serial.available() and Serial.read() functions we use to get information from the serial port. In this case though, you can see we have a val variable and that it is multiplied by .49 to select the note we wanted from the scaleNotes[ ] array. What on earth is going on here?

For those out there familiar with integer math, you caught us red-handed. In this case, we are using the tone() function to play a certain note and you will notice that the second argument this function accepts is scaleNotes[thisNote]. This is exactly where we are going to use our attention score readings to select which note we want to play.

Let’s take a look at the integers in our array which we have selected to be the notes in our “brain powered melody”:

				
					int scaleNotes[] = 
{
NOTE_E1, NOTE_F1, NOTE_G1, NOTE_A1, NOTE_B1, NOTE_C1, NOTE_D1,
NOTE_E2, NOTE_F2, NOTE_G2, NOTE_A2, NOTE_B2, NOTE_C2, NOTE_D2,
NOTE_E3, NOTE_F3, NOTE_G3, NOTE_A3, NOTE_B3, NOTE_C3, NOTE_D3,
NOTE_E4, NOTE_F4, NOTE_G4, NOTE_A4, NOTE_B4, NOTE_C4, NOTE_D4, 
NOTE_E5, NOTE_F5, NOTE_G5, NOTE_A5, NOTE_B5, NOTE_C5, NOTE_D5,  
NOTE_E6, NOTE_F6, NOTE_G6, NOTE_A6, NOTE_B6, NOTE_C6, NOTE_D6,
NOTE_E7, NOTE_F7, NOTE_G7, NOTE_A7, NOTE_B7, NOTE_C7, NOTE_D7,
};
				
			

If you count each item inside of this array, you notice there are 49 different notes. Remember, our attention score is representing in this code as the variable val and has a minimum value of 0 and maximum value of 100. So then let’s think, if we want our maximum attention level to correspond with the highest item (49) on our integer array, what should we multiply our attention score by?

If you noticed the .49 you win the big prize! 100 times .49 is 49. This will also work for other integers in between. As our attention score rises from 0, we will hit other notes. For instance, if our attention score is 10, our simple algorithm will multiply 10*.49 and give us a val of 4.9. We have declared this variable as a char which will remove the decimal point just give us a value of 4. This function will then give us the note scaleNotes[4] and play the note we requested.

In the code below we will therefore be able to play the whole notes E, F, G, A, B, C and D in seven different octaves. Go ahead and create your project and see if you can hear your attention rise and fall.

The Code

				
					
#include "pitches.h"
char val;
int piezioPin = 4; //please note the project on the Arduino website uses pin 8. Any digital pin you wish to use is acceptable
int scaleNotes[] = 
{
NOTE_E1, NOTE_F1, NOTE_G1, NOTE_A1, NOTE_B1, NOTE_C1, NOTE_D1,
NOTE_E2, NOTE_F2, NOTE_G2, NOTE_A2, NOTE_B2, NOTE_C2, NOTE_D2,
NOTE_E3, NOTE_F3, NOTE_G3, NOTE_A3, NOTE_B3, NOTE_C3, NOTE_D3,
NOTE_E4, NOTE_F4, NOTE_G4, NOTE_A4, NOTE_B4, NOTE_C4, NOTE_D4, 
NOTE_E5, NOTE_F5, NOTE_G5, NOTE_A5, NOTE_B5, NOTE_C5, NOTE_D5,  
NOTE_E6, NOTE_F6, NOTE_G6, NOTE_A6, NOTE_B6, NOTE_C6, NOTE_D6,
NOTE_E7, NOTE_F7, NOTE_G7, NOTE_A7, NOTE_B7, NOTE_C7, NOTE_D7,
};
int thisNote;
int noteDurations = 4;
int noteDuration = 1000/noteDurations;


void setup() {
  // put your setup code here, to run once:

pinMode(piezioPin, OUTPUT);
Serial.begin(115200);
}

void loop() {
  // put your main code here, to run repeatedly:

while (Serial.available() > 0){
  val = Serial.read();
}
thisNote = val * (.49);
tone(piezioPin, scaleNotes[thisNote], noteDuration);
delay(noteDuration *1.3);
}
				
			

If you can successfully run this project, what else could you create with this knowledge? Could you use other notes? Could you play an actual melody of different notes together with a certain attention score range?