Introduction: Arduino Sensor With LED and Buzzer

For this project, we will be making an "alarm" type circuit. This circuit will measure how far away an object is and then set off a warning sign if the object is too close. For the circuit, we will need:

-Ultra Sonic Sensor

-Male to Male jumper wires, about 12

-Arduino Uno

-Green and Red LED

-Active Buzzer

-Two 220ohm resistors

Step 1: Connecting the Ultrasonic Sensor

Place the sensor on the breadboard, this will make it easier to stay stationary and read values. Also notice the four pins on it. The far left is the power, the far right is the ground, and the echo and trig are the two that you will connect to the arduino. The trig goes to pin 10 and echo to pin 11. Also, go ahead and make sure the ground and the power are connected from the arduino to the breadboard.

Step 2: Connecting the LEDs

Place the red and green LEDs on the bread board and make sure you can tell the difference between the cathode and anode. The anode is positive and the cathode is negative. Connect the anode to a 220 ohm resistor and then connect the resistor to pin 6 for red and 4 for green. Connect the cathode to the ground column.

Step 3: Connecting the Buzzer

Dont let the buzzer fool you, the buzzer is smaller and black, not big and red. But for the buzzer, make sure you can tell which end is positive and which is negative. Connect the negative to ground and the Positive to pin 12. This is an active buzzer, so it only receives high and low power.

Step 4: Code

Import the NewPing library, it should be on arduino website. After that, the code is pretty straight forward.

#include
int echoPin = 10; //pin 10 for the echo

int trigPin = 9; //pin 9 for trigger

int buzzPin = 12; //pin 12 for the buzzer

int ledRed = 6; //pin 6 for red LED

int ledGreen = 4; //pin 4 for green LED

int duration, inches, cm; // establish variables for duration of the ping, and the distance result

NewPing sonar(trigPin, echoPin, 200); //sets up the sonar function and limits distance to 200 cm

void setup() {

pinMode(ledRed, OUTPUT);

pinMode(ledGreen, OUTPUT);

pinMode(buzzPin, OUTPUT);

Serial.begin(9600); //sets up serial monitor

}

void loop() {

delay(100);

Serial.print("Ping: ");

Serial.print(sonar.ping_cm());

Serial.println("cm");

//warning

if(sonar.ping_cm()<10)

{

digitalWrite(ledRed, HIGH);

digitalWrite(ledGreen, LOW);

digitalWrite(buzzPin, HIGH);

}

//no warning

if(sonar.ping_cm()>=10)

{

digitalWrite(ledGreen, HIGH);

digitalWrite(ledRed, LOW);

digitalWrite(buzzPin, LOW);

}

}//end loop

Step 5: Warning

To make sure all is working, check the positive and negative ends to see that all are connected right. Also the code will not work if the NewPing library is not installed.