Gyroscope-Arduino That Produces Sound
by vmehta01 in Circuits > Arduino
724 Views, 3 Favorites, 0 Comments
Gyroscope-Arduino That Produces Sound
You will learn how to generate music through an accelerometer and an MPU6050 gyroscope in this article (also referred to as a "gyro sensor")
Installing the Arduino Libraries
You'll require the I2C library and MPU6050 sensor library, which you can access through this file folder
Next, open Arduino IDE and click on Sketch- include library-Add.ZIP library. Add I2C library and MPU6050 folder in your Arduino library.
Once you have these files, click on file-examples-MPU6050-MPU6050_raw.
Upload the code found in the MPU6050_raw and it should display on the serial monitor.
If it's showing the output data, then you can assume the sensor has successfully interfaced with the Arduino.
Adding Music Notes on Arduino
Download music notes. Open the song file of your choice. In my project,I used Furelise.
Then you have to combine the code from music notes file and MPU6050_raw.
You can edit the tempo, number of const int melody, number of notes to include in const int melody. You can also edit the if condition to get more variation with every movement.
Copy the code and edit to your preference.
#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 #define REST 0
int tempo = 80; int buzzer = 11; #include "I2Cdev.h" #include "MPU6050.h"
// Arduino Wire library is required if I2Cdev I2CDEV_ARDUINO_WIRE implementation // is used in I2Cdev.h #if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE #include "Wire.h" #endif
const int melodya[] PROGMEM = {
// Fur Elise - Ludwig van Beethovem // Score available at https://musescore.com/user/28149610/scores/528194...
//starts from 1 ending on 9 NOTE_E5, 10, NOTE_DS5, 8, NOTE_E5, 5 };
const int melodyb[] PROGMEM = {
// Fur Elise - Ludwig van Beethovem // Score available at https://musescore.com/user/28149610/scores/528194...
//starts from 1 ending on 9 NOTE_B4, 13, NOTE_D5, 4, NOTE_C5, 16,
};
const int melodyc[] PROGMEM = {
// Fur Elise - Ludwig van Beethovem // Score available at https://musescore.com/user/28149610/scores/528194...
//starts from 1 ending on 9 NOTE_C4, 6, NOTE_E4, 4, NOTE_A4, 8, NOTE_B4, -8 };
const int melodyd[] PROGMEM = {
// Fur Elise - Ludwig van Beethovem // Score available at https://musescore.com/user/28149610/scores/528194...
//starts from 1 ending on 9 NOTE_E4, 16, NOTE_GS4, 16, NOTE_B4, 16, NOTE_C5, 8 }; const int melodye[] PROGMEM = {
// Fur Elise - Ludwig van Beethovem // Score available at https://musescore.com/user/28149610/scores/528194...
//starts from 1 ending on 9 REST, 16, NOTE_E4, 16, NOTE_E5, 16, NOTE_DS5, 16, }; const int melodyf[] PROGMEM = {
// Fur Elise - Ludwig van Beethovem // Score available at https://musescore.com/user/28149610/scores/528194...
//starts from 1 ending on 9 NOTE_E5, 1 }; MPU6050 accelgyro; //MPU6050 accelgyro(0x69); // <-- use for AD0 high
int16_t ax, ay, az; int16_t gx, gy, gz;
// uncomment "OUTPUT_READABLE_ACCELGYRO" if you want to see a tab-separated // list of the accel X/Y/Z and then gyro X/Y/Z values in decimal. Easy to read, // not so easy to parse, and slow(er) over UART. #define OUTPUT_READABLE_ACCELGYRO
// uncomment "OUTPUT_BINARY_ACCELGYRO" to send all 6 axes of data as 16-bit // binary, one right after the other. This is very fast (as fast as possible // without compression or data loss), and easy to parse, but impossible to read // for a human.
//#define OUTPUT_BINARY_ACCELGYRO int notesa = sizeof(melodya) / sizeof(melodya[0]) / 2; int notesb = sizeof(melodyb) / sizeof(melodyb[0]) / 2; int notesc = sizeof(melodyc) / sizeof(melodyc[0]) / 2; int notesd = sizeof(melodyd) / sizeof(melodyd[0]) / 2; int notese = sizeof(melodye) / sizeof(melodye[0]) / 2; int notesf = sizeof(melodyf) / sizeof(melodyf[0]) / 2; // this calculates the duration of a whole note in ms int wholenote = (60000 * 4) / tempo;
int divider = 0, noteDuration = 0;
#define LED_PIN 13 bool blinkState = false;
void setup() { // join I2C bus (I2Cdev library doesn't do this automatically) #if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE Wire.begin(); #elif I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_FASTWIRE Fastwire::setup(400, true); #endif
// initialize serial communication // (38400 chosen because it works as well at 8MHz as it does at 16MHz, but // it's really up to you depending on your project) Serial.begin(38400);
// initialize device Serial.println("Initializing I2C devices..."); accelgyro.initialize();
// verify connection Serial.println("Testing device connections..."); Serial.println(accelgyro.testConnection() ? "MPU6050 connection successful" : "MPU6050 connection failed");
// use the code below to change accel/gyro offset values /* Serial.println("Updating internal sensor offsets..."); // -76 -2359 1688 0 0 0 Serial.print(accelgyro.getXAccelOffset()); Serial.print("\t"); // -76 Serial.print(accelgyro.getYAccelOffset()); Serial.print("\t"); // -2359 Serial.print(accelgyro.getZAccelOffset()); Serial.print("\t"); // 1688 Serial.print(accelgyro.getXGyroOffset()); Serial.print("\t"); // 0 Serial.print(accelgyro.getYGyroOffset()); Serial.print("\t"); // 0 Serial.print(accelgyro.getZGyroOffset()); Serial.print("\t"); // 0 Serial.print("\n"); accelgyro.setXGyroOffset(220); accelgyro.setYGyroOffset(76); accelgyro.setZGyroOffset(-85); Serial.print(accelgyro.getXAccelOffset()); Serial.print("\t"); // -76 Serial.print(accelgyro.getYAccelOffset()); Serial.print("\t"); // -2359 Serial.print(accelgyro.getZAccelOffset()); Serial.print("\t"); // 1688 Serial.print(accelgyro.getXGyroOffset()); Serial.print("\t"); // 0 Serial.print(accelgyro.getYGyroOffset()); Serial.print("\t"); // 0 Serial.print(accelgyro.getZGyroOffset()); Serial.print("\t"); // 0 Serial.print("\n"); */
// Wait for the specief duration before playing the next note.
// configure Arduino LED pin for output pinMode(LED_PIN, OUTPUT); }
void loop() { // read raw accel/gyro measurements from device accelgyro.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
// these methods (and a few others) are also availabled //accelgyro.getAcceleration(&ax, &ay, &az); //accelgyro.getRotation(&gx, &gy, &gz);
#ifdef OUTPUT_READABLE_ACCELGYRO // display tab-separated accel/gyro x/y/z values Serial.print("a/g:\t"); Serial.print(ax); Serial.print("\t"); Serial.print(ay); Serial.print("\t"); Serial.print(az); Serial.print("\t"); Serial.print(gx); Serial.print("\t"); Serial.print(gy); Serial.print("\t"); Serial.println(gz); #endif
if(gx>10000) { musica(); } if(gx<-10000) { musicd(); } if(gy>10000) { musicb(); } if(gy<-10000) { musicf(); }
if(ay>10000) { musicc(); } if(ay<-10000) { musicd(); } if(ax>20000) { musice(); } if(ax<-20000) { musica(); } // blink LED to indicate activity blinkState = !blinkState; digitalWrite(LED_PIN, blinkState); }
void musica() { for (int thisNote = 0; thisNote < notesa * 2; thisNote = thisNote + 2) {
// calculates the duration of each note divider = pgm_read_word_near(melodya+thisNote + 1); if (divider > 0) { // regular note, just proceed noteDuration = (wholenote) / divider; } else if (divider < 0) { // dotted notes are represented with negative durations!! noteDuration = (wholenote) / abs(divider); noteDuration *= 1.5; // increases the duration in half for dotted notes } tone(buzzer, pgm_read_word_near(melodya+thisNote), noteDuration * 0.9); delay(noteDuration);
// stop the waveform generation before the next note. noTone(buzzer); } }
void musicb() { for (int thisNote = 0; thisNote < notesb * 2; thisNote = thisNote + 2) {
// calculates the duration of each note divider = pgm_read_word_near(melodyb+thisNote + 1); if (divider > 0) { // regular note, just proceed noteDuration = (wholenote) / divider; } else if (divider < 0) { // dotted notes are represented with negative durations!! noteDuration = (wholenote) / abs(divider); noteDuration *= 1.5; // increases the duration in half for dotted notes } tone(buzzer, pgm_read_word_near(melodyb+thisNote), noteDuration * 0.9); delay(noteDuration);
// stop the waveform generation before the next note. noTone(buzzer); } }
void musicc() { for (int thisNote = 0; thisNote < notesc * 2; thisNote = thisNote + 2) {
// calculates the duration of each note divider = pgm_read_word_near(melodyc+thisNote + 1); if (divider > 0) { // regular note, just proceed noteDuration = (wholenote) / divider; } else if (divider < 0) { // dotted notes are represented with negative durations!! noteDuration = (wholenote) / abs(divider); noteDuration *= 1.5; // increases the duration in half for dotted notes } tone(buzzer, pgm_read_word_near(melodyc+thisNote), noteDuration * 0.9); delay(noteDuration);
// stop the waveform generation before the next note. noTone(buzzer); } }
void musicd() { for (int thisNote = 0; thisNote < notesd * 2; thisNote = thisNote + 2) {
// calculates the duration of each note divider = pgm_read_word_near(melodyd+thisNote + 1); if (divider > 0) { // regular note, just proceed noteDuration = (wholenote) / divider; } else if (divider < 0) { // dotted notes are represented with negative durations!! noteDuration = (wholenote) / abs(divider); noteDuration *= 1.5; // increases the duration in half for dotted notes } tone(buzzer, pgm_read_word_near(melodyd+thisNote), noteDuration * 0.9); delay(noteDuration);
// stop the waveform generation before the next note. noTone(buzzer); } }
void musice() { for (int thisNote = 0; thisNote < notese * 2; thisNote = thisNote + 2) {
// calculates the duration of each note divider = pgm_read_word_near(melodye+thisNote + 1); if (divider > 0) { // regular note, just proceed noteDuration = (wholenote) / divider; } else if (divider < 0) { // dotted notes are represented with negative durations!! noteDuration = (wholenote) / abs(divider); noteDuration *= 1.5; // increases the duration in half for dotted notes } tone(buzzer, pgm_read_word_near(melodye+thisNote), noteDuration * 0.9); delay(noteDuration);
// stop the waveform generation before the next note. noTone(buzzer); } }
void musicf() { for (int thisNote = 0; thisNote < notesf * 2; thisNote = thisNote + 2) {
// calculates the duration of each note divider = pgm_read_word_near(melodyf+thisNote + 1); if (divider > 0) { // regular note, just proceed noteDuration = (wholenote) / divider; } else if (divider < 0) { // dotted notes are represented with negative durations!! noteDuration = (wholenote) / abs(divider); noteDuration *= 1.5; // increases the duration in half for dotted notes } tone(buzzer, pgm_read_word_near(melodyf+thisNote), noteDuration * 0.9); delay(noteDuration);
// stop the waveform generation before the next note. noTone(buzzer); } }