Easiest way to program buttons/knobs

ep user

Jan 9, 2018
15
Joined
Jan 9, 2018
Messages
15
I want to set up my first programmable buttons/knobs (or small display) for electronic control. What is the easiest way to get started if I have not set up anything before?

Moderators note : removed color to improve contrast
 
Last edited by a moderator:

[email protected]

Sep 26, 2013
1
Joined
Sep 26, 2013
Messages
1

1. Buttons (Digital Input)


Required Components:​


  • Push button
  • 10kΩ resistor (for pull-down, or use Arduino's internal pull-up)
  • Arduino (Uno, Nano, etc.)

Wiring (Internal Pull-Up):​


  • One leg of button to GND
  • Other leg to D2 (digital pin)

    =================================

    const int buttonPin = 2; // Button connected to pin 2

    void setup() {
    pinMode(buttonPin, INPUT_PULLUP); // Use internal pull-up
    Serial.begin(9600);
    }

    void loop() {
    if (digitalRead(buttonPin) == LOW) { // LOW means pressed
    Serial.println("Button Pressed");
    delay(200); // Simple debounce
    }
    }


    ===============

    const int buttonPin = 2; // Button connected to pin 2

    void setup() {
    pinMode(buttonPin, INPUT_PULLUP); // Use internal pull-up
    Serial.begin(9600);
    }

    void loop() {
    if (digitalRead(buttonPin) == LOW) { // LOW means pressed
    Serial.println("Button Pressed");
    delay(200); // Simple debounce
    }
    }


    ===================

    Knobs (Analog Input - Potentiometer)


    Required Components:​

    • Potentiometer (10kΩ is fine)
    • Arduino

    Wiring:​

    • One side of pot to 5V
    • Other side to GND
    • Center pin (wiper) to A0

      ========================================

      const int potPin = A0;

      void setup() {
      Serial.begin(9600);
      }

      void loop() {
      int value = analogRead(potPin); // Reads 0-1023
      Serial.println(value);
      delay(100); // Limit print rate
      }



      ================

      Rotary Encoder (Optional for advanced knobs)


      Required:​

      • KY-040 rotary encoder or similar
      • Arduino
      • Pull-up resistors (or internal)

      Wiring:​

      • CLK → D2
      • DT → D3
      • GND → GND
        • → 5V

      Code (with Encoder Library):

      #include <Encoder.h>

      Encoder knob(2, 3); // CLK and DT pins

      void setup() {
      Serial.begin(9600);
      }

      void loop() {
      static long position = -999;
      long newPos = knob.read();
      if (newPos != position) {
      Serial.println(newPos);
      position = newPos;
      }
      }



      regards
      IHSAN ULLAH KHAN

 

danadak

Feb 19, 2021
960
Joined
Feb 19, 2021
Messages
960
Buttons need to be debounced in code unless Micro does that for you in HW. Many
modern micros, especially ones that have capacitive touch capability on pins, have
that in their features. Like Cypress (now Infineon) and their ARM cores.

Reading analog stuff generally you would average some samples to remove noise
from the reading. In many cases one may not care and just use the raw single sample
value.

Some debounce methods -


Regards, Dana.
 
Last edited:

ep user

Jan 9, 2018
15
Joined
Jan 9, 2018
Messages
15
Thanks. I thought about Arduino but was hoping there was a simpler or kit approach. Maybe something that you just tell it what buttons there are, and what the output should be; this output would then easily be connected to the controlled devices.

Moderators note : removed color to improve contrast
 
Last edited by a moderator:

danadak

Feb 19, 2021
960
Joined
Feb 19, 2021
Messages
960
Thanks. I thought about Arduino but was hoping there was a simpler or kit approach. Maybe something that you just tell it what buttons there are, and what the output should be; this output would then easily be connected to the controlled devices.

Moderators note : removed color to improve contrast
There are ICs that simply debounce a button, but thats a waste of budget, board space,
parts count...

 

ep user

Jan 9, 2018
15
Joined
Jan 9, 2018
Messages
15
Seems someone should make a kit that is more pre-assembled, or maybe a beginner Arduino setup that needs only a few hours learning.
 

Martaine2005

May 12, 2015
5,152
Joined
May 12, 2015
Messages
5,152
There are!.
Called shields or modules.
Even libraries of sketches too.
The hard work is done for you, and you only need to apply the necessary ancillaries.
Nothing is perfect and you will need a certain amount of customisation or code edits to get it to your requirements.
 

danadak

Feb 19, 2021
960
Joined
Feb 19, 2021
Messages
960
If you dont know how to code look into block programming, lots of fun,
easy, and fast to get something working.

An example of debouncing a pin -

1753474270568.png

mBlock takes you block config and generates the Arduino code from it.

Once you learn a block programmer there are many other variants with common and unique
capabilities, Snap4Arduino, Visuino, Tuniot, Nodered, Flow code, Visuino, some free, other
pay to play.

Some examples :


 
Last edited:
Top