Debouncing various sensors

Hi all,

I am having trouble getting my head around debouncing for the various sensors I am using. I have the bounce2 library and have used the bounce library before only with buttons.

This time and I am using a range finder, a slider (potentiometer), an accelerometer, a wind sensor, and a button. I have included the code that I have done so far.

Every reading jitters by an integer or 2 and I was wondering if anyone could point me in the right direction to be able to get more stable readings?

Do you debounce say a wind sensor which has a more continous flow of reading in the same way as a button?

Thanks!

Asha

/*
  Analog Input
 Demonstrates analog input by reading an analog sensor on analog pins 0-5 and
 sending out a serial message of the value of the sensor attached to that pin.  
 
 The circuit:
 * Potentiometers attached to analog inputs 0-5
 * center pin of the potentiometer to the analog pin
 * one side pin (either one) to ground
 * the other side pin to +5V
  
 
Adapted from David Cuartielles analog input tutorial in the examples. 
 modified 30 Aug 2011
 By Tom Igoe
 
 This example code is in the public domain.
 
 http://arduino.cc/en/Tutorial/AnalogInput
 
 */

//int sensorPin = A0;    // select the input pins for the potentiometer
//int sensorValue = 0;  // variable to store the values coming from the sensors
int sensorPin1 = A1;
int sensorValue1 = 0;
int sensorPin2 = A2;    
int sensorValue2 = 0;  
int sensorPin3 = A3;
int sensorValue3 = 0;
int sensorPin4 = A4;   
int sensorValue4 = 0;  
int sensorPin5 = A5;
int sensorValue5 = 0;
//int sensorPin6 = A6;   
//int sensorValue6 = 6;  
int pushButton = 3;
int buttonState = 0;
#define trigPin 13
#define echoPin 12

void setup() {
  
 Serial.begin(57600);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
 pinMode(pushButton, INPUT);
}

void loop() {
  
    long duration, distance;
  digitalWrite(trigPin, LOW);  // Added this line
  delayMicroseconds(2); // Added this line
  digitalWrite(trigPin, HIGH);
//  delayMicroseconds(1000); - Removed this line
  delayMicroseconds(10); // Added this line
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration/2) / 29.1;
  if (distance < 250) {  // This is where the LED On/Off happens
  
    Serial.print(distance);
    Serial.print(" ");
       
  }
  ;
  
  int buttonState = digitalRead(pushButton);
  
  Serial.print(buttonState);
 Serial.print(" ");
  // read the value from the sensor:
  sensorValue1 = analogRead(1);    
  // print hello and sensor number to screen 
  //b for routing this sensors data
  //prints value to screen
  Serial.print (sensorValue1);
  Serial.print(" ");
   // stop the program for <sensorValue> milliseconds     
   ;
   
  // read the value from the sensor number 2:  
  sensorValue2 = analogRead(2); 
  // print hello and sensor number to screen 
 
  Serial.print(sensorValue2);
    Serial.print(" ");
   // stop the program for <sensorValue> milliseconds     
   ;
   
    // read the value from the sensor number 3:  
  sensorValue3 = analogRead(3); 
  // print hello and sensor number to screen     

  Serial.print (sensorValue3);
 Serial.print(" ");
   // stop the program for <sensorValue> milliseconds     
   ;
   
    // read the value from the sensor number 4:  
  sensorValue4 = analogRead(4); 
  // print hello and sensor number to screen 

  Serial.print (sensorValue4);
 Serial.print(" ");
   // stop the program for <sensorValue> milliseconds     
   ;
   
    // read the value from the sensor number 5:  
  sensorValue5 = analogRead(5); 
  //print hello and sensor number to screen 

 Serial.println (sensorValue5);
   // stop the program for <sensorValue> milliseconds     
  // delay(20);
   
    // read the value from the sensor number 6:  
  //sensorValue5 = analogRead(5); 
  // print hello and sensor number to screen 

  //Serial.println (sensorValue5);

  
  // stop the program for <sensorValue> milliseconds     
   ;      
delay (20);
}

Debounce is for switches that mechanically bounce the contacts when the switch is actuated. Debounce can be done in software or hardware (cap across the switch). There are methods for conditioning the signals from other sensors depending on the type of output from the sensor. For analog signals you can use filtering or averaging to get more stable readings. For signals from an optical pickup, you can put them through a Schmitt trigger to get clean pulses with good rise times. Good wiring practices can prevent noise pickup. Keep signal wires away from power wires, keep wires as short as possible, use lots of decoupling caps, ect.

Thanks groundfungus,

I do use a bit of javascript in the software to get a a weighted running average which helps but has the tradeofff with the speed of the response. I will definately take your tips on board and try and just neaten everything up.

:slight_smile:

I've hit upon a very nice debounce technique, which is done in software. Suppose you have a button, to be read on the pin named "button Pin". Define an unsigned long variable called "wait", and initially set wait = 0 in the setup() routine.

Now, use the following code when reading the button.

state = digitalRead(buttonPin);
if ((state == LOW) && (millis() > wait))
{
//Do whatever the button initiates.
wait = millis() + 500;
}

So, the result of this will be that the button may mechanically bounce, but the action associated
with the button will not occur again within 1/2 second. That is adequate time for most debouncing.

I have used this technique for buttons and interrupts, and it works very smoothly. Of course you can
do debouncing down in the microsecond range by using micros() as the clock.

Hope this is useful.

John Doner

Here is an interesting analysis of the bounce behavior of a random collection of switches: http://www.eng.utah.edu/~cs5780/debouncing.pdf Most switches bounced for around 1500 microseconds, on average, but there is quite an amazing range of behaviors (it took up to 157 milliseconds for one switch to open).

jremington, that is the best de-bouncing guide on the planet. I've attempted to tackle those scenarios here, but haven't tested them all yet.

chewiesmissus, note that jitter is increased by use of delays. The last delay(20) in itself adds 20 ms of jitter to all readings.

So, for example , if duration = 100 ms, the last delay represents 20% jitter.
If you use pulseIn to get 10 readings so duration becomes 1000 ms, then the last delay will now represent 2% jitter.

Replacing the delays with a method like shown in the blink without delay example would greatly reduce jitter and improve response.

Every reading jitters by an integer or 2

Yes it will. The reading from any A/D can only ever be plus or minus a leas significant bit.

Many thanks for all the great responses!

I shall have a reading crunch and try what you guys have suggested!

Cheers!