Blog

Radar Speedometer with Arduino or ESP32

4/4/2023 4 min read
Radar Speedometer with Arduino or ESP32

Building your own radar system for speed measurement is a fascinating project that provides insights into the world of high-frequency technology and the Doppler effect. In this tutorial, we explain step-by-step how to construct a radar and speedometer using a 10.525 GHz transmitter and an Arduino or ESP32 microcontroller.


Basics: The Doppler Effect

The Doppler effect, where the frequency of a signal changes due to the movement of a source or observer, forms the foundation for speed measurement in this project. By leveraging this principle, the relative speed of a moving object can be determined precisely.


Target Audience

This project is aimed at electronics enthusiasts, makers, and hobbyists who want to expand their skills and gain practical experience with radar technology. All necessary components and steps are described in detail so that even beginners can successfully build their own radar.


Interesting Insight

A somewhat alarming insight is that theoretically nearly every modern smartphone or router has the basic technology to function as a radar that can detect moving objects in its vicinity.

Both mobile phones and routers emit and receive high-frequency signals, similar to a radar system. These signals could theoretically be used to measure the Doppler shift to determine the speed of moving objects.

Modern smartphones also feature powerful processors and sensors capable of performing the necessary calculations. With appropriate software and adjustments, radio modules and signal processing techniques could, in principle, be used for radar measurements.


Required Hardware

  • CQRobot Microwave Motion Sensor (10.525 GHz)
  • ESP32 or Arduino microcontroller
  • Breadboard
  • Jumper wires

(Products see sidebar)


Wiring

Connect the 10.525 GHz sensor to your Arduino or ESP32. The data wire is connected to GPIO16, as the provided code uses this pin. If another pin is used, the code must be adjusted accordingly.

Additionally, the Arduino IDE is required. No external libraries are used.


Code

const int sensorPin = 16;  // GPIO pin where the sensor is connected
const float frequency = 10.525e9;  // Frequency of the microwave sensor in Hz
const float c = 3e8;  // Speed of light in m/s
const int measureDuration = 100;  // Measurement duration in milliseconds
const int numMeasurements = 10;  // Number of measurements for averaging

volatile unsigned long pulseCount = 0;

void IRAM_ATTR handlePulse() {
  pulseCount++;
}

void setup() {
  Serial.begin(115200);  // Serial communication at 115200 baud
  pinMode(sensorPin, INPUT);  // Set sensor pin as input
  attachInterrupt(digitalPinToInterrupt(sensorPin), handlePulse, RISING);
}

void loop() {
  float totalSpeed = 0.0;
  unsigned long startTime = millis();  // Start time for loop

  for (int i = 0; i < numMeasurements; i++) {
    unsigned long measurementStartTime = millis();
    pulseCount = 0;

    // Measure for 100 ms
    delay(measureDuration);

    unsigned long duration = millis() - measurementStartTime;
    unsigned long pulses = pulseCount;

    float pulseFrequency = (float)pulses / duration * 1000;  // Pulse frequency in Hz
    float speed_m_s = pulseFrequency * c / (2 * frequency);  // Speed in m/s
    float speed_km_h = speed_m_s * 3.6;  // Speed in km/h

    totalSpeed += speed_km_h;
  }

  float averageSpeed = totalSpeed / numMeasurements;

  Serial.print("Average Speed: ");
  Serial.print(averageSpeed);
  Serial.println(" km/h");

  // Output every second
  unsigned long endTime = millis();
  unsigned long loopDuration = endTime - startTime;
  if (loopDuration < 1000) {
    delay(1000 - loopDuration);
  }
}

Code Explanation

  • sensorPin: GPIO pin where the sensor is connected
  • frequency: Frequency of the microwave sensor (10.525 GHz)
  • c: Speed of light
  • measureDuration: Duration per measurement (100 ms)
  • numMeasurements: Number of measurements for averaging

The variable pulseCount counts incoming pulses. The interrupt function handlePulse() is called on each rising edge and increments the counter.

In setup():

  • Initialize the serial interface (115200 baud)
  • Set the sensor pin as input
  • Setup an interrupt on rising edges

In loop():

  • Perform ten measurements each of 100 ms
  • Calculate pulse frequency
  • Convert to speed (m/s → km/h)
  • Output the averaged speed
  • Time synchronization for one output per second

Measuring Speed

After uploading the script to the Arduino or ESP32:

  1. Power the microcontroller
  2. Open the serial console in the Arduino IDE
  3. Set baud rate to 115200

The radar automatically measures the speed of objects in the sensor's detection range. The average speed is output every second on the console. Move objects in front of the sensor to test the functionality.


Additional Tips

  • Many 10.525 GHz sensors have a potentiometer to adjust sensitivity
  • Fine-tuning can significantly improve measurement accuracy
  • The metal plate on one side of the sensor serves as shielding
  • Measurements are only possible on the sensor's open side

Conclusion

If anything is unclear, feel free to leave a comment. In the future, it is planned to integrate the system with Home Assistant.