├── README.md ├── arduino_radar.ino └── pythonr_radar.py /README.md: -------------------------------------------------------------------------------- 1 | # Arduino Python DIY Mini Radar 2 | 3 | ![DIY mini radar](https://cdn-images-1.medium.com/max/800/1*nQndlfQPKmCjFNvkeW6HDQ.gif) 4 | 5 | How to build an inexpensive mini radar system with a live dashboard. For further information please refer to the following [tutorial](https://towardsdatascience.com/build-a-diy-mini-radar-using-arduino-python-and-streamlit-c333006681d7). 6 | -------------------------------------------------------------------------------- /arduino_radar.ino: -------------------------------------------------------------------------------- 1 | #include 2 | Servo myservo; 3 | int val = 0; // variable to store the sensor value 4 | int analogPin = A0; // sensor pin 5 | String pval = "*"; // string variable to send sensor value over COM port 6 | 7 | void setup() { 8 | Serial.begin(9600); // setup serial communication 9 | myservo.attach(3); // attach motor to PWM pin 3 10 | myservo.write(90); // rotating motor to initial position 11 | } 12 | 13 | void loop() { 14 | for(int i=0;i<=180;i=i+2){ 15 | myservo.write(i); // rotating motor CW 16 | val = 100095*(pow(analogRead(analogPin),-0.966)); // sensor calibration equation 17 | if ((val>1500) || (val<0)){ // disregarding erroneous readings 18 | val = NULL; 19 | } 20 | Serial.println(i + pval + val); // sending motor position and sensor reading over COM port 21 | delay(50); 22 | } 23 | for(int i=180;i>=0;i=i-2){ 24 | myservo.write(i); // rotating motor CCW 25 | val = 100095*(pow(analogRead(analogPin),-0.966)); // sensor calibration equation 26 | if ((val>1500) || (val<0)){ // disregarding erroneous readings 27 | val = NULL; 28 | } 29 | Serial.println(i + pval + val); // sending motor position and sensor reading over COM port 30 | delay(50); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /pythonr_radar.py: -------------------------------------------------------------------------------- 1 | import serial 2 | import streamlit as st 3 | import plotly.graph_objects as go 4 | import time 5 | 6 | arduino = serial.Serial(port='COM5', baudrate=9600, parity=serial.PARITY_NONE,stopbits=serial.STOPBITS_ONE,bytesize=serial.EIGHTBITS) #Change COM3 to whichever COM port your arduino is in 7 | 8 | st.sidebar.title('Radar') 9 | info_bar = st.empty() 10 | info_1 = st.empty() 11 | info_2 = st.empty() 12 | radar_placeholder = st.empty() 13 | 14 | r = [0]*180 15 | 16 | theta = [0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 17 | 36, 38, 40, 42, 44, 46, 48, 50, 52, 54, 56, 58, 60, 62, 64, 66, 68, 70,72, 18 | 74, 76, 78, 80, 82, 84, 86, 88, 90, 92, 94, 96, 98, 100, 102, 104,106, 108, 19 | 110, 112, 114, 116, 118, 120, 122, 124, 126, 128, 130, 132,134, 136, 138, 20 | 140, 142, 144, 146, 148, 150, 152, 154, 156, 158, 160,162, 164, 166, 168, 21 | 170, 172, 174, 176, 178, 180, 182, 184, 186, 188,190, 192, 194, 196, 198, 22 | 200, 202, 204, 206, 208, 210, 212, 214, 216, 218, 220, 222, 224, 226, 228, 23 | 230, 232, 234, 236, 238, 240, 242, 244, 246, 248, 250, 252, 254, 256, 258, 24 | 260, 262, 264, 266, 268, 270, 272, 274, 276, 278, 280, 282, 284, 286, 288, 25 | 290, 292, 294, 296, 298, 300, 302, 304, 306, 308, 310, 312, 314, 316, 318, 26 | 320, 322, 324, 326, 328, 330, 332, 334, 336, 338, 340, 342, 344, 346, 348, 27 | 350, 352, 354, 356, 358] 28 | 29 | def radar_gauge(val,pos,placeholder): 30 | fig = go.Figure() 31 | pos = int(int(pos)/2) 32 | r[pos] = val 33 | 34 | fig.add_trace(go.Scatterpolar( 35 | r=r, 36 | theta=theta 37 | )) 38 | 39 | r2 = [0]*180 40 | r2[pos] = 1500 41 | fig.add_trace(go.Scatterpolar( 42 | r=r2, 43 | theta=theta 44 | )) 45 | 46 | fig.update_layout( 47 | polar=dict( 48 | radialaxis=dict( 49 | visible=True, 50 | range=[0, 1500] 51 | ), 52 | ), 53 | showlegend=False 54 | ) 55 | placeholder.write(fig) 56 | 57 | if st.sidebar.button('Start radar'): 58 | info_bar.info('Radar started') 59 | 60 | try: 61 | arduino.open() 62 | except: 63 | pass 64 | 65 | if st.sidebar.button('Stop radar'): 66 | info_bar.warning('Radar stopped') 67 | try: 68 | arduino.close() 69 | except: 70 | pass 71 | 72 | while True: 73 | arduino.flushInput() 74 | arduino.flushOutput() 75 | arduino.flush() 76 | try: 77 | val = arduino.readline().decode().strip('\r\n').split('*')[1] 78 | except: 79 | val = 0 80 | pos = arduino.readline().decode().strip('\r\n').split('*')[0] 81 | info_1.info(('Range (mm) = **%s**' % (val))) 82 | info_2.info(('Position (°) = **%s**' % (pos))) 83 | radar_gauge(val,pos,radar_placeholder) 84 | time.sleep(0.05) 85 | 86 | info_bar.warning('Radar stopped') 87 | 88 | try: 89 | arduino.close() 90 | except: 91 | pass 92 | --------------------------------------------------------------------------------