'Recognizing the need is the primary condition for design.'

Charles Eames

'To design is to communicate clearly by whatever means you can control or master.'
Milton Glaser

Distance Sensor with LEDs and Buzzer

Components Required: 

 

1X Distance / Proximity sensor

1X Passive buzzer

1X Red LED

1X Yellow
1X Green LED

3X 10k resistors

Wiring Instructions:

Use the instructions below with the image gallery to connect the components as follows:

 

 

5V + and - GND power from board to breadboard

1: Connect the 5V+ on the mega board to the + red rail on the breadboard, then connect the GND on the mega board to the - blue rail on the breadboard. 

 

Distance / Proximity sensor
2. Connect the PIR motion sensor's Ecco pin to digital pin 6 of the Arduino Mega then, Connect Trigger pin on the sensor to digital pin 7 of the Arduino Mega, Then connect VCC 5V+ and GND of the sensor to the power + 5V and negative -0V rails on the breadboad. 

 

Buzzer
3. Connect one terminal of the buzzer to digital pin 9 of the Arduino Mega. Connet the other terminal to 5V+ on the breadboard power rail. 

 

LED's
4. Connect the positive (long leg) terminal of each LED to digital pins 2(Green), 3(Yellow), and 4(Red) of the Arduino Mega. .

 

5. Connect the other (short leg) terminal of each LED to GND (ground) of the Arduino Mega, using a resistor in series if required (depending on the LED specifications).

 

Use the code below or in the file at the bottom of the page:

// C++ code
// pin 2 = Green LED, pin 3 = Yellow LED, pin 4 = Red Led, pin 9 = Buzzer
int distanceThreshold = 35;
 
int cm = 0;
 
int inches = 0;
 
long readUltrasonicDistance(int triggerPin, int echoPin)
{
  pinMode(triggerPin, OUTPUT);  // Clear the trigger
  digitalWrite(triggerPin, LOW);
  delayMicroseconds(2);
  // Sets the trigger pin to HIGH state for 10 microseconds
  digitalWrite(triggerPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(triggerPin, LOW);
  pinMode(echoPin, INPUT);
  // Reads the echo pin, and returns the sound wave travel time in microseconds
  return pulseIn(echoPin, HIGH);
}
 
void setup()
{
  Serial.begin(9600);
  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(9, OUTPUT);
}
 
void loop()
{
  // set threshold distance to activate LEDs
  distanceThreshold = 35;
  // measure the ping time in cm
  cm = 0.01723 * readUltrasonicDistance(7, 6);
  // convert to inches by dividing by 2.54
  inches = (cm / 2.54);
  Serial.print(cm);
  Serial.print("cm, ");
  Serial.print(inches);
  Serial.println("in");
  if (cm > distanceThreshold) {
    digitalWrite(2, LOW);
    digitalWrite(3, LOW);
    digitalWrite(4, LOW);
    digitalWrite(9, HIGH);
  }
  if (cm <= distanceThreshold && cm > distanceThreshold - 10) {
    digitalWrite(2, HIGH);
    digitalWrite(3, LOW);
    digitalWrite(4, LOW);
    digitalWrite(9, HIGH);
  }
  if (cm <= distanceThreshold - 10 && cm > distanceThreshold - 25) {
    digitalWrite(2, HIGH);
    digitalWrite(3, HIGH);
    digitalWrite(4, LOW);
    digitalWrite(9, HIGH);
  }
  if (cm <= distanceThreshold - 25 && cm > distanceThreshold - 35) {
    digitalWrite(2, HIGH);
    digitalWrite(3, HIGH);
    digitalWrite(4, HIGH);
    digitalWrite(9, LOW);
  }
  delay(100); // Wait for 100 millisecond(s)
}
You can also use this TXT file to get the code
Code for dist sensor with LEDs and Buzze[...]
Text document [77.0 KB]
Print | Sitemap
© Julian Kupper