I'm doing a very simple closed loop control with, an incremental quadrature encoder(2 channels, 8 bits), a L298N motor driver module and an arduino Mega2560 board. The L298N module is powered with an external 12V DC voltage for a dc motor. The external DC power, the encoder and the mega board share the same GND.
In the code below I just receive the position from the encoder, and send pwm signal to the L298N module. It works well for low duty cycle, but when I increase the duty cycle (for example, the equivalent voltage is 12V*105/255=4.94V), the output information on the serial monitor just stops, nothing shown on the monitor, but the motor can run at that time. When I unplug the encoder's GND from the commen GND (i.e., the encoder doesn't work now), even with high duty cycle, the serial monitor can output the right information, the motor also works well.
Is there some influence from the encoder? It seems like the interrupt conflicts with the serial communication. I'm a beginner of arduino, hope someone can figure it out, thanks!
Yes, the encoder interrupts can block the interrupts required for serial printing.
How fast is your motor turning?
How many encoder counts per revolution? Can you link the specs for the encoder?
Increasing the serial baud rate to 115200 will help.
Thanks a lot for your help!
The encoder is AMT102-V ()[https://www.mouser.com/ProductDetail/CUI-Devices/AMT102-V?qs=WyjlAZoYn533PQfgnorrMg%3D%3D] , with default ppr is 2048, I only use the A and B phase of the encoder here. The motor speed for 100% duty cycle is 1750RPM.
Increasing the baud rate do help increase the serial response, it does print information correct now. However, when I used high duty cycles(say150/255), the output time sequence didn't update (the time stopped at the initial time, while the loop numbers did update, seen from the serial monitor)
Resolution selected via adjustable DIP switch, pre-set to 2048 PPR. All resolutions are listed as pre-quadrature, meaning the final number of counts is PPR x 4
At 1200 rpm you have 20x2048x4 = 163,840 quadrature interrupts per second. Approximately 6 microseconds between interrupts. Each interrupt may take 4 microseconds to enter execute and exit. There is not much processor time left over for other things like printing or incrementing the millis() counter.
I'm certain you don't need the resolution of 4x2048 encoder counts per revolution.
Set the dip switches to a more appropriate resolution for how you want to use the encoder.
There is also an index pulse (one per revolution) which you may also find adequate resolution for your use.
Thanks so much! I really appreciate your help!
I'm also doing the same thing, I decrease the resolution to 500ppr, now everything works fine! Thank you so much! you have a good night!