Sound Sensor Not Working in My Circuit

Hi, I am working on a head-controlled mouse project for a class of mine for a quadriplegic client. It involves the use of an MPU6050 module for mouse movement control, and if the mouse is held in a certain spot, it clicks. My teacher wanted me to include some sort of functionality to get the right click to work, so I'm employing a sound sensor at the moment because that is what I have available in the kit I was provided.

Here is my circuit-- I'm sorry for the terrible picture btw-- I don't think this is the issue of my error, but I thought I would attach it just in case:

I think the error is in my code, but I'm not sure what my error is and how to fix it. Please see the following. Thank you in advance for helping me with my project and taking the time to help me!

/* Code to control the mouse pointer with the movement of the head
 * and assisted click.
 * Change the values at vx and vy (+300 and -100 in my case) 
 * setting them with the TEST code.
 * 
 * Gabry295 (edits by lagoudemos)
*/

#include <Wire.h>
#include <I2Cdev.h>
#include <MPU6050.h>
#include <Mouse.h>

#define SENSOR_PIN 5

MPU6050 mpu;
int16_t ax, ay, az, gx, gy, gz;
int vx, vy, vx_prec, vy_prec;
int count=0;

int lastState = HIGH; //the previous state from the input pin
int currentState; //the current reading from the input pin

void setup() {
  Serial.begin(9600); //initialize serial communication at 9600 bits per second
  Wire.begin();
  mpu.initialize();
  pinMode (SENSOR_PIN, INPUT);  //initializes the arduino's pin as an input
  if (!mpu.testConnection()) {
    while (1);
    }
}

void loop() {
  mpu.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);

  vx = (gx+500)/200;  // "+5" because the x axis of gyroscope give values about -350 while it's not moving. Change this value if you get something different using the TEST code, chacking if there are values far from zero.
  vy = -(gz-100)/200; // same here about "-100"
  
  Mouse.move(vx, vy);

  currentState = digitalRead (SENSOR_PIN); 
  
  if ( (vx_prec-5)<=vx && vx<=vx_prec+5 && (vy_prec-5)<=vy && vy<=vy_prec+5) { // checking the pointer doesn't move too much from its actual position: (in this case a 10 pixel square)
    count++;                                                                  
    if(count == 200){ // the click will happen after 4 seconds the pointer has stopped in the 10px square: 20ms of delay 200 times it's 4000ms = 2s
      if (!Mouse.isPressed(MOUSE_LEFT)) {
        Mouse.press(MOUSE_LEFT);
        count = 0;
      }
    }
    else {
      if (Mouse.isPressed(MOUSE_LEFT)) {
        Mouse.release(MOUSE_LEFT);
      }
    }
  }

  else if (lastState == HIGH && currentState == LOW)
  {
    Mouse.press(MOUSE_RIGHT);
    Serial.println("The sound has been detected");
  }

  else if (lastState == LOW && currentState == HIGH)
  {
    Mouse.release(MOUSE_RIGHT);
    Serial.println("The sound has disappeared");
  }
  
  else {
    vx_prec = vx; // updating values to check the position of the pointer and allow the click
    vy_prec = vy;
    count = 0;
    }
  lastState = currentState;
  delay(20);
}

That's not good. What would helpers look for? Why do You think there's an error?

Did it work correctly before you edited it?

Sorry, i should clarify, the click function with the sound sensor just isnt working. There is no output for print statement saying the sound isnt or is detected.

Yeah :frowning:

  • Have you proven the sound sensor works with a simple diagnostic sketch ?

  • You need to have the comparator potentiometer set so an output is created with sound input.

Example:


#define sensorPin 8

// Variable to store the time when last event happened
unsigned long lastEvent = 0;

void setup() {
	pinMode(sensorPin, INPUT);	// Set sensor pin as an INPUT
	Serial.begin(9600);
}

void loop() {
	// Read Sound sensor
	int sensorData = digitalRead(sensorPin);

	// If pin goes LOW, sound is detected
	if (sensorData == LOW) {
		
		// If 25ms have passed since last LOW state, it means that
		// the clap is detected and not due to any spurious sounds
		if (millis() - lastEvent > 25) {
			Serial.println("Clap detected!");
		}
		
		// Remember when last event happened
		lastEvent = millis();
	}
}

Don't be sorry, get better at taking photos. So far that photograph just shows wires going off out of frame. We have absolutely no idea to where these go to on the Arduino. Try posting a real schematic.

Haven't got one? Then how on earth could you make it in the first place?

Care to elaborate? Did you test each change before making the next one?

Hi, @lagoudemos

Can you please post a copy of your circuit, a picture of a hand drawn circuit in jpg, png?
Hand drawn and photographed is perfectly acceptable.
Please include ALL hardware, power supplies, component names and pin labels.

Disconnect the IMU and write some code to JUST detect the sound.

Forget the IMU for the moment, lets build your project in stages, proving each stage before adding the next.

Tom... :smiley: :+1: :coffee: :australia:

1 Like

Hi all.

I appreciate all of your comments and suggestions, but I ended up getting it to work out. Sorry I didn't take the time to draw out the diagram prior to that, but as I suspected it wasn't a circuit problem but a problem with my code. If you're curious here is the revised version of my code that is properly functional!

Cheers!

/* Code to control the mouse pointer with the movement of the head
   and assisted click.
   Change the values at vx and vy (+300 and -100 in my case)
   setting them with the TEST code.

   Gabry295 (edits by lagoudemos)
*/

#include <Wire.h>
#include <I2Cdev.h>
#include <MPU6050.h>
#include <Mouse.h>
#define SENSOR_PIN 5

MPU6050 mpu;
int16_t ax, ay, az, gx, gy, gz;
int vx, vy, vx_prec, vy_prec;
int count = 0;

int lastState; //the previous state from the input pin
int currentState; //the current reading from the input pin

void setup() {
  Serial.begin(9600); //initialize serial communication at 9600 bits per second
  Wire.begin();
  mpu.initialize();
  if (!mpu.testConnection()) {
    while (1);
  }
  pinMode (SENSOR_PIN, INPUT);  //initializes the arduino's pin as an input
  lastState = digitalRead(SENSOR_PIN);
  }

void loop() {
  mpu.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);

  vx = (gx + 500) / 200; // "+5" because the x axis of gyroscope give values about -350 while it's not moving. Change this value if you get something different using the TEST code, chacking if there are values far from zero.
  vy = -(gz - 100) / 200; // same here about "-100"
  currentState = digitalRead (SENSOR_PIN);
  Mouse.move(vx, vy);

  if (lastState == HIGH && currentState == LOW)
   {
    Mouse.press(MOUSE_RIGHT);
    Serial.println("The sound has been detected");
  }

  else if (lastState == LOW && currentState == HIGH)
   {
    Mouse.release(MOUSE_RIGHT);
    Serial.println("The sound has disappeared");
  }

  if ( (vx_prec - 5) <= vx && vx <= vx_prec + 5 && (vy_prec - 5) <= vy && vy <= vy_prec + 5) { // checking the pointer doesn't move too much from its actual position: (in this case a 10 pixel square)
    count++;
    if (count == 200) { // the click will happen after 4 seconds the pointer has stopped in the 10px square: 20ms of delay 200 times it's 4000ms = 2s
      if (!Mouse.isPressed(MOUSE_LEFT)) {
        Mouse.press(MOUSE_LEFT);
        count = 0;
      }
    }
    else {
      if (Mouse.isPressed(MOUSE_LEFT)) {
        Mouse.release(MOUSE_LEFT);
      }
    }
  }

  else {
    vx_prec = vx; // updating values to check the position of the pointer and allow the click
    vy_prec = vy;
    count = 0;
  }
  lastState = currentState;
  delay(20);
}
type or paste code here