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;
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
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.
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
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
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.
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
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?