The first step in a robotics project is to get a motor spinning. Once you’ve done that you quickly learn that not all motors go the same speed, even if they are the same part number! There are always variations that relate to voltage, environment, and manufacturing changes. So, the second step is to figure out how fast it’s going! Turns out that’s not so easy, but the best way to get started is to add an encoder wheel and an optical or magnetic counter. As the motor turns, the attached encoder wheel spins, causing the counter to detect each passing spoke and that lets your microcontroller count and determine speed.
If you want to do one better, add a second counter, and now you can tell direction as well as speed! All this wiring is kind of a pain, so that’s why this motor is really nice! It has a magnetic wheel and two hall effect sensors already attached. Using this motor is a breeze, and it’s a nice small motor as well, in the ‘standard’ N20 size.
Provide 4.5 to 6V DC (nominal) to the white and red wires – these connect to your motor driver, and can be PWM’d for speed adjustment and direction by using an H-bridge.
Connect the black wire to your microcontroller ground pin, and the blue wire to 3-5V DC (we tried both, works fine) use whichever voltage your microcontroller uses. Then you can read the hall effect outputs on the yellow and green wires. We have an example sketch here for Arduino, it can be adapted to other languages – basically you just want to interrupt on one of the encoder pins, use count the time since the last interrupt, and multiply the count time by 14-counts-per-revolution and the gear ratio.
This particular DC motor comes with 1:150 gear ratio, uses 6V nominal power for the motor and draws about 100mA (200mA when stalled). The gear ratio will not affect the current draw but does change the torque and RPM. See below for the no load/rated/stall current, RPM and torque for a range of ratios!
TECHNICAL DETAILS:
- Dimensions (excluding shaft): 30.5mm x 18.5mm
- Wire Length: ~150mm long
Link:
Arduino Code:
below is the arduino code for Controlling N20 Micro Gear Encoder Motor with arduino
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
#define MotFwd 3 // Motor Forward pin #define MotRev 5 // Motor Reverse pin int encoderPin1 = 2; //Encoder Output 'A' must connected with intreput pin of arduino. int encoderPin2 = 3; //Encoder Otput 'B' must connected with intreput pin of arduino. volatile int lastEncoded = 0; // Here updated value of encoder store. volatile long encoderValue = 0; // Raw encoder value void setup() { pinMode(MotFwd, OUTPUT); pinMode(MotRev, OUTPUT); Serial.begin(9600); //initialize serial comunication pinMode(encoderPin1, INPUT_PULLUP); pinMode(encoderPin2, INPUT_PULLUP); digitalWrite(encoderPin1, HIGH); //turn pullup resistor on digitalWrite(encoderPin2, HIGH); //turn pullup resistor on //call updateEncoder() when any high/low changed seen //on interrupt 0 (pin 2), or interrupt 1 (pin 3) attachInterrupt(0, updateEncoder, CHANGE); attachInterrupt(1, updateEncoder, CHANGE); } void loop() { for (int i = 0; i <= 500; i++){ digitalWrite(MotFwd, LOW); digitalWrite(MotRev, HIGH); Serial.print("Forward "); Serial.println(encoderValue); } delay(1000); for (int i = 0; i <= 500; i++){ digitalWrite(MotFwd, HIGH); digitalWrite(MotRev, LOW); Serial.print("Reverse "); Serial.println(encoderValue); } delay(1000); } void updateEncoder(){ int MSB = digitalRead(encoderPin1); //MSB = most significant bit int LSB = digitalRead(encoderPin2); //LSB = least significant bit int encoded = (MSB << 1) |LSB; //converting the 2 pin value to single number int sum = (lastEncoded << 2) | encoded; //adding it to the previous encoded value if(sum == 0b1101 || sum == 0b0100 || sum == 0b0010 || sum == 0b1011) encoderValue --; if(sum == 0b1110 || sum == 0b0111 || sum == 0b0001 || sum == 0b1000) encoderValue ++; lastEncoded = encoded; //store this value for next time } |
Some explanation about the code.
This code will run motor in Forward direction for some time and then run in Reverse direction meanwhile we can get the encoder value at our Serial Monitor.
When motor moves in forward direction encoder value will increment and if motor moves in reverse direction the encoder value will decrease.
This is very basic code just to get idea how we can get encoder value from the encoder.
In next post we will se how we can perform position control using PID on N20 motor.
Encoder value calculation
Encoder pulse per revolution = 28
so one rotation of output shaft = Encoder pulse per revolution X Gear Ratio
So pulse per one rotation = 28 X 100
= 2800 Pulse per revolution.
Reviews
There are no reviews yet.