Digital Sensors


Encoder

Grove - Encoder encodes the rotation signal from the axis and output the signal by electric pulse.

Interface: Digital

Arduino Libraries: Ai Esp32 Rotary Encoder

Arduino Examples: File > Examples > Ai Esp32 Rotary Encoder > *




Encoder
#include "AiEsp32RotaryEncoder.h"

#define ROTARY_ENCODER_A_PIN 35 // One pin
#define ROTARY_ENCODER_B_PIN 34 // Other
// #define ROTARY_ENCODER_BUTTON_PIN 0 NO BUTTON FOR ESP32
#define ROTARY_ENCODER_STEPS 4

AiEsp32RotaryEncoder rotaryEncoder = AiEsp32RotaryEncoder(ROTARY_ENCODER_A_PIN, ROTARY_ENCODER_B_PIN, -1, -1, ROTARY_ENCODER_STEPS);

void IRAM_ATTR readEncoderISR()
{
    rotaryEncoder.readEncoder_ISR();
}

void setup()
{
    Serial.begin(115200);
    rotaryEncoder.begin();
    rotaryEncoder.setup(readEncoderISR);
    rotaryEncoder.setBoundaries(0, 1000, false); //minValue, maxValue, circleValues true|false (when max go to min and vice versa)
    rotaryEncoder.setAcceleration(250);
}

void loop()
{
    if (rotaryEncoder.encoderChanged())
    {
        Serial.println(rotaryEncoder.readEncoder());
    }
    /* NO BUTTON FOR ESP32
    if (rotaryEncoder.isEncoderButtonClicked())
    {
        Serial.println("button pressed");
    }
    */
}

/*  // For ptotopie connect:
#include "AiEsp32RotaryEncoder.h"
 
#define ROTARY_ENCODER_A_PIN 35 // One pin
#define ROTARY_ENCODER_B_PIN 34 // Other
#define ROTARY_ENCODER_STEPS 4
 
int encStatePrevious = 0;
 
AiEsp32RotaryEncoder rotaryEncoder = AiEsp32RotaryEncoder(ROTARY_ENCODER_A_PIN, ROTARY_ENCODER_B_PIN, -1, -1, ROTARY_ENCODER_STEPS);
 
void IRAM_ATTR readEncoderISR()
{
rotaryEncoder.readEncoder_ISR();
}
 
void setup()
{
Serial.begin(115200);
rotaryEncoder.begin();
rotaryEncoder.setup(readEncoderISR);
rotaryEncoder.setBoundaries(0, 1000, false); //minValue, maxValue, circleValues true|false (when max go to min and vice versa)
rotaryEncoder.setAcceleration(250);
encStatePrevious = rotaryEncoder.readEncoder();
}
 
 
void loop()
{
  if (rotaryEncoder.encoderChanged())
  {
    Serial.println(int(rotaryEncoder.readEncoder()));
    if (int(rotaryEncoder.readEncoder()) > encStatePrevious) {
      Serial.println("UP");
      encStatePrevious = int(rotaryEncoder.readEncoder());
    }
    if (int(rotaryEncoder.readEncoder()) < encStatePrevious) {
      Serial.println("DOWN");
      encStatePrevious = int(rotaryEncoder.readEncoder());
    }
  }

} */

Ananlog Sensors


Joystick Serial Read
 
  • No labels