Part 4 - The 'tone' function

In this exercise we are going to use the 'tone' function to make a sound with a buzzer. Looking at the breadboard circuit diagram on the left, we start to see the limitations of this view and the mess it can create with what is still a fairly simple circuit. We might be able to clean it up a bit by routing the wires around and using a bigger breadboard, but really it is better to start working with a proper circuit diagram as is shown on the right. I create my circuit diagrams with the excellent and very accessible software 'Fritzing' which allows you to design in breadboard view and proper circuit diagram view, as well as designing a PCB (Printed Circuit Board) for manufacturing. Be warned, the Arduino forum does not take kindly to messy breadboard views being posted when asking for help! 

The circuit:

For this exercise we want to use the button to play a sound and light the LED, the frequency of the sound and the colour of the LED will be defined by where our knob is turned to. When we release the button we want the LEDs to go off and the sound to stop. 

  • Look at the reference for 'tone()' here, depending on your solution you may also want to look at 'notone()' [N.B. - you should notice that the reference says that the use of tone() will interfere with PWM output on pins 3 and 11, lucky we did not use them for our LED huh... Seriously though, you need to check the reference and look out for caveats like this or they can trip you up!]
  • Refer to the 'Button' and 'AnalogInOutSerial' sketches from the examples that we have made use of before
  • Make use of our 'LEDanimate' function that we created from this resource to choose a colour

+ Hint 1

You need to declare pins and variables for the button, the pot, the buzzer and the LEDs

+ Hint 2

Your LED pins should be set as OUTPUT and you can set the button pin as an input

+ Hint 3

The main loop needs to:

  • check the button state
  • read the pot value
  • it might be a good idea to create separate functions for LED anmation, playing a tone and turning everything off
    • if the button is pressed, run our LED animation and play a tone
    • if the button is not pressed, turn LEDs off and stop the tone
  • your tone playing function will want to read the potPin, map the 0-1023 value to something useful for audio frequencies (e.g. 220-4400) and play the tone

+ The solution

 * This sketch makes use of a button and pot
 * input to play a tone and light an RGB LED
 * For the LED animation we have used elements 
 * of the color mixer code here: 
 * , 
 * Luke Woodbury 13 Jan 2017
//Pot pin and variable to store value
const int potPin = 0;
int potVal = 0; 

//button pin and variable to store state
const int buttonPin = 12;
int buttonState = 0; 

//LED pins and variables to store values
const int redPin = 10; 
const int grnPin = 6;  
const int bluPin = 9;  
int redVal = 0;
int grnVal = 0;
int bluVal = 0;

//buzzer pin and variable
const int buzzPin = 11;
int toneVal = 100;

void setup()
  pinMode(redPin, OUTPUT);   // sets the pins as output
  pinMode(grnPin, OUTPUT);   
  pinMode(bluPin, OUTPUT); 

  pinMode(buttonPin, INPUT);  //...and input

void loop(){
  //check the button state
  buttonState = digitalRead(buttonPin);
  //if button is pressed do something
  if (buttonState == HIGH){
    //call our LED function
    //call our tone function
    //call our allStop function

void LEDanimate(){
  //get the pot value
  potVal = analogRead(potPin);
  if (potVal < 341)  // Lowest third of the pot range (0-340)
    potVal = (potVal * 3) / 4; // Normalize to 0-255

    redVal = 256 - potVal;  // Red from full to off
    grnVal = potVal;        // Green from off to full
    bluVal = 1;             // Blue off
  else if (potVal < 682) // Middle third of pot range (341-681)
    potVal = ( (potVal-341) * 3) / 4; // Normalize to 0-255

    redVal = 1;            // Red off
    grnVal = 256 - potVal; // Green from full to off
    bluVal = potVal;       // Blue from off to full
  else  // Upper third of potentiometer"s range (682-1023)
    potVal = ( (potVal-683) * 3) / 4; // Normalize to 0-255

    redVal = potVal;       // Red from off to full
    grnVal = 1;            // Green off
    bluVal = 256 - potVal; // Blue from full to off
  analogWrite(redPin, redVal);   // Write values to LED pins
  analogWrite(grnPin, grnVal); 
  analogWrite(bluPin, bluVal);  

void playTone(){
  //get the pot value
  potVal = analogRead(potPin);
  toneVal = map(potVal, 0, 1023, 220, 4400);
  tone(buzzPin, toneVal);

void allStop(){

<Previous            Next >