Speed measuring module arduino driver

Insert picture description here
The so-called speed measurement is the rotation speed of the motor, not the moving speed of the object, but the angular speed.

The speed measuring module is a U-shaped card slot. Insert picture description here
The principle is the same as that of the tracking module. It is an infrared transceiver tube, and the blocking is 1, and the non-blocking is 0; then used with the code wheel, the code wheel:Insert picture description here

There are 20 gaps and 20 occlusions on the code wheel. In this way, the code wheel is installed on the rotating shaft of the motor. When the motor rotates, the speed measuring module will continuously output high and low level changes.

Wiring as shown

Insert picture description here

float n = 0;      

float time;  

float Speed;  

void setup(){

  Serial.begin(9600);

  attachInterrupt(0,count,CHANGE);    

}

void loop(){

  time = millis();

  Speed =  (n/40)/(time/60000) ;

  Serial.println(Speed);

}

void count(){

  n += 1;

}


There are two sentences of code that are more important. Let us explain the above code:

attachInterrupt(interrupt,function,mode); 

这是一句中断函数,函数有三个参数:

interrupt  ——  中断源。可选值0和1,0 对应2号引脚;1 对应3号引脚。这里我们写的 0 ,即外部中断接数字引脚2号引脚。

function  ——  中断处理函数。即发生中断时,去做什么。这里我们让它发生中断时,做 n+=1。用来计数模块被码盘遮挡时,电平变化的次数。

mode  ——  触发模式。触发模式有四种类型:LOW(低电平触发)、CHANGE(电平变化时触发)、RISING(低电平变为高电平时触发)、FALLING(高电平变为低电平时触发)。这里我们选择CHANGE(电平变化时触发),前面讲了,码盘有20个缝隙,20个遮挡,这样,电机转一转时,为40次触发,即n=40,为n/40转。

millis();

   This function is used to obtain the length of time the machine is running. The return value of the function is unsigned long (millisecond ms). The longest recording time of the system is 9 hours and 22 minutes. If it exceeds, it starts from 0.

Therefore, the rotation speed we measured is the average angular velocity since the system started, not the real-time velocity.

   After understanding these two functions, look at the above code, it is not difficult to understand, the final output is the motor speed, unit: rpm.

   Of course, if the motor is installed on the car, the speed of the wheel circumference = car moving speed. n Wheel circumference = total distance traveled by car.

   We refer to the code of the L298N motor drive, drive a decelerating DC motor on a car, and perform a speed test. In the next video effect display, we set the PWM to control the motor speed to three different values ​​of 50, 100, and 200. You can see When the speed changes obviously, and basically changes linearly, it can be seen that this module is still possible.
Insert picture description here

Published 153 original articles · Like 248 · Visits 330,000+

Guess you like

Origin blog.csdn.net/qq_45172832/article/details/105418720