I took the knock code..and edited it to my needs..
what I am trying to do is:
1.) have a piezo sensor hoked up to Ananlog pin 0
2.) read this 'output' from the piezo sensors with the Arduino
3.) take the results from Piezo and have a led mirror the output on PWM (so tiny/low flicker..or bright flickering)
/* Vibration detection using piezo sensor with matching PWM led output
This sketch reads a piezo element to detect a vibration.
It reads an analog pin and parses the value into a PWM value represented by the LED.
The circuit:
* + connection of the piezo attached to analog in 0
* - connection of the piezo attached to ground
* 1-megohm resistor attached from analog in 0 to ground
edited from:
http://www.arduino.cc/en/Tutorial/Knock.pde
*/
// led connected to digital pin 13
const int powerLed = 13;
//declare pin 9 to be an pwmLed (pin 9 is PWM enabled pin)
const int pwmLED = 9;
// the piezo is connected to analog pin 0
const int piezoSensor = A0;
// threshold value to decide when to 'detect' above/over a cetain vibration level
const int threshold = 100;
// variable to store the read value
int pwmVal = 0;
// variable to store the value read from the sensor pin
int sensorReading = 0;
void setup() {
// declare the powerLed as as OUTPUT
pinMode(powerLed, OUTPUT);
//turn ON powerLED
digitalWrite(powerLed, HIGH);
// declare analog pin 0 as input
pinMode(A0, INPUT);
// turn on serial port
//Serial.begin(9600);
}
void loop() {
//read the piezo sensor
sensorReading = analogRead(piezoSensor);
//Serial.println(sensorReading);
// analogRead values go from 0 to 1023, analogWrite values from 0 to 255
pwmVal = sensorReading / 4;
analogWrite(pwmLED, pwmVal);
//Serial.println("PWM: ");
//Serial.println(pwmVal);
delay(25); // delay to avoid overloading the serial port buffer
}
I have my 1 megaOhm resistor arcross GND and analog pin0
piezo V++ goes to analog pin 0 (after resistor lead)
piezo GND goes to GND ( after resistor lead)
I have reada few mentions..but 'where' do I put a cap to keep/hold this peek signal for a better circuit?
(I am new to this and still learning when I have time) =)
I mean the code 'works' (sorta).. but I have to bang on the piezo sensor I got from RadioShack..
is there a way to make it more sensitive?
for exampel with sensor on my desk.. laying flat..
if I tap my fingers on desktop..it should react..
but I have to bang pretty hard..or even hit the sensor physically itself..??
Just from looking at the tutorial, is the sensor firmly taped to your desk? Also, did u try adjusting the threshold? If u really can't get it sensitive enough, you could try an op amp....
Cap doesn't really seen necessary. It looks like the problem is that you are dividing the value by 4, but that makes very small values. You should use the original knock code to see what values you get, and adjust the range because of that. For example, if your desired highest possible hit-strength only reads a 150 in the serial monitor, then instead of dividing by 4, you should use map(value, 0, 150, 0, 255).
you may in fact be right.. (I have no clue).. I picked that 'idea' up through my searching.. (maybe Grumpy Mike?).. where it was stated that the piezo sensor only shows/holds the value for a very small time.. so a cap used will keep and hold.. for a certain amount of time.. letting you get longer/more accurate readings, that would otherwise be skipped/missed?
2.) re: dividing by 4.
if Analog sensor outs out 0-1023.. and PWM only goes to range of 0-255.. wouldnt dividing by 4 give me a matching 'relative' value for PWM output?
If you can see changes in the serial monitor values when you hit it at the desired strength--then a cap is not necessary, as you can implement a holding function in software. If you do not see results, the changes are happening too quickly for the arduino to pick up, and a cap becomes very useful.
The reason you should not divide by four is because it is unlikely that your piezo outputs anything near 5 volts when you tap your desk. The sample code from the tutorial shows that 100 is a good threshold value. With your code dividing by four, this corresponds to a less than half a volt of PWM output --- not enough to light an LED. Thus, by adjusting the sensitivity of your function for a much smaller range, you can eliminate all of the wasted space at the upper end of your range, so that a value of say, 150 corresponds to a pwm of 255. The map function is basically like a proportion. Read more about it here: http://www.arduino.cc/en/Reference/Map