Increment Rotary encoder working on MEGA but not on NANO

Hi all,

pls move this topic if it is in the wrong section.

I have a simple Encoder counter for increase and decrease for every 100th step it sets an output nothing fancy.

The program works fine without a problem on a Arduino Mega 256 but due to its overkill on this project i have decided to move it to a nano.

But now the issue starts iam not getting any readings on the nano.

I have added a serial start up print to see if the nano is processing.

se blow my code for the MEGA does anyone have a suggestion why this is not working on the nano?

volatile unsigned int temp, counter = 0; // This variable will increase or decrease depending on the rotation of the encoder

void setup() {
    Serial.begin(9600);
  Serial.println("Setup complete. Starting program...");

  pinMode(2, INPUT_PULLUP); // internal pullup input pin 2 (interrupt 0 on Arduino Nano)
  pinMode(3, INPUT_PULLUP); // internal pullup input pin 3 (interrupt 1 on Arduino Nano)
  pinMode(7, OUTPUT);       // D7 as output
  pinMode(8, OUTPUT);       // D8 as output
  pinMode(9, INPUT_PULLUP); // D9 as input with internal pullup

  // Setting up interrupt
  // A rising pulse from encoder activates ai0(). AttachInterrupt 0 is DigitalPin nr 2 on Arduino Nano.
  attachInterrupt(digitalPinToInterrupt(2), ai0, RISING);

  // B rising pulse from encoder activates ai1(). AttachInterrupt 1 is DigitalPin nr 3 on Arduino Nano.
  attachInterrupt(digitalPinToInterrupt(3), ai1, RISING);
}

void loop() {
  // Send the value of counter
  if (counter != temp) {
    Serial.println(counter);
    temp = counter;
  }

  // If D9 is read as HIGH, reset counter to 0
  if (digitalRead(9) == LOW) {
    counter = 0;
  }

  // If counter exceeds 14000, reset counter to 0
  if (counter > 14000) {
    counter = 0;
  }
}

void ai0() {
  // ai0 is activated if DigitalPin nr 2 is going from LOW to HIGH
  // Check pin 3 to determine the direction
  if (digitalRead(3) == LOW) {
    counter++;
    // For every 100th rising count of counter, set D7 high
    if (counter % 100 == 0) {
      digitalWrite(7, HIGH);
      Serial.print("D7 HIGH on increment: ");
      Serial.println(counter);
      delay(10);  // Brief delay to ensure the signal is detectable
      digitalWrite(7, LOW);
    }
  } else {
    counter--;
    // For every 100th falling count of counter, set D8 high
    if (counter % 100 == 0) {
      digitalWrite(8, HIGH);
      Serial.print("D8 HIGH on decrement: ");
      Serial.println(counter);
      delay(10);  // Brief delay to ensure the signal is detectable
      digitalWrite(8, LOW);
    }
  }
}

void ai1() {
  // ai1 is activated if DigitalPin nr 3 is going from LOW to HIGH
  // Check pin 2 to determine the direction
  if (digitalRead(2) == LOW) {
    counter--;
    // For every 100th falling count of counter, set D8 high
    if (counter % 100 == 0) {
      digitalWrite(8, HIGH);
      Serial.print("D8 HIGH on decrement: ");
      Serial.println(counter);
      delay(10);  // Brief delay to ensure the signal is detectable
      digitalWrite(8, LOW);
    }
  } else {
    counter++;
    // For every 100th rising count of counter, set D7 high
    if (counter % 100 == 0) {
      digitalWrite(7, HIGH);
      Serial.print("D7 HIGH on increment: ");
      Serial.println(counter);
      delay(10);  // Brief delay to ensure the signal is detectable
      digitalWrite(7, LOW);
    }
  }
}

The rotary encoder is working on 5-24V and has A & B outputs for clockwise and counterclock

Delays and printing inside interrupt service routines can cause problems.

How fast is the encoder pulsing?

it is a 600p/rotation.

Dont realy understand why the mega can handle it but the nano not :frowning:

Please do the following:

  1. Annotated Schematic:
  • Show how your circuit is wired, including all connections, power sources, ground lines, and any other relevant components.
  • Clearly label each part in the schematic, especially critical components like displays, relays, contactors, sensors, and controllers.
  1. Technical Information Links:
  • Provide links to the datasheets or technical documentation for each component. This information helps us understand the specifications, pin configurations, and electrical characteristics of the parts involved.
  • Avoid links to sales pages like Amazon unless they include complete technical details; instead, look for manufacturer or supplier datasheets.

Why This is Important:

  • Post an Accurate Diagnosis: An annotated schematic helps identify issues such as incorrect wiring, missing components, or potential sources of interference.
  • Proper Recommendations: Understanding the exact parts used allows us to suggest the right mitigation techniques, such as specific filters, grounding methods, or alternative components.
  • Time-Saving: Providing this information as links to technical information upfront saves time and helps avoid multiple back-and-forth exchanges, leading to faster and more effective troubleshooting.
1 Like

I meant speed in pulses per second. How fast is the encoder spinning during normal operation?

I do not know either. And dont have your nano and mega setups to test. Maybe there's a mistaken connection? But from what you've shown, there's lots of sloppiness in the code that can cause undesired operation. The 9600 baud rate looks very slow compared to trying to print stuff based on interrupts. Besides trying to use delay and print inside interrupts, you are also trying to use and modify counter outside of the interrupts. You should make a copy of or changes to counter within noInterrupts()/interupts() so the value doesn't get changed mid-read/write.

With those sorts of basic issues, I'm not sure how well it actually performs on your Mega, and what suggestions to the code would make it work differently/better on the Nano. Is it a plain old Nano?

Hi all

here is the datasheet -> Photoelectric Incremental Rotary Encoder.pdf - Google Drive

Schematic Nano

Schematic Mega

I think its pretty self explainatory as i didnt have the encoder as a Fritzing part

its a standard Rev 3 clone Nano nothing fancy.

The code could be cleaned up yes i agree but the Print out works fine on the mega.

For the pulses per second it is very slow.

Iam using it to build a puzzel for an Escape Room the encoder has a gear on top that is rotated by a second gear which has a keyhole insert to be turned.

Every ISR I have ever written sets 1 bool variable. Everything else is done in the main loop. Get rid of any delay statements and replace with millis loops. Do NOT power the motor/encoder from a board pin, use a separate power supply. That is your problem, the NANO can only supply a few tens of ma.

3 Likes

thanks i will give it a try BRB

Are the Nano pins working?

Make two sketches for the Nano.

  1. DIO
  • read DIO on 2 and 3
  • short ground to 2 or 3, read a 0
  • short Vcc to 2 or 3, read a 1
  1. Interrupts
  • attach an interrupt to 2 and 3
  • short ground to 2 or 3
  • read the interrupt on 2 or 3

For example (sketch 1):

//**********************************************************************
// 1. Connect a jumper wire to GND
// 2. Upload the sketch
// 3. Touch the jumper wire to any pin 2 through 12 and A0 through A5
// 4. The output will show the pin(s) you are touching
//
// This shows pin D3 sensing ground through jumper...
// 12 11 10 D9 D8 D7 D6 D5 D4 D3 D2 A0 A1 A2 A3 A4 A5
//  1  1  1  1  1  1  1  1  1  0  1  1  1  1  1  1  1
//**********************************************************************

// I/O pins for the Uno/Nano, adjust for your board
int pin[] = {13, 12, 11, 10, 9, 8,  7, 6, 5, 4, 3, 2, 14, 15, 16, 17, 18, 19};
int size = sizeof(pin) / sizeof(pin[0]);

void setup() {
  Serial.begin(115200);
  for (int i = 0; i < size; i++) {
    pinMode(pin[i], INPUT_PULLUP); // pins held HIGH through internal 10k resistor
  }
}

void loop() {
  int size = sizeof(pin) / sizeof(pin[0]);
  Serial.println(F(" 13 12 11 10 D9 D8 | D7 D6 D5 D4 D3 D2 | A0 A1 A2 A3 A4 A5"));
  for (int i = 0; i < size; i++) {
    int state = digitalRead(pin[i]);
    if (i == 6 || i == 12)
      Serial.print(" |  ");
    else
      Serial.print("  ");
    Serial.print(state);
  }
  Serial.println();
  delay(1000);  // allow jumper to be moved before scrolling off the page
}
1 Like

Thanks the pins are working

it was indeed a undervoltage issue

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.