Measure Frequency/Mph of Piezo Vibration Sensor(Analog)

Hello! I want to measure frequency or the mph of MEAS piezo vibration sensor I bought from SparkFun, however, the articles/posts I've read suggest I use a pulseIn()--pulseIn() works only with a digital pin. I cannot use it because the vibration sensor I use has an analog output :~

Can anyone help me on how can I compute for the frequency and mph?

I'll be computing frequency for machine vibration and mph for fan airflow.

I found a code that measures mph, however, it is for a speed belt.
Here's the code from PopSci.com:

/*************************************************************************
 * 
 * >>PopSci Speedbelt<<
 * 
 * Determine your stride length-
 * 
 * Stride Length = 28.5"
 * 
 * Set sensor sample rate at 30 seconds-
 * 
 * Stride length x strides (vibrations) per 60 sec-
 * 
 * ((# strides x 2) x stride length)/12 = feet/minute
 * 
 * Convert to MPH-
 * 
 * (x feet/minute / 5280 feet) x 60 = speed MPH
 * 
 * Here are some resources for Stride Calculators:
 * 
 * Johnson Outdoors Gear LLC -
 * http://www.tech4o.com/detail.aspx?id=181
 * 
 * Walking with Attitude - 
 * http://www.walkingwithattitude.com/fitness-tools/stride-length
 * 
 *************************************************************************/

// Arduino 1.0

//unsigned char i;
//unsigned char j;
///*Port Definitions*/
//int Max7219_pinCLK = 10;
//int Max7219_pinCS = 9;
//int Max7219_pinDIN = 8;
//unsigned char disp1[13][8]={
//  {
//    0x3C,0x42,0x42,0x42,0x42,0x42,0x42,0x3C  }
//  ,//0
//  {
//    0x10,0x18,0x14,0x10,0x10,0x10,0x10,0x10  }
//  ,//1
//  {
//    0x7E,0x2,0x2,0x7E,0x40,0x40,0x40,0x7E  }
//  ,//2
//  {
//    0x3E,0x2,0x2,0x3E,0x2,0x2,0x3E,0x0  }
//  ,//3
//  {
//    0x8,0x18,0x28,0x48,0xFE,0x8,0x8,0x8  }
//  ,//4
//  {
//    0x3C,0x20,0x20,0x3C,0x4,0x4,0x3C,0x0  }
//  ,//5
//  {
//    0x3C,0x20,0x20,0x3C,0x24,0x24,0x3C,0x0  }
//  ,//6
//  {
//    0x3E,0x22,0x4,0x8,0x8,0x8,0x8,0x8  }
//  ,//7
//  {
//    0x0,0x3E,0x22,0x22,0x3E,0x22,0x22,0x3E  }
//  ,//8
//  {
//    0x3E,0x22,0x22,0x3E,0x2,0x2,0x2,0x3E  }
//  ,//9
//  //{0x8,0x14,0x22,0x3E,0x22,0x22,0x22,0x22},//A
//  //{0x3C,0x22,0x22,0x3E,0x22,0x22,0x3C,0x0},//B
//  //{0x3C,0x40,0x40,0x40,0x40,0x40,0x3C,0x0},//C
//  //{0x7C,0x42,0x42,0x42,0x42,0x42,0x7C,0x0},//D
//  //{0x7C,0x40,0x40,0x7C,0x40,0x40,0x40,0x7C},//E
//  //{0x7C,0x40,0x40,0x7C,0x40,0x40,0x40,0x40},//F
//  //{0x3C,0x40,0x40,0x40,0x40,0x44,0x44,0x3C},//G
//  {
//    0x44,0x44,0x44,0x7C,0x44,0x44,0x44,0x44  }
//  ,//H
//  //{0x7C,0x10,0x10,0x10,0x10,0x10,0x10,0x7C},//I
//  //{0x3C,0x8,0x8,0x8,0x8,0x8,0x48,0x30},//J
//  //{0x0,0x24,0x28,0x30,0x20,0x30,0x28,0x24},//K
//  //{0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x7C},//L
//  {
//    0x81,0xC3,0xA5,0x99,0x81,0x81,0x81,0x81  }
//  ,//M
//  //{0x0,0x42,0x62,0x52,0x4A,0x46,0x42,0x0},//N
//  //{0x3C,0x42,0x42,0x42,0x42,0x42,0x42,0x3C},//O
//  {
//    0x3C,0x22,0x22,0x22,0x3C,0x20,0x20,0x20  }
//  ,//P
//  //{0x1C,0x22,0x22,0x22,0x22,0x26,0x22,0x1D},//Q
//  //{0x3C,0x22,0x22,0x22,0x3C,0x24,0x22,0x21},//R
//  //{0x0,0x1E,0x20,0x20,0x3E,0x2,0x2,0x3C},//S
//  //{0x0,0x3E,0x8,0x8,0x8,0x8,0x8,0x8},//T
//  //{0x42,0x42,0x42,0x42,0x42,0x42,0x22,0x1C},//U
//  //{0x42,0x42,0x42,0x42,0x42,0x42,0x24,0x18},//V
//  //{0x0,0x49,0x49,0x49,0x49,0x2A,0x1C,0x0},//W
//  //{0x0,0x41,0x22,0x14,0x8,0x14,0x22,0x41},//X
//  //{0x41,0x22,0x14,0x8,0x8,0x8,0x8,0x8},//Y
//  //{0x0,0x7F,0x2,0x4,0x8,0x10,0x20,0x7F},//Z
//};

//defines the Sensor pin connections
//int GroundPin= A0;
int sensePin= 0; //A1
//defines normal and threshold voltage levels
int normalReading= 0;
int threshold= 10;

//Define stride
float strideLength = 1; // set 28.5
//Feet per minute
float FPM = 0;
//MPH
int MPH = 0;

//void Write_Max7219_byte(unsigned char DATA)
//{
//  unsigned char i;
//  digitalWrite(Max7219_pinCS,LOW);
//  for(i=8;i>=1;i--)
//  {
//    digitalWrite(Max7219_pinCLK,LOW);
//    digitalWrite(Max7219_pinDIN,DATA&0x80);// Extracting a bit data
//    DATA = DATA<<1;
//    digitalWrite(Max7219_pinCLK,HIGH);
//  }
//}
//void Write_Max7219(unsigned char address,unsigned char dat)
//{
//  digitalWrite(Max7219_pinCS,LOW);
//  Write_Max7219_byte(address);
//  //address,code of LED
//  Write_Max7219_byte(dat);
//  //data,figure on LED
//  digitalWrite(Max7219_pinCS,HIGH);
//}
//void Init_MAX7219(void)
//{
//  Write_Max7219(0x09, 0x00);
//  Write_Max7219(0x0a, 0x03);
//  Write_Max7219(0x0b, 0x07);
//  Write_Max7219(0x0c, 0x01);
//  Write_Max7219(0x0f, 0x00);
//}
////decoding :BCD
////brightness
////scanlimit;8 LEDs
////power-down mode:0,normal mode:1
////test display:1;EOT,display:0

//this function returns the average reading of the sensePin when no large vibration is detected
int calibrate()
{
  int n= 100;
  long total=0;
  for (int i=0; i<100; i++) {
    total= total + analogRead(sensePin);
    delay(1);
  }
  return total/n;
}

void setup()
{
  Serial.begin(9600);
//  pinMode(Max7219_pinCLK,OUTPUT);
//  pinMode(Max7219_pinCS,OUTPUT);
//  pinMode(Max7219_pinDIN,OUTPUT);
//  delay(50);
//  Init_MAX7219();
  //sets Sensor GroundPin as output pin, GroundPin being set to LOW
//  pinMode(GroundPin, OUTPUT);
//  digitalWrite(GroundPin, LOW);
  normalReading= calibrate();
}

void loop()
{
  int numberofStrides = 0;
  //Determine the speed
  {
    //if the sensor reading is higher than the threshold value, then the stride is turned incremented
    for (int i=0; i<30000; i++)
    {
      int reading= analogRead(sensePin);
      if (reading > normalReading + threshold)
      {
        //Stride
        //digitalWrite(LEDPin, HIGH);
        numberofStrides++;
      }
    }
    //Calculate feet per minute
    FPM = ((numberofStrides * 2) * strideLength)/12;
//    Serial.print("FPM: "); Serial.println(FPM);
    //Convert to MPH
    MPH = (FPM / 5280) * 60;
    Serial.print("MPH: "); Serial.println(MPH);
  }

  //Display the final speed
  //for(j=0;j<13;j++)

//  if (MPH < 10) {
//    j=MPH;
//    if (j < 1) {
//      j = 0;
//    }
//    for(i=1;i<9;i++)
//      Write_Max7219(i,disp1[j][i-1]);
//    delay(500);
//  }
//
//  if (MPH > 9) {
//    j=MPH/10;
//    for(i=1;i<9;i++)
//      Write_Max7219(i,disp1[j][i-1]);
//    delay(500);
//    j=MPH % 10;
//    for(i=1;i<9;i++)
//      Write_Max7219(i,disp1[j][i-1]);
//    delay(500);
//  }
//
//  //Display M
//  {
//    j=11;
//    for(i=1;i<9;i++)
//      Write_Max7219(i,disp1[j][i-1]);
//    delay(500);
//  }
//
//  //Display P
//  {
//    j=12;
//    for(i=1;i<9;i++)
//      Write_Max7219(i,disp1[j][i-1]);
//    delay(500);
//  }
//
//  //Display H
//  {
//    j=10;
//    for(i=1;i<9;i++)
//      Write_Max7219(i,disp1[j][i-1]);
//    delay(500);
//  }

}

(without comments)

/*************************************************************************
 * 
 * >>PopSci Speedbelt<<
 * 
 * Determine your stride length-
 * 
 * Stride Length = 28.5"
 * 
 * Set sensor sample rate at 30 seconds-
 * 
 * Stride length x strides (vibrations) per 60 sec-
 * 
 * ((# strides x 2) x stride length)/12 = feet/minute
 * 
 * Convert to MPH-
 * 
 * (x feet/minute / 5280 feet) x 60 = speed MPH
 * 
 * Here are some resources for Stride Calculators:
 * 
 * Johnson Outdoors Gear LLC -
 * http://www.tech4o.com/detail.aspx?id=181
 * 
 * Walking with Attitude - 
 * http://www.walkingwithattitude.com/fitness-tools/stride-length
 * 
 *************************************************************************/

// Arduino 1.5

//defines the Sensor pin connections
int sensePin= 0; 
//defines normal and threshold voltage levels
int normalReading= 0;
int threshold= 10;

//Define stride
float strideLength = 1; // set 28.5
//Feet per minute
float FPM = 0;
//MPH
int MPH = 0;


//this function returns the average reading of the sensePin when no large vibration is detected
int calibrate()
{
  int n= 100;
  long total=0;
  for (int i=0; i<100; i++) {
    total= total + analogRead(sensePin);
    delay(1);
  }
  return total/n;
}

void setup()
{
  Serial.begin(9600);
  normalReading= calibrate();
}

void loop()
{
  int numberofStrides = 0;
  //Determine the speed
  {
    //if the sensor reading is higher than the threshold value, then the stride is turned incremented
    for (int i=0; i<30000; i++)
    {
      int reading= analogRead(sensePin);
      if (reading > normalReading + threshold)
      {
        //Stride
        numberofStrides++;
      }
    }
    //Calculate feet per minute
    FPM = ((numberofStrides * 2) * strideLength)/12;
//    Serial.print("FPM: "); Serial.println(FPM);
    //Convert to MPH
    MPH = (FPM / 5280) * 60;
    Serial.print("MPH: "); Serial.println(MPH);
  }
}

I don't know what the stride does?

I'd focus first on getting the output of the device "measurable"

it will create an AC output if it is vibrating, you will want to come up with a way to get that voltage down to manageable levels first.

have you attached a resistor/diode so that you can get this:

int reading= analogRead(sensePin);

into a scale you can work with?

BulldogLowell:
have you attached a resistor/diode so that you can get this:

int reading= analogRead(sensePin);

into a scale you can work with?

Yes, I placed a 1M ohm pull down resistor to A0.

I'd focus first on getting the output of the device "measurable"

it will create an AC output if it is vibrating, you will want to come up with a way to get that voltage down to manageable levels first.

The truth is, I don't know where to start. Could you help me on what should I convert 0-1023 values into?

Start from here http://interface.khm.de/index.php/lab/experiments/frequency-measurement-library/
Can you get an osciloscope and post an image of the signals waveform? So, comparator approach from the link may be sensitive down to a few mV. If piezo outputs less than that, you may need an amplifier.

Magician:
Start from here http://interface.khm.de/index.php/lab/experiments/frequency-measurement-library/
Can you get an osciloscope and post an image of the signals waveform? So, comparator approach from the link may be sensitive down to a few mV. If piezo outputs less than that, you may need an amplifier.

Sir, is it okay if I use a digital pin for my sensor? The code in the link you gave me only displays "FreqPeriod Library Test" even when I bend my sensor.

The code you gave me is now working :slight_smile: I fixed my wiring. Thank you sir!
Here are some of the data

frequency: 0.060508 Hz
frequency: 0.260950 Hz
frequency: 132.698608 Hz
frequency: 173.190750 Hz
frequency: 152.872497 Hz
frequency: 233.388275 Hz
frequency: 99.201446 Hz
frequency: 171.879135 Hz
frequency: 558.497680 Hz
frequency: 150.975662 Hz

The only problem is, if I'll be using 3 vibration sensor I'll need an external analog comparator, yes? Could you recommend one for me?

Sir, is it okay if I use a digital pin for my sensor?

Yes, if you build a circuitry to bias comparator inputs.

The code in the link you gave me only displays "FreqPeriod Library Test" even when I bend my sensor.

Bend? Do not, you can easily destroy sensor or your arduino. Tip a pencil on a desk, where the sensor placed

Magician:
Bend? Do not, you can easily destroy sensor or your arduino. Tip a pencil on a desk, where the sensor placed

That is how this sensor works, a vibration will cause the sensor to bend and is sensed by detecting a voltage created by the piezoelectric effect.

arduinoTime,

I believe (and the forum will be able to tell you better) that you may be able to use an interrupt to detect the voltage change if you can get it to output a high enough voltage but not exceed 5V.

you could use a diode to prevent the reverse voltage swing, so essentially create a waveform with a rising and falling edge.

There are a lot of great EE types that could help you here, even if it to show you that it cannot be done without some form of analog to digital circuitry.

You mention mph (do you mean speed?) if so how is a vibration sensor going to
measure speed? Or have you omitted some vital detail?

MarkT:
You mention mph (do you mean speed?) if so how is a vibration sensor going to
measure speed? Or have you omitted some vital detail?

Yes, sir, I need speed. I want to place it on a fan like a strip of paper taped in an aircon but it can determine the speed of the air coming out. I've read that frequency is somehow connected to speed? and I was hoping if I finish the frequency I can move on to speed?

In response to edited reply #5:

The only problem is, if I'll be using 3 vibration sensor I'll need an external analog comparator, yes? Could you recommend one for me?

No, you need 3 arduinos. Comparator internally configured to ICP. What is the ICP , look in the data sheet of AtMega328.
I'm not advising on the part number and sourcing. Post another topic.
With alll due respect, have you considered to build a project with LEGO, instead of arduino?

arduinoTime:

MarkT:
You mention mph (do you mean speed?) if so how is a vibration sensor going to
measure speed? Or have you omitted some vital detail?

Yes, sir, I need speed. I want to place it on a fan like a strip of paper taped in an aircon but it can determine the speed of the air coming out. I've read that frequency is somehow connected to speed? and I was hoping if I finish the frequency I can move on to speed?

Placing it on the fan would cause the wires to get tangled up very quickly.

If you mean it sits in the airflow I don't expect great response. Air turbulence
will cause some chaotic vibration, the average amplitude of which will depend on
airspeed, but not with great accuracy. Also the amplitude may be very small from
just airflow.

Perhaps if its very close to the blades it will pick up the frequency of blades passing by?

arduinoTime:
Sir, is it okay if I use a digital pin for my sensor? The code in the link you gave me only displays "FreqPeriod Library Test" even when I bend my sensor.

The code you gave me is now working :slight_smile: I fixed my wiring. Thank you sir!
Here are some of the data

frequency: 0.060508 Hz

frequency: 0.260950 Hz
frequency: 132.698608 Hz
frequency: 173.190750 Hz
frequency: 152.872497 Hz
frequency: 233.388275 Hz
frequency: 99.201446 Hz
frequency: 171.879135 Hz
frequency: 558.497680 Hz
frequency: 150.975662 Hz




The only problem is, if I'll be using 3 vibration sensor I'll need an external analog comparator, yes? Could you recommend one for me?

Sir, can I take a look at the circuit that you used to get the frequency using the piezo sensor? Thank you Sir.

I would be interested in that as well, thanks!

http://interface.khm.de/index.php/lab-log/piezo-disk-preamplifier/

piezo disk preamp ?

this seems to solve this problem, it states that u can use a piezo disk even as a audible sound spectrum microphone, if i understand correctly