Hello everyone, I hope I am in the right section. I am working on a balance bot. I am using motors with optical encoders to calculate the speed of the bot.
Motor Specs:
12V, 300RPM, 270 pulses per channel.
No datasheet is available for the encoder or motor from the seller.
I am using the following code to count the number of pulses:
The problem is that I am not getting the right number of pulses from the encoder. Sometimes the number of pulses increases even when the motor is at rest. Sometimes it counts more than 1000 pulses for one rotation which doesn't make sense. I have checked my connection like a 1000 times and I am sure there is nothing wrong with it. All grounds are common.
So do I need any kind of debouncing for an optical encoder? Using pull-up resistor on the A and B pin have no effect. I have tried using another motor, another Arduino board and another PC.
Based on the symptoms (no schematic!) my money is on floating inputs.
If so, solution: set pinMode to INPUT_PULLUP or add external pull-up resistors; if you have the common of encoder wired to Vcc use pull-down resistors instead.
This is my first answer on the forum. My guess would be that because loop only does println(), it is constantly interrupted and cannot finish. I would try a short delay after println() to give println() time to finish. Besides, only doing println() "clogs" the monitor.
Try speeding up the serial baud rate to no less than 115200. That will empty the serial output buffer quicker.
Serial.println() puts chars into the serial output buffer. Serial interrupt occurs when the UART finishes sending a char so the ISR can get the next one going.
If the serial output buffer fills then loop() will wait blocked from more printing until the last to-print char is in the buffer which at 9600 baud takes more than a millisecond per char sent. Serial sends bits; 1 start bit + 8 data bits + 1 stop bit, 9600 baud is 960 chars/second. --- but that won't keep the pin 2 RISING ISR from counting. The printed count may skip values but the count should be right .
When you wired the encoder, did you connect encoder ground to Arduino ground?
Thank you everyone for taking some time to reply. I got it working now. The problem was all the grounds were not connected to each other indeed. They were connected visually but when I checked with multimeter for continuity, they were not. I think I didn't make the solder paths correctly on my PDB.
Solder path on the PDB looks OK visually but all the grounds are not connected to each other. They were working fine earlier. What can be the reason?
Rahulk13:
Thank you everyone for taking some time to reply. I got it working now. The problem was all the grounds were not connected to each other indeed. They were connected visually but when I checked with multimeter for continuity, they were not. I think I didn't make the solder paths correctly on my PDB.
Solder path on the PDB looks OK visually but all the grounds are not connected to each other. They were working fine earlier. What can be the reason?
Is your PCB home made or from a PCB company.
Look for thin cracks in the tracks and near where solder pads are.
I hope you made decent width tracks.