Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /********* Pleasedontcode.com **********
- Pleasedontcode thanks you for automatic code generation! Enjoy your code!
- - Terms and Conditions:
- You have a non-exclusive, revocable, worldwide, royalty-free license
- for personal and commercial use. Attribution is optional; modifications
- are allowed, but you're responsible for code maintenance. We're not
- liable for any loss or damage. For full terms,
- please visit pleasedontcode.com/termsandconditions.
- - Project: Encoder Display
- - Source Code NOT compiled for: Arduino Nano ESP32
- - Source Code created on: 2025-07-15 22:52:52
- ********* Pleasedontcode.com **********/
- /****** SYSTEM REQUIREMENTS *****/
- /****** SYSTEM REQUIREMENT 1 *****/
- /* Capture the rotational angle in degrees from the */
- /* KY-040 rotary encoder and show the result on an */
- /* LCD screen through I2C communication, leveraging */
- /* the SimpleEncoder library. */
- /****** END SYSTEM REQUIREMENTS *****/
- /* START CODE */
- /****** DEFINITION OF LIBRARIES ******/
- #include <Wire.h>
- #include <SimpleEncoder.h> // http://github.com/EasyG0ing1/SimpleEncoder
- #include <LCDIC2.h> // https://github.com/offcircuit/LCDIC2
- /****** FUNCTION PROTOTYPES *****/
- void setup(void);
- void loop(void);
- /***** DEFINITION OF DIGITAL INPUT PINS *****/
- const uint8_t encoder_KY_040_CLK_PIN_D2 = 2;
- const uint8_t encoder_KY_040_DT_PIN_D3 = 3;
- const uint8_t encoder_KY_040_SW_PIN_D4 = 4;
- /***** DEFINITION OF I2C PINS *****/
- const uint8_t angleDisplay_LCD1602I2C_I2C_PIN_SDA_A4 = A4;
- const uint8_t angleDisplay_LCD1602I2C_I2C_PIN_SCL_A5 = A5;
- const uint8_t angleDisplay_LCD1602I2C_I2C_SLAVE_ADDRESS = 39;
- /****** DECLARATION OF LIBRARY OBJECT INSTANCES *****/
- // Initialize SimpleEncoder with CLK, DT, SW pins
- SimpleEncoder encoder(encoder_KY_040_CLK_PIN_D2, encoder_KY_040_DT_PIN_D3, encoder_KY_040_SW_PIN_D4);
- // Initialize LCD with I2C address, 16 columns, 2 rows
- LCDIC2 lcd(angleDisplay_LCD1602I2C_I2C_SLAVE_ADDRESS, 16, 2);
- /****** VARIABLE DECLARATION *****/
- float angleDegrees = 0;
- void setup(void)
- {
- // Initialize serial communication for debugging (optional)
- Serial.begin(9600);
- // Setup encoders pins
- pinMode(encoder_KY_040_CLK_PIN_D2, INPUT);
- pinMode(encoder_KY_040_DT_PIN_D3, INPUT);
- pinMode(encoder_KY_040_SW_PIN_D4, INPUT_PULLUP);
- // Initialize LCD
- if (lcd.begin()) {
- lcd.clear();
- lcd.setCursor(0, 0);
- lcd.print("Rotary Encoder");
- lcd.setCursor(0, 1);
- lcd.print("Angle: 0 deg");
- }
- }
- void loop(void)
- {
- // Read encoder for clockwise rotation
- if (encoder.CLOCKWISE)
- {
- angleDegrees += 1;
- encoder.CLOCKWISE = false; // Reset the flag
- }
- // Read encoder for counterclockwise rotation
- if (encoder.COUNTERCLOCKWISE)
- {
- angleDegrees -= 1;
- encoder.COUNTERCLOCKWISE = false; // Reset the flag
- }
- // Optional: Clamp angle between 0 and 360
- if (angleDegrees >= 360)
- {
- angleDegrees -= 360;
- }
- else if (angleDegrees < 0)
- {
- angleDegrees += 360;
- }
- // Display the angle on the LCD
- lcd.setCursor(0, 1);
- lcd.print("Angle: ");
- lcd.print(angleDegrees);
- lcd.print(" deg ");
- // Optional: Print to serial for debugging
- Serial.print("Current angle: ");
- Serial.println(angleDegrees);
- // Small delay to improve readability and debounce
- delay(100);
- }
Advertisement
Add Comment
Please, Sign In to add comment