Rotary encoder (PRODUCT ID: 377) and Uno

Breakout boards, sensors, other Adafruit kits, etc.

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
jeff009
 
Posts: 48
Joined: Sun Jun 10, 2012 11:54 am

Rotary encoder (PRODUCT ID: 377) and Uno

Post by jeff009 »

First, a comment/suggestion: It's great that there's a tutorial on the rotary encoder with the Trinket (the "Trinket USB Volume Knob" tutorial at https://learn.adafruit.com/trinket-usb- ... b?view=all), but my project uses a Uno. Searching the forum, I see that others have ran into the same issue, and it would be helpful if there was a tutorial on using the rotary encoder with a Uno.

Now onto my problem...
I'm using the rotary encoder with a Uno. I originally tried to use the code posted at https://learn.adafruit.com/trinket-usb- ... b?view=all, and port it over to the Uno, but that wasn't working out so I scrapped that idea and started from scratch and switched over to using the Encoder library (found at http://www.pjrc.com/teensy/td_libs_Encoder.html).

I have the encoder portion working, but I'm not able to get the push button to work. Here's how my sketch is wired:
encoder_wiring.png
encoder_wiring.png (68.32 KiB) Viewed 4458 times
And here is my code:

Code: Select all

#include <Encoder.h>

const int ButtonPin = 1;
const int Encoder_A = 2;
const int Encoder_B = 3;

int buttonState = 0;		// variable for reading the pushbutton status


// Use pins 2 & 3 for Uno
// For other boards, see http://www.pjrc.com/teensy/td_libs_Encoder.html
Encoder myEnc(Encoder_A, Encoder_B);


void setup()
{
	Serial.begin(115200);
	Serial.println("Encoder Test:");
	
	pinMode(ButtonPin, INPUT);
}

long oldPosition  = -999;

void loop()
{
	// Handle the encoder
	long newPosition = myEnc.read();
	if (newPosition != oldPosition)
	{
		oldPosition = newPosition;
		//Serial.println(newPosition);
	}
	

	// Handle the push button
	// read the state of the pushbutton value:
	buttonState = digitalRead(ButtonPin);

	// check if the pushbutton is pressed.
	// if it is, the buttonState is HIGH:
	if(buttonState == HIGH)
	{
		// turn LED on:
		//digitalWrite(ledPin, HIGH);
		Serial.println("Button is pressed");
	}
	else
	{
		// turn LED off:
		//digitalWrite(ledPin, LOW);
		Serial.println("Button is off");
	}
}
I tried to wire my sketch as close as I could to that in the Trinket USB Volume Knob tutorial.
The Encoder library requires the pins to be 2 & 3, so I moved them.
The volume knob tutorial mentions that pin 1 on the Trinket shares the LED and it acts as a pull down resistor, so I added a resistor between the push button data out pin on the encoder, and pin 1 on my Uno.

When I run my sketch, both the button on and button off states are constantly being fired. I'm stuck and could use some help.

Thanks,

Jeff

User avatar
Franklin97355
 
Posts: 23910
Joined: Mon Apr 21, 2008 2:33 pm

Re: Rotary encoder (PRODUCT ID: 377) and Uno

Post by Franklin97355 »

There are several encoder tutorials on the web here, and here also you are using pin 1 as your button but pin 0 and pin 1 are the serial port and should not be used for other things unless you are not using serial.

User avatar
jeff009
 
Posts: 48
Joined: Sun Jun 10, 2012 11:54 am

Re: Rotary encoder (PRODUCT ID: 377) and Uno

Post by jeff009 »

Re. the pin for the button:
Thanks, I just changed it from pin 1 to pin 8

Re. the tutorials you referenced:
The links mention encoders, but (for the most part) not the push button. The encoder part I have working. It's the push button which I'm having difficulties trying to get to work. The first link you referenced has some examples on it - for example the "Interrupt Example (the Encoder interrupts the processor)" example has a push button, but I haven't been able to get the push button portion of the code to work.

Jeff

Locked
Please be positive and constructive with your questions and comments.

Return to “Other Products from Adafruit”