A Diffrent Alarm- Arduino With Distance Sensor

In this tutorial I'm going to teach you how to do an alarm with 2 sensors. This alarm clock will only stop beeping when the you get up of bed and turn on the lights. For this alarm clock, it will be necessary 1 arduino, 2 breadboards( can be 1), 1 distance sensor, 1 buzzer, 1 LED, 1 light sensor, 2 resistors, a USB cable and some wires.

The HC-SR04 Ultrasonic sensor has 4 pins: Vcc, GND, TrigPin and EchoPin. The Vcc and GND need to be connected to 5 volts and the GND pin respectively in the arduino board. The trig must be connected to pin 9 and the echopin must be connected to pin 10. The trigpin emits sound waves at a very high frequency, that we can hear. When those waves hit an object, they come back and are received The EchoPin reads those waves and measures the distance between the object and the sensor. It calculates the distance by measuring the time it took fron the object and come back. After that it divides by 2, because the wave made 2 travels( going and coming back). The buzzer is connected to pin 13 and to GND in the arduino board. The light sensor works by allowing high voltages to pass through it (low resistance), however, when it is dark, a low voltage (high resistance) pass through it, creating light. The light sensor is connected to the resistors, to the LED, and to the AO, in the arduino board. This is the explained code:

<p>// defining variables<br>
int Led = 5;
int lightSensorPin = A0;
int analogValue = 0;
const int trigPin = 9;
const int echoPin = 10;
long duration;
int distance;</p><p>//defining the functions
void checkDistance(){
digitalWrite(trigPin, LOW); //The trigpin sent the wave
digitalWrite(trigPin, HIGH); //The trigpin received the wave.
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);// echopin measures the distance
distance= duration*0.034/2; 
if(distance<30 && distance>15){ // if the distance is smaller than 30 cm and bigger than 15 cm,
  tone(13,2000);//it will buzz in 2000 hertz frequency
else if(distance<14 && distance>1){ //if the distance is smaller than 14cm and bigger than 1 cm,
  tone(13,600); // it will buzz in 600 hertz frequency
   noTone (13);
void light(){
    analogValue = analogRead(lightSensorPin);
    if(analogValue < 400){
      digitalWrite(Led, HIGH); 
      digitalWrite(Led, LOW);
}// preparing the computer and defining outputs and inputs
void setup(){
pinMode(trigPin, OUTPUT); 
pinMode(echoPin, INPUT); 
pinMode(13, OUTPUT);
pinMode(Led, OUTPUT);  
// Loops
void loop(){



    • Tape Contest

      Tape Contest
    • Weaving Challenge

      Weaving Challenge
    • Organization Contest

      Organization Contest