Uno vs Nano - different behaviour

Hi All,
I'm building a circuit which involes a rotary encoder, and have been using the simple code from this site which I've copied in below. It works fine with my Uno (Elegoo Uno R3), but when I use the same code on my Nano (Elegoo again) it all goes wrong, and the serial monitor just shows a stream of '0's down the page, flicking to -1 when I turn the encoder. Now from hours of searching, this makes no sense as the Uno and Nano should be interchangeable as far as I understand. I've checked the wiring, tried changing pins and even a new Nano, but no joy. Am I missing something, because I'm just about all out of logical explanations for this? Any thoughts would be much appreciated.
Thanks,
Paul

#define CLK 3
#define DT 4
#define SW 5
int counter = 0;
int state;
int Laststate;

void setup() {
pinMode(CLK,INPUT);
pinMode(DT,INPUT);
pinMode(SW,INPUT);
Serial.begin(9600);

Laststate = digitalRead(CLK);
}

void loop() {
state = digitalRead(CLK);
if(state != Laststate)
{
if(digitalRead(DT) != state)
{
counter++;
}
else
{
counter--;
}

Serial.println(counter);
}

if(digitalRead(SW) != 1)
{
counter=0;
Serial.println(counter);
}

Laststate= state;

}

Is the code in your Original Post the exact code that works properly on your Uno? If not please post that version.

Also make a couple of simple pencil diagrams showing how everything is connected to the Uno and to the Nano and post photos of the drawings. See this Simple Image Guide

And to make it easy for people to help you please modify your post and use the code button </> so your code looks like this and is easy to copy to a text editor. See How to use the Forum

…R

A plausible explanation for a difference between the Uno and Nano could be the behaviour of the input pins when these float.
Try replacing the INPUT with INPUT_PULLUP and see if it is better. Depending on the encoder, external pulldown resistors could also be better.

Thanks for your help. Apologies, I should have provided more details. Picture of the two circuits below:

And the exact code I used is here:

#define CLK 5
#define DT 4
#define SW 6
int counter = 0;
int state;
int Laststate; 

void setup() {
  pinMode(CLK,INPUT);
  pinMode(DT,INPUT);
  pinMode(SW,INPUT);
  Serial.begin(9600);

  Laststate = digitalRead(CLK);
}

void loop() {
  state = digitalRead(CLK);
  if(state != Laststate)
  {
    if(digitalRead(DT) != state)
    {
      counter++;
      }
      else
      {
        counter--;
        }
  
         Serial.println(counter);
  }
  
  if(digitalRead(SW) != 1)
  {
    counter=0;
    Serial.println(counter);
    }
    
    Laststate= state;
   
   }

In terms of the pullup option, that was something I had tried in terms of changing the INPUTS to INPUT_PULLUP but that didn't sort it unfortunately.
Thanks,
Paul

You have something on Pin6 that is not in your drawings.

...R

Yes, that's the rotary enocder switch, but didn't wire that up to concentrate on the rotary reading first. It just resets the counter when pressed, so works fine without (i.e. with the Uno)

Ok, just looked closely - with a magnifying glass - at the rotary enocoder, and it appears to have E01 resistors built onto it, which I understand are 1 MΩ. Could too much resistance be the issue? I hadn't even contemplated this as a possibility.
Paul

I guess you are reading the resistors upsidedown. 103 would be 10k. E01 would be, as you said, 1Mohm.

Here is the Sparkfun Rotary Encoder data sheet: https://cdn.sparkfun.com/datasheets/Components/LED/EC12PLRVF-D-24K-24-24C-0206-6H(SPEC).pdf

There is enough English in it to see the 10k pullup resistors. Is your the same ?. Rotary Encoder - Illuminated (Red/Green) - COM-15140 - SparkFun Electronics

If the device has built in pull up resistors, then I can imagine only a wiring error to explain the different behaviour between a Nano and a Uno.

Thanks. Ha, there's a reaonable chance it says 103, the writing is incredibly small! I don't think mine is exactly the same model - mine is the ky-040 - but very similar. I agree, wiring would appear to be the problem, but I've checked it every which way and still the same result. Very frustrating as makes absolutely no sense!

In case anyone has this problem in the future, the problem was that I was using digital pin 13 on the Nano but not on the Uno which behaves differently from the other digital pins. From this page:

NOTE: Digital pin 13 is harder to use as a digital input than the other digital pins because it has an LED and resistor attached to it that's soldered to the board on most boards. If you enable its internal 20k pull-up resistor, it will hang at around 1.7V instead of the expected 5V because the onboard LED and series resistor pull the voltage level down, meaning it always returns LOW. If you must use pin 13 as a digital input, set its pinMode() to INPUT and use an external pull down resistor.

Hi,
Where in your circuit are you using pin13?

Tom.. :slight_smile:

Ah, I got that wrong on my diagram! My mistake. If I’d put that on there this probably would have been spotted about 12 hours ago. It used to be on pin 5, but had a bit of a soldering issue so changed to pin 13. Pin 5 RIP

Hi,
Are you soldering wires to your Arduinos while prototyping?

Tom.. :slight_smile:

pallen33:
Ah, I got that wrong on my diagram!

GIGO :slight_smile:

...R

I know, it's not great practice soldering to the board whilst prototyping, but I don't currently have enough jumper wires, so this was my only solution unfortunately

pallen33:
I know, it’s not great practice soldering to the board whilst prototyping, but I don’t currently have enough jumper wires, so this was my only solution unfortunately

Why not solder to a jumper wire rather than to the board?

…R

Yes, good idea, thanks Robin!