Devices & Components
10 jumper wires 150mm male
Arduino Uno Rev3
MG90S Metal Geared Micro Servo
Ultrasonic Sensor - HC-SR04
SSD1306 OLED Display
Breadboard 100x160
Software & Tools
wokwi
Project description
Code
Radar_Scanner
cpp
Code PPI Radar Scan Display using Oled and Arduino
1#include <Wire.h> 2#include <Adafruit_GFX.h> 3#include <Adafruit_SSD1306.h> 4#include <Servo.h> 5#include <NewPing.h> 6 7#define SCREEN_WIDTH 128 8#define SCREEN_HEIGHT 64 9#define OLED_RESET -1 10 11Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); 12Servo servo; 13NewPing sonar(9, 10, 200); // TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE 14 15int centerX = SCREEN_WIDTH / 2; 16int centerY = SCREEN_HEIGHT / 2 + 25; // Shift the semi-circle and sweep line downwards by 25 pixels 17int radius = 30; 18int detectedX = -1; 19int detectedY = -1; 20 21void setup() { 22 display.begin(SSD1306_SWITCHCAPVCC, 0x3C); 23 display.display(); 24 delay(2000); // Pause for 2 seconds 25 display.clearDisplay(); 26 27 servo.attach(6); // Attach the servo to pin 6, update with your actual servo pin 28 servo.write(0); // Initialize servo position 29} 30 31void loop() { 32 display.clearDisplay(); 33 34 // Sweep the servo from 0 to 180 degrees 35 for (int angle = 0; angle <= 180; angle++) { 36 servo.write(angle); 37 delay(15); // Adjust the delay as needed 38 39 // Measure distance using the ultrasonic sensor 40 int distance = sonar.ping_cm(); 41 42 // Calculate coordinates for displaying the angle and distance on the OLED 43 int x = centerX + int(radius * cos(radians(angle))); 44 int y = centerY - int(radius * sin(radians(angle))); // Invert the y-coordinate 45 46 // Draw a line to represent the sweep 47 display.drawLine(centerX, centerY, x, y, SSD1306_WHITE); 48 49 if (distance != 0 && distance > 50) { 50 // Detected an object within 50 cm, mark the point 51 detectedX = x; 52 detectedY = y; 53 } 54 55 // Draw the line for the detected point if it exists 56 if (detectedX != -1 && detectedY != -1) { 57 display.drawLine(centerX, centerY, detectedX, detectedY, SSD1306_WHITE); 58 } 59 60 int startAngle = 180; // Start angle in degrees 61 int endAngle = 360; // End angle in degrees 62 63 // Draw the semi-circle using lines 64 for (int semi_angle = startAngle; semi_angle <= endAngle; semi_angle++) { 65 int x_semi = centerX + radius * cos(radians(semi_angle)); 66 int y_semi = centerY + radius * sin(radians(semi_angle)); // Invert the y-coordinate 67 display.drawPixel(x_semi, y_semi, SSD1306_WHITE); 68 } 69 70 // Display the angle and distance numerically 71 display.setTextSize(1); 72 display.setTextColor(SSD1306_WHITE); 73 display.setCursor(0, 0); 74 display.print("Angle: "); 75 display.println(angle); 76 display.print("Distance: "); 77 display.print(distance); 78 display.println(" cm"); 79 80 display.display(); 81 delay(10); // Small delay for display 82 display.clearDisplay(); 83 } 84 85 delay(100); // Pause before starting the next sweep 86 87 // Reset detected point for the next sweep 88 detectedX = -1; 89 detectedY = -1; 90 91 // Sweep the servo from 180 to 0 degrees 92 for (int rev_angle = 180; rev_angle >= 0; rev_angle--) { 93 servo.write(rev_angle); 94 delay(15); // Adjust the delay as needed 95 96 // Measure distance using the ultrasonic sensor 97 int distance = sonar.ping_cm(); 98 99 // Calculate coordinates for displaying the angle and distance on the OLED 100 int x = centerX + int(radius * cos(radians(rev_angle))); 101 int y = centerY - int(radius * sin(radians(rev_angle))); // Invert the y-coordinate 102 103 // Draw a line to represent the sweep 104 display.drawLine(centerX, centerY, x, y, SSD1306_WHITE); 105 106 if (distance != 0 && distance < 50) { 107 // Detected an object within 50 cm, mark the point 108 detectedX = x; 109 detectedY = y; 110 } 111 112 // Draw the line for the detected point if it exists 113 if (detectedX != -1 && detectedY != -1) { 114 display.drawLine(centerX, centerY, detectedX, detectedY, SSD1306_WHITE); 115 } 116 117 int startAngle = 180; // Start angle in degrees 118 int endAngle = 360; // End angle in degrees 119 120 // Draw the semi-circle using lines 121 for (int semi_angle = startAngle; semi_angle <= endAngle; semi_angle++) { 122 int x_semi = centerX + radius * cos(radians(semi_angle)); 123 int y_semi = centerY + radius * sin(radians(semi_angle)); // Invert the y-coordinate 124 display.drawPixel(x_semi, y_semi, SSD1306_WHITE); 125 } 126 127 // Display the angle and distance numerically 128 display.setTextSize(1); 129 display.setTextColor(SSD1306_WHITE); 130 display.setCursor(0, 0); 131 display.print("Angle: "); 132 display.println(rev_angle); 133 display.print("Distance: "); 134 display.print(distance); 135 display.println(" cm"); 136 137 display.display(); 138 delay(10); // Small delay for display 139 display.clearDisplay(); 140 } 141 142 delay(100); // Pause before starting the next sweep 143}
Downloadable files
Screenshot of the circuit
Simple schematic but better use breadboard
Screenshot 2024-05-25 180214.jpg

Comments
Only logged in users can leave comments