I'm new to Arduino and am planning on making a simple program in Arduino that will play and loop a sound file on the computer whilst controlling the loudness of the music based on reading from a light sensor.

I was wondering if the first part, playing and looping the music on the computer is possible, because the second part of controlling the volume with the light sensor seems more straight forward if I had the first part down.


  • 2
    Did you really mean “play and loop a sound file on the computer”? Of course it is possible. How to do it depends on your computer’s OS, and it is not a Arduino question. Your Arduino would simply send the light readings through the serial port, and how you use those readings is up to you. – Edgar Bonet Sep 22 '16 at 7:52
  • You probably could make an analog circuit that does this. Use the gain of a mosfet together with a photoresistor. Or make the Arduino read out a photoresistor and control some type of volume circuitry. – Paul Jul 20 '17 at 19:58

Having the Arduino play the audio doesn't really make sense for the scenario you describe.

I would suggest you write an application on the PC that plays music and takes in input from and external source that is used to control the volume. You could then use the Arduino (connected via USB) to relay the light sensor readings to your PC application. The PC application then does all the "heavy lifting" since it has significantly more resources available than compared to the Arduino.

| improve this answer | |

Assuming you are asking about an Arduino Uno, the Arduino Uno is playing the sound and the project is not using a sound shield.

I was wondering if the first part, playing and looping the music on the computer is possible

The standard sampling rate of telephony (usually consider the lowest quality mono audio) is 8KHz. The ATmega328 found on the Arduino Uno has 32 KB of memory. This means, if all this memory were available, it would only hold 4 seconds of sound. Of course you need some of this memory to hold programming. So the time you have for the recording would be less.

You might conceive of compression techniques that double or triple the length of the recording. But it is not likely you will be able to implement a complex decompression algorithm such as mp3.

This project uses milliseconds long sound samples and a looping technique to recreate the sound of a Scottish Chanter.

second part of controlling the volume with the light sensor

If a short recording is acceptable, most sound generating programs use one of the PWM ATmega328 pins. As the previously linked to Arduino project does. Usually the magnitude of the sample is written into the PWM duty cycle register. This makes the average voltage of the PWM output roughly similar to the magnitude of the stored value.

To control volume, divide this value by various numbers making it smaller before writing it to the PWM duty cycle register.

There are several common light sensor technologies available. I would recommend a photo resistor because they have a slow reaction time which has a tendency to remove noise. And are, in general, easier to use then, say, a photo diode.

| improve this answer | |
  • The question is about controlling volume on the computer, just FYI :p – Len Sep 22 '16 at 10:16

Not the answer you're looking for? Browse other questions tagged or ask your own question.