I'm having issues trying to figure out how to wire it up correctly. I have a/b directly into digital pins, but it's not reading the left/right movements for some reason. I saw someone mentioning 330k or 10k resistors on the q&a but can't find anything concrete as nothing works. Looking for support on this.
do the best I can without photos. One side has two pins. That is the pushbutton +&- (positive & ground). simple
What's hard is the 3 pin side.
A, C, B (left, center, right).
A connects to CLK (GPIO pin) directly, and is also connected to a + bus via a 330Ohm resistor
B connects to DT (GPIO pin) directly & is also connected to the + bus via 330Ohm resistor
C connects directly to ground
The resistor they mention is internal just ignore that comment and connect the center pin to GND and left and right to two pins like 2 and 3 on a UNO and Use the encoder library
For the other side where you have two pins, connect one to pin 4 and the other to ground and in your setup do
pinMode(4, INPUT_PULLUP);
Then read that pin with digitalRead() and it will be LOW when the button is pressed
oh thank god. I'm so glad the resistors are internal. Still though, not sure why it isn't working... I'll post my code below:
int switchPin = 7; // button pin
int switchState = HIGH; // button value
int pinA = 4; // Rotary encoder Pin A
int pinB = 6; // Rotary encoder Pin B
int pinAstateCurrent = LOW; // Current state of Pin A
int pinAStateLast = pinAstateCurrent; // Last read value of Pin A
int total = 0;
void setup() {
Serial.begin (9600); // Initialise the serial monitor
pinMode (switchPin, INPUT_PULLUP); // Enable the switchPin as input with a PULLUP resistor
pinMode (pinA, INPUT); // Set PinA as input
pinMode (pinB, INPUT); // Set PinB as input
}
void loop() {
// BUTTON
switchState = digitalRead(switchPin); // Read the digital value of the switch (LOW/HIGH)
// If the switch is pressed (LOW), print message
if (switchState == LOW) {
Serial.println("Switch pressed");
}
// ROTATION DIRECTION
pinAstateCurrent = digitalRead(pinA); // Read the current state of Pin A
// If there is a minimal movement of 1 step
if ((pinAStateLast == LOW) && (pinAstateCurrent == HIGH)) {
if (digitalRead(pinB) == HIGH) { // If Pin B is HIGH
Serial.println("Left"); // Print on screen
total = total + 1;
Serial.println(total);
} else {
Serial.println("Right"); // Print on screen
total = total - 1;
Serial.println(total);
}
}
pinAStateLast = pinAstateCurrent; // Store the latest read value in the currect state variable
}
I'm not sure I follow... are you saying that I need to hold the button down to show the turns? Obviously that's not the case here. I do notice that if I wiggle the wires, sometimes it triggers it as a turn. However that's just me messing with the leads. Is there an easy way to show a schematic of my wiring?