LCD Counter with Pull Down Resistor and Debounce

Overview:
This tutorial will go over the basic functions of a button and a LCD screen. By the end of the tutorial you will be able to properly wire both components and use them for your own projects.
Components:
1X Arduino Compatible Board
1X Jumper Wire Set
1X 220Ω Resistors
1X 10kΩ Resistor
1X button
1X potentiometer

Let’s go over 2 of the most important items for that can be used for the Arduino platform. The button and the LCD screen.

THE BUTTON!
This is a very simple item, but in practice can be a little tricky to get working properly. The 2 common things you need to be aware of are floating pins and button bouncing.

A floating pin will give you random spikes which can cause a false reading. This is caused because the digital IO pins are very sensitive to interference or capacitance. To get rid of this you will need to use a pull down (or pull up) resistor to pull it to a known state.

Button bouncing occurs when you press the button, but the controller sees it as 2,3, or even more presses. This is caused because the contacts bounce off each other very briefly before they settle to fully closed position. We’ll fix this problem in the arduino code. Because the bounces last only a few milliseconds, we can use we can add a time check to make sure that the button has changed state for a certain amount of time.

LCD Display
These are super easy to set up and all you really need is a bunch of jumper wires, a 220Ω resistor, a 10Ω potentiameter and, the liquidcrystal library from the arduino IDE. From there just follow the code and diagram and then you should be up and running!
layout

Code:

We will be ultilizing very basic functions such as analog read, analog write (PWM), and digital write.

#include <LiquidCrystal.h> //pulls code from the LCD library and makes life easy

LiquidCrystal lcd(9, 8,7, 6, 5, 4); //defines (RS,enable,D4,D5,D6,D7) pins
#define button 2 //defines digital pin 2 as an IO
bool buttonState=false;//button starts off low
int count =0;//variable that will be incremented starting from 0
unsigned long timeStamp=0; //saves current time when a button has been pressed.  
                           //defined as a positive long number

void setup()
{
  Serial.begin(9600);
  pinMode(button,INPUT);  //decalares button as an input
  lcd.begin(16,2);        //defines the size of the LCD display
  lcd.setCursor(0,0);     //sets where the LCD will start writing 0,0 is top left
}

void loop() {
  if (digitalRead(button)==HIGH){ //loop to see if button is pressed
    if(buttonState==false&& millis()-timeStamp>200){ /*makes sure the the button isn't being held down and that 
      200ms has passed since last press.  this stops the button from bouncing.  Try making it a low value and you 
      will the number jump a few times each time you press the button*/
    }
      count++;  //increments count
      buttonState=true;//saves that button is pressed
      timeStamp=millis();    //saves time that button is pressed
    }}    else{
      buttonState=false;    //saves that the button is not pressed
  }
  
  lcd.setCursor(0,0);      //starts curser at 0,0
  lcd.print(count);        //prints the counter to the LCD 
  }
 


corra02

Leave a Reply

Your email address will not be published. Required fields are marked *