Learn Arduino with Olympia Circuits
Learn Arduino
  • Home
    • Get Started
    • How to Use This Site
  • Electronics
    • The Basics
    • Electricity Flows like Water
    • Electronic Components
    • The Arno Board
  • Programming
    • The Basics
    • setup and loop Blocks
    • Variables and Arrays
    • Connecting with the Pins
    • Flow Control >
      • if Statement
      • Loops
      • Delays
    • Functions
    • Serial Communication
    • USB
    • Some Thoughts
  • Projects
    • Projects 1 >
      • 1.01: Blink
      • 1.02 Blink x2
      • 1.03 Blink Faster
      • 1.04 LED Chase!
      • 1.05 Wait To Blink
      • 1.06 Blink a Little Faster Now
      • 1.07 LED Fade
      • 1.08 RGB Blink
      • 1.09 Change RGB Color with SW1
      • 1.10 Fade RGB Colors
      • 1.11 Reaction Time Game
    • Projects 2 >
      • 2.01 Hello World
      • 2.02 Talk Back
      • 2.03 ASCII Values
      • 2.04 Ski Game
      • 2.05 Demonstration of the String Object
    • Projects 3 >
      • 3.01 Read the Potentiometer
      • 3.02 ASCIIbet Soup
      • 3.03 Potentiometer sets LED Brightness
      • 3.04 Potentiometer Sets Blink Rate
      • 3.05 LED Chase, Part II
    • Projects 4 >
      • 4.01 Bringing the Piezo to Life
      • 4.02 Controlling the Piezo with a Function
      • 4.03 Piezo C Major
      • 4.04 Piezo Greensleaves
      • 4.05 Piezo Metronome
      • 4.06 Piezo as an Input
      • 4.07 Piezo as an Input 2
      • 4.08 Metronome II
      • 4.09 Piezo Playback
      • 4.10 Piezo Fireworks
      • 4.11 Piezo Mosquito
    • Projects 5 >
      • 5.01 The Phototransistor
      • 5.02 Light and Sound
      • 5.03 Light and Sound II
    • Projects 6 >
      • 6.01 EEPROM
      • 6.02 I2C Address Scan
      • 6.03 Read the I2C Temperature Sensor
      • 6.04 High Temperature Alarm
    • Projects 7 >
      • 7.01 Arno Phone Home
      • 7.02 Keyboard Alphabet
      • 7.03 Move Mouse
      • 7.04 Draw Squares
    • Special Projects >
      • Bike Light Demo
  • References
    • Arno Pin Key
    • Arno Schematic
    • Project Index

Project 4.01 Bringing the Piezo to Life

A sound is created using a piezo element by rapidly switching its voltage source on and off.  The rate at which we switch the piezo creates a particular tone.  Upload this sketch and listen to the tone it creates.


Concepts:  delayMicroseconds

Circuits:
  • Circuit 4


  • Select Sketch

    ///////////////////////////////////////////////////
    //Project 4.01 Piezo Beep, Beep
    long freq;
    long period;
    long aSecond = 1000000;
    int piezo = 12;
    void setup(){
    pinMode(piezo,OUTPUT);
    }
    void loop(){
    freq = 4000;
    period = aSecond/freq;
    for(long k = 0; k < freq/10; k++){
    digitalWrite(piezo,HIGH);
    delayMicroseconds(period/2);
    digitalWrite(piezo,LOW);
    delayMicroseconds(period/2);
    }
    delay(1000);
    }
    ///////////////////////////////////////////////////

    The frequency of a tone is expressed in hertz (Hz), which is the number of vibrations per second.  We use three variables: the frequency in hertz (freq), the number of microseconds between vibrations (period), and the number of microseconds in a second (aSecond):

    long freq;

    long period;

    long aSecond = 1000000;

    At the beginning of the loop() block we set the frequency:

    freq = 4000;

    Next, we need to calculate how many microseconds that need to pass between each vibration.  The math is simple:

    period = aSecond/freq;

    Now we’re ready to enter the loop()  block.  We’re going to create the vibrations one by one.  Since freq is vibrations/second, we can use it to determine how long the tone lasts.  We create a tone of 1/10th of a second by looping from 0 to freq/10:

    for(long k = 0; k < freq/10; k++){

    At this point the code is simple: we switch the piezo on and off.   Since we want a cycle to be completed every period, we switch it on for period/2 and then off for period/2.  The delay command would really limit the range of frequencies we could create. Using delayMicroseconds allows us to control the frequency to the nearest 2 microseconds (two one-millionth of a second!):

        digitalWrite(piezo,HIGH);

        delayMicroseconds(period/2);

        digitalWrite(piezo,LOW);

        delayMicroseconds(period/2);

    Finally, we delay a second between tones before starting all over again:

    delay(1000);

    Back to Projects 4

    Copyright Olympia Circuits LLC 2014. All Rights Reserved.