Output Devices

There are many other output dievices that you can attach to the Aruduino, other than LEDs. However, you always have to be careful that the output device does not take too much current. As usual, we will include resistors in the circuit to slow down the current and make sure the Arduino does not get damaged. Unfortunately, this means that large, powerful devices like loudspeakers and big motors cannot be used directly. There are ways around this, but they need more components that are not included in your kit.

Speaker Circuit

The Arduino digital outputs can send varying electrical signals to a small speaker to make it vibrate and produce sound. It is very simple to connect a speaker (but you MUST include a resistor!), as shown in the pictures below

Speaker Circuit

Testing the Speaker

Connect a speaker to the Arduino as shown above. Then connect the Arduino to your computer and open the IDE (Integrated Development Environment). Here is a simple sketch that will make the Arduino vibrate the speaker:

       
            /*  Testing the speaker
             Richard Taylor
             2014-12-11
             */
 
            int T = 2;  // period of vibration in milliseconds
            int speakerPin = 8;  // speaker connected to pin 8 with a resistor

            void setup() {
                pinMode(speakerPin, OUTPUT);
            }

            void loop() {
              digitalWrite(speakerPin, HIGH);
              delay(int(T/2));    // delays are half the period
              digitalWrite(speakerPin, LOW);
              delay(int(T/2));
            }
        

Notice that this sketch is very similar to blinking an LED. In fact, it just turns the power to the speaker on and off very quickly. Our ears are much better at hearing very fast vibrations that our eyes are! In the sketch above, the "period" of the vibration is 2 milliseconds (2 ms). The power is on for half of the period (T/2) and off for the other half of the period. Try changing the value of T in the global variables section of the sketch to other, larger values. As the period gets larger, the sound gets_______________. The inverse of the period is called frequency. If the period is 2 ms, the frequency is 500 cycles per second. (1/500 = 0.002 = 2 ms) How long a period (how low a frequency) do you have to make before it stops sounding like a musical note and just sounds like a fast clicking sound?

This method of making musical notes is not very easy to use because the delay function can only take integer values so only a limited number of different tones are possible. However, the Arduino programming language includes a special command called "tone" that you can use to create a signal with a particular frequency. Frequency means how quickly the signal changes back and forth in cycles per second (also called Hertz or Hz). Low frequencies are low notes and high frequencies are high notes. Musicians like to use a middle tone of "A" with a frequency of 440 Hz to tune their instruments.

       
            /*
              Play a Note
              Plays a single tone on aspeaker circuit:
                8-ohm speaker plus 100 ohm resistor on digital pin 8
 
              by Richard Taylor
             2014-08-15
            */

            void setup() {
              // no setup needed
            }

            void loop() {
                // play the tone using pin 9 with frequency 440 Hz for 1000 ms = 1 second
              tone(8, 440, 1000);
                // wait for 2 seconds after starting the tone before repeating
              delay(2000);
            }
        

Once you've got this working, try making changes to the sketch. Try different frequencies, lengths and delays. Can you play a series of notes? Can you play a recognizable tune? (This tutorial web page contains a list of musical notes and their frequencies: http://arduino.cc/en/Tutorial/Tone. The tutorial itself contains a lot more complicated sketch that you don't need right now.)

Next page: sensing other things

Back to the main menu.