Guys Can you Help me about my project.using ULTRASONIC SENSOR US-100

i have a project using this items

Arduino , US-100 Ultrasonic sensor, Buzzer and LED

i have code for my ULTRASONIC sensor

//Demo sketch
//This sketch will output distance into via the UART port

//port assignment
//change as may be necessary

const int trigger = 6;
const int echo = 7;
float distance;

void setup(){
Serial.begin(9600);
pinMode(trigger, OUTPUT);
pinMode(echo, INPUT);
}
void loop(){

//Trigger US-100 to start measurement
//Set up trigger
digitalWrite(trigger, LOW);
delayMicroseconds(5);
//Start Measurment
digitalWrite(trigger, HIGH);
delayMicroseconds(10);
digitalWrite(trigger, LOW);
//Acquire and convert to mtrs
distance = pulseIn(echo, HIGH);
distance = distance*0.0001657;
//send result to UART
Serial.println(distance);
delay(50);
}

in Serial Monitor it is okay. the distance starts 0.01 to 0.55.

my problems are how can i put code for buzzer in led for example if a object pass through or a object incoming to sensor then the buzzer and LED will act. i watch some videos but there ultrasonic is different version.

i hope you can help me.

GOD Bless. :slight_smile:

instead of

Serial.println(distance);
if (distance<0.50)
{
digitalWrite(13,HIGH);
}
else
{
digitalWrite(13,LOW);
}

Serial.println(distance);

Thanks to help Sir.

i want to connect may RED LED and buzzer to my sensor and arduino for security

for example if a object is incoming to sensor. the RED LED will light and then the buzzer will alarm in front of sensor

there is a code for that ?

THANK you.

jheiyomura:
there is a code for that ?

Pretty sure there is... but it's really up to you to go first, try to get it to work, then get help here if and when you get stuck.

If you're new to Arduino, you need to work through some of the examples in the IDE (File > examples) or here.

Thank you mister .!

YES im beginner or newbie

i try the example for SENSOR like PING

PING - Detecting objects with an ultrasonic range finder.

Code:

  • Ping))) Sensor

This sketch reads a PING))) ultrasonic rangefinder and returns the
distance to the closest object in range. To do this, it sends a pulse
to the sensor to initiate a reading, then listens for a pulse
to return. The length of the returning pulse is proportional to
the distance of the object from the sensor.

The circuit:

  • +V connection of the PING))) attached to +5V
  • GND connection of the PING))) attached to ground
  • SIG connection of the PING))) attached to digital pin 7

created 3 Nov 2008
by David A. Mellis
modified 30 Aug 2011
by Tom Igoe

This example code is in the public domain.

*/

// this constant won't change. It's the pin number
// of the sensor's output:
const int pingPin = 7;

void setup() {
// initialize serial communication:
Serial.begin(9600);
}

void loop()
{
// establish variables for duration of the ping,
// and the distance result in inches and centimeters:
long duration, inches, cm;

// The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
// Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
pinMode(pingPin, OUTPUT);
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(5);
digitalWrite(pingPin, LOW);

// The same pin is used to read the signal from the PING))): a HIGH
// pulse whose duration is the time (in microseconds) from the sending
// of the ping to the reception of its echo off of an object.
pinMode(pingPin, INPUT);
duration = pulseIn(pingPin, HIGH);

// convert the time into a distance
inches = microsecondsToInches(duration);
cm = microsecondsToCentimeters(duration);

Serial.print(inches);
Serial.print("in, ");
Serial.print(cm);
Serial.print("cm");
Serial.println();

delay(100);
}

long microsecondsToInches(long microseconds)
{
// According to Parallax's datasheet for the PING))), there are
// 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
// second). This gives the distance travelled by the ping, outbound
// and return, so we divide by 2 to get the distance of the obstacle.
// See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
return microseconds / 74 / 2;
}

long microsecondsToCentimeters(long microseconds)
{
// The speed of sound is 340 m/s or 29 microseconds per centimeter.
// The ping travels out and back, so to find the distance of the
// object we take half of the distance travelled.
return microseconds / 29 / 2;
}

i try this code but still not working because may sensor is US-100 5 pins then on the sensor example is only 3 pins

my devices are:

arduino
ultrasonic sensor us-100
1 RED LED
and BUZZER

there is a way to connect this 4 devices in 1 program but i watch some tutorials but only testing but there is no code.

i need your help. for my project. :slight_smile:

i hope you can help me.

Why are you looking at that Ping code: you already have demo code for your sensor.

rogerClark already gave you a snippet that puts an LED on if the distance is less than 0.5 (which he just used as an example).

You need to apply your mind here: work through the examples and get a feel for how these things work. Don't expect to be able to Google and find a ready-made answer. And especially, don't expect project help (this is school stuff I assume?) wihout your going first. Folks here are only too willing to help, but not going to do the work for you.

Apart from that, you need to be able to write down the process you would follow without any code. You talk of an object "incoming".... so how would you monitor that manually assuming you had a ruler and a clock? Get that clear, before coding.

Thank you mister. :slight_smile:

i'll try it. but im beginner

i have a question.
there is possible to connect these 3 devices ??

jheiyomura:
i have a question.
there is possible to connect these 3 devices ??

Yes, but normally you have to reduce the polling rate of each to give echoes from other sensors time to die away, before initiating a new measurement.

Thank you guys :slight_smile: i have some codes here

but i want to insert my RED LED and buzzer. :slight_smile: but i dont know how to input the codes here:

i want to see if an object is incoming to sensor and then the red led will be light and the buzzer will be alert :slight_smile:

const int trigPin = 2;
const int echoPin = 4;

void setup() {
// initialize serial communication:
Serial.begin(9600);
}

void loop()
{
// establish variables for duration of the ping,
// and the distance result in inches and centimeters:
long duration, inches, cm;

// The sensor is triggered by a HIGH pulse of 10 or more microseconds.
// Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
pinMode(trigPin, OUTPUT);
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);

// Read the signal from the sensor: a HIGH pulse whose
// duration is the time (in microseconds) from the sending
// of the ping to the reception of its echo off of an object.
pinMode(echoPin, INPUT);
duration = pulseIn(echoPin, HIGH);

// convert the time into a distance
inches = microsecondsToInches(duration);
cm = microsecondsToCentimeters(duration);

Serial.print(inches);
Serial.print("in, ");
Serial.print(cm);
Serial.print("cm");
Serial.println();

delay(100);
}

long microsecondsToInches(long microseconds)
{
// According to Parallax's datasheet for the PING))), there are
// 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
// second). This gives the distance travelled by the ping, outbound
// and return, so we divide by 2 to get the distance of the obstacle.
// See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
return microseconds / 74 / 2;
}

long microsecondsToCentimeters(long microseconds)
{
// The speed of sound is 340 m/s or 29 microseconds per centimeter.
// The ping travels out and back, so to find the distance of the
// object we take half of the distance travelled.
return microseconds / 29 / 2;
}