Project tutorial
Arduino Sonar

Arduino Sonar © MIT

Use HC-SR04 on Arduino with Processing

  • 69,554 views
  • 32 comments
  • 132 respects

Components and supplies

About this project

Radar Screen Visualisation for HC-SR04 

Arduino Code:  

Sends sensor readings for every degree moved by the servo values sent to serial port to be picked up by Processing 

https://github.com/faweiz/My-Arduino/blob/master/arduino_radar/Arduino_radar_HC_SR04/Arduino/Arduino_radar_server_arduino/Arduino_radar_server_arduino.ino

Processing Code: 

Maps out an area of what the HC-SR04 sees from a top down view. Takes and displays 2 readings, one left to right and one right to left. Displays an average of the 2 readings

https://github.com/faweiz/My-Arduino/blob/master/arduino_radar/Arduino_radar_HC_SR04/Processing-code/Arduino_radar_client_processing/Arduino_radar_client_processing.pde

Any questions are welcome!

Github: https://github.com/faweiz

Portfolium: https://portfolium.com/faweiz

Linkedin: https://www.linkedin.com/in/faweiz

Code

Untitled fileArduino
/*
https://www.hackster.io/faweiz/arduino-radar
Radar Screen Visualisation for HC-SR04
Sends sensor readings for every degree moved by the servo
values sent to serial port to be picked up by Processing
*/

#include <NewPing.h>
#include <Servo.h> 
 
#define TRIGGER_PIN  2   // Arduino pin 2 tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN     3   // Arduino pin 3 tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 150 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.
#define SERVO_PWM_PIN 9 //set servo to Arduino's pin 9
 
// means -angle .. angle
#define ANGLE_BOUNDS 80
#define ANGLE_STEP 1
 
int angle = 0;
 
// direction of servo movement 
// -1 = back, 1 = forward 
int dir = 1;
 
Servo myservo;
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); 
 
void setup() {
  Serial.begin(9600); // initialize the serial port:
  myservo.attach(SERVO_PWM_PIN); //set servo to Arduino's pin 9
}
 
void loop() {
 
  delay(50);
  // we must renormalize to positive values, because angle is from -ANGLE_BOUNDS .. ANGLE_BOUNDS
  // and servo value must be positive
  myservo.write(angle + ANGLE_BOUNDS);
  
  // read distance from sensor and send to serial
  getDistanceAndSend2Serial(angle);
  
  // calculate angle 
  if (angle >= ANGLE_BOUNDS || angle <= -ANGLE_BOUNDS) {
    dir = -dir;
  }
  angle += (dir * ANGLE_STEP);  
}
 
int getDistanceAndSend2Serial(int angle) {
  int cm = sonar.ping_cm();
  Serial.print(angle, DEC);
  Serial.print(",");
  Serial.println(cm, DEC);
  
}
Arduino_radar_server_arduino.ino
Arduino_radar_client_processing.pde

Schematics

HC_SR04 with arduino sketch
Hc sr04%20sketch

Comments

Author

12278792 1225491704144475 2521946915381291926 n
Tony ZHANG
  • 3 projects
  • 68 followers

Published on

August 30, 2015

Members who respect this project

DefaultLogo 300x300Default935302 103230376547073 788875580 nDefaultM2DefaultDefault

and 124 others

Project replicated by

Img 0184 o1j0fhhxwrDefault
See similar projects
you might like

Similar projects you might like

Windows 10 IoT Core: UltraSonic Distance Mapper

Project showcase by Anurag S. Vasanwala

  • 51,861 views
  • 23 comments
  • 138 respects

Programming ATtiny85 with Arduino Uno

Project tutorial by Arjun Ganesan

  • 214,109 views
  • 74 comments
  • 185 respects

Detecting Obstacles and Warning - Arduino and Ultrasonic

Project tutorial by Ammar Atef Ali

  • 71,760 views
  • 40 comments
  • 110 respects

Motion Following Motorized Camera Base

Project tutorial by Lindsay Fox

  • 113,766 views
  • 68 comments
  • 287 respects

Vertical Hydroponic Farm

Project in progress by Paul Langdon

  • 55,610 views
  • 21 comments
  • 291 respects

Ultrasonic Map-Maker using an Arduino Yun

Project tutorial by Satyavrat Wagle

  • 51,983 views
  • 37 comments
  • 152 respects
Add projectSign up / Login