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.11 Piezo Mosquito

 We don’t have a way built into the hardware of Circuit 4 to control the volume of the piezo.  In this project, we explore an approach to controlling its volume through software (i.e., the sketch that controls the piezo).  When we set the piezo voltage to HIGH, it changes shape. This causes a pulse of air to move out of it.  We then switch the voltage from HIGH to LOW to prepare for the next pulse.  If we switch the voltage back to low sooner, we might be able to make soften the pulse.  This could reduce the volume without changing the frequency.  In this project, we change how long the piezo voltage is either HIGH or LOW to create a sound that’s like a mosquito buzzing around your head.

Concepts: digitalWrite, frequencies

Circuits: 
  • Circuit 4


  • Select Sketch

    ///////////////////////////////////////////////////
    //Project 4.08 Piezo Mosquito
    int piezo = 12;
    long buzzAway = 0;
    long level = 0;
    void setup(){
    pinMode(piezo,OUTPUT);
    }
    void loop(){
    buzzAway = random(25,100);
    for(level = 1; level < 50; level++){
    piezoTone(1047,buzzAway,level);
    }
    buzzAway = random(25,100);
    for(level = 50; level > 1; level--){
    piezoTone(1047,buzzAway,level);
    }
    }
    void piezoTone(long freq, long duration, long partOn){
    long aSecond = 1000000;
    long period = aSecond/freq;
    duration = duration*1000;
    duration = duration/period;
    for(long k = 0; k < duration; k++){
    digitalWrite(piezo,HIGH);
    delayMicroseconds(partOn);
    digitalWrite(piezo,LOW);
    delayMicroseconds(period - partOn);
    }
    }
    ///////////////////////////////////////////////////

    Let’s look first at the piezoTone function.  We changed it for this project by adding an extra argument, partOn.

    void piezoTone(long freq, long duration, long partOn){

    In the past, we calculate the amount of time that needs to elapse between pulses to create a certain frequency.  Now, we use ­isOn to set how long the piezo voltage remains HIGH and then set it LOW for the remainder of period:

        digitalWrite(piezo,HIGH);

        delayMicroseconds(partOn);

        digitalWrite(piezo,LOW);

        delayMicroseconds(period - partOn);

    Back to the setup() block.  We set the speed that the mosquito approaches by setting buzzAway to a random value between 25 and 100 milliseconds.  The variable level is the number of microseconds that the piezo is HIGH during each pulse.  We use a for loop to increment this value for 1 to 50.  Through trial and error, we found that the piezo reaches full volume by the time level reaches 50 microseconds:

      buzzAway = random(25,100);

      for(level = 1; level < 50; level++){

       piezoTone(1047,buzzAway,level);

      } 

    Next, we repeat this code, but reset the speed of buzzAway and increment level from 50 to 1 to simulate the mosquito buzzing away from our ear:

      buzzAway = random(25,100);

      for(level = 50; level > 1; level--){

       piezoTone(1047,buzzAway,level);

      } 

    And that’s it!

    Back to Projects 4

    Copyright Olympia Circuits LLC 2014. All Rights Reserved.