$USD
  • EUR€
  • £GBP
  • $USD
TUTORIALS RoboticsArduino

How to Play With MiniQ 2WD Complete Robot Kit v2.0- Lesson 7 (Encoder)

DFRobot Mar 24 2017 805

Lesson 7. Encoder

Key Point

You have learned how to control the speed of motors, but the speed depends on the power  you give. When something happens to your battery, the speed will also change at the same time.  So if we want to control the speed accurately, we need real time speed of motors, thus we need a new device: encoder.
You Will:
1. Learn how to use the encoder and review how to use interrupt function
2. Know how an encoder work
3. Get the speed of motors and control it
4. Equipment needed: Micro USB CableMiniQ 2WD Complete robot Kit v2.0


Upload Sample Code

Open the sample code: speed.ino

1) Sample code

Upload the code and open the serial monitor, you will see:

3) Data when right motor runs

You can change the speed(PWM) of motors in the code( this is not the real speed), thus to measure the real speed in different PWM. For example: calculate the number of times which the voltage change in 100ms, if 100 times changing, it means in 1s it will  change 1000 times, and if the gear has 50 teeth, it will be run 1000/50=20 circle in 1 second,  just get the diameter of the wheel and you will know the real speed.


Principle


1. Encoder
It is easy to understand that two motors will run differently through they have the same power and the same parameter, takes your robot for example, if you make both of the motors run at the same PWM, the robot can not run exactly straightly. So we need to know the real speed of motors. Thus encoder comes here.




4) Encoder Principal

The principle of encoder is the same as the infrared line tracking sensor, it just output the state of whether it has gear teeth in front. And for, MCU, just calculate  these data.
1. Interrupt
We have learned about interrupt, and the picture below shows which pin can be used as interrupt input of the number of interrupt in different kinds of Arduino boards.
Pin 2 in Uno or Mega2560 can be used as number 0 interrupt but in Leonardoit is Pin 3.

5) Interrupt number and Pin number of different borads

Interrupt helps us do something in time, and it also receives several kinds of  input for trigger:
LOW: when it has low input, trigger the interrupt CHANGE: when the input change its voltage
RISING: when the input changes from low voltage to high voltage FALLING: when the input changes from high voltage to low voltage
Interrupt function:
attachInterrupt(interrupt, function, mode) interrupt:number of interrupt
function:the name of your function when interrupt was triggered, this function can’t return or receive any parameters.
mode:Which kind of input can trigger the interrupt For example:
attachInterrupt(R,right_count , CHANGE); //attach isr function R=2,right_count is the function,CHANGE is the kind of trigger.
And notice that when interrupt function runs, the program in the loop will stop.




6) How does an encoder work


Encoder is widely used in many place, if you want to control your speed well, you can learn more about the arithmetic about PID or fuzzy.

 

Code Analysis

Define the data about the interrupt
    attachInterrupt(R,right_count , CHANGE); //attach isr function
    attachInterrupt(L,left_count, CHANGE); //attach isl function
Print the speed of right motor
r_speed_reality=r_val/(time_step/1000);
  Serial.print("r_val:");
  Serial.print(r_val);
  Serial.print("\t r_speed_reality:");
  Serial.print(r_speed_reality);
  // calculate speed
The interrupt function of the right motor
void right_count()
{
    //Serial.println("r");
r_val++;
}


Circuit Design



7) Encoder circuit

The circuit is just use a chip SN74LVC1G14DBV to deal with the signal that infrared sensor gives, and transmit to Arduino. The chip can make the signal received from infrared sensor more stable.
 
You may face these questions:
The speed is always showing 0,
First, check the program, if someone changed your code, you need to get your precious one back.
If the first step didn’t work, it is usually because of the location of the wheel, they didn’t fit the infrared sensors well by transport or any other reasons. Now let’s try to solve this problem.
Upload the code below to for print the value in computer:
void setup()
  {
  pinMode(0,INPUT);//right encoder
  pinMode(1,INPUT);//left encoder
  Serial.begin(9600);//Serial initiation
  }
void loop()
{
    int x=digitalRead(0);//Read the value of right encoder
    int y=digitalRead(1);//Read the value of left encoder
    Serial.print(x);//print the value through computer
    Serial.print(" "); 
    Serial.println(y); 
    delay(500);
}

Open the serial monitor and you will see:


8) Unchanged value

Adjust the location of wheel
Pull out or push into the wheel a little,then turn the wheel, if you see the value changes, the situation is OK:

9) The left side is OK

Generally the location is where the wheel starts to meet the sensor:

10) The location of IR sensor

When value is always 1, it means the sensor can always detect a body, you should pull the wheel out a little.
Now upload speed.ino, and try to understand it!

REVIEW