Magnetic Encoder Reading

Good Day,

Recently I am working with Arduino Mega 2560 to read encoder on DC motor. This is the DC motor I used (http://www.shayye.com.tw/product-inner.aspx?f=s&i=117) and encoder already included on it (http://www.shayye.com.tw/product-inner.aspx?f=s&i=2198). I am using Arduino Mega 2560 and Arduino IDE 1.8.1. For the driver I am using LN298 DC motor driver.

This is the code that I tried:

int encoderValue=0;

void setup()
{
Serial.begin(9600);
pinMode(2,INPUT);
attachInterrupt(2,count,FALLING);
pinMode(8,OUTPUT);
pinMode(9,OUTPUT);
pinMode(10,OUTPUT);
} 

void loop()
{
for (EncoderValue;EncoderValue<100;)
{
digitalWrite(8,HIGH);
digitalWrite(9,HIGH);
digitalWrite(10,LOW);
Serial.print ("encoder Value:"); Serial.println(EncoderValue);
}
}

void count()
{
  if(digitalRead(9)==HIGH)
  {
    encoderValue++;
  }
  if(digitalRead(9)==LOW)
  {
    encoderValue--;
  }
}

The problem is my encoder value didn’t show any change and it made my motor not stopping at all. What did I do wrong here? I hope someone can explain to me.

volatile int encoderValue

(.. strange code)

Hi,
Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?
Have you got pullup resistors for the output of the encoder?
From the PDF file at your link.
encoder_connections.jpg

Tom... :slight_smile:

encoder_connections.jpg