Go Down

Topic: Calculating Pulse from encoder to Ard. Duemilanove (Read 2188 times) previous topic - next topic


Jan 13, 2011, 06:30 pm Last Edit: Jan 13, 2011, 06:39 pm by zolofy Reason: 1
[size=12]May I know how to do simple program for Arduino Duemilanove to count pulse from encoder and display the result in Serial Monitor. The encoder that we are using has 12 pulse for one rotation.

I already tried the example program that has been given by Arduino website http://arduino.cc/en/Tutorial/ButtonStateChange is this suitable program for this case?[/size]


Is this encoder a hardware closure encoder? If it is a digital pulse then not really.
No trees were harmed by the transmission of this message. However a few million electrons were temporarily inconvenienced.


Depends on what you need to display, do you want to display the amount of pulses per second for example? Or the speed? Or simply output a string on serial monitor on every pulse? I can think of a few approaches for this depending on your end result. What is it you need to know?
No trees were harmed by the transmission of this message. However a few million electrons were temporarily inconvenienced.


I want to know the pulse that has been generated by the encoder thank you


I want to know the pulse that has been generated by the encoder

You want to know that a pulse was generated, or when the pulse was generated, or how long the pulse was? Or, all of the above?

What do you want to do when a pulse arrives?

One does not calculate a pulse. The encoder generates a pulse. It is necessary to detect that a pulse has occurred, and to do something with that simple fact.

What you want to do with that fact has a lot to do with how you detect that pulse, and with what else your Arduino is doing.

You need to provide a lot more details.


Jan 14, 2011, 09:10 pm Last Edit: Jan 14, 2011, 09:13 pm by zolofy Reason: 1
OK what I need here is a simple program for my microcontroller that can read the pulse generated by the encoder. The encoder will generate 12 pulse per rotation, and this is the formula that I need to include in this program 0.43x inches (x=pulse), because the main objective of the robot is to measure the distance.


Can he not just use PulseIn()? Seriously don't know, just trying to be helpful.


So, now we know that you want to simply detect that a change has occurred. We don't know what else the robot is doing, or how critical it is to not miss a pulse.

If missing pulses is terrible, and the interrupt pins are available, the using an interrupt handler that is triggered when the pulse occurs is possible. If the interrupt pins are not available, the Arduino is not terribly busy, and missing a pulse isn't critical, then polling the encoder pin is generally good enough.

If the pulse width is high relative to the polling frequency, then interrupts are not required.

Let's assume that they are not. In that case, the encoder is just a switch.

int state = digitalRead(encoderPin);
if(state == HIGH)
  // encoder sent a pulse. Do something...


Jan 14, 2011, 09:30 pm Last Edit: Jan 14, 2011, 09:31 pm by zolofy Reason: 1
is this program suitable for my case? actually this is my encoder http://www.pololu.com/catalog/product/1217

Code: [Select]
/* Read Quadrature Encoder
 * Connect Encoder to Pins encoder0PinA, encoder0PinB, and +5V.
 * Sketch by max wolf / www.meso.net
 * v. 0.1 - very basic functions - mw 20061220

int val;
int encoder0PinA = 3;
int encoder0PinB = 4;
int encoder0Pos = 0;
int encoder0PinALast = LOW;
int n = LOW;

void setup() {
  pinMode (encoder0PinA,INPUT);
  pinMode (encoder0PinB,INPUT);
  Serial.begin (9600);

void loop() {
  n = digitalRead(encoder0PinA);
  if ((encoder0PinALast == LOW) && (n == HIGH)) {
    if (digitalRead(encoder0PinB) == LOW) {
    } else {
    Serial.print (encoder0Pos);
    Serial.print ("/");
  encoder0PinALast = n;


is this program suitable for my case?

Possibly. It depends on the RPM of the axle.

In the real application, you won't be outputting to the serial port every time the encoder position changes, so then it will work for fairly high RPM rates.

Increasing the baud rate would cause shorter outage windows while serial data is being transmitted.


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!

via Egeo 16
Torino, 10131