Skip to content


Arduino Sound Part 2: Hello World

SoundChip This is the second in a series of articles about generating sound with an Arduino. The first article covered the various methods available for sound generation with an Arduino. In this article we take a small step; “Hello World” for Arduino sound. We prepare for our future experiments by hooking up a PC or powered speaker so we can hear the Arduino sing.

Driving a speaker.
To get started we need to drive a speaker. We could use a headset but to share sounds with others we need a speaker. Rather then build an audio power amplifier we can use our desktop PCs line input or a set of powered PC speakers.

Using a PC for sound output
The ideal setup would be to use a line input on your PC. The PC will do the work of amplifying the sound and driving the speakers. This has the added benefit of making it easy to record or use a program that lets you view the audio signals in real-time.

PCSoundConnectionsMost PCs today have a sound card with speaker outputs, microphone input, and a line input. Look at your sound connections, the connectors are usually color coded. Blue for line input, green for speaker out, and pink for microphone. You should find all three next to each other on the rear of your PC.

I recommend that you use the line input for these experiments. The microphone input might be OK but may require some careful circuitry and would be very sensitive to the voltage levels. The microphone input is designed for low level signals and provides amplification. The Line input is best for this project as it is designed for connection to an external device like an MP3 player. Please don’t use the microphone input unless you know what your doing and/or don’t mind breaking it.

Using Powered Speakers
A set of powered PC speakers would do the job as well. They have built in amplifiers to drive internal speakers and should work fine with line level signals. You will be all set for public demonstrations as you will have the sound output already taken care of. It might even be possible to build your project inside the speaker box. I bet there is lots of room inside the typical PC speaker box.

External speaker Amplifier
The big guns… Use your home stereo to really pump out the sound or build a custom speaker amp kit. This would be a good way to get enough power for a large presentation or event. Maybe you just want a powerful sound to scare the pants off someone next Halloween.

Line Input
For just about any project with sound output we can plan for connection to a line input jack. I think its safe to assume that any line input would be AC coupled and would accept signals at 1 Volt Peak to Peak.

AC Coupling?
AC coupling means that the input has a series capacitor which blocks DC levels. Any DC voltage on the line input will decay to 0V. This input capacitor will distort the sound slightly depending on it’s value. You need a large value to pass low frequencies without loosing signal levels.

1 Volt Peak to Peak (1VPP)?
This means that the signal waveforms we output need to max out at 1 Volt from top to bottom. The average value is not important here as the AC coupling will set the average to 0 Volts effectively. So if we are generating a signal that is output from a 5VDC source, we need to make sure that the difference between the lowest voltage and the highest voltage is no more then 1 Volt. I like to bias single supply analog signals to 1/2 the supply voltage. Since the supply is typically 5V the middle voltage is 2.5V. To meet the 1VPP requirement we keep the output between 3V and 2V or a swing of +/- 0.5 around 2.5V. This is bound to be confusing right now. When we get to looking at waveforms it will make more since.

Getting connected
BreadboardAudioJacks The line input on your sound card or powered speaker is probably a 1/8″ stereo jack. The neatest way to get hooked up would be to salvage one of these jacks from some gadget you don’t need anymore. I managed to scrounge up several jacks that work great with solderless breadboards. I use a long audio extension cable to run from the breadboard to the line input on my PC.

Another way is to find an old audio cable or purchase one at Radio Shack. Maybe you have an old Headset you can cut up. Wikipedia has an article showing the various types of stereo audio jacks and includes the pin outs. These jacks have three rings, the tip is left audio, the middle ring is right audio and the last ring is ground.

VoltageDividerWe will be using a Bit-Bang output from an I/O pin which switches from 0 to 5V so we need to attenuate it to about 1VPP. I recommend a simple voltage divider
as shown here. Just place the two resistors on your breadboard or build them into your cable. Leave room for two if you want to do stereo.

You probably don’t need a volume control as most things you would connect to already have one. To add volume control replace the 1K resistor with a potentiometer (POT). You might even find a dual pot to control the left and right channels at the same time. Add a volume POT by replacing the 1K resistor with a 1K pot as shown here.

“Hello World” in Sound
A “Hello World” program is software lingo for the simplest program we can do as a meaningful test. Just what we need to test out our development environment. In this case we need to hear some sound to see that everything is ready for our experiments.

“Hello World” in sound would be a simple Bit-Bang tone output from your microcontroller. As described in the previous article we just toggle an I/O pin on the Arduino. With this simple program we can test the connection to our speakers.

Here a version of “Hello World” for Arduino sound. It is a bit more advanced then it has to be. Usually a “Hello World” program should be the bare minimum but I could not resist a few good programming practices. Look over the code and read my description before you use it.

//Arduino Sound Hello World
//Created by David Fowler of uCHobby.com
//Define the I/O pin we will use for our sound output
#define SOUNDOUT_PIN 9

void setup(void){
//Set the sound out pin to output mode
pinMode(SOUNDOUT_PIN,OUTPUT);
}

void loop(void){
//Generate sound by toggling the I/O pin High and Low
//Generate a 1KHz tone. set the pin high for 500uS then
//
low for 500uS to make the period 1ms or 1KHz.

//Set the pin high and delay for 1/2 a cycle of 1KHz, 500uS.
digitalWrite(SOUNDOUT_PIN,HIGH);
delayMicroseconds(500);

//Set the pin low and delay for 1/2 a cycle of 1KHz, 500uS.
digitalWrite(SOUNDOUT_PIN,LOW);
delayMicroseconds(500);
}

Starting with the “void setup(void)” function. This function is run once when the microcontroller starts and is where we set the sound pin to operate in the output mode. I use the “SOUNDOUT_PIN” name which is defined a few lines above. I could have just put “9″ for digital pin 9 but using the define this way lets you change it to a different pin without having to hunt down every place the pin is used in the program. We can change the 9 to a 10 in the define and every place “SOUNDOUT_PIN” is used, changes automatically.

Next is the void loop(void) function. This function will toggle the I/O pin at 1Khz, a frequency that should be easy for anyone to hear. The Arduino system code will call Loop just after it calls setup (described above) then keep calling it every time it returns. All we need to do is generate one complete cycle in the loop function.

Changing the Frequency
The code above should generate a 1KHz tone. The frequency is caused by the toggling I/O pin. We get a 1Khz tone because there are 1000 cycles of the I/O pin per second. A cycle is one set of 0 state followed by the 1 state. This 0 to 1 back to 0 state happens 1000 times a second so we get a 1KHz tone.

To change the frequency we need to use different delay times. A longer delay would decrease the frequency because it would take longer to make a single cycle. The opposite is true for increasing the frequency. To generate 2Khz, twice 1Khz, we would half the times for both the high and low delays.

To calculate a new delay time for a given frequency, divide 1 by the frequency. This is also called taking the reciprocal. t=1/f where t is the cycle time and f is the frequency. We need to spread this total delay between the two delays, the high delay and the low delay. Just divide t by 2 to get the time we want the pin to stay high then low. This generates a square wave with a cycle time equal to t. In this case t=1mS so we need to set the pin high for 500uS (micro seconds) then low for the same. For a 2Khz tone, use 250uS for each of the delays.

Copy and paste this code or use the project file here. Once everything is connected and ready, load your Arduino and prepare to hear a tone.

Warning
This could be very loud so start with the volume turned way down. Remember to keep it down between experiments. You could hurt yourself if unexpectedly blasted with a 1KHz tone…

The sound is not very pure as your really generating an on/off or square wave type output. You can filter this with an RC (resistor capacitor) circuit but that will significantly attenuate the signal as you vary the frequency.

If you do not hear the sound, then check your wiring and the settings on your sound output system. Try using a volt meter on the output pin, it should read about 1/2 the supply voltage or about 2.5V. Mine read about 2.3V on the AC or DC scale. You could also connect an LED via a series resistor to the output pin. It should light dimmer then if the pin were at a constant state.

Here is a sample of the sound my Arduino made with a slightly modified version of the “Hello World” program above. I changed the code to generate 1000 cycles then delay for 1 second. The result is a beep beep beep forever. Try loading this sound sample into a sound editing program to see the waveform. The picture below shows the sound waveform in Audacity. Audacity is a great sound editor and also works well for recording and cutting samples from MP3 files. I highly recommend it for sound experiments. If you know of another program that should be considered please share it in the comments.SoundPulses

You can also try filtering the square wave a bit to get a different sound quality. Try placing a small capacitor such as a 0.1uF or 0.01uF across the resistor to ground. Also play with the code delays to generate other tones. Maybe you want to generate a simple sequence of tones to make some music.

Homework:
Try to do the following on your own. These exercises will prepare you for the next steps. If you have trouble try posting a comment asking for help.

  • Change the frequency of the tone
  • Make your version do the beep beep as I did above
  • Create a function to generate the tones
    I recommend that you create a function that takes the desired frequency or cycle time, the duration to play the sound for, and possibly the pin to drive. If you want to get the function done fast, use Paul Badger’s “freqout” function.
  • Install a sound editing program and take a look at some recorded sounds from your project.
  • Use an oscilloscope to see the signals while you play with the frequency or filter capacitors.
    One great way to learn is to look at the signals. If you don’t have an oscilloscope on your bench there are programs that work with the sound card input on your PC. I have tried a few programs and would welcome any suggestions. The one I like so far is Soundcard Oscilloscope by Christian Zeitnitz. Someone should do an article about these programs; hint.SoundcardOszilloscopeScreenShot

Parts 2, 3, …
The next article will work on creating nice sin wave outputs. I have played around with a few options and have settled on an R2R network. I also reconfigured a timer to generate regular timed interrupts for an accurate sample update for the R2R DAC. I expect to cover this in detail with future articles. I may break the timer interrupt and R2R DAC up into their own articles. I want to keep these short and too the point.

Comments Please
I would like to hear your thoughts on this article and sound generation in general. If you do something interesting or create a music sequence please tell us in the comments. Have any of you used software that turns the sound card input into an O-Scope? Another sound editor program perhaps? What do you recommend?

Posted in Discovering, Microcontroller, Projects.


15 Responses

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.

  1. pK - BricoGeek.com DIY in Spanish version says

    Wow, cool proyect. Maybe can be fine for my POV to do some sounds while turning. Maybe a wireless clock with alarm can be done! What do you think dfowler? ;)

  2. dfowler says

    pK,

    Sounds (LOL) like a good idea to me.

    I once consulted on a clock that played recorded bird sounds from an EPROM for the Alarm. It even ramped up the volume to gently wake the user. Although the clock worked fine, I dont think it woke people up. I know I could sleep all day listening to birds. Mabe we could use the recorded sound of a police siren or maybe a car accident…

  3. Berni says

    I suggest you give PWM a try.I tryed generating triangular and sine waveforms with that and it produced a amazingly clean waveform. I was running the PWM at frequency’s over 100Khz on a dsPIC and it had no problems generating a sine in the entire audio range.Made it sweep over the whole audio range and its amplitude is also adjustable.

    It was just some playing around, i had ideas to try store longer samples in external memory and play that instead of just looping a sine from memory.

  4. dfowler says

    Berni,

    I will try PWM very soon. It has the potential to be the best option of all. Uses a single I/O pin, acts as a DAC, easy to filter, plenty of resolution, may even make an amplifier easier to build.

    Thanks for the heads up.

  5. Berni says

    Oh and aculy the bit banging circuit you posted is what i had on the PWM pin and i got a completely clear signal in to the PC,meaty the sound card has enough input capacitance.

    If you ask me the PWM is the best method.It works just as good as a DAC but without the stupid serial line going to it.Setting the output level is just a mater of writing to memory so next to no CPU load.But the MCU has to be capable of working well at very high frequencys.(And have hardware PWM or CPU has a LOT of work)

    I was wondering if a serial EEPROM is fast enough for playback of uncompressed samples i guess atlest 64Kbit/s is needed

  6. Ryan says

    Have you considered using pulse width modulation to get a nice sine wave form?… if the thing can output fast enough it might work.

  7. Ryan says

    oops it helps if i read everyone else first (sorry)

  8. deastman says

    Thank you for writing these articles! I’m currently looking into using the Arduino to generate 0-10v control voltage signals to interface with my modular synthesizer. As far as I can tell, a DAC would be required to get a sufficient level of precision (only 256 values available from a digital out). I’m hoping you’ll address this in a future installment. Thanks again :)

  9. Trumpetto says

    Hi dfowler,
    Good work! I searched for a small synth solution for my arduino based electronic trumpet and found this nice list a while ago;
    http://jonbro.tk/blog/2007/09/23/single-chip-synths/

  10. Jim Thomas says

    I’ll add another voice to PWM, but with a twist. Earlier this year I used PWM to generate an analog output using a digital I/O pin on an AVR. I had very little capacitance to work with on this line, which made the cutoff frequency too high even when the bits were flying as fast as I could make them go. My first stab looked something like this:

    for(i=0; i

  11. Jim Thomas says

    …continued
    for(i=0; i

  12. Jim Thomas says

    guess you can’t post code in the comments.

  13. dfowler says

    Jim,

    Send the code to me via email at dfowler@uCHobby. I will find a way to get it into a comment for you. At the very least, I can add it to the end of this article with credit given to you.

    You might consider submitting it along with a description as an article to uCHobby. Doing so can get you an Arduino kit.

  14. DrLucas says

    I know it has been a while since this was made, but do you know of a way to generate specific tones? such as the keys to a piano?

  15. dfowler says

    DrLucas,

    The code and methods described in part 3 will generate the musical notes like a piano. There is no envelope control so the notes are not as nice but this maybe what you are looking for.