Sensor Debounce

Post here about your Arduino projects, get help - for Adafruit customers!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
User avatar
lduepner
 
Posts: 32
Joined: Thu Jun 11, 2015 1:53 pm

Sensor Debounce

Post by lduepner »

So I working on a project that turns a pump on and off depending on a sensor state however I am running into a problem with the sensor bouncing between on and off quickly. The first thing I tried was putting a capacitor on the signal line to try and filter out some of the noise, this certainly slowed down the flickering of my indicator led however it did not remove it to an acceptable level, now one option would be to put a bigger cap on it but is there a way to do this in the program with out using delay()?

Here is the code I have so far with out any debounce written in, it is based off the zoetrope code from the starter set.

Code: Select all

const int SensorPin = 13; // the number of the sensor pin
const int MotorPin =  9; // the number of the motor pin

int SensorState = 0;  // variable for reading the sensor's status


void setup() {
  // initialize the motor pin as an output:
  pinMode(MotorPin, OUTPUT); 
  // initialize the sensor pin as an input:
  pinMode(SensorPin, INPUT);  
}

void loop(){
  // read the state of the switch value:
  SensorState = digitalRead(SensorPin);

  // check if the switch is pressed.
  if (SensorState == HIGH) {     
    // turn motor on:    
    digitalWrite(MotorPin, HIGH);  
  } 
  else {
    // turn motor off:
    digitalWrite(MotorPin, LOW); 
  }
}
Last edited by adafruit_support_bill on Tue Jul 28, 2015 6:00 pm, edited 1 time in total.
Reason: please use the </> button when submitting code. press </>, then paste your code between the [code] [/code] tags.

User avatar
adafruit_support_bill
 
Posts: 88088
Joined: Sat Feb 07, 2009 10:11 am

Re: Sensor Debounce

Post by adafruit_support_bill »

Track the last state change with millis() and ignore any state transitions within some time window. For something like a pump motor, you can probably use a fairly long time window.

something like this:

Code: Select all

if ((sensorState != motorState) && (millis() - lastStateChange > 100))
{
    motorState = sensorState;
    digitalWrite(MotorPin, motorState);
    lastStateChange = millis();
}
    

User avatar
lduepner
 
Posts: 32
Joined: Thu Jun 11, 2015 1:53 pm

Re: Sensor Debounce

Post by lduepner »

adafruit_support_bill wrote:Track the last state change with millis() and ignore any state transitions within some time window. For something like a pump motor, you can probably use a fairly long time window.

something like this:

Code: Select all

if ((sensorState != motorState) && (millis() - lastStateChange > 100))
{
    motorState = sensorState;
    digitalWrite(MotorPin, motorState);
    lastStateChange = millis();
}
    
would I add that to the beginning of my void loop?

User avatar
lduepner
 
Posts: 32
Joined: Thu Jun 11, 2015 1:53 pm

Re: Sensor Debounce

Post by lduepner »

Like this perhaps

Code: Select all

const int SensorPin = 13; // the number of the switch pin
const int MotorPin =  9; // the number of the motor pin

int SensorState = 0;  // variable for reading the switch's status
int MotorState = 0;
int LastStateChange = 0;

void setup() {
  // initialize the motor pin as an output:
  pinMode(MotorPin, OUTPUT); 
  // initialize the switch pin as an input:
  pinMode(SensorPin, INPUT);  
}

void loop(){
  // read the state of the switch value:
  SensorState = digitalRead(SensorPin);

 if ((SensorState != MotorState) && (millis() - LastStateChange > 5000))
{
    MotorState = SensorState;
    digitalWrite(MotorPin, MotorState);
    LastStateChange = millis();
}
}
   

User avatar
adafruit_support_bill
 
Posts: 88088
Joined: Sat Feb 07, 2009 10:11 am

Re: Sensor Debounce

Post by adafruit_support_bill »

Yes, something like that. You can adjust the time window as needed.

User avatar
lduepner
 
Posts: 32
Joined: Thu Jun 11, 2015 1:53 pm

Re: Sensor Debounce

Post by lduepner »

adafruit_support_bill wrote:Yes, something like that. You can adjust the time window as needed.
alright I will give that a try and let you know if it works out.

User avatar
lduepner
 
Posts: 32
Joined: Thu Jun 11, 2015 1:53 pm

Re: Sensor Debounce

Post by lduepner »

so that code certainly decreased the amount of flickering i got out of my indicator led ( it is hooked up on the same output as the line that goes to the pump) however I was under the impression that the above code was only supposed to allow a state change if the input read the same state after an interval set in the code. i have the interval set for 10 seconds but I a state change on the output quicker than that.

User avatar
Franklin97355
 
Posts: 23910
Joined: Mon Apr 21, 2008 2:33 pm

Re: Sensor Debounce

Post by Franklin97355 »

code was only supposed to allow a state change if the input read the same state after an interval set in the code.
It only does a motor state change if the sensor state is different than the motor state and 5 seconds has elapsed.

User avatar
lduepner
 
Posts: 32
Joined: Thu Jun 11, 2015 1:53 pm

Re: Sensor Debounce

Post by lduepner »

franklin97355 wrote:
code was only supposed to allow a state change if the input read the same state after an interval set in the code.
It only does a motor state change if the sensor state is different than the motor state and 5 seconds has elapsed.
yes sorry I miss stated what it does, however that does not change the fact that I was getting a state change faster than the programmed interval. I believe the problem might be in the fact that I did not use the long command when I initialized the LastStateChange variable. I noticed in the arduino debounce example that they use long whenever they are going to be counting with millis().

User avatar
adafruit_support_bill
 
Posts: 88088
Joined: Sat Feb 07, 2009 10:11 am

Re: Sensor Debounce

Post by adafruit_support_bill »

Yes. millis() returns a long.

User avatar
lduepner
 
Posts: 32
Joined: Thu Jun 11, 2015 1:53 pm

Re: Sensor Debounce

Post by lduepner »

adafruit_support_bill wrote:Yes. millis() returns a long.
so the program should really be more like this

Code: Select all

const int SensorPin = 13; // the number of the switch pin
const int MotorPin =  9; // the number of the motor pin

int SensorState = 0;  // variable for reading the switch's status
int MotorState = 0;
long LastStateChange = 0;

void setup() {
  // initialize the motor pin as an output:
  pinMode(MotorPin, OUTPUT); 
  // initialize the switch pin as an input:
  pinMode(SensorPin, INPUT);  
}

void loop(){
  // read the state of the switch value:
  SensorState = digitalRead(SensorPin);

 if ((SensorState != MotorState) && (millis() - LastStateChange > 2500))
{
    MotorState = SensorState;
    digitalWrite(MotorPin, MotorState);
    LastStateChange = millis();
}
}
   
with the LastStateChange initialized with the command long

User avatar
lduepner
 
Posts: 32
Joined: Thu Jun 11, 2015 1:53 pm

Re: Sensor Debounce

Post by lduepner »

so I have been running the following code to control my pump,

Code: Select all

const int SensorPin = 13; // the number of the switch pin
const int MotorPin =  9; // the number of the motor pin
const int LedPin = 8;

int SensorState = 0;  // variable for reading the switch's status
int MotorState = 0;
long LastStateChange = 0;

void setup() {
  // initialize the motor pin as an output:
  pinMode(MotorPin, OUTPUT); 
  // initialize the switch pin as an input:
  pinMode(LedPin, OUTPUT);
  pinMode(SensorPin, INPUT);  
}

void loop(){
  // read the state of the switch value:
  SensorState = digitalRead(SensorPin);

 if ((SensorState != MotorState) && (millis() - LastStateChange > 5000))
{
    MotorState = SensorState;
    digitalWrite(MotorPin, MotorState);
    digitalWrite(LedPin, MotorState);
    LastStateChange = millis();
}
}
   
I am running into a bug where the state gets stuck and won't change. I have observed this in both directions (pump not turning on and pump not turning off). Does anyone have any thoughts on what might be causing this particular bug?
On a completely different non programming note, does anyone have any idea why my pump struggles to run through a mosfet but not when hooked directly to power and ground when the fet is hooked to the same source?

User avatar
adafruit_support_bill
 
Posts: 88088
Joined: Sat Feb 07, 2009 10:11 am

Re: Sensor Debounce

Post by adafruit_support_bill »

What kind of sensor are you using? If a switch, do you have a pullup or pulldown resistor on it?

User avatar
lduepner
 
Posts: 32
Joined: Thu Jun 11, 2015 1:53 pm

Re: Sensor Debounce

Post by lduepner »

This is the sensor I am using,
http://www.amazon.com/gp/product/B009NA ... ge_o00_s01
I have it wired as per the manufacture with a 4.7 kilo ohm pull up resistor on the signal.

User avatar
adafruit_support_bill
 
Posts: 88088
Joined: Sat Feb 07, 2009 10:11 am

Re: Sensor Debounce

Post by adafruit_support_bill »

I don't see any obvious faults in the code. You might try adding in a Serial.println statement to display the sensor value - just to make sure it is giving you the output you are expecting from it.

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino”