Arduino Radar Scanner with OLED Display and Servo Sweep Visualization
An Arduino-powered radar system that uses a sweeping ultrasonic sensor and displays detected objects as fading blips on an OLED screen.
Components and supplies
1
Arduino Uno Rev3
1
Grove - OLED Display 0.96"
1
MG90S Micro-servo motor
1
Ultrasonic Sensor - HC-SR04
Apps and platforms
1
Arduino IDE 2.0 (beta)
Project description
Code
CODE
js
CODE
1#include <Servo.h> 2#include <Wire.h> 3#include <Adafruit_GFX.h> 4#include <Adafruit_SSD1306.h> 5 6#define SCREEN_WIDTH 128 7#define SCREEN_HEIGHT 64 8#define OLED_RESET -1 9Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); 10 11#define TRIG_PIN 9 12#define ECHO_PIN 10 13#define SERVO_PIN 11 14 15Servo servo; 16const int maxDistance = 50; 17const int centerX = SCREEN_WIDTH / 2; 18const int centerY = SCREEN_HEIGHT - 1; 19const int maxRadius = 50; 20 21struct Blip { 22 float angle; 23 int distance; 24 unsigned long timestamp; 25 bool active; 26}; 27 28Blip blips[20]; 29int blipCount = 0; 30unsigned long lastUpdate = 0; 31const long fadeTime = 3000; 32 33void setup() { 34 pinMode(TRIG_PIN, OUTPUT); 35 pinMode(ECHO_PIN, INPUT); 36 servo.attach(SERVO_PIN); 37 servo.write(0); 38 39 if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { 40 for(;;); 41 } 42 display.clearDisplay(); 43 display.display(); 44} 45 46float measureDistance() { 47 digitalWrite(TRIG_PIN, LOW); 48 delayMicroseconds(2); 49 digitalWrite(TRIG_PIN, HIGH); 50 delayMicroseconds(10); 51 digitalWrite(TRIG_PIN, LOW); 52 53 long duration = pulseIn(ECHO_PIN, HIGH, 30000); 54 if (duration == 0) return maxDistance; 55 56 float distance = duration * 0.034 / 2; 57 return (distance > maxDistance) ? maxDistance : distance; 58} 59 60void addBlip(float angle, int distance) { 61 if (blipCount < 20 && distance < maxDistance) { 62 blips[blipCount].angle = angle; 63 blips[blipCount].distance = distance; 64 blips[blipCount].timestamp = millis(); 65 blips[blipCount].active = true; 66 blipCount++; 67 } 68} 69 70void updateBlips() { 71 unsigned long currentTime = millis(); 72 for (int i = 0; i < blipCount; i++) { 73 if (blips[i].active && (currentTime - blips[i].timestamp > fadeTime)) { 74 blips[i].active = false; 75 76 for (int j = i; j < blipCount - 1; j++) { 77 blips[j] = blips[j + 1]; 78 } 79 blipCount--; 80 i--; 81 } 82 } 83} 84 85void drawBlips() { 86 unsigned long currentTime = millis(); 87 for (int i = 0; i < blipCount; i++) { 88 if (blips[i].active) { 89 float age = (currentTime - blips[i].timestamp) / (float)fadeTime; 90 int alpha = 255 * (1.0 - age); // Fade effect 91 int x = centerX + (blips[i].distance * maxRadius / maxDistance) * cos(radians(blips[i].angle)); 92 int y = centerY - (blips[i].distance * maxRadius / maxDistance) * sin(radians(blips[i].angle)); 93 94 if (age < 0.5) { 95 96 display.drawLine(x-2, y-2, x+2, y+2, SSD1306_WHITE); 97 display.drawLine(x-2, y+2, x+2, y-2, SSD1306_WHITE); 98 } else { 99 100 display.drawPixel(x, y, SSD1306_WHITE); 101 } 102 } 103 } 104} 105 106void drawRadar() { 107 108 display.drawCircle(centerX, centerY, maxRadius / 2, SSD1306_WHITE); 109 display.drawCircle(centerX, centerY, maxRadius, SSD1306_WHITE); 110} 111 112void loop() { 113 static int angle = 0; 114 static bool forward = true; 115 static unsigned long lastServoMove = 0; 116 static unsigned long lastMeasurement = 0; 117 118 unsigned long currentTime = millis(); 119 120 121 if (currentTime - lastServoMove >= 25) { 122 servo.write(angle); 123 if (forward) { 124 angle += 2; 125 if (angle >= 180) { 126 angle = 180; // Ensure exact boundary 127 forward = false; 128 } 129 } else { 130 angle -= 2; 131 if (angle <= 0) { 132 angle = 0; // Ensure exact boundary 133 forward = true; 134 } 135 } 136 lastServoMove = currentTime; 137 } 138 139 140 if (currentTime - lastMeasurement >= 100) { 141 float distance = measureDistance(); 142 if (distance < maxDistance) { 143 addBlip(angle, (int)distance); 144 } 145 lastMeasurement = currentTime; 146 } 147 148 149 if (currentTime - lastUpdate >= 33) { 150 display.clearDisplay(); 151 updateBlips(); 152 drawRadar(); 153 154 // Draw sweep line 155 int sweepX = centerX + maxRadius * cos(radians(angle)); 156 int sweepY = centerY - maxRadius * sin(radians(angle)); 157 display.drawLine(centerX, centerY, sweepX, sweepY, SSD1306_WHITE); 158 159 drawBlips(); 160 display.display(); 161 lastUpdate = currentTime; 162 } 163}
Comments
Only logged in users can leave comments