Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- 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.
- First, assemble and connect the Pololu Magnetic Encoder Kit to your micro metal gearmotors and your Arduino as follows:
- Assemble the encoder kit onto the micro metal gearmotors as per the instructions in the encoder kit's user guide.
- Connect the encoder A channel output (OUTA) of the first motor to Arduino pin 2 (interrupt 0).
- Connect the encoder B channel output (OUTB) of the first motor to Arduino pin 4.
- Connect the encoder A channel output (OUTA) of the second motor to Arduino pin 3 (interrupt 1).
- Connect the encoder B channel output (OUTB) of the second motor to Arduino pin 5.
- Connect the ground (GND) of both encoders to the Arduino ground.
- Connect the voltage (VCC) of both encoders to the Arduino 5V supply.
- Next, upload the following code to your Arduino:
- 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.
- 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.
- 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.
- */
- // Encoder pins
- const int encoderA1 = 2; // First motor, channel A
- const int encoderB1 = 4; // First motor, channel B
- const int encoderA2 = 3; // Second motor, channel A
- const int encoderB2 = 5; // Second motor, channel B
- // Encoder counts
- volatile long encoderCount1 = 0;
- volatile long encoderCount2 = 0;
- void setup() {
- Serial.begin(9600);
- // Set encoder pins as inputs with internal pull-ups
- pinMode(encoderA1, INPUT_PULLUP);
- pinMode(encoderB1, INPUT_PULLUP);
- pinMode(encoderA2, INPUT_PULLUP);
- pinMode(encoderB2, INPUT_PULLUP);
- // Attach interrupt handlers
- attachInterrupt(digitalPinToInterrupt(encoderA1), updateEncoder1, CHANGE);
- attachInterrupt(digitalPinToInterrupt(encoderA2), updateEncoder2, CHANGE);
- }
- void loop() {
- // Print encoder counts every second
- Serial.print("Encoder 1: ");
- Serial.print(encoderCount1);
- Serial.print(" Encoder 2: ");
- Serial.println(encoderCount2);
- delay(1000);
- }
- void updateEncoder1() {
- if (digitalRead(encoderA1) == digitalRead(encoderB1)) {
- encoderCount1++;
- } else {
- encoderCount1--;
- }
- }
- void updateEncoder2() {
- if (digitalRead(encoderA2) == digitalRead(encoderB2)) {
- encoderCount2++;
- } else {
- encoderCount2--;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement