How to drain voltage from a digital pin quickly

Hi All,

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();
}

error.png

You can’t “drain voltage”. .

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.
piezo-oscilloscope.png

Pieter

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).

Please provide a schematic of your circuit, and a data sheet or link for your sensor.

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 may need a "damping" resistor across the sensor to shorten the period of oscillation Mark S.

A (1Megohm) resistor across the piezo is needed.
Not so much for dampening, but to “drain” :slight_smile: the charge of the piezo capacitor.
Leo…

markba633csi: You may need a "damping" resistor across the sensor to shorten the period of oscillation Mark S.

Simple schematic diagram please ?

Wawa: A (1Megohm) resistor across the piezo is needed. Not so much for dampening, but to "drain" :) the charge of the piezo capacitor. Leo..

Hey Wawa,

Could you please sketch a schematic diagram for this?

charith87: Could you please sketch a schematic diagram for this?

Piezo between A0 and ground. 1 Megohm resistor between A0 and ground.

Google "Arduino knock sensor" (images). If you had gone through the examples that come with the IDE... Leo..

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..

No worries guys.

I just completed the requirement using interrupts.

Cheers

Code can’t fix a “floating” pin problem.
An analogue pin with a capacitor (piezo) attached to it will eventually drift towards ~1/3VCC.
Leo…