Use Arduino to make automatic car speed measurement

Reference article: https://blog.csdn.net/qq_39097425/article/details/85245668

The principle of speed measurement: using a reed switch or a Hall sensor, both of which have the same properties, trigger when a magnet approaches, and generate high and low level signals. Bind the sensor to the frame and put magnets on the wheels. When the magnet is triggered once, it is regarded as the wheel turning once, and the perimeter of the wheel can be obtained by using elementary school mathematics knowledge to obtain the speed per hour.

Reed pipe:Insert picture description here

Hall sensor:Insert picture description here

In addition to proximity switches such as reed switches and Hall sensors, vibration switches can be used. Fix the device on the inside of the pedals and use the cadence to calculate the speed (this method is only suitable for spinning), and the cadence of the rider can also be used.

Vibration switch:Vibration switch

The Arduino code is as follows:

long lastturn;
float SPEED = 0;
float DIST;
float w_length = 2.050;
boolean isRun = false;

void setup() {
    
    
  pinMode(2, INPUT_PULLUP);
  Serial.begin(9600);               
  attachInterrupt(digitalPinToInterrupt(2), sens, FALLING);  
  lastturn = millis();
}

void sens() {
    
    
  if (millis() - lastturn > 50) {
    
    
    isRun = true;
    SPEED = w_length / ((float)(millis() - lastturn) / 1000) * 3.6;   
    lastturn = millis();                                              
    Serial.print("速度: ");
    Serial.print(SPEED);
    Serial.println(" km/h");
    isRun = false;
  }
}

void loop() {
    
    
  if ((millis() - lastturn) > 2000 && !isRun) {
    
        
    SPEED = 0;                             
    Serial.println("无速度");
    delay(1000);
  }
}

Currently, there is a known problem. If no signal is obtained for more than 2 seconds when driving at a low speed, "No Speed" will be printed. This problem can be eliminated when the front-end program is made.

The actual effect is as follows:Insert picture description here

Guess you like

Origin blog.csdn.net/hp150119/article/details/110432134