kartinka ei ole

Инкрементальный энкодер EC-11

Краткое описание

Инкрементальный (или инкрементный, от англ. increment — «увеличение») энкодер (датчик угла поворота) — это устройство, которое преобразовывает вращательное движение вала в серию электрических импульсов, позволяющих определить направление и угол его вращения. Также, исходя из найденных величин, можно определить и скорость вращения.

Инкрементальные энкодеры бывают оптическими, магнитными, механическими и т.д. Вне зависимости от принципа устройства все инкрементальные энкодеры на выходе генерируют 2 линии (A и B) с импульсами смещенными относительно друг друга. Именно по смещению импульсов можно судить о направлении вращения. А по количеству импульсов — об угле поворота.

Подключение EC-11

  • Gnd -> Gnd
  • S1 -> Arduino D8
  • S2 -> Arduino D9
  • Key -> Arduino D10
  • +5V -> +5V

Сcылки на сайт с библиотекой и источником информацией

  • Сайт с информацией
  • Сайт с библиотекой
  • Схема подключения и код подключения модуля к Arduino

                                
    // Rotary Encoder Inputs
    #define S1 8
    #define S2 9
    #define Key 10
    
    int counter = 0;
    int currentStateS1;
    int lastStateS1;
    String currentDir ="";
    unsigned long lastButtonPress = 0;
    
    void setup() {
        
        // Set encoder pins as inputs
        pinMode(S1,INPUT);
        pinMode(S2,INPUT);
        pinMode(Key, INPUT_PULLUP);
    
        // Setup Serial Monitor
        Serial.begin(9600);
    
        // Read the initial state of S1
        lastStateS1 = digitalRead(S1);
    }
    
    void loop() {
        
        // Read the current state of S1
        currentStateS1 = digitalRead(S1);
    
        // If last and current state of S1 are different, then pulse occurred
        // React to only 1 state change to avoid double count
        if (currentStateS1 != lastStateS1  && currentStateS1 == 1){
    
            // If the S2 state is different than the S1 state then
            // the encoder is rotating CCW so decrement
            if (digitalRead(S2) != currentStateS1) {
                counter --;
                currentDir ="CCW";
            } else {
                // Encoder is rotating CW so increment
                counter ++;
                currentDir ="CW";
            }
    
            Serial.print("Direction: ");
            Serial.print(currentDir);
            Serial.print(" | Counter: ");
            Serial.println(counter);
        }
    
        // Remember last S1 state
        lastStateS1 = currentStateS1;
    
        // Read the button state
        int btnState = digitalRead(Key);
    
        //If we detect LOW signal, button is pressed
        if (btnState == LOW) {
            //if 50ms have passed since last LOW pulse, it means that the
            //button has been pressed, released and pressed again
            if (millis() - lastButtonPress > 50) {
                Serial.println("Button pressed!");
            }
    
            // Remember last button press event
            lastButtonPress = millis();
        }
    
        // Put in a slight delay to help debounce the reading
        delay(1);
                                
                            

    © Вёрстку делал Даниэль Усов