Friday 25 March 2016

Tone() function - Arduino

Arduino - The Tone function
The Tone function is an easy way to make some noise with your Arduino.
 
A basic circuit. Example 1
The resistor is 100 Ohms.
Just need a Piezo speaker, breadboard & an Uno
 
The black leads connect to GND
 
 we need just 2 arguments
  1 pin
  2 frequency in Hz - determines pitch
 
tone(Pin, Hz)
tone(piezoPin, 100)
The frequency can be any number between 0 and 65,535.
Note that most noises that can be heard with the human ears range between 2k & 5k
 

Here is the code:
//-------------------------------------
// demo of tone function

// variables
int piezoPin = 8;

void setup(){
}

void loop(){
 
  /*
  we need just 2 arguments
  1 pin
  2 frequency in Hz - determines pitch
  */
  // create a tone ... tone();
  tone(piezoPin, 100); // play around with this last number
 
}
//---------------------------------------------------
 
 Example 2
Creating a beat.
Add a duration period - the last parameter (and a delay to your code).
 
The tone function is
tone(Pin, Hz,duration in milliseconds)
tone(piezoPin, 100, 500)
 
Here is an example of your new code
 
//----------------
// demo of tone function 2

// variables
int piezoPin = 8;

void setup(){
}

void loop(){
 
  /*
  we need just 3 arguments
  1 pin
  2 frequency in Hz - determines pitch
  3 duration
  */
  // create a tone ... tone();
  tone(piezoPin, 3000, 500);
 
  delay(1500);
 
}
//-----------------------------
 
Note, to add 1 second between each sound
you need to use a duration of 500ms, and a delay of 1500ms
 
 
Limitations
Don't use analogWrite on pins 3 & 11 if you are using the tone function

 
 Links
+ https://www.programmingelectronics.com/an-easy-way-to-make-noise-with-arduino-using-tone/
+ https://www.youtube.com/watch?v=1_LMAgO14z0
 
 ---------------------------------
------------------------------------- 

No comments:

Post a Comment