Hello all, I've searched the post but can only find info relating to one Rotary encoder, I'm trying to make a wheel box/ Flight trim box for use on my pc flight simulator there is five ky-040 Rotary encoders and four on/off/on switches Ive got everything drilled and fitted to a project box but struggling what Arduino varient to use and find a wiring diagram/code Each (KY-040) encoder has five pins 5v/grnd which speak for themselves, ClocK, Data, Switch, I think all the encoders will use 15pins and the switches will use 10pins, I've read that one of the pins need to be connected to digital and I assume that the other two can be be digital or analogue ? I'm unsure as to what pin needs to be the digital, I've looked for hours on Google and you tube but none give a good explanation I'm a complete novice to this hence asking for help.
The KY-040 are rotary encoders, not potentiometers. This page may help.
For more info, Google "arduino ky-040".
Thankyou groundfungus
Feirypackman:
I think all the encoders will use 15pins and the switches will use 10pins,
Each encoder *will * require two inputs for its signals however, the encoder commons can all be tied together, usually to Vcc. Same applies to the switches, all the 'common' pins of the switches can be tied together, to GND or supply, while the NO contacts go to their respective Arduino inputs. In all cases the switch inputs must be pulled to either GND (down) or supply voltage (up) by resistors. The inputs can be configured to use built-in pullups if desired.
Hi,
Welcome to the forum.
google I2C rotary encoder
Tom...
I wrote this on a Mega. It's got five pins that can be used for pin interrupts. In the code I use a rising edge of phase A (CLK) to generate an interrupt for each channel. The state of phase B (DT) indicates direction. Because the code only checks the rising edges of A you lose some resolution. You still get 20 pulses per revolution. You can see if the concept works for you.
I used ints to capture the value. Since ints can range from 32767 down to -32768 that should be plenty (1600 revolutions of the encoder in either direction...)
Each time something changes (encoder event, button change of state) the values are printed to the serial monitor at 115200.
You should be able to use this as a basis for future code to talk to the flight sim. It will at least allow you to sanity check your setup.
#define CHANNEL_1 0
#define CHANNEL_2 1
#define CHANNEL_3 2
#define CHANNEL_4 3
#define CHANNEL_5 4
const byte pinEnc1_A = 2; //connect to CLK pin of encoder
const byte pinEnc1_B = 4; //connect to DT pin of encoder
//connect GND to GND of encoder
const byte pinEnc2_A = 3; //...
const byte pinEnc2_B = 5;
const byte pinEnc3_A = 18;
const byte pinEnc3_B = 6;
const byte pinEnc4_A = 19;
const byte pinEnc4_B = 7;
const byte pinEnc5_A = 20;
const byte pinEnc5_B = 8;
const byte pinSw1 = 9; //NO switches; ground when pressed
const byte pinSw2 = 10;
const byte pinSw3 = 11;
const byte pinSw4 = 12;
const byte pinSw5 = 13;
const byte grEncPins[] =
{
pinEnc1_A, pinEnc1_B,
pinEnc2_A, pinEnc2_B,
pinEnc3_A, pinEnc3_B,
pinEnc4_A, pinEnc4_B,
pinEnc5_A, pinEnc5_B
};
const byte grSwPins[] =
{
pinSw1,
pinSw2,
pinSw3,
pinSw4,
pinSw5
};
volatile bool
bEvent;
int
encChannels[5];
byte
swStates,
lastSwitch[5];
char
szStr[50];
void setup( void )
{
Serial.begin(115200);
for( int i=0; i<10; i++ )
pinMode( grEncPins[i], INPUT_PULLUP );
swStates = 0;
for( int i=0; i<5; i++ )
{
pinMode( grSwPins[i], INPUT_PULLUP );
lastSwitch[i] = digitalRead( grSwPins[i] );
if( lastSwitch[i] == HIGH )
swStates |= (1<<i);
//
encChannels[i] = 0;
}//for
attachInterrupt( digitalPinToInterrupt(pinEnc1_A), ISR_Enc1, RISING );
attachInterrupt( digitalPinToInterrupt(pinEnc2_A), ISR_Enc2, RISING );
attachInterrupt( digitalPinToInterrupt(pinEnc3_A), ISR_Enc3, RISING );
attachInterrupt( digitalPinToInterrupt(pinEnc4_A), ISR_Enc4, RISING );
attachInterrupt( digitalPinToInterrupt(pinEnc5_A), ISR_Enc5, RISING );
bEvent = false;
}//setup
void loop( void )
{
ReadSwitches();
if( bEvent )
{
bEvent = false;
for( int i=0; i<5; i++ )
{
sprintf( szStr, "SW%d %s Encoder %d: %d\n",
i+1,
(swStates & (1 << i)) ? "OPEN ":"CLOSED",
i+1,
encChannels[i] );
Serial.print( szStr );
}//for
Serial.println("");
}//if
}//loop
#define READ_INIT 0
#define READ_DEBOUNCE 1
void ReadSwitches( void )
{
byte
nowRead;
static unsigned long
timeDebounce = 0;
static byte
lastRead,
stateReading = READ_INIT,
readingSw = 0;
switch( stateReading )
{
case READ_INIT:
lastRead = digitalRead( grSwPins[readingSw] );
timeDebounce = millis();
stateReading = READ_DEBOUNCE;
break;
case READ_DEBOUNCE:
if( millis() - timeDebounce < 20 )
return;
nowRead = digitalRead( grSwPins[readingSw] );
if( nowRead == lastRead )
{
if( nowRead != lastSwitch[readingSw] )
{
bEvent = true;
lastSwitch[readingSw] = nowRead;
if( nowRead == LOW )
swStates &= ~(1 << readingSw);
else
swStates |= (1 << readingSw);
}//if
}//if
if( readingSw < 4 )
readingSw++;
else
readingSw = 0;
stateReading = READ_INIT;
break;
}//switch
}//ReadSwitches
void ISR_Enc1( void )
{
delayMicroseconds(20);
if( digitalRead( pinEnc1_A ) == LOW )
return;
bEvent = true;
if( digitalRead( pinEnc1_B ) == LOW )
{
if( encChannels[CHANNEL_1] < 32767 )
encChannels[CHANNEL_1]++;
}//if
else
{
if( encChannels[CHANNEL_1] > -32768 )
encChannels[CHANNEL_1]--;
}//else
}//ISR_Enc1
void ISR_Enc2( void )
{
delayMicroseconds(20);
if( digitalRead( pinEnc2_A ) == LOW )
return;
bEvent = true;
if( digitalRead( pinEnc2_B ) == LOW )
{
if( encChannels[CHANNEL_2] < 32767 )
encChannels[CHANNEL_2]++;
}//if
else
{
if( encChannels[CHANNEL_2] > -32768 )
encChannels[CHANNEL_2]--;
}//else
}//ISR_Enc2
void ISR_Enc3( void )
{
delayMicroseconds(20);
if( digitalRead( pinEnc3_A ) == LOW )
return;
bEvent = true;
if( digitalRead( pinEnc3_B ) == LOW )
{
if( encChannels[CHANNEL_3] < 32767 )
encChannels[CHANNEL_3]++;
}//if
else
{
if( encChannels[CHANNEL_3] > -32768 )
encChannels[CHANNEL_3]--;
}//else
}//ISR_Enc3
void ISR_Enc4( void )
{
delayMicroseconds(20);
if( digitalRead( pinEnc4_A ) == LOW )
return;
bEvent = true;
if( digitalRead( pinEnc4_B ) == LOW )
{
if( encChannels[CHANNEL_4] < 32767 )
encChannels[CHANNEL_4]++;
}//if
else
{
if( encChannels[CHANNEL_4] > -32768 )
encChannels[CHANNEL_4]--;
}//else
}//ISR_Enc4
void ISR_Enc5( void )
{
delayMicroseconds(20);
if( digitalRead( pinEnc5_A ) == LOW )
return;
bEvent = true;
if( digitalRead( pinEnc5_B ) == LOW )
{
if( encChannels[CHANNEL_5] < 32767 )
encChannels[CHANNEL_5]++;
}//if
else
{
if( encChannels[CHANNEL_5] > -32768 )
encChannels[CHANNEL_5]--;
}//else
}//ISR_Enc5