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.07 Piezo as an Input 2 

In Project 4.06 we found that it was difficult to record a quick tap on the piezo using the Arno’s analog-digital converter (ADC). The voltage created by striking the piezo tended to linger on the pin. In this project, we add a function to quickly pull the piezo pin back to near zero volts after it’s tapped.  Most of this sketch is identical to Project 4.06, except for the additional function clearPiezo which is explained below.

Upload this sketch to the Arno, wait a moment, and then open the serial monitor.  Strike the piezo.  You should see that the signal lasts one millisecond or less. 

Concepts:  analogRead, boolean variable type, functions, switching pinMode

Circuits:
  • Circuit 1
  • Circuit 4


  • Select Sketch

    ///////////////////////////////////////////////////
    //Project 4.07 Piezo as an Input II
    int piezo = A11;
    int knock;
    long now = 0;
    boolean firstKnock;
    int LED1 = 13;
    void setup(){
    Serial.begin(9600);
    pinMode(LED1,OUTPUT);
    clearPiezo();
    }
    void loop(){
    knock = analogRead(piezo);
    firstKnock = true;
    while(knock > 40){
    digitalWrite(LED1,HIGH);
    if(firstKnock==true) now = millis();
    firstKnock = false;
    Serial.print(millis()-now);
    Serial.print( );
    Serial.println(knock);
    clearPiezo();
    knock = analogRead(piezo);
    }
    digitalWrite(LED1,LOW);
    }
    void clearPiezo(){
    pinMode(piezo,OUTPUT);
    digitalWrite(piezo,LOW);
    delay(1);
    pinMode(piezo,INPUT);
    }
    ///////////////////////////////////////////////////

    The main difference between this sketch and the previous one is the clearPiezo function.  The function changes the piezo pin very briefly to an output, sets its value to LOW to pull the voltage to near zero, and then resets the pin to an input:

    void clearPiezo(){

      pinMode(piezo,OUTPUT);

      digitalWrite(piezo,LOW);

      delay(1);

      pinMode(piezo,INPUT);

    }

    The function is called in the setup() block to clear an lingering voltage:

      clearPiezo();

    Because we have better control of the piezo pins voltage, we can use a lower threshold in the loop() block to detect a strike:

    while(knock > 40){

    We record millis() with the variable now before calling the clearPiezo function. The clearPiezo function adds a millisecond or so, which would affect how precisely we record the timing of the strike:

        digitalWrite(LED1,HIGH);

        if(firstKnock==true) now = millis();

        firstKnock = false;

        Serial.print(millis()-now);

        Serial.print(" ");

        Serial.println(knock);

        clearPiezo();

    With a simple change to the code, we get a piezo signal that is much cleaner and easier to interpret.  Now let’s use that signal in the next two projects!

    Back to Projects 4

    Copyright Olympia Circuits LLC 2014. All Rights Reserved.