Devices & Components
Arduino Nano
Servos (Tower Pro MG996R)
Jumper wires (generic)
Ultrasonic Sensor - HC-SR04 (Generic)
Breadboard (generic)
Hardware & Tools
Hot glue gun (generic)
Soldering iron (generic)
Software & Tools
Arduino IDE
Project description
Code
Arduino Code for radar project
arduino
it rotate servo and find distance using ultrasonic sensor
1// Includes the Servo library 2#include <Servo.h>. 3 4// Defines Tirg and Echo pins of the Ultrasonic Sensor 5const int trigPin = 10; 6const int echoPin = 11; 7// Variables for the duration and the distance 8long duration; 9int distance; 10 11Servo myServo; // Creates a servo object for controlling the servo motor 12 13void setup() { 14 pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output 15 pinMode(echoPin, INPUT); // Sets the echoPin as an Input 16 Serial.begin(9600); 17 myServo.attach(12); // Defines on which pin is the servo motor attached 18} 19void loop() { 20 // rotates the servo motor from 15 to 165 degrees 21 for(int i=15;i<=165;i++){ 22 myServo.write(i); 23 delay(30); 24 distance = calculateDistance();// Calls a function for calculating the distance measured by the Ultrasonic sensor for each degree 25 26 Serial.print(i); // Sends the current degree into the Serial Port 27 Serial.print(","); // Sends addition character right next to the previous value needed later in the Processing IDE for indexing 28 Serial.print(distance); // Sends the distance value into the Serial Port 29 Serial.print("."); // Sends addition character right next to the previous value needed later in the Processing IDE for indexing 30 } 31 // Repeats the previous lines from 165 to 15 degrees 32 for(int i=165;i>15;i--){ 33 myServo.write(i); 34 delay(30); 35 distance = calculateDistance(); 36 Serial.print(i); 37 Serial.print(","); 38 Serial.print(distance); 39 Serial.print("."); 40 } 41} 42// Function for calculating the distance measured by the Ultrasonic sensor 43int calculateDistance(){ 44 45 digitalWrite(trigPin, LOW); 46 delayMicroseconds(2); 47 // Sets the trigPin on HIGH state for 10 micro seconds 48 digitalWrite(trigPin, HIGH); 49 delayMicroseconds(10); 50 digitalWrite(trigPin, LOW); 51 duration = pulseIn(echoPin, HIGH); // Reads the echoPin, returns the sound wave travel time in microseconds 52 distance= duration*0.034/2; 53 return distance; 54}
Downloadable files
arduino radar circuit diagram
I made it by my own
arduino radar circuit diagram
arduino radar circuit diagram
I made it by my own
arduino radar circuit diagram
Comments
Only logged in users can leave comments