Cirkit Designer LogoCirkit DesignerYour all-in-one circuit design IDE Launch Cirkit Designer App

RP2040 Zero Rotary Encoder Interface with Serial Monitoring

View and Edit Project In Cirkit DesignerImage of RP2040 Zero Rotary Encoder Interface with Serial Monitoring

Circuit Documentation

Summary

This circuit involves an RP2040 Zero microcontroller connected to a rotary encoder. The rotary encoder is used to read rotational input and a button press, which are then processed by the microcontroller. The state of the rotary encoder and button press are printed to the serial monitor.

Component List

RP2040 Zero

Rotary Encoder

Wiring Details

RP2040 Zero

Rotary Encoder

Code Documentation

/*
 * Rotary Encoder Test
 * This sketch reads the state of a rotary encoder connected to an RP2040 Zero
 * and prints the state to the serial monitor. The rotary encoder has three pins
 * connected to the microcontroller: clk (pin 29), dt (pin 28), and sw (pin 27).
 */

#define CLK_PIN 29
#define DT_PIN 28
#define SW_PIN 27

int lastClkState;
int currentClkState;
int counter = 0;

void setup() {
  Serial.begin(9600);
  pinMode(CLK_PIN, INPUT);
  pinMode(DT_PIN, INPUT);
  pinMode(SW_PIN, INPUT);
  lastClkState = digitalRead(CLK_PIN);
}

void loop() {
  currentClkState = digitalRead(CLK_PIN);
  if (currentClkState != lastClkState) {
    if (digitalRead(DT_PIN) != currentClkState) {
      counter++;
      if (counter > 256) counter = 0;
    } else {
      counter--;
      if (counter < 0) counter = 256;
    }
    Serial.println(counter);
  }
  lastClkState = currentClkState;
  if (digitalRead(SW_PIN) == LOW) {
    Serial.println("Button pressed");
    delay(500); // Debounce delay
  }
}

This code initializes the pins connected to the rotary encoder and reads their states in the loop function. The state of the rotary encoder is printed to the serial monitor, and if the button is pressed, a message is printed. The counter variable keeps track of the encoder's position.