Sensor audio problem

Hi Folks,

Appologies if this is considered a cross post between here and adafruit where I also asked the question. Just a newbie looking for some guidance. :slight_smile:
I'm working with an Arduino UNO, the Sharp IR Sensor (GP2Y0A21YK0F), and a 3.5mm Stereo Headphone Jack (ADAFRUIT PRODUCT ID: 1699). My problem is that once I load the following code onto the arduino I receive a constant high pitched tick/pulse of audio, but when my sensor trips the threshold I recieve the proper tone from the playtone function. I would like for the ticking/pulsing to go away and be left with silence until my 'distsensor' is tripped (see code). Can someone point out what I'm doing wrong? Thanks in advance folks! >> I could always email an audio file to someone if that would help. Thought - it sounds like the

int pinSpeaker = 10;           //Set up a speaker on a PWM pin (digital 9, 10, or 11)
int val = 0;                    // variable for reading the pin status

void setup() {
  // put your setup code here, to run once:
Serial.begin(9600);           // set up Serial library at 9600 bps
Serial.println(F("Sonar test!"));
pinMode(pinSpeaker, OUTPUT);  // declare speaker as output
}

void loop() { 
   int distsensor, i;
   long time;

   distsensor = 0;
   for (i=0; i<8; i++) {
     distsensor += analogRead(0);
     //delay(500);
   }
   distsensor /= 8;
 
   Serial.print(F("Sensor = ")); Serial.println(distsensor);

   if (distsensor > 300) {
     playTone(300, 160);
    //delay(150);
   }
   //if (distsensor > 500) {
}
void playTone(long duration, int freq) {
    duration *= 500;
    int period = (1.0 / freq) * 1000000;
    long elapsed_time = 0;
    while (elapsed_time < duration) {
        digitalWrite(pinSpeaker,HIGH);
        delayMicroseconds(period / 2);
        digitalWrite(pinSpeaker, LOW);
        delayMicroseconds(period / 2);
        elapsed_time += (period);

    }
}

Don't know what causes the ticks, however, you could try to use the Arduino Tone library, it uses timer interrupts instead of delays, which is a much 'cleaner' approach.

Hi,

Note that Adafruit's is a completely different forum. Cross posting between here and there is not an issue.

--Michael

The high pitched noise is inherent in sticking a speaker on a digital output - you are hearing the
digital noise on the 5V supply caused by the microcontroller running or the Sharp sensor or both.

Listening to such noise can even be a useful way to debug code as the sounds depends on the
pattern of behaviour of the code.

Removing it is done by buffering the signal using an inverter/buffer chip powered from a clean
power supply used just for audio (best if that's not from a DC-DC converter direct, but from
a linear voltage regulator which can reject switching noise). That gives a clean version of the
logic signal without all the hash from the digital supply.

PieterP:
Don't know what causes the ticks, however, you could try to use the Arduino Tone library, it uses timer interrupts instead of delays, which is a much 'cleaner' approach.

Hi and thanks for responding... I did try the tone library but encountered the same results (see code). What I did learn is that if I comment out the pinspeaker I do not get the ticking, but I also do not get the tone. To that end I think MarkT is correct in that the ticking I'm hearing is caused by the Sharp sensor. Unless this can be rseolved by code (which I don't think is the case, I'm going to have to start looking into a linear voltage regulator.

//int pinSpeaker = 10;           //Set up a speaker on a PWM pin (digital 9, 10, or 11)
int val = 0;                    // variable for reading the pin status

void setup() {
  // put your setup code here, to run once:
Serial.begin(9600);           // set up Serial library at 9600 bps
Serial.println(F("Sonar test!"));
//pinMode(pinSpeaker, OUTPUT);  // declare speaker as output
}

void loop() { 
   int distsensor, i;
   //long time;

   distsensor = 0;
   for (i=0; i<8; i++) {
     distsensor += analogRead(0);
     //delay(500);
   }
   distsensor /= 8;
 
   Serial.print(F("Sensor = ")); Serial.println(distsensor);

  //if (distsensor < 300) {
   // noTone(A2);
    //playTone(300, 160);
    //delay(150);
   //}
   if (distsensor > 300) {
    tone(A2, 750, 250);
   }
   else
   {
    noTone(A2);
   }
  
}
/*void playTone(long duration, int freq) {
    duration *= 500;
    int period = (1.0 / freq) * 1000000;
    long elapsed_time = 0;
    while (elapsed_time < duration) {
        digitalWrite(pinSpeaker,HIGH);
        delayMicroseconds(period / 2);
        digitalWrite(pinSpeaker, LOW);
        delayMicroseconds(period / 2);
        elapsed_time += (period);

    }
}
 */

MarkT:
The high pitched noise is inherent in sticking a speaker on a digital output - you are hearing the
digital noise on the 5V supply caused by the microcontroller running or the Sharp sensor or both.

Listening to such noise can even be a useful way to debug code as the sounds depends on the
pattern of behaviour of the code.

Removing it is done by buffering the signal using an inverter/buffer chip powered from a clean
power supply used just for audio (best if that's not from a DC-DC converter direct, but from
a linear voltage regulator which can reject switching noise). That gives a clean version of the
logic signal without all the hash from the digital supply.

Hi Mark and thanks for your reply. Perfect! The voltage regulator did the trick producing the exact results I was looking for. Guess who learned something new about digital output today :slight_smile:

Thanks again to all of you.