Build a DIY Mini Radar Using Arduino, Python and Streamlit
By: M Khorasani
Tags: Python, Data Visualization, Arduino, Embedded Systems, Dashboard

Build a DIY Mini Radar Using Arduino, Python and Streamlit

Mini radar dashboard. Visual by author.

Introduction

For those of us in the business of robotics, specifically the competitive type the likes of sumo-wrestling and RoboCup bots, we know that detecting objects for avoidance or for collision rather cannot possibly be overstated. While more often than not you would inundate your robot with a slew of IR sensors to detect objects all around, it is infact possible to build your own mini radar to achieve the same effect with far less resources. Here I will show you how to use an IR distance measuring sensor coupled with a servo motor to create a DIY mini radar and also how to visualize the results in real-time with a dazzling dashboard.

1. Arduino

To begin with, we will use an Arduino Uno board to read distance values from the Sharp distance measuring sensor. Subsequently we will mount the sensor onto the arm of a servo motor using double sided tape and connect the wiring as shown below.

Arduino, IR distance measuring sensor and servo motor — image by author.

Once you have connected the sensor and servo, proceed with uploading the following program to the Arduino.

Please note that depending on which sensor you use and your supply voltage, you may need to recalibrate the sensor. The current sensor can read values between 20–150cm, if necessary recalibrate the motor experimentally by reading the sensor measurements for known distances and using that to dervie a new equation.

To verify that the Arduino and sensor are working as expected, open the serial monitor (Tools > Serial monitor) to ensure that values are being recorded and sent over the serial port as shown below.

Arduino serial monitor — image by author.

2. Python

Now that the Arduino, sensor and servo are working and sending values to the serial port, we need to acquire the readings in Python to produce our radar dashboard. To interface our Python script with the Arduino through a serial USB connection, we will need to download and install Pyserial. Go ahead and fire up Anaconda or any other Python IDE of your choice and type the following command:

pip install pyserial

To produce a graphical user interface where we can visualize and interact with our dashboard we will use Streamlit. This highly versatile web framework allows you to quickly develop and deploy applications to a web server or alternatively run them locally on your browser. And to display a radar plot of the sensor readings we will use Plotly. This is a Python binding for JavaScript’s highly interactive data visualization framework that allows you to render dazzling visuals at your fingertips.

Proceed by saving the following source code in your local directory:

The above code will initiate a connection to the Arduino and then will continuously read radar position and sensor values from the serial port until prompted to stop. It will also continuously render and update the radar plot.

To run the above script type the following commands in Anaconda prompt:

cd C:/Users/.../local_directory

streamlit run python_radar.py

Results

And there you have it, a DIY mini radar in action!

If you want to learn more about data visualization and Python, then feel free to check out the following (affiliate linked) courses:

Data Visualization with Python:

Python for Everybody Specialization:

Introduction to Programming IoT Specialization:

The source code for this tutorial can be found in the following GitHub repository.

In addition, feel free to subscribe to Medium and explore more of my tutorials here.