Go Down

Topic: Encoder into Arduino Uno (Read 122 times) previous topic - next topic

roger290

Hello:
I am new to this forum. I am working with some high school students building a car to run in a competition. The car needs to go a distance and stop. We are using an Arduino Uno to control the car. We have a Velleman VMA03 shield controlling the motor and using the encoder below attached to the rear wheel for the distance feedback.


https://www.amazon.com/Signswise-Incremental-Encoder-Dc5-24v-Voltage/dp/B00UTIFCVA/ref=sr_1_1?ie=UTF8&qid=1480958546&sr=8-1&keywords=encoder+600

The encoder is hooked into pins 4 and 5 of the Arduino. We are having problems getting the proper counts out of the encoder. When we turn the wheel by hand the counts are close to 600 counts per revolution. If we tell the program to run 1200 counts the wheel runs about 15 seconds before it stops. The motor is running slow so we are not over speeding the encoder.

My background is Mechanical Engineering. I am good at the mechanical stuff and I am terrible at the electrical stuff. Any help would be great.

Thanks
Roger

cattledog

To get assistance, you will have to provide both your code, and a diagram of how you have the encoder connected.

Post your code in between the code tags  found at the </> in the tool bar. It should appear in a white box like this
Code: [Select]
your code will be here

If you can reduce your program to a simplified version which demonstrates the problem it would be helpful.

MarkT

Pins 4 and 5 don't support direct interrupts - you can use pin change interrupts however.

You never want to read a high resolution encoder except by interrupts, they are far too fast to handle
in software (10,000 transitions a second is not difficult to achieve).
[ I will NOT respond to personal messages, I WILL delete them, use the forum please ]

roger290

Thank you for the information. I do not have the code because it is on the students computer. I will try to get it from him. The encoder has 4 wires the red wire is hooked to 5 volts the black wire id ground the white and green wires are the signal wires and they are hooked to pins 4 and 5. It sounds correct that pins 4 and 5 will not support high speed inputs because the problem gets worse the faster the wheel turns.

The Vellman board uses pin 3, we will need to contact Vellman to see if we can free up pin 3. Pin 2 is open so if we can not free up pin 3 we can hook up one leg of the encoder to pin 2 and get 300 pulses per revolution.

When we were working on this on Saturday the one student talked about using interupts. He made some changes to the code and we took the Vellman shield off the Arduino board and hoked the encoder directly into the Arduino pins 2 and 3. We got no inputs. Maybe his code was bad. Does anyone have some code showing using interupts and some information on using interupts that I can show to the student? He is a very good programmer and maybe some sample code will help him along.

While I have your ear I have another question. We currently have a simple DC motor to driver the car. We have an Oriental Motor Stepper motor ordered and we are going to get it tomorrow. The part number is PKP244D08A-L. It is a bipolar stepper motor  with 1.8 degree steps. To get the car speed we want and with the belt ratios we have the motor will need to turn 1800 RPM. Will the Arduino have a problem pulsing the motor at that speed? Will I need some special code to get it to go that fast? I have read some information on the web with people having problems getting stepper motors to go fast.

I appreciate and information. The students are looking to me for answers and I do not have any.

Thanks Again

Roger


jremington

Stepping motors are generally a poor choice for robot wheel drives. They are slow, low torque, inefficient and use too much processor time.

cattledog

#5
Dec 06, 2016, 05:20 am Last Edit: Dec 06, 2016, 05:22 am by cattledog
One thing to check is that both outputs from the encoder to the arduino have pull up resistors. 4.7K is a good value.

As suggested earlier, you can use pin change interrupts on D4 and D5 and get full resolution.

Quote
Pin 2 is open so if we can not free up pin 3 we can hook up one leg of the encoder to pin 2 and get 300 pulses per revolution.
You need to decide if 300 counts/rev or 600 counts/rev. It sounds like there may be alot going on in your program, and reducing the interrupts by half, may not be a bad idea in terms of load on the processor.

Here's some code for an interrupt on pin2 and the other input on pin 4. You may need to modify what is forward and reverse. The code uses digitalRead(), and can be made faster with direct port readings.

Code: [Select]
#define outputA 2
#define outputB 4
volatile int counter = 0;
int copy_counter;
int last_copy_counter;

void setup() {
  pinMode (outputA, INPUT);
  pinMode (outputB, INPUT);

  Serial.begin (115200);
  Serial.println("Starting Encoder Counts");

  attachInterrupt(digitalPinToInterrupt(2), readEncoder, CHANGE);
}
void loop() {
  noInterrupts();
  copy_counter = counter;
  interrupts();

  if (copy_counter != last_copy_counter)
  {
    Serial.print("Counts: ");
    Serial.println(copy_counter);
  }
  last_copy_counter = copy_counter;
}

void readEncoder()
{
  if (digitalRead(outputA) != digitalRead(outputB))
  {
    counter ++;
  }
  else
  {
    counter --;
  }
}


MarkT

A little searching suggests this library:  http://playground.arduino.cc/Main/RotaryEncoders#Example14

Which claims to use pin change interrupts - haven't used it myself, but it should be a starting point.

But we haven't seen the existing code yet - perhaps it is using this library?
[ I will NOT respond to personal messages, I WILL delete them, use the forum please ]

roger290

Mark:
Thanks for the information. I passed it along to the student programming the Arduino. He will look at it and see if he understands it.

Thanks Again
Roger

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy