Hi guys,
I have a problem. I am trying to use my rotary encoders and interrupts. Whenever I try to use interrupts to count the number of rotation hits, the value is totally wrong. If I rotate the wheel by hand the value is correct but when I use the analogWrite function for PWM, the value is way higher then it should be. I've even tried using the digitalWrite function for 100% duty cycle PWM but I get the same result, an incorrect count value for the encoders. I am using DFRobot encoders:
http://www.dfrobot.com/index.php?route=product/product&product_id=98 The resolution is: 20 PPR.
here is my code:
#define encoder1PinB 3
#define rightMotorIn 5
#define rightMotorOut 6
volatile unsigned int encoder1Pos = 0;
int dutyCycleRight = 200;
void setup() {
pinMode(encoder1PinB, INPUT);
digitalWrite(rightMotorOut, LOW);
attachInterrupt(1, doEncoder1, RISING);
Serial.begin (9600);
Serial.println("start"); // a personal quirk
}
void loop(){
analogWrite(rightMotorIn, dutyCycleRight);
}
void doEncoder1() {
encoder1Pos++;
Serial.print(encoder1Pos);
Serial.print('\n');
}
Any help would be awesome!
Thanks!
Hi,
I have used the sample code to test my encoder with my DFRobot Romeo and it works fine.
Here's a list of what I'm using.
1. Arduino IDE 1.01
2. DFRobot Romeo controller
3. Same encoders from DFRobot
Generally speaking, if you could get the right feedback from the encoders, when turning the wheels by hand. But it's not working properly when running the motors. It means that your power supply is not strong enough. So the working voltage of your system is not stable @5v(maybe lower than 4v). Then the sensor can't work properly. Then you get the wrong interrupts.
1.So I suggest you test your 4wd/2wd with the external power supply such as 7.4v battery or 7v power adapter to supply power for your Arduino board. The max current of usb is just 800mA. So the single power supply from USB is not suitable for testing the sensors when running the motors.
2.Test the working voltage of the encoders to avoid the power supply problems.


This's my connection pictures.