shashank t s
Published © GPL3+

Intelligent Wireless Sense-Cube

It is a wireless sensor cube built using Sony Spresense board which will help trigger actions based on the orientation of cube on a desk.

IntermediateFull instructions provided8 hours499

Things used in this project

Hardware components

Spresense boards (main & extension)
Sony Spresense boards (main & extension)
×1
Inertial Measurement Unit (IMU) (6 deg of freedom)
Inertial Measurement Unit (IMU) (6 deg of freedom)
×1
XL6009
×1
Adafruit Lithium Ion Polymer Battery
×1
Speaker: 0.25W, 8 ohms
Speaker: 0.25W, 8 ohms
×1
Stereo Audio Amplifier PAM8403
×1
Bluetooth Low Energy (BLE) Module (Generic)
×1

Hand tools and fabrication machines

Hot glue gun (generic)
Hot glue gun (generic)
Soldering iron (generic)
Soldering iron (generic)

Story

Read more

Schematics

Circuit diagram for peripherals

Connections are made as shown in the image

Circuit Diagram for Power Supply

Li-Po with charger circuit and Boost converter

Code

spresense.ino

Arduino
Code to be compiled and uploaded to Sony Spresense using Arduino IDE
//Include all libraries here

#include "MPU6050.h"
#include "Wire.h"
#include <SoftwareSerial.h>
#include <SDHCI.h>
#include <Audio.h>

MPU6050 accelgyro;
int16_t ax, ay, az;
int16_t gx, gy, gz;

int accel_read_x = 0;

SoftwareSerial mySerial(10, 11); // RX, TX
int ble_read = 0;
const int LED_1 = 7;
const int LED_2 = 6;
const int LED_3 = 5;

File myFile;

bool ErrEnd = false;

/**
 * @brief Audio attention callback
 *
 * When audio internal error occurc, this function will be called back.
 */

static void audio_attention_cb(const ErrorAttentionParam *atprm)
{
  puts("Attention!");
  
  if (atprm->error_code >= AS_ATTENTION_CODE_WARNING)
    {
      ErrEnd = true;
   }
}


void setup() {
  // join I2C bus (I2Cdev library doesn't do this automatically)
  Wire.begin();
  Serial.begin(9600);
  mySerial.begin(9600);
  accelgyro.initialize();
  pinMode(LED_1, OUTPUT);
  pinMode(LED_2, OUTPUT);
  pinMode(LED_3, OUTPUT);
  
  // start audio system
  theAudio = AudioClass::getInstance();

  theAudio->begin(audio_attention_cb);

  puts("initialization Audio Library");

  /* Set clock mode to normal */
  theAudio->setRenderingClockMode(AS_CLKMODE_NORMAL);

  /* Set output device to speaker with first argument.
   * If you want to change the output device to I2S,
   * specify "AS_SETPLAYER_OUTPUTDEVICE_I2SOUTPUT" as an argument.
   * Set speaker driver mode to LineOut with second argument.
   * If you want to change the speaker driver mode to other,
   * specify "AS_SP_DRV_MODE_1DRIVER" or "AS_SP_DRV_MODE_2DRIVER" or "AS_SP_DRV_MODE_4DRIVER"
   * as an argument.
   */
  theAudio->setPlayerMode(AS_SETPLAYER_OUTPUTDEVICE_SPHP, AS_SP_DRV_MODE_LINEOUT);

  /*
   * Set main player to decode stereo mp3. Stream sample rate is set to "auto detect"
   * Search for MP3 decoder in "/mnt/sd0/BIN" directory
   */
  err_t err = theAudio->initPlayer(AudioClass::Player0, AS_CODECTYPE_MP3, "/mnt/sd0/BIN", AS_SAMPLINGRATE_AUTO, AS_CHANNEL_STEREO);

  /* Verify player initialize */
  if (err != AUDIOLIB_ECODE_OK)
    {
      printf("Player0 initialize error\n");
      exit(1);
    }

  /* Open file placed on SD card */
  myFile = theSD.open("Sound.mp3");   //Time is UP audio
  /* Verify file open */
  if (!myFile)
    {
      printf("File open error\n");
      exit(1);
    }
  printf("Open! %d\n",myFile);

  /* Send first frames to be decoded */
  err = theAudio->writeFrames(AudioClass::Player0, myFile);

  if ((err != AUDIOLIB_ECODE_OK) && (err != AUDIOLIB_ECODE_FILEEND))
    {
      printf("File Read Error! =%d\n",err);
      myFile.close();
      exit(1);
    }

  puts("Play!");

  /* Main volume set to -16.0 dB */
  theAudio->setVolume(-160);
  theAudio->startPlayer(AudioClass::Player0);

}

void loop() {
  // read raw accel/gyro measurements from device
  accelgyro.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
  accel_read_x = ax;
  mySerial.write(ax);
  if ((accel_read_x > 1220) && (accel_read_x < 1350)) //Orientation 1 : The Original position
  {
  
digitalWrite(LED_1,HIGH);
digitalWrite(LED_2,LOW);
digitalWrite(LED_3,LOW);
  }
   else if ((accel_read_x > 234) && (accel_read_x < 720)) //Orientation 2
  {
  
  puts("loop!!");

  /* Send new frames to decode in a loop until file ends */
  int err = theAudio->writeFrames(AudioClass::Player0, myFile);

  /*  Tell when player file ends */
  if (err == AUDIOLIB_ECODE_FILEEND)
    {
      printf("Main player File End!\n");
    }

  /* Show error code from player and stop */
  if (err)
    {
      printf("Main player error code: %d\n", err);
      goto stop_player;
    }

  if (ErrEnd)
    {
      printf("Error End\n");
      goto stop_player;
    }

  /* This sleep is adjusted by the time to read the audio stream file.
     Please adjust in according with the processing contents
     being processed at the same time by Application.
  */

  usleep(40000);


  /* Don't go further and continue play */
  return;

stop_player:
  sleep(1);
  theAudio->stopPlayer(AudioClass::Player0);
  myFile.close();
  exit(1);
digitalWrite(LED_2,HIGH);
digitalWrite(LED_1,LOW);
digitalWrite(LED_3,LOW);
  }
     else if ((accel_read_x > 1770) && (accel_read_x < 2180)) //Orientation 3
  {
digitalWrite(LED_3,HIGH);
digitalWrite(LED_1,LOW);
digitalWrite(LED_2,LOW);
  }
  
  
  if (mySerial.available()) {
    Serial.print(mySerial.read());
    ble_read =  mySerial.read();
    if (ble_read == '1') {
      digitalWrite(13, LOW);
    }
    if (ble_read == '2') {
      digitalWrite(13, HIGH);
    }
  }

}

Credits

shashank t s

shashank t s

2 projects • 2 followers

Comments