Last Updated: 23/09/2021

Functions Lesson 4 - tone() and noTone() with piezo speaker

Functions >> Functions Lesson 4 - tone() and noTone() with piezo speaker

Lesson Contents

Using tone() to create a signal for a piezo speaker
Using noTone() to stop the sound
Using tone() with a tone time length
Using tone() and a an LED independently with millis() timers

Circuit

Circuit:

Piezo speaker + to Pin 8

Piezo speaker - to Gnd

Example 1: ToneV1.ino

Click to Download code:ToneV1.ino

Simple use of tone() and noTone() functions

 
/*
  
 23/09/2021
 ToneV1

 Basics of using tone() and noTone() with piezo speaker

 Circuit:
 Speaker+ to pin 8 (TonePin)
 Speaker- to Gnd

 Minimum Frequency = 31
 Maximum Frequency = 65535 (This is why it needs to be an unsigned int).

 Maximum usuable frequency on a piezo speaker about 10000

*/
const int tonePin = 8;
unsigned int myLowTone = 31;//minimum frequency
unsigned int myHighTone = 10000;



void setup() {
  Serial.begin(9600);
  Serial.println("ToneV1");
  tone(tonePin, myLowTone);
  //wait 2 seconds
  delay(2000);
  //Turn the noise off
  noTone(tonePin);
  delay(1000);
  tone(tonePin, myHighTone);
  //wait 2 seconds
  delay(2000);
  //Turn the noise off with noTone() function
  noTone(tonePin);
}

void loop() {
  //Uncomment the block below to cycle through tones with set time lengths.
  /*
  int q;
  //q+= 100 increments q by 100 
  for(q=31;q<10000;q+= 100){
  //create a tone of frequency q for 100 miliseconds (0.1) seconds
    tone(tonePin, q,100);
    Serial.println(q);
    //delay is not stopping the sound
    delay(200);  
  }
 */
}

 

Example 2: ToneV2.ino

Using tone with and LED using millis() timer

Click to Download code:ToneV2.ino

 

 
/*
  
 23/09/2021
 ToneV2
Using tone() with a millis() timer alongside and led flashing/sounding at different rates
 
 Circuit:
 Speaker+ to pin 8 (TonePin)
 Speaker- to Gnd

 Minimum Frequency = 31
 Maximum Frequency = 65535 (This is why it needs to be an unsigned int).

 Maximum usuable frequency on a piezo speaker about 10000

*/
const int TonePin = 8;
const int ledPin = 13;//use the UNO built in LED

//see Lesson 7 - delay() and millis() http://www.digitaltown.co.uk/lesson7delaymillis.php
//for information on using millis() as a timer instead of delay()
unsigned long ledTimer;
unsigned long soundTimer;
int ledState;

void setup() {
  Serial.begin(9600);
  Serial.println("ToneV2");
  pinMode(ledPin,OUTPUT);
}

void loop() {
  
  if(millis() > soundTimer){
    soundTimer = millis() + 1000;
    tone(TonePin, 500,800);//tone of 500 for 0.8 seconds 800 milliseconds
  }
  
  if(millis() > ledTimer){
    ledTimer = millis() + 200;
    if(ledState > 0){
      ledState = 0;
      digitalWrite(ledPin,LOW);
    }else{
      ledState = 1;
      digitalWrite(ledPin,HIGH);
    }
    
  }
  
  /*
  int q;
  //q+= 100 increments q by 100 
  for(q=31;q<10000;q+= 100){
  //create a tone of frequency q for 100 miliseconds (0.1) seconds
    tone(TonePin, q,100);
    Serial.println(q);
    //delay is not stopping the sound
    delay(200);  
  }
  */
}

Additional Resource Links

Don't forget to use the Reference in you Arduino IDE for more help on this lesson see tone()

Comments


This site has been designed to be child friendly, this means that comments cannot be added to videos or directly to the site.
To add a comment or ask a question please email the address in this image: and use Functions Lesson 4 - tone() and noTone() with piezo speaker as a reference.