Sonar Alarm



Introduction: Sonar Alarm

This is a small project that took a suprising amount of time to put together but here it is.

I will not go into detail on how to make it look just as you in the picture, because i simply cant be botherd to do so.

Its just a wooden box with holes where i put the arduino in.

What can you espect??

If you follow this tutorial you will have a sonar detector that will light an led when you get too close, and a rotor where you can place the sonar ontop to move it left or right with the buttons. You can also press the third button to let the rotor spin 180 degrees.

Without further a do lets start buidling.


3: buttons

1: led

1: breadboard

1: sonar detector

1: arduino rotor

1: arduino

Lots of cables

1: 255 resistor

1: brain

Step 1: Step 1

Place your buttons on the board

Step 2: Step 2

Connect the top right part of each button with a cable and connect that cable with the blue section of the board.

put one wire on the 5v hole of the arduino, and connect that with the top red section of the board.

Put one wire on the GND hole of the arduino, and connect that with the top blue section of the board.

Step 3: Step 3

connect the red cable of your rotor to the red section of your board, and connect the black/brown cable of the rotor with the blue section of your board.

Now connect the yellow/orange cable of your rotor to hole 10 of your arduino.

Step 4: Step 4

Connect the red cable of your sensor to the red section of your arduino, do the same with the black cable but put it in the blue section instead. Now connect the trigger cable with hole 13 of your arduino and echopin to hole 9.

Step 5: Step 5

Place a led on the board and connect the smal end of its legs with a cable to the blue section of the board, and place the resitor ajasond to the long leg of the led light, then connect that part with a cable and connect that cable to hole 3 on your arduino.

Step 6: Step 6

Connect the red cable of your buzzer with hole 3 of your arduino, and connect the blac kcable with the blue section of your board.

Step 7: Step 7

Good job! now just copy paste the code bellow and you're done hurray.

#include // add servo library
#define sw1_pin 6 #define sw2_pin 5 #define sw3_pin 11

Servo myservo; // create servo object to control a servo

volatile boolean sw1 = false; volatile boolean sw2 = false; volatile boolean sw3 = false;

uint8_t sw1ButtonState = 0; uint8_t sw2ButtonState = 0; uint8_t sw3ButtonState = 0;

uint8_t lastsw1ButtonState = 0; uint8_t lastsw2ButtonState = 0; uint8_t lastsw3ButtonState = 0;

int pos1 = 0;

const int trigPin = 13; const int echoPin = 9;

const int buzzer = 3;

const int ledPin = 12;

long duration; int distance; int safetyDistance;

void setup() { Serial.begin(9600); //Sonar pinMode(trigPin, OUTPUT); pinMode(echoPin, INPUT);

//Buzzer pinMode(buzzer, OUTPUT); pinMode(ledPin, OUTPUT);

//Rotor pinMode(sw1_pin, INPUT_PULLUP); pinMode(sw2_pin, INPUT_PULLUP); pinMode(sw3_pin, INPUT_PULLUP); myservo.attach(10); // attaches the servo on pin 9 to the servo object }

void loop() {

digitalWrite(trigPin, LOW); delayMicroseconds(10);

digitalWrite(trigPin, HIGH); delayMicroseconds(10); digitalWrite(trigPin, LOW);

duration = pulseIn(echoPin, HIGH); distance = duration*0.034/2;

Serial.print("Distance: "); Serial.println(distance);

safetyDistance = distance; if (safetyDistance <= 25) { digitalWrite(buzzer, HIGH); Serial.print("Close"); digitalWrite(ledPin, HIGH); }else{ digitalWrite(buzzer, LOW); digitalWrite(ledPin, LOW); }

checkIfSw1ButtonIsPressed(); checkIfSw2ButtonIsPressed(); checkIfSw3ButtonIsPressed();

if( sw1){ Serial.println("sw1"); sw1 = false; sw3 = false; myservo.write(20); delay(15); } if( sw2){ Serial.println("sw2"); sw2 = false; sw3 = false; myservo.write(170); delay(15); }

if( sw3){ Serial.println("sw3"); sw3 = false; sw1 = false; safetyDistance = 50; for (pos1 = 0; pos1 < 180; pos1 += 1) { myservo.write(pos1);

delay(10); } for (pos1 = 180; pos1 > 1; pos1 -= 1) { myservo.write(pos1);

delay(10); }

} } //end update

//int calculateDistance(){ // // digitalWrite(trigPin, LOW); // delayMicroseconds(2); // // Sets the trigPin on HIGH state for 10 micro seconds // digitalWrite(trigPin, HIGH); // delayMicroseconds(10); // digitalWrite(trigPin, LOW); // duration = pulseIn(echoPin, HIGH); // Reads the echoPin, returns the sound wave travel time in microseconds // distance= duration*0.034/2; // return distance; //}

void checkIfSw1ButtonIsPressed() { sw1ButtonState = digitalRead(sw1_pin); if (sw1ButtonState != lastsw1ButtonState) { if ( sw1ButtonState == 0) { sw1=true; } delay(50); } lastsw1ButtonState = sw1ButtonState; }

void checkIfSw2ButtonIsPressed() { sw2ButtonState = digitalRead(sw2_pin); if (sw2ButtonState != lastsw2ButtonState) { if ( sw2ButtonState == 0) { sw2=true; } delay(50); } lastsw2ButtonState = sw2ButtonState; }

void checkIfSw3ButtonIsPressed() { sw3ButtonState = digitalRead(sw3_pin); if (sw3ButtonState != lastsw3ButtonState) { if ( sw3ButtonState == 0) { sw3=true; } delay(50); } lastsw3ButtonState = sw3ButtonState; }



    • Spotless Contest

      Spotless Contest
    • Space Challenge

      Space Challenge
    • Science of Cooking

      Science of Cooking

    We have a be nice policy.
    Please be positive and constructive.