I'm having an issue. I'm trying to use the encoder but I'm wondering if it broke.
When I'm letting the light hitting the encoder, I get a reading of 4,85 V (i'm using the USB as power supply) so it's normal, but when I'm blocking the light, I get a reading of 3,something V. So I can't get my attachInterrupt() fonction to work.
I also tested my code and it's seems to work, when I unplug the 5V pin from the encoder, I got a reading in my program. I'm gonna post it anyway if someone want to check it.
double encoderPin1 = 2, encoderPin2=3,
//Variables
double rayon=1.75;
volatile int toursM1, toursM2;
int tours;
// Fonction pour compter le nombre de tours
void count1() {
toursM1++;
Serial.print("Tours:");
Serial.println(toursM1);
}
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(13,OUTPUT);
Serial.print("Initialisation du test:");
pinMode(encoderPin1, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(encoderPin1), count1, LOW);
}
void loop() {
// put your main code here, to run repeatedly:
digitalWrite(13,HIGH);
delay(3000);
digitalWrite(13,LOW);
delay(2000);
}
// Fonction pour compter le nombre de tours
void count1() {
toursM1++;
Serial.print("Tours:");
Serial.println(toursM1);
}
You can not have Serial.print within an ISR. Serial.print requires interrupts, and interrupts are disabled within the ISR Please review Nick Gammon's tutorial on interrupts. Gammon Forum : Electronics : Microprocessors : Interrupts
You can not have Serial.print within an ISR. Serial.print requires interrupts, and interrupts are disabled within the ISR Please review Nick Gammon's tutorial on interrupts. http://www.gammon.com.au/interrupts
Yeah I read this post after I published in the forum. I already took out the Serial.print from the ISR.
I did further test, the issue I have is that I can't get a voltage bellow 3.8V so it's always considered a HIGH by the pin. I don't know what to do anymore, I'm contacting the online shop.
cattledog:
Can you provide documentation on the sensor you are using? Can you attach a sketch which shows how you have connected the sensor to the Arduino?
The hard part is that sadly I don't have any documentation and I can't find any on the web
I connect it throw the 5V pin, ground PIN and 2 pin.
However, I think the only issue I have is that the sensor is MEGA succeptible to light. When I close all the light, it worked. However when I hide the light with my hand, it still too much light.
Many encoders use LEDs and phototransistor detectors and can be light sensitive. You will have to shield it somehow, or buy a better one. Magnetic encoders might be better in your application.
jremington:
Many encoders use LEDs and phototransistor detectors and can be light sensitive. You will have to shield it somehow, or buy a better one. Magnetic encoders might be better in your application.
I'm gonna hide it in the chassis of the robot, if it doesn't work, I'll try those magnetic encoders! Thank you for the suggestion.
It looks like you're able to measure the signal voltage, and you get 4.85 with light, 3.xx when blocked with hand. Note that your hand isn't a good way to block the light as the phototransistor in the encoder will be sensitive to any reflectance and be sensitive to colour. Test with a piece of paper or something that's flat black I'll bet now you get a wider range of voltage.
To boost the range further, you do need a pulldown resistor as suggested by outsider.
Try some tests and record the readings below:
[color=teal][b]Pulldown Ω Light Blocked Obstructed by[/b][/color]
∞ 4.85V 3.??V Hand
∞ 4.85V ____V Flat Black Paper
33K ____V ____V Flat Black Paper
10K ____V ____V Flat Black Paper
3.3K ____V ____V Flat Black Paper
1K ____V ____V Flat Black Paper
The Light value should remain above 3.3V to reliably be detected as HIGH.
The Blocked value should go below 1.2V to reliably be detected as LOW.
dlloyd:
It looks like you're able to measure the signal voltage, and you get 4.85 with light, 3.xx when blocked with hand. Note that your hand isn't a good way to block the light as the phototransistor in the encoder will be sensitive to any reflectance and be sensitive to colour. Test with a piece of paper or something that's flat black I'll bet now you get a wider range of voltage.
To boost the range further, you do need a pulldown resistor as suggested by outsider.
∞ 4.85V 3.??V Hand
∞ 4.85V ____V Flat Black Paper
33K ____V ____V Flat Black Paper
10K ____V ____V Flat Black Paper
3.3K ____V ____V Flat Black Paper
1K ____V ____V Flat Black Paper
The Light value should remain above 3.3V to reliably be detected as HIGH.
The Blocked value should go below 1.2V to reliably be detected as LOW.
Which one works best???
It's a good idea! However maybe I wasnt clear, I got around 3,something with the white piece of plastic with the holes in it. When I tried with a ruler or hand, it worked like a charm.
Does panting the white piece of plastic in black would help?
I'll try to find those pulldown resistor in a shop. I don't own any and I don't really know what it is ^^.
dlloyd:
It should help ... especially if the plastic is already translucent white. Use flat (dull) black if possible.
They're called Carbon Through Hole Resistors 1/4W power rating. Their values are:
I'll see if I can make it work without the resistor. In fact I know what it is now that I saw the picture. I didn't know what it was cause of the K, I'm used to the symbole ohm (that can't be use obviously here).
I'll try to block the light a maximum by modifying the chassis of the robot tomorrow. If I still can't get it to work, I'll look for a friend in electrical engineering to get some resistors for the tests. Someone also told me about Smitth triggers. It seems to be a great solution too.
Also, if the issue comes from the little piece of plastic. I can print a new one at my university. I have access to some 3D printers and I know how to use them.
Thank you all for your help, it's really appreciated!
I now have 5 possible solutions to work on this week and it helps me a lot.
I hope everything we'll be working by the end of the week so I'll be able to start the robotic arm next week!
I'm sorry it feels like it a double post. It wasn't by any means. I didn't wanted to start a big topic of why my encoder wasn't working right now, before I had more info with some tests and that I finished my exam tomorrow.
Thank you for your patience, I'm really stressed out because it's a big project (at lest for me) that I have to hand off in one month and a half. I really want to do my project well and it seems that I have some issues to see the big pictures and I focus on the small things I don't understand. English isn't my native language and sometimes and I don't really get all your answers correctly and I'm not confortable asking for explanations.
I don't know if I should close the topics I made by posting updates because I don't wanna make a up in the forum feed, taking the place of people that actualy need help. I didn't wanted my old post to come back, it was closed for me since I was able to use the encoder by using a ruler to hide the light. In my mind, I moved to the next step, including the encoder to the robot. I didn't updated the post and the post came back.