Advertisement
microrobotics

Pololu ENC Kit for Micro Metal Motors

Apr 22nd, 2023
1,157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. The Pololu Magnetic Encoder Kit for Micro Metal Gearmotors is a compact way to add quadrature encoders to your micro metal gearmotors. This example demonstrates how to use an Arduino to read the encoder counts from a pair of encoders attached to micro metal gearmotors.
  3.  
  4. First, assemble and connect the Pololu Magnetic Encoder Kit to your micro metal gearmotors and your Arduino as follows:
  5.  
  6. Assemble the encoder kit onto the micro metal gearmotors as per the instructions in the encoder kit's user guide.
  7. Connect the encoder A channel output (OUTA) of the first motor to Arduino pin 2 (interrupt 0).
  8. Connect the encoder B channel output (OUTB) of the first motor to Arduino pin 4.
  9. Connect the encoder A channel output (OUTA) of the second motor to Arduino pin 3 (interrupt 1).
  10. Connect the encoder B channel output (OUTB) of the second motor to Arduino pin 5.
  11. Connect the ground (GND) of both encoders to the Arduino ground.
  12. Connect the voltage (VCC) of both encoders to the Arduino 5V supply.
  13. Next, upload the following code to your Arduino:
  14.  
  15. This code initializes the encoder pins as inputs with internal pull-ups and attaches interrupt handlers to the A channel pins. The interrupt handlers updateEncoder1 and updateEncoder2 update the encoder counts based on the changes in the A channel and the current state of the B channel. In the loop(), the encoder counts are printed to the serial monitor every second.
  16.  
  17. After uploading the code to your Arduino and opening the serial monitor, you should see the encoder counts for both motors being printed. The counts will update as the motors rotate.
  18.  
  19. You can modify the code to control the motors and track their positions, speeds, or distances based on the encoder counts according to your project requirements.
  20. */
  21.  
  22. // Encoder pins
  23. const int encoderA1 = 2; // First motor, channel A
  24. const int encoderB1 = 4; // First motor, channel B
  25. const int encoderA2 = 3; // Second motor, channel A
  26. const int encoderB2 = 5; // Second motor, channel B
  27.  
  28. // Encoder counts
  29. volatile long encoderCount1 = 0;
  30. volatile long encoderCount2 = 0;
  31.  
  32. void setup() {
  33.   Serial.begin(9600);
  34.  
  35.   // Set encoder pins as inputs with internal pull-ups
  36.   pinMode(encoderA1, INPUT_PULLUP);
  37.   pinMode(encoderB1, INPUT_PULLUP);
  38.   pinMode(encoderA2, INPUT_PULLUP);
  39.   pinMode(encoderB2, INPUT_PULLUP);
  40.  
  41.   // Attach interrupt handlers
  42.   attachInterrupt(digitalPinToInterrupt(encoderA1), updateEncoder1, CHANGE);
  43.   attachInterrupt(digitalPinToInterrupt(encoderA2), updateEncoder2, CHANGE);
  44. }
  45.  
  46. void loop() {
  47.   // Print encoder counts every second
  48.   Serial.print("Encoder 1: ");
  49.   Serial.print(encoderCount1);
  50.   Serial.print("  Encoder 2: ");
  51.   Serial.println(encoderCount2);
  52.   delay(1000);
  53. }
  54.  
  55. void updateEncoder1() {
  56.   if (digitalRead(encoderA1) == digitalRead(encoderB1)) {
  57.     encoderCount1++;
  58.   } else {
  59.     encoderCount1--;
  60.   }
  61. }
  62.  
  63. void updateEncoder2() {
  64.   if (digitalRead(encoderA2) == digitalRead(encoderB2)) {
  65.     encoderCount2++;
  66.   } else {
  67.     encoderCount2--;
  68.   }
  69. }
  70.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement