IR sensor signal to speaker

I am totally new totally new to arduino and am doing a school project with my daughter

using the following code to sense a distance. How do I make the arduino send a signal to speaker indicating different distances?

Simple IR Distance Sensor using a Panasonic pna4602m IR sensor and an IR led.

I am using the following tutorial code. I would appreciate any help

//define pins. I used pins 4 and 5
#define irLedPin 4 // IR Led on this pin
#define irSensorPin 5 // IR sensor on this pin

int irRead(int readPin, int triggerPin); //function prototype

void setup()
pinMode(irSensorPin, INPUT);
pinMode(irLedPin, OUTPUT);
// prints title with ending line break
Serial.println(“Program Starting”);
// wait for the long string to be sent

void loop()
Serial.println(irRead(irSensorPin, irLedPin)); //display the results
delay(10); //wait for the string to be sent


  • This function can be used with a panasonic pna4602m ir sensor
  • it returns a zero if something is detected by the sensor, and a 1 otherwise
  • The function bit bangs a 38.5khZ waveform to an IR led connected to the
  • triggerPin for 1 millisecond, and then reads the IR sensor pin to see if
  • the reflected IR has been detected
    int irRead(int readPin, int triggerPin)
    int halfPeriod = 13; //one period at 38.5khZ is aproximately 26 microseconds
    int cycles = 38; //26 microseconds * 38 is more or less 1 millisecond
    int i;
    for (i=0; i <=cycles; i++)
    digitalWrite(triggerPin, HIGH);
    digitalWrite(triggerPin, LOW);
    delayMicroseconds(halfPeriod - 1); // - 1 to make up for digitaWrite overhead
    return digitalRead(readPin);

That particular IR receiver can't determine distance. As your notes say, it returns zero if it's receiving, and one if it's not.

I changed to IR distance sensor (GP2YoA21YK0F). Do I need to make changes to the code?

Do I need to make changes to the code?

Well, duh!

What, EXACTLY, do you want to send to the speaker if the distance is 4cm? 38cm? 127cm? If you are expecting the Arduino to speak the distance, I think you are going to be disappointed.

Well, duh!

Seriously?, you have an appalling attitude Mr Scarpa (the world is a small place!) and I bet you wouldn't speak to someone in the real world like that, this attitude only costs you customers

It costs nothing to be nice

Okay, here’s some code. (It’s just a basic theremin) Please note that this will only sense objects from 4" (10 cm) away to 32" (80 cm). Connect the black wire to ground. Connect the red wire to 5V. Finally, connect the white wire to A0. Also, connect a speaker using this tutorial to pin 8. Note: it doesn’t have to be an Uno. Here’s the code (untested):

int recvPin=A0; //IR sensor
int speaker=8;  //speaker (8 ohm)
void setup(){
  Serial.begin(9600); //Remove to disable Serial
void loop(){
  double volt=analogRead(recvPin) * 5 / 1023; //analog-to-volt- 0=0V,1023=5V
  if(volt<0.5){ //adjust this number to change the sensitivity for not playing.
    noTone(speaker); // just to make sure that nothing plays when an object is too far away
    int freq=(volt - 0.5) * (10000 - 1000) / (3 - 0.5) + 1000; //volt to frequency: 0.5V=1000Hz,3V=10000Hz (both audible)
    tone(speaker,freq); //play it
    Serial.print("Volts: ");            //print debug info. You don't need this,
    Serial.print(volt);                 //you can remove these lines from up one line
    Serial.print("V Frequency: ");
    Serial.println("Hz");               //to here
  delay(5); //just to keep power consumption down, you don't need this - or you could change it.

Note: I am not responsible for anything bad that could happen. If you want to check my source, click here. To debug, open Serial Monitor in 9600 baud.

Note: that will only work with a peizo buzzer. You will need a 100-ohm resistor from the speaker to pin 8 for an 8ohm speaker.