Pages: [1]   Go Down
Author Topic: Ultrasonic Sensor HC-SR04 controlling  (Read 3613 times)
0 Members and 2 Guests are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Im a total noob on Arduino staff so any suggestions for improvement are very welcome.
I have created the follwing scetch that displays and evaluates data derived from an ultrasonic Sensor HC-SR04. I have also attached a fritizing drawing. I wish this might help someone.

Quote
/*
  Aronis Projects
  This Project Uses Arduino, an Ultrasonic (HC-SR04) sensor, a servo motor
  and a LCD 16x02.
  Data from the HC-SR04 sensor is translated via ultrasonic library and displayed on the LCD (again using the LCD library).
 If the distance from target is equal or below 3 cm then the servo motor moves 180 degrees. When the distance gets greater than
3 cm then the motor moves to its starting position. 
  The circuit:
 * LCD RS pin to digital pin 2
 * LCD Enable pin to digital pin 3
 * LCD D4 pin to digital pin 4
 * LCD D5 pin to digital pin 5
 * LCD D6 pin to digital pin 6
 * LCD D7 pin to digital pin 7
 * LCD R/W pin to ground
 * 10K variable resistor:
 * ends to +5V and ground
 * wiper to LCD VO pin (pin 3)
 * Servo motor signal terminal connects to arduino digital pin 11.
 * HC-SR04 Trigger pin hooks to digital pin 12
 * HC-SR04 Echo pin to digital pin 13
 
 */

// include the library code:
#include <LiquidCrystal.h>
// include the ultrasonic library
#include "Ultrasonic.h"
// ultrasonic trig pin at digital pin 12 and echo pin at digital 13
Ultrasonic ultrasonic(12,13);
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(2, 3, 4, 5, 6, 7);

// include the servo library
#include <Servo.h>
Servo myservo;  // create servo object to control a servo
int pos = 0;  // define position as integer
int change = 0; // variable to avoid constant use of the servo

void setup() {
   myservo.attach(11);  // attaches the servo on pin 11 to the servo object
  // set up the LCD's number of columns and rows:
  lcd.begin(16, 2);
  // Print a message to the LCD.
  lcd.print("ARONIS PROJECTS!");
  lcd.setCursor(0,1); // Go to the second line
  lcd.print("****************");
  delay(3000); // wait 3 secs
   lcd.clear(); // Clear screen
   // Print standard message
      lcd.setCursor(0, 0);
      lcd.print("Distance from");
      lcd.setCursor(0,1);
      lcd.print("target=");
  
 }

void loop() {
  pos = ultrasonic.Ranging(CM); // Get the distance from sensor
 lcd.setCursor(8,1);
      lcd.print(ultrasonic.Ranging(CM));
      lcd.print("cm   ");
      delay(100);
     
     if (change == 0 && pos<= 3) {
        myservo.write(170);
        change = 1;
  }
  if (change == 1 && pos> 3) {
  myservo.write(1);
  change = 0;
  }
}


* Lcd_servo_ultrasonic.fzz (9.64 KB - downloaded 36 times.)
Logged

North Queensland, Australia
Offline Offline
Edison Member
*
Karma: 74
Posts: 2225
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Looks like a nice example.
I would recommend:

Change the 'change' type to a boolean it more closely reflects what its being used for.
Also you use 'pos' to store the sensor value used in calculations. You should re-use 'pos' to send to the display also as re-reading the sensor can produce odd results ( screen says <= 3 but pos says 4 ).

EDIT:

Also I think the library ( ultrasonic ) may return floats ( decimal numbers ).
By using an int the decimal places are dropped off. ( 1.99 is not rounded up to 2 )
Then again, it may only return int values.
« Last Edit: February 07, 2012, 07:14:09 am by pYro_65 » Logged


0
Offline Offline
Jr. Member
**
Karma: 1
Posts: 59
Desenvolvimento de Projeto Eletronico
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

When you have success in using HC-SR04 could post if possible how far away you could get.
I have one that is only 1.5 meters and the manual should be 4 meters.
thank you
Edson

I use this program in a Arduino 2560

/*
  HC-SR04 Ping distance sensor]
  VCC to arduino 5v GND to arduino GND
  echo pin 22 trigger pin 24
*/

int echoPin = 22;
int trigPin = 24;

void setup() {
  Serial.begin (9600);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
}

void loop() {
  unsigned long duration, cm;
  digitalWrite(trigPin, LOW);
  delayMicroseconds(10);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(20);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  cm = duration / 29 / 2;
 /* Sound velocity=29cm/microsec,
    then divide by 2 because of the return time */
  Serial.print(cm);
  Serial.println(" cm");
  delay(1000);
}

Logged

North Queensland, Australia
Offline Offline
Edison Member
*
Karma: 74
Posts: 2225
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I managed 3 metres with mine, above that was erratic. The humidity level and other variables may have an effect on its maximum distance.
Logged


0
Offline Offline
Jr. Member
**
Karma: 1
Posts: 59
Desenvolvimento de Projeto Eletronico
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

http://www.maxbotix.com/documents/MB1000_Datasheet.pdf
Have you ever used the maxbotix?
The circuit is so simple to do everthing that promise.
Thanks
Edson
Logged

Pages: [1]   Go Up
Jump to: