| 
  • If you are citizen of an European Union member nation, you may not use this service unless you are at least 16 years old.

  • You already know Dokkio is an AI-powered assistant to organize & manage your digital files & messages. Very soon, Dokkio will support Outlook as well as One Drive. Check it out today!

View
 

ultrasound_led

Page history last edited by Kristin Lucas 15 years, 6 months ago
Program an ultrasound sensor

 

Program an ultrasound sensor to turn an LED on. When motion is detected within a range of the ultrasound sensor the LED with turn on. Follow this patch to connect a push button to your Arduino board.   
 
Download the diagram and code:  ultrasound_led.pdf
 
 
// Use an ultrasound sensor to turn an LED on. Sensor sends a wave out and then listens for it to bounce back. Stand at different distances from the sensor until you trigger the LED to turn on.
 
int ultraSoundpin = 7;
int ledPin = 13;
unsigned long ultrasoundDuration;
void setup() {
  beginSerial(9600);
}
void loop() {
  // switch pin to output
  pinMode(ultraSoundpin, OUTPUT);
   pinMode(ledPin, OUTPUT);
  // send a low, wait 2 microseconds, send a high then wait 10 microseconds
  digitalWrite(ultraSoundpin, LOW);
  delayMicroseconds(2);
  digitalWrite(ultraSoundpin, HIGH);
  delayMicroseconds(10);
  digitalWrite(ultraSoundpin, LOW);
   // switch pin to input
  pinMode(ultraSoundpin, INPUT);
  // wait for a pulse to come in as high
  ultrasoundDuration = pulseIn(ultraSoundpin, HIGH);
  // output
  Serial.print(ultrasoundDuration);
  Serial.print("\t");
  Serial.print(ultrasoundDuration/58, DEC);
  Serial.print(" cm");
  Serial.println();
  
  if(ultrasoundDuration < 4000){ // if the first sensor works make the LED flash.
  digitalWrite(ledPin, HIGH);
}
 else
{
 digitalWrite(ledPin, LOW);
}
 delay(100);

Comments (0)

You don't have permission to comment on this page.