My project is taking an input from a Piezo sensor for vibration and switch on a light and a bluetooth. I have assigned a threshold value to check whether vibration sensor value goes beyond that value if so then switch on the light & BT. However what i see is, once sensor read the Piezo value ex: 300 while threshold is 200, then it takes a while to come below 200. Until then it gives values are varied from 300 to 200. Sometime it goes up (some odd value 1023). How could I drain voltage in a pin quickly.
I really dont understand what the solution is. I doubt the problem is with Piezo sensor.
const int ledPin = 8;
const int btPin = 4;
char val ;
const int wakeUpPin = A0;
const int threshold = 200;
int sensorReading = 0; // variable to store the value read from the sensor pin
int sensCount = 0;
void setup() {
pinMode(ledPin, OUTPUT); // declare the ledPin as as OUTPUT
Serial.begin(9600);
}
void wakeUp()
{
digitalWrite(ledPin,HIGH);
digitalWrite(btPin, HIGH);
}
void sleepDown()
{
digitalWrite(ledPin,LOW);
digitalWrite(btPin, LOW);
sensCount = 0;
}
void readBluetooth()
{
if (Serial.available())
{
val = Serial.read();
if ( val == '1')
{
Serial.println(5);
}
else if (val == '2')
{
Serial.println(10);
}
else if (val == '3')
{
Serial.println(20);
}
else if (val == '4')
{
Serial.println(50);
}
else if (val == '9')
{sleepDown();}
delay (100);
}
}
void loop() {
sensorReading = analogRead(wakeUpPin);
Serial.println(sensorReading);
if (sensorReading >= threshold)
{
sensCount = sensCount + 1;
if (sensCount == 1)
{
wakeUp();
}
}
readBluetooth();
}
You need to test your sensor separately with a voltmeter to see how it responds to changes in vibration. I've not looked at your code , but would then look to an error there - e.g. Use a power supply to simulate the sensor and see how the Arduino responds to changes in voltage .
Always break things down to find the problem , by isolating a part at a time
The amplitude of the vibration will decay exponentially. You can't just stop it from vibrating once you've measured it.
Take a look at how buttons are "debounced", you can do the same for your piezo. Or set a flag when you detect a peak (e.g. > 200), and reset it when it drops below a low threshold (e.g. < 100). Don't allow triggering the lights/bluetooth if the flag is set.
Here's what hitting a piezo looks like on a scope. As you can see, the voltage oscillates and even goes negative for half of the cycle. The amplitude decays more or less exponentially. The total time scale of this image is 12ms.
What I want to do is switching off BT and LED manually. But the issue is even I switch off it manually, it starts again bcz values coming from PIN is still high (Condition becomes True). It takes so long to come down. Strange thing is even after removing the jumper wires from arduino board still serial monitor is getting values for sensor reading. Thatswhy I guessed it's something to do with board or PIN (A0).
A piezo has an almost infinite DC resistance. I'll bet you have it hooked up to the input without any bias or load resistor. That will make the DC voltage wander around randomly and give wildly inaccurate readings. You can actually google some circuits for this that work.
You might try lower resistance values, maybe something like 100k ohm or less. Using sensors frequently requires some experimentation...And a scope is invaluable if you can borrow one
Mark
The average scope with (1:1 probe) has a 1Megohm impedance.
That could influence the circuit more than an Arduino pin.
Loading a piezo drops low frequency response.
Leo..