Advertisement
microrobotics

Pololu N20 Magnetic Encoder for Micro Metal Gearmotors

Apr 5th, 2023
1,568
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. To use the Pololu N20 Magnetic Encoder for Micro Metal Gearmotors, you'll need an Arduino to read the encoder's outputs and a motor driver to control the gearmotor. In this example, I'll use an MX1919-DIP motor driver mentioned earlier.
  3.  
  4. Here's an example code to control the speed and direction of the motor using the MX1919-DIP driver and read the encoder counts from the Pololu N20 Magnetic Encoder:
  5.  
  6. This code rotates the motor clockwise at full speed for 2 seconds, reads and prints the encoder counts, stops for 1 second, rotates the motor counterclockwise at half speed for 2 seconds, reads and prints the encoder counts again, and then stops for 1 second. The cycle will then repeat.
  7.  
  8. Connect the MX1919-DIP's PWM and DIR inputs to the Arduino's digital pins 3 and 2, respectively, and the encoder's outputs (A and B) to digital pins 4 and 5. Make sure to provide an appropriate power supply to the driver's motor voltage and ground pins, and connect the motor to the driver's output terminals.
  9.  
  10. Remember to install the Encoder library (https://github.com/PaulStoffregen/Encoder) to your Arduino IDE before uploading the code.
  11. */
  12.  
  13. #include <Encoder.h>
  14.  
  15. // Motor control pins
  16. const int pwmPin = 3;
  17. const int dirPin = 2;
  18.  
  19. // Encoder pins
  20. const int encoderPinA = 4;
  21. const int encoderPinB = 5;
  22.  
  23. // Create an Encoder object
  24. Encoder myEncoder(encoderPinA, encoderPinB);
  25.  
  26. void setup() {
  27.   // Motor control pin setup
  28.   pinMode(pwmPin, OUTPUT);
  29.   pinMode(dirPin, OUTPUT);
  30.  
  31.   // Encoder pin setup
  32.   pinMode(encoderPinA, INPUT_PULLUP);
  33.   pinMode(encoderPinB, INPUT_PULLUP);
  34.  
  35.   // Start serial communication
  36.   Serial.begin(9600);
  37. }
  38.  
  39. void loop() {
  40.   // Rotate the motor clockwise at full speed
  41.   digitalWrite(dirPin, HIGH);
  42.   analogWrite(pwmPin, 255);
  43.   delay(2000);
  44.  
  45.   // Stop the motor and print encoder counts
  46.   analogWrite(pwmPin, 0);
  47.   long encoderCounts = myEncoder.read();
  48.   Serial.print("Encoder counts: ");
  49.   Serial.println(encoderCounts);
  50.   myEncoder.write(0); // Reset encoder count
  51.   delay(1000);
  52.  
  53.   // Rotate the motor counterclockwise at half speed
  54.   digitalWrite(dirPin, LOW);
  55.   analogWrite(pwmPin, 127);
  56.   delay(2000);
  57.  
  58.   // Stop the motor and print encoder counts
  59.   analogWrite(pwmPin, 0);
  60.   encoderCounts = myEncoder.read();
  61.   Serial.print("Encoder counts: ");
  62.   Serial.println(encoderCounts);
  63.   myEncoder.write(0); // Reset encoder count
  64.   delay(1000);
  65. }
  66.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement