When purchased, they come with an encoder with a USB output that can be plugged into either a PC or a Raspberry Pi. But I want to use it to experiment with an arduino instead as I'm new to it. I've successfully connected the ground pin and the signal pin to correctly determine a button press using the following code:
const int buttonPins[] = {2, 3, 4}; // Digital pins for buttons (currently just using a single button)
const int numButtons = 3;
void setup() {
for (int i = 0; i < numButtons; i++) {
pinMode(buttonPins[i], INPUT_PULLUP); // Enable internal pull-up resistors
}
pinMode(LED_BUILTIN, OUTPUT);
Serial.begin(9600); // Start serial for debugging
}
void loop() {
for (int i = 0; i < numButtons; i++) {
if (digitalRead(buttonPins[i]) == LOW) { // Button pressed
Serial.print("Button ");
Serial.print(i + 1);
Serial.println(" is pressed");
}
}
delay(50); // Debounce delay
}
But I can't get the LED to work... It lights up if I connect it to the Encoder and then my PC, so I know it's not a DOA. But I don't know how to successfully toggle it (or even have it permanently on) using the Arduino. I've tried connecting the LED pin to both the 5V output and the digital output pins and setting pinMode to high, but still, no light.
On the bottom of the button it says 330R so I'm guessing I shouldn't need an additional resistor. Unfortunately I do not have a multimeter. Any help is appreciated!
That does not make any sense. If you want our pin to be an output, you set it to OUTPUT and then use digitalWrite() to set it HIGH or LOW
My best advise would be to buy a multimeter. Those buttons only have 3 pins so 1 ground, 1 for the button and 1 for the LED but trying all the combinations may damage it
Welcome to the forum. I assume you intend bypassing whatever that USB controller board offers.
I quoted you saying something just wrong.
To illuminate an LED, set a pin to OUTPUT mode, then write HIGH or LOW to the pin.
The LED (with series current limiting resistor, probably you are right 330 ohms already present, but it woukd be nice to be sure) can be wired between that pin and ground
const byte somePin = 7; // run this experiment using pin 7
pinMode(somePin, OUTPUT);
digitalWrite(somePin, HIGH);
If that's what you meant to say you tried, you'll have to be as careful talking to us as you are programming!
Sorry about that, that was indeed what I meant to say! Thanks for the help.
Yes, I'm trying to bypass the USB encoder board, I've included pictures of it here regardless if it can provide any information (the buttons are to be connected to the K1-K12 positions). The LED in the button does not light up when connected to either the 5V output or using the code snippet you provided. From the detailed description on Amazon it says
"The buttons are 5V LED Illuminated push buttons with build-in Microswitches inside.
The buttons default as lighted all the time."
Unfortunately there are no additional documentation, instructions or schematics in the box. My best guess is that the Arduino is unable to provide enough current for the LED, but since I don't have any external power sources I can't verify that.
Oh wow. Trial and error goes a long way. Flipping Ground and LED from my previous assumption did the trick. I guess I got it all mixed up previously and since I got the button press to register I just assumed it was correct and never reevaluated. Thank you all for the help, appriciate it!
Indeed. With a few paths to take in a situation like this, sometime the quickest is to just do what you did.
I breathe a sigh of relief, even though I was quite sure you would not damage anything.
LEDs can be wired to an output pin in two ways, most common is between the pin and ground, so HIGH illuminates the LED.
But you could just as well wire it between 5 volts and the pin, observing the same polarity natch, in which case LOW would be the output that would light it up.
Same same for switches. We recommend wiring switches between the pin and ground, and using INPUT_PULLUP as the pin mode. This requires no extra components, but it means a switch will read LOW when pressed.
A nice habit for making code flexible as well as readable is to
# define LED_ON LOW
# define LED_OFF HIGH
up top and always use those like
digitalWrite(someLEDPin, LED_OFF);
in the case of switches wired to ground, use
# define PRESSED LOW
# define NOT_PRESSED HIGH
and again always
if (digitalRead(someButtonPin) == PRESSED) {
// whatever…
}
Readers won't need to comb through the code or look at schematics to see what your intent is.