Making an Arduino Ultrasone Radar With Python and Opencv drawing

by BartvanS in Circuits > Sensors

2105 Views, 3 Favorites, 0 Comments

Making an Arduino Ultrasone Radar With Python and Opencv drawing

Arduino HC-SR04 radar system
Untitled.png

I have always been fascinated with radar technologies. It is very interesting how objects can be detected and the range be calculated. As a result of this I always wanted to make my own simple radar.

Preparation

Needed hardware

  • Arduino board(any board is fine as long it has usb connection) I used an arduino nano
  • Distance sensor. I used the HC-SR04 ultrasone sensor
  • Servo or steppermotor. You can use any kind of motor as long you can turn in degrees in a specific amount

Software

Code

Download the code from my repository and install the dependencies as per step 1. For the arduino code you need to install platformio in the editor visual studio code.

https://github.com/BartvanS/radar

Explanation Arduino Setup

Untitled.png

In this step I will explain the most crucial part of the project, the hardware.

As you can see in the video, I placed the ultrasone sensor in a 3d printed casing which i place on top of a old 3d print that a servo can hold. If you would like to print your own sensor holder you can find a lot of premade models on sites like thingiverse.

If you don't own a 3d printer you could craft something yourself from cardboard and glue the components together.

The code:

This enables the servo, sets it at the 0 degrees startposition and sets the correct pin modes of the ultrasone sensor.

Sadly I only have 180 degree servos. As practice you could modify the coming code to support a continious 360* servo/encoder motor.

Arduino Loop

Untitled.png

First define the variables.

In the loop you constantly move to the next degree. After the servo has turned, get the distance of the ultrasone sensor. This is the data we want to serially send to the computer.

The protocol I use is: {degree}|{distance(in cm)}

Explanation Radar Drawing

radar.png

As the python code is fairly large I will only explain what each file does. This way you can figure out yourself how to draw a radar using opencv.

index.py

This is, as the name suggests, the starting point of our program. This file receives the serial data and updates the radar image based on a schedule.

pySerial.py

Using the serial libary this file reads a line from the serial data we sent from the arduino by calling the "read_line" function.

radar.py

This file keeps track of the generated lines, calculates the coordinates where the lines and dots need to appear and generates the image.

canvas.py

This file handles the actual drawing. It has custom functions that are handy to use. The radar class makes use of this canvas class to draw the image.