Ultrasonic Motion Sensor code -
https://goo.gl/PCL9Qo
https://goo.gl/PCL9Qo
/*Ultrasonic sensor Pins:
VCC: +5VDC
Trig : Trigger (INPUT) - Pin11
Echo: Echo (OUTPUT) - Pin 13
GND: GND
*/
WE'LL TRY THIS FIRST
#define trigPin 13 #define echoPin 12 #define led 7 void setup() { Serial.begin (9600); pinMode(trigPin, OUTPUT); pinMode(echoPin, INPUT); pinMode(led, OUTPUT); } void loop() { long duration, distance; digitalWrite(trigPin, LOW); delayMicroseconds(2); digitalWrite(trigPin, HIGH); delayMicroseconds(10); digitalWrite(trigPin, LOW); duration = pulseIn(echoPin, HIGH); distance = (duration/2) / 29.1; if (distance < 10) { digitalWrite(led,HIGH); } else { digitalWrite(led,LOW); }
Serial.print(distance); Serial.println(" cm"); delay(500); }
=================================
ALTERNATIVE
// Copy the following and paste into your Arduino software and delete the template provided (the void setup and void loop).
#define trigPin 13 #define echoPin 12 #define led 7 void setup() { Serial.begin (9600); pinMode(trigPin, OUTPUT); pinMode(echoPin, INPUT); pinMode(led, OUTPUT); } void loop() { long duration, distance; digitalWrite(trigPin, LOW); delayMicroseconds(2); digitalWrite(trigPin, HIGH); delayMicroseconds(10); digitalWrite(trigPin, LOW); duration = pulseIn(echoPin, HIGH); distance = (duration/2) / 29.1; if (distance < 10) { digitalWrite(led,HIGH); } else { digitalWrite(led,LOW); }
Serial.print(distance); Serial.println(" cm"); delay(500); }
=================================
ALTERNATIVE
// Copy the following and paste into your Arduino software and delete the template provided (the void setup and void loop).
int trigPin = 11; //Trig - white Jumper
int echoPin = 13; //Echo - yellow Jumper
long duration, cm, inches;
void setup() {
//Serial Port begin
Serial.begin (9600);
//Define inputs and outputs
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}
void loop()
{
// 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:
digitalWrite(trigPin, LOW);
delayMicroseconds(5);
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
cm = (duration/2) / 29.1;
inches = (duration/2) / 74;
Serial.print(inches);
Serial.print("in, ");
Serial.print(cm);
Serial.print("cm");
Serial.println();
delay(250);
}
No comments:
Post a Comment