# Arduino Lesson 10. Making Sounds

## Overview

In this lesson, you will learn how to make sounds with your Arduino. First you will make the Arduino play a 'musical' scale and then combine this with a photocell, to make a Theremin-like instrument that changes the pitch played as you wave your hand over the photocell.

![](https://cdn-learn.adafruit.com/assets/assets/000/002/293/medium800/learn_arduino_overview.jpg?1396781409)

# Arduino Lesson 10. Making Sounds

## Parts

# Arduino Lesson 10. Making Sounds

## Playing a Scale

For the first part of this lesson, the only thing on the breadboard is the Piezo buzzer. One pin of the piezo sounder goes to GND connection and the other to digital pin 12.

![](https://cdn-learn.adafruit.com/assets/assets/000/002/300/medium800/learn_arduino_just_sounder.jpg?1396781468)

![](https://cdn-learn.adafruit.com/assets/assets/000/002/303/medium800/learn_arduino_fritzing.jpg?1396781518)

Program your Arduino with the following sketch:

```auto
/*
Adafruit Arduino - Lesson 10. Simple Sounds
*/

int speakerPin = 12;

int numTones = 10;
int tones[] = {261, 277, 294, 311, 330, 349, 370, 392, 415, 440};
//            mid C  C#   D    D#   E    F    F#   G    G#   A

void setup()
{
  for (int i = 0; i &lt; numTones; i++)
  {
    tone(speakerPin, tones[i]);
    delay(500);
  }
  noTone(speakerPin);
}

void loop()
{
}
```

To play a note of a particular pitch, you specify the frequency. See the following section on sound. The different frequencies for each note are kept in an array. An array is like a list. So, a scale can be played by playing each of the notes in the list in turn.

The 'for' loop will count from 0 to 9 using the variable 'i'. To get the frequency of the note to play at each step, we use 'tone[i]'. This means, the value in the 'tones' array at position 'i'. So, for example, 'tones[0]' is 261, 'tones[1]' is 277 etc.

The Arduino command 'tone' takes two parameters, the first is the pin to play the tone on and the second is the frequency of the tone to play.

When all the notes have been played, the 'noTone' command stops that pin playing any tone.

We could have put the tone playing code into 'loop' rather than 'setup', but frankly the same scale being played over and over again gets a bit tiresome. So in this case, 'loop' is empty.

To play the tune again, just press the reset button.

# Arduino Lesson 10. Making Sounds

## Sound

Sound waves are vibrations in the air pressure. The speed of the vibrations (cycles per second or Hertz) is what makes the pitch of the sound. The higher the frequency of the vibration, the higher the pitch.

![](https://cdn-learn.adafruit.com/assets/assets/000/002/301/medium800/learn_arduino_sound.png?1396781508)

Middle C is usually defined as a frequency of 261 Hz. If you turn a digital output on and off again 261 times every second then that output will be middle C.

To hear the output, we need to attach something that will convert the electrical signal into sound waves. This can be done with a loudspeaker or as we have used here a piezo sounder.

Piezo sounders use a special crystal that expands and contracts as an electrical signal passes through it. This will generate a tone that we can hear.

# Arduino Lesson 10. Making Sounds

## Pseudo-Theremin

The Theramin ([http://en.wikipedia.org/wiki/Theremin](http://en.wikipedia.org/wiki/Theremin)) is a musical instrument that makes spooky synthesized sounds as you wave your hands in front of it. It was used in the theme music for the original Star Trek series.

We are going to make a similar instrument, albeit a lot less musical, but it will change the pitch of the note as you wave your hand in front of it.

We can leave the piezo sounder where it is, attached directly to the Arduino, but we will need the breadboard for the photocell and resistor that are going to control the pitch.

![](https://cdn-learn.adafruit.com/assets/assets/000/002/302/medium800/learn_arduino_fritzing2.jpg?1396781513)

# Arduino Lesson 10. Making Sounds

## Arduino Code

Upload the following code on to your Arduino board.

```auto
/*
Adafruit Arduino - Lesson 10. Pseudo Thermin
*/

int speakerPin = 12;
int photocellPin = 0;

void setup()
{
}

void loop()
{
  int reading = analogRead(photocellPin);
  int pitch = 200 + reading / 4;
  tone(speakerPin, pitch);
}
```

The sketch is actually really straightforward. We simply take an analog reading from A0, to measure the light intensity. This value will be in the range of something like 0 to 700.

We add 200 to this raw value, to make 200 Hz the lowest frequency and simply add the reading divided by 4 to this value, to give us a range of around 200Hz to 370Hz.

# Arduino Lesson 10. Making Sounds

## Other Things to Do

Try changing the value 4 in the line below to lower and higher values. &nbsp;

```auto
int pitch = 200 + reading / 4;
```

This will expand or restrict the range of frequencies.  
  
&nbsp;Returning to the first sketch, try and modify it to play a tune. Hint, you can just change the values in the 'tones' array. Note that if you change the number of notes from 10 notes, then you will need to change 'numTones' accordingly.

[Click Here for the Next Lesson](http://learn.adafruit.com/adafruit-arduino-lesson-11-lcd-displays-1)
 **About the Author**  
  
Simon Monk is author of a number of books relating to Open Source Hardware. The following books written by Simon are available from Adafruit:&nbsp;[Programming Arduino](https://www.adafruit.com/products/1019 "Link: https://www.adafruit.com/products/1019"),&nbsp;[30 Arduino Projects for the Evil Genius](https://www.adafruit.com/products/868)&nbsp;and&nbsp;[Programming the&nbsp;Raspberry Pi](https://www.adafruit.com/index.php?main_page=adasearch&q=programming+raspberry+pi).
## Featured Products

### Piezo Buzzer

[Piezo Buzzer](https://www.adafruit.com/product/160)
Piezo buzzers are used for making beeps, tones and alerts. This one is petite but loud! Drive it with 3-30V peak-to-peak square wave. To use, connect one pin to ground (either one) and the other pin to a square wave out from a timer or microcontroller. For the loudest tones, stay around 4 KHz,...

In Stock
[Buy Now](https://www.adafruit.com/product/160)
[Related Guides to the Product](https://learn.adafruit.com/products/160/guides)
### Photo cell (CdS photoresistor)

[Photo cell (CdS photoresistor)](https://www.adafruit.com/product/161)
CdS cells are little light sensors. As the squiggly face is exposed to more light, the resistance goes down. When it's light, the resistance is about ~1KΩ, when dark it goes up to ~10KΩ.

To use, connect one side of the photocell (either one, it's symmetric) to power...

In Stock
[Buy Now](https://www.adafruit.com/product/161)
[Related Guides to the Product](https://learn.adafruit.com/products/161/guides)
### Premium Male/Male Jumper Wires - 40 x 6" (150mm)

[Premium Male/Male Jumper Wires - 40 x 6" (150mm)](https://www.adafruit.com/product/758)
Handy for making wire harnesses or jumpering between headers on PCB's. These premium jumper wires are 6" (150mm) long and come in a 'strip' of 40 (4 pieces of each of ten rainbow colors). They have 0.1" male header contacts on either end and fit cleanly next to each other...

In Stock
[Buy Now](https://www.adafruit.com/product/758)
[Related Guides to the Product](https://learn.adafruit.com/products/758/guides)
### Half Sized Premium Breadboard - 400 Tie Points

[Half Sized Premium Breadboard - 400 Tie Points](https://www.adafruit.com/product/64)
This is a cute, half-size breadboard with&nbsp;400 tie points, good for small projects. It's 3.25" x 2.2" / 8.3cm&nbsp;x 5.5cm&nbsp;with a standard double-strip in the middle and two power rails on both sides.&nbsp;You can pull the power rails off easily to make the breadboard as...

In Stock
[Buy Now](https://www.adafruit.com/product/64)
[Related Guides to the Product](https://learn.adafruit.com/products/64/guides)
### Adafruit METRO 328 Fully Assembled - Arduino IDE compatible

[Adafruit METRO 328 Fully Assembled - Arduino IDE compatible](https://www.adafruit.com/product/50)
We sure love the ATmega328 here at Adafruit, and we use them&nbsp;_a lot_&nbsp;for our own projects. The processor has plenty of GPIO, Analog inputs, hardware UART SPI and I2C, timers and PWM galore - just enough for most simple projects. When we need to go small, we use a <a...></a...>

Out of Stock
[Buy Now](https://www.adafruit.com/product/50)
[Related Guides to the Product](https://learn.adafruit.com/products/50/guides)

## Related Guides

- [Arduino Lesson 3. RGB LEDs](https://learn.adafruit.com/adafruit-arduino-lesson-3-rgb-leds.md)
- [Metal Inlay Capacitive Touch Buttons](https://learn.adafruit.com/metal-inlay-capacitive-touch-buttons.md)
- [Analog Feedback Servos](https://learn.adafruit.com/analog-feedback-servos.md)
- [Line Following Zumo Robot Using Simulink](https://learn.adafruit.com/line-following-zumo-robot-programmed-with-simulink.md)
- [Arduino Lesson 12. LCD Displays - Part 2](https://learn.adafruit.com/adafruit-arduino-lesson-12-lcd-displays-part-2.md)
- [Arduino Lesson 17. Email Sending Movement Detector](https://learn.adafruit.com/arduino-lesson-17-email-sending-movement-detector.md)
- [Collin's Lab: MIDI](https://learn.adafruit.com/collins-lab-midi.md)
- [Making Adabot: Part 2](https://learn.adafruit.com/making-adabot-part-2.md)
- [Trinket Audio Player](https://learn.adafruit.com/trinket-audio-player.md)
- [Arduino GPS Clock](https://learn.adafruit.com/arduino-clock.md)
- [Arduino Lesson 7. Make an RGB LED Fader](https://learn.adafruit.com/adafruit-arduino-lesson-7-make-an-rgb-led-fader.md)
- [Arduino Lesson 4. Eight LEDs and a Shift Register](https://learn.adafruit.com/adafruit-arduino-lesson-4-eight-leds.md)
- [Adafruit Proto Screw Shield](https://learn.adafruit.com/adafruit-proto-screw-shield.md)
- [Ladyada's Learn Arduino - Lesson #0](https://learn.adafruit.com/ladyadas-learn-arduino-lesson-number-0.md)
- [Adafruit 2.8" TFT Touch Shield v2 - Capacitive or Resistive](https://learn.adafruit.com/adafruit-2-8-tft-touch-shield-v2.md)
