About Me Assignments Projects

Week 11: Input Devices

Rotary Encoder

This week's assignment was to test input devices using different development boards. I used a rotary encoder as an input device to explore its functionality and integration with my project.

Rotary Encoder Diagram

Introduction to Rotary Encoders

A rotary encoder is an electromechanical device that converts the angular position or motion of a shaft or axle to an analog or digital code. Rotary encoders are used in a wide range of applications that require precise measurement of rotation, such as in industrial controls, robotics, and consumer electronics.

Types of Rotary Encoders

There are two main types of rotary encoders:

How Rotary Encoders Work

A rotary encoder consists of a disk, usually made of glass or plastic, with alternating transparent and opaque segments. As the disk rotates, it interrupts a light beam, generating pulses that are counted by the controlling device. The number of pulses per rotation indicates the resolution of the encoder.

Rotary Encoder Diagram

image courtsy howtomechatronics.com

Applications of Rotary Encoders

Rotary encoders are versatile and used in various applications, including:

PCB Design with ATtiny1614 Microcontroller

Components Used:

ATtiny1614 Pinout Diagram

schematic

i used EASYEDA for drawing the pcb and started of with the schemitcs

ESC Circuit Diagram

downloded the schematic

Designing the PCB in EasyEDA

then i used EASYEDA for drawing the pcb and started of with the pcb and exporeted the gerber

Integrating Rotary Encoder into PCB Design

Wiring the Rotary Encoder to the PCB

Integrating a rotary encoder with the ATtiny1614 microcontroller involves connecting the encoder's pins to the microcontroller's GPIO pins, specifically using interrupt-capable pins for optimal performance. Here’s how to proceed:

  1. Identify Rotary Encoder Pins:
  2. Connect Rotary Encoder Pins to ATtiny1614:
  3. Using Interrupts for Rotary Encoder:

By following these steps in EasyEDA, you can effectively design and integrate a rotary encoder into your PCB with the ATtiny1614 microcontroller. This approach ensures precise control and input detection directly from the encoder, enhancing the functionality of your embedded system project.

please refer the documentation on week 9 to get more information on designing pcb with easy eda

ESC Circuit Diagram

downloded the gerber here

PRODUCTION

then i used fab modes to send the files to the milling machine and milled the pcb and assembled it

ESC Circuit Diagram

please refer the documentation on week 6 to get more information on USING FABMODS AND PCB MILLING

Rotary Encoder Code

The following code demonstrates how to interface with a rotary encoder using an Arduino. The code reads the position of the rotary encoder and prints the position to the serial monitor.

        #define ENCODER_PIN_A 9  // Pin connected to encoder A output
        #define ENCODER_PIN_B 8  // Pin connected to encoder B output
        #define ENCODER_BUTTON_PIN 0
        #define LED_PIN 2     // the number of the LED pin
        
        
        volatile int encoderCount = 0;
        
        
        bool ledState = false;         // the current state of the LED
        int buttonState;               // the current reading from the input pin
        bool lastButtonState = false;  // the previous reading from the input pin
        
        // the following variables are long because the time, measured in milliseconds,
        // will quickly become a bigger number than can be stored in an int.
        unsigned long lastDebounceTime = 0;  // the last time the output pin was toggled
        unsigned long debounceDelay = 50;    // the debounce time; increase if the output flickers
        
        
        void setup() {
          pinMode(ENCODER_BUTTON_PIN, INPUT_PULLUP);
          pinMode(LED_PIN, OUTPUT);
        
          pinMode(ENCODER_PIN_A, INPUT);
          pinMode(ENCODER_PIN_B, INPUT);
          attachInterrupt(digitalPinToInterrupt(ENCODER_PIN_A), encoderISR, CHANGE);
        
        
          Serial.begin(9600);
        }
        
        void loop() {
            // Print the encoder count
            Serial.print("Encoder Count: ");
          Serial.println(encoderCount);
          delay(100);  // Update every second
        
        
          // read the state of the switch into a local variable:
          int reading = digitalRead(ENCODER_BUTTON_PIN);
        
          // check to see if you just pressed the button
          // (i.e., the input went from LOW to HIGH), and you've waited long enough
          // since the last press to ignore any noise:
        
          // If the switch changed, due to noise or pressing:
          if (reading != lastButtonState) {
            // reset the debouncing timer
            lastDebounceTime = millis();
          }
        
          if ((millis() - lastDebounceTime) > debounceDelay) {
            // whatever the reading is at, it's been there for longer than the debounce
            // delay, so take it as the actual current state:
        
            // if the button state has changed:
            if (reading != buttonState) {
              buttonState = reading;
        
              // only toggle the LED if the new button state is HIGH
              if (buttonState == HIGH) {
        
                encoderCount = 0;
                ledState = !ledState;
              }
            }
          }
        
        
        
          // set the LED:
          digitalWrite(LED_PIN, ledState);
        
          // save the reading. Next time through the loop, it'll be the lastButtonState:
          lastButtonState = reading;
        }
        
        void encoderISR() {
          // Read the state of pin B
          if (digitalRead(ENCODER_PIN_A) == digitalRead(ENCODER_PIN_B)) {
            encoderCount++;
          } else {
            encoderCount--;
          }
          //   Serial.print("Encoder Count: ");
          // Serial.println(encoderCount);
        }
        
      

Explanation of the Code

This sketch handles the reading of a rotary encoder's output to track its rotation count and the reading of a button to reset the encoder count and toggle an LED. The sketch includes debounce logic to ensure reliable button presses.

Pin Definitions

Global Variables

Setup Function

In the setup function:

Loop Function

In the loop function:

Encoder Interrupt Service Routine (ISR)

The encoderISR function:

Testing the Rotary Encoder

DOWNLOAD THE CODE

download the CODE here

DOWNLOAD THE DESIGN FILES

downloded the gerber here

here are the original design files [easy eda]

download the easyeda files here

GROUP Assignment

This week's group assingment was to probe an input device's analog levels and digital signals. We chose Hall Effect sensor as the input device.

ESC Circuit Diagram see the group assingment here