Problem with encoder

Hello everyone .

I just got an encoder like this one https://www.robotshop.com/letsmakerobots/files/userpics/u24/Photo_enterupter.jpg

and an encoder wheel like this one http://image.absoluteastronomy.com/images/topicimages/d/da/daisy_wheel_printer.gif

from a printer but I dont know what Im doing wrong .

 

The code I use is the following :

int senspin = 8;
int curval = LOW;
int oldval = LOW;
int newval = LOW;
int count = 0;
void setup(){
  pinMode(senspin,INPUT);
  Serial.begin(9600);
}

void loop(){
 
  curval = digitalRead(senspin);
  if(curval == HIGH){
     newval = HIGH;
     if(newval == oldval){
       Serial.println("No rotation.");
     }
     if(newval != oldval){
       count++;
     }
  }
  else if(curval == LOW){
     newval = LOW;
     if(newval == oldval){
       Serial.println("No rotation.");
     }
     if(newval != oldval){
       count++;
     }
  }
  oldval = newval;
 
  Serial.println(curval);
  Serial.println(count);
}

 

It may be a hardware problem though(I mean the way I put the wheel between the emmiter and the receiver ).

Some photos:

l.jpg

ll.jpg

lll.jpg

 

Hi Flokos

A couple things - 
First tell us what is going on?  I take it its constantly LOW and not changing?

Second … in C … you have to ALWAYS be careful regarding assigning vs comparing

For example - I’m guessing you wanted this line

if(newval = oldval)

really to be this

if(newval == oldval)

because assigning is ALWAYS true :stuck_out_tongue:

 

 

actually…

if (newval = oldval) {

puts(“true”);

} else {

puts(“false”);

}

WILL print false if oldval is 0


@grog , yeap it is constantly low .

Can you verify your circuit?

Can you verify your circuit? How about a schematic?

It’s always best to verify the physical connection before you dive into code.


There is no schematic I just plugged the sensor to my arduino

3.3v to pwr of the sensor , gnd to gnd and signal to pin 8 of the arduino.

I thought there is no need for an extra circuit because the sensor was soldered on a pcb and I thought that

 there is no need of something else because the company hasn’'t added anything else  .

Any suggestion ?

Start at the beginning

Can you draw a diagram of how it is hooked up?

CtC’s post on this encoder

CtC’s post on this encoder states that, “the LED portion of this sensor will require a 330R resistor and the input pin should be pulled down with a 10k resistor to gnd (if it isn’t allready pulled down on the board itself)”.

Later in his post, he mentions that there is a spot on the attached circuit board to install the 330 ohm resistor.

If you read the comments to the post, you’ll see a discussion about using pull-up Vs pull-down resistors. The Arduino has internal pull-up resistors, which you need to enable.

wait a sec…

Your link above shows a stand-alone part, not a breakout board. Also, if you got the parts from Sparkfun, the part you are showing in your link is the old, discontinued part. I would assume you are using the newer version which is a 5-pin, digital unit. The do look alike, but one is analog and one is digital --kinda a big difference.

Also, what does the data sheet say? Is this a 3.3v part or is it 5v?

Before we can do ANYTHING, we need good links to the actual parts you are using. Maybe a picture.

oldval = 1

if (newval = oldval) {
             print (“your right”);
}

The other part…

The “encoder wheel” --With the confusion about the sensors, we should be sure you are using the correct wheel as well. You said that the one you have is “like this one”…

Is it a wheel designed for slot-encoders or reflective ones? Can we get a link to the actual part you are using?

 


I will upload a photo of the wheel too ,just wait a couple of minutes ok ?

**Got it **

I just saw your pictures. --The encoder you are showing will not work the way it is now.

A board (with a part attached to it) that you remove from something does not a break-out board make.

When you buy something from say, sparkfun, the breakout board you can buy for that part is designed to be a stand-alone unit. It will have the needed pull-up resistors, for example. A manufacturer doesn’t always put parts together on the same board. Yes, the encoder itself is attached to that board, but all the resistors needed could be on another board in a different part of the product.

First off, you need to confirm your pin-out. Figure out what side is what for the LED and which is the collector/emmiter for the sensor  side. Next, confirm the LED still works. Then, with a multi-meter and a pull-up resistor, see if it actually toggles from low to high as you pass objects through it. Once you know it is working, then write a simple test program --for get about counting wheel revs here, we just need to know if the uC can see the high/low.

Once all that is done, then start working on your code.