Arduino: Ultraviolet Sensor Distance Detection with LED

 Arduino Sensor: Ultrasonic Sensor

    What if you had the power to send out a wave of sound, that has such a high frequency that no one can hear it, and detect objects based off of how the sound bounces back? Many organisms in this world have that power, including dolphins and bats. They use the power of echolocation to sense objects from hundreds of yards away, and you can do this too, with the help of an Arduino! You will also need an ultrasonic sensor, which will behave like a bat as it sends high-frequency sound waves at objects, and senses how far it really is. So, lets dive deep into the world of the ultrasonic sensor!




How does this module work?

    There are 4 pins on the HC-SR04(ultraviolet sensor). It is mainly used for object detection and measurement. The first pin is the VCC pin, which is used as the power input for the sensor. The next pin is the Trig pin, short for Trigger Pin. If you set it as high, it will send a 10 microsecond burst of ultraviolet waves in a specific frequency. The Trigger Pin is the connection that connects to one of the 'eyes' of the sensor - the 'eye' that will send out a wave. The Echo Pin will connect to the 'eye' of the sensor that will receive the reflected pulse of energy that the trigger pin sent out. The last pin, the GND pin, short for ground, is responsible for connecting the circuit by taking the positive electrons, and sending the negatively charged electrons back to the Arduino's GND pin. 






What you will need for this project:

  • An Arduino board(I am using an Arduino Uno for this)
  • An ultrasonic sensor(https://tinyurl.com/bdybk8m5 is a link to shop for the sensor)
  • At least 6 male-to-male jumper wires
  • A computer with the Arduino IDE installed on it
  • An LED(light emitting diode)
  • 220 Ohm Resistor



Code for this project:


// defines pins numbers                                              
const int trigPin = 9;                                               
const int echoPin = 10;                                              
const int ledPin = 13;                                               
                                                                     
// defines variables                                                 
long duration;                                                       
int distance;                                                        
int safetyDistance;                                                  
                                                                     
                                                                     
void setup() {                                                       
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output           
pinMode(echoPin, INPUT); // Sets the echoPin as an Input             
pinMode(ledPin, OUTPUT);                                             
Serial.begin(9600); // Starts the serial communication               
}                                                                    
                                                                     
                                                                     
void loop() {                                                        
// Clears the trigPin                                                
digitalWrite(trigPin, LOW);                                          
delayMicroseconds(2);                                                
                                                                     
// Sets the trigPin on HIGH state for 10 micro seconds               
digitalWrite(trigPin, HIGH);                                         
delayMicroseconds(10);                                               
digitalWrite(trigPin, LOW);                                          
                                                                     
// Reads the echoPin, returns the sound wave travel time in          microseconds                                                         
duration = pulseIn(echoPin, HIGH);                                   
                                                                     
// Calculating the distance                                          
distance= duration*0.034/2;                                          
                                                                     
safetyDistance = distance;                                           
if (safetyDistance <= 5){                                            
  digitalWrite(ledPin, HIGH);                                        
}                                                                    
else{                                                                
  digitalWrite(ledPin, LOW);                                         
}                                                                    
                                                                     
// Prints the distance on the Serial Monitor                         
Serial.print("Distance: ");                                          
Serial.println(distance);                                            
}                                                                    
This code enables the LED to shine after a certain distance, in this case: 5. You can copy-paste this code onto the Arduino IDE, and make changes there. For example, you can change the delay, the distance any object will be detected, or the pins that you use.



How to wire the Arduino


  1. You get your ultrasonic sensor, the HC-SR04, and attach it to the breadboard.
  2. The Ultrasonic sensor has 4 pins, the VCC pin, the GND pin, the Trig pin, and the echo pin.
  3. Have a wire going from the Arduino's 5V pin connecting to the VCC pin.
  4. Have a wire going from the Arduino's GND pin to the sensor's GND pin.
  5. Connect the Arduino's I/O 9 pin to the sensor's Trig pin.
  6. Have the Arduino's I/O 10 pin to the sensor's Echo pin.
  7. Now, to connect the LED that will alert us -- connect the LED onto the breadboard, away from the Ultraviolet Sensor.
  8. connect your resistor to the longer leg of the LED (+)
  9. Have a wire going from the other end of the resistor to the Arduino's pin 13
  10. Connect your LED's shorter pin(-) to the Arduino's GND pin.
  11. Connect the A-type USB to the computer and to your Arduino
  12. Upload the code on the IDE.
  13. Watch your program run!                                                                                                             
This blog and experiment is inspired from Arduinomodules.info  and written by Avviin Chandrasekaran, 8th Grade Student at Kennedy Middle School, Cupertino
California.

Avviin is interested in learning Science and computer concepts and programming.
He was doing Scratch programming from 7 year old and made multiple Youtube
Videos.






Comments