Motion to Sound

The accelerometer at the center of the Circuit Playground board allows for some great effects when it comes to using lights and sound.

The code below reads the accelerometer and outputs sound depending on the speed of motion.

// Adafruit Circuit Playground - Movement to Sound    Support Open Source, buy Adafruit
//   2016-08-07 Version 1  by Mike Barela for Adafruit Industries
// Uses the CircuitPlayground library 

#include <Adafruit_CircuitPlayground.h>

void setup() {
  CircuitPlayground.begin();   // initialize the Circuit Playground library
  Serial.begin(9600);
}
 
void loop() {
   float movementX, movementY, movementZ, movement;
   uint16_t sound;
   if(CircuitPlayground.slideSwitch()) {   // sense & play when slide whitch at "+"
      movementX = abs(CircuitPlayground.motionX());  // read the X motion (absolute value)
      movementY = abs(CircuitPlayground.motionY());  // read the Y motion (absolute value)
      movementZ = abs(CircuitPlayground.motionZ());  // read the Z motion (absolute value)
      movement = movementX + movementY + movementZ;  // aggregate the movement sensed
      Serial.println(movement);
      sound = (int) map(movement, 8.0, 60.0, 440.0, 1760.0); // map movement to music values
      CircuitPlayground.playTone(sound, 500);   //   play sound for 500 milliseconds
   }
}

The more rapidly you shake your Circuit Playground, the higher the pitch of sound it output. Be careful of your power cord or battery pack while swinging it around.

Any movement in the X, Y, or Z axis is detected.  The abs function makes movement in either direction (negative or positive) positive as we're just looking to detect movement, not movement in a specific direction. All movement is added together and printed out on the serial monitor for inspection. The movement is mapped from values observed from 8 to 60 (you can change these if you observe different minimum and maximum values). I picked the low A and higher A notes from previous examples but you can use any frequency range you want.

Applications

  • Bicycle sounds - connect to your bike spokes near the tire/rim side. Use tie wraps or other connection to ensure it does not slip of fall off or your battery pack does not fly off.  Now the faster you pedal, the higher the sound.
  • Skate sounds - placed on your board, it will make different sounds based on how hast you are going.
  • Drop sensor - makes a sound if Circuit Playground detects it has fallen.
Last updated on 2016-08-14 at 03.40.20 AM Published on 2016-08-17 at 02.09.02 PM