реклама:

Rotary Encoder. Part 10.

Rotary Encoder. Connection.

Next step is connecting of the encoder to Arduino . Wiring is also not difficult, but you will need a 10K resistor (10 kilo ohms).

Here is a table of conformity between Encoder and Arduino:

Encoder pins Arduino pins
CLK19
DT18
SW28
+,VCC5V
GNDGND

Wiring diagram:

Rotary Encoder

To connect the encoder it is necessary to use 5 wires and 1 resistor 10K

With regard to a 10K resistor. It can be soldered directly to the board of the encoder. It is not known for what purpose the developers do not put it on by default. It is possible to solder SMD resistor or PTH resistor.

Arduino encdoder, 10K resistor

You may either solder the resistor directly to the encoder board, or put it between the wires as shown in the previous scheme - contacts between SW and + 5V

After connecting the encoder, you may download the example below and make sure that it works correctly.

During the execution of the example, you should see «House4u Released», the brightness of the display must be 50% If you press the encoder, it should be changed to «House4u Pressed». If you turn the encoder to the right - that the brightness of the display must be increased if the left - then decrease.

Sketch:

// include the library code:
#include <LiquidCrystal.h>
#include <Encoder.h>

#define PWM_LED_PIN 3 
#define PIN_LCD_RS 39
#define PIN_LCD_EN 41
#define PIN_LCD_D4 43
#define PIN_LCD_D5 45
#define PIN_LCD_D6 47
#define PIN_LCD_D7 49

#define PIN_ENCODER_CLK 19
#define PIN_ENCODER_DT 18
#define PIN_ENCODER_SW 28

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd( PIN_LCD_RS, PIN_LCD_EN, PIN_LCD_D4, PIN_LCD_D5, PIN_LCD_D6, PIN_LCD_D7 );
Encoder myEnc( PIN_ENCODER_DT, PIN_ENCODER_CLK );

int Bright = 128;
long oldPosition = 0;
int i = 0;

void setup() {   
  analogWrite( PWM_LED_PIN, Bright );
  
  lcd.begin(16, 2); 
  lcd.print("House4u sol.proj"); 
  lcd.setCursor(0, 1);  
}

void loop() 
{       
    lcd.setCursor(0, 1);
    long newPosition = myEnc.read() / 4;
   
    if (newPosition > oldPosition) 
    {       
      lcd.print("                ");
      lcd.setCursor( 8 + (i % 8), 1 );
      lcd.print(">");
      Bright += 5;
      if ( Bright > 255 )
        Bright = 255;
    }
    else
    if (newPosition < oldPosition)
    {
      lcd.print("                ");
      lcd.setCursor( 7 - (i % 8), 1 );
      lcd.print("<");           
      Bright -= 5;
      if ( Bright < 0 )
        Bright = 0;
    }
    
    if ( newPosition != oldPosition)
    {
      i = (i + 1)%16;
      oldPosition = newPosition;
      analogWrite( PWM_LED_PIN, Bright );
    }
    
    lcd.setCursor(8, 0);
    if ( digitalRead(PIN_ENCODER_SW) == LOW )
      lcd.print("Pressed ");
    else
      lcd.print("Released");  
        
    delay( 5 ); 
}

The result of the example:

Arduino encdoder, result of the example


<< BackNext >>


Share with friends:

Donate