Go Down

Topic: Want to emulate a rotary encoder, have a couple of questions. (Read 5542 times) previous topic - next topic


It *IS* for a push button. I was attempting to make sure the inputs were working properly on the Arduino Uno. I figured it would be simpler with a push button that a rotary encoder. Once I figure out what I need to get the results I'm looking for with the button, then I can move up to testing with the RE.



Try this modified version of the Debounce example found with the Arduino IDE.
Tell us what it prints when you activate the button.

  Also, a piece of wire from GND to pin 2 "the button pin" can also be used to simulate a switch. Just insert the wire and remove it back and forth.

Code: [Select]

Each time the input pin goes from LOW to HIGH (e.g. because of a push-button
press), the output pin is toggled from LOW to HIGH or HIGH to LOW.  There's
a minimum delay between toggles to debounce the circuit (i.e. to ignore

The circuit:
* LED attached from pin 13 to ground
* pushbutton attached from pin 2 to +5V
* 10K resistor attached from pin 2 to ground

* Note: On most Arduino boards, there is already an LED on the board
connected to pin 13, so you don't need any extra components for this example.

created 21 November 2006
by David A. Mellis
modified 30 Aug 2011
by Limor Fried

This example code is in the public domain.


// constants won't change. They're used here to
// set pin numbers:
const int buttonPin = 2;     // the number of the pushbutton pin
const int ledPin =  13;      // the number of the LED pin

// Variables will change:
int ledState = HIGH;         // the current state of the output pin
int buttonState;             // the current reading from the input pin
int lastButtonState = LOW;   // the previous reading from the input pin

// the following variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long lastDebounceTime = 0;  // the last time the output pin was toggled
long debounceDelay = 50;    // the debounce time; increase if the output flickers

void setup() {
  //start serial connection
  //configure pin2 as an input and enable the internal pull-up resistor
  pinMode(2, INPUT);
  digitalWrite(2, HIGH); //enables pull-up resitor
  pinMode(13, OUTPUT);

void loop() {
  // read the state of the switch into a local variable:
  int sensorVal = digitalRead(2);
  // check to see if you just pressed the button
  // (i.e. the input went from LOW to HIGH),  and you've waited
  // long enough since the last press to ignore any noise: 

  // If the switch changed, due to noise or pressing:
  if (sensorVal != lastButtonState) {
    // reset the debouncing timer
    lastDebounceTime = millis();

  if ((millis() - lastDebounceTime) > debounceDelay) {
    // whatever the sensorVal is at, it's been there for longer
    // than the debounce delay, so take it as the actual current state:
    buttonState = sensorVal;

  // set the LED using the state of the button:
  digitalWrite(13, buttonState);

  // save the sensorVal.  Next time through the loop,
  // it'll be the lastButtonState:
  lastButtonState = sensorVal;
Good links: Eagle tutorial= http://www.youtube.com/playlist?list=PLDE1858BD83D19C70
General Arduion tutorials = http://tronixstuff.wordpress.com


Unfortunately, I can't use that one. I need to use a ground for the button. Since it's still mounted on a very expensive piece of equipment, and it is referenced to ground there. I don't want to try and shoot 5v into all the grounds. I'm beginning to wonder if I damaged my pullup resistors...at one point while testing something for another part of this project, I got a message saying I exceeded the current capacity of my USB port and it shut it down...unfortunately, being Thanksgiving, Radio Shack won't be open for me to pick up some resistors to test with.

I was simply jumping the wire back and forth w/o the switch, and that was how I determined the inputs/sketch seemed to be working as they should. I even tried adding a diode inline on the switch/pin 2 side to see if it had any effect, and it didn't.


Go Up