In this tutorial, I’ll explain how to make a ultrasonic sensor use distance to activate an LED. There are lots of creative uses for this. Read on to explore-
For me, this is a part of a larger project to make a bike path that automatically illuminates when being used. Two of these sensors will be placed across from one another to find the bike within the lane. This will be tested with a single LED here in the tutorial, although the ultimate project could use several, or in our case, an entire strip. This is a great base to a larger project, with lots of applications!
Parts List:
- Arduino Uno
- A HC-SR04 Ultrasonic Sensor
- 2 wires
- 1 LED
- 1 330ohm resistor
Setup:
The board is easy to set up. Here are some images to help you:
Importantly-
– White is Echo- plug this into pin 8
– Brown is Trig- plug this into pin 7
– Black is GND – plug into ground
– Red is VCC – plug into 5V
The Code
The code is short and sweet. You can copy it from here to make a single LED light up, as shown- but it’s way more fun if you connect it to a couple more!
I know there are a lot of people who can explain this sensor better than I can, including most of the internet. I’ll tell you what I do know- the trig sends ultrasonic “pings” out into the air, which hit an object, and echo reads the bounce back. The sensor measures the distance upon its return- and because of this round trip journey, you’ll see in the code that we do a calculation to split this distance in two!
You’ll use the serialĀ monitor to receive information about this distance. Here, I have the sensor tell me when the item being sensed is “Too far, can’t reach,” – but you can easily switch the code to tell you when something is too close.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
//tutorial: using distance to active light //this code uses a HC-SR04 sensor to turn on a LED; start here to learn, and apply to something big! //requires: arduino, HC-SR04 sensor, two wires, a 330 resistor, and an LED- and a knowledge of if/else statements! //see arduino reference pages for more info! //11/15/2017 //first connect your board: //VCC to 5V //GRD to ground //echo to a digital pin (here, 8) //trig to a digital pin (here, 7) //LED to a digital pin (here, 13)- connect this to a resistor, that connects to ground //next you define your variables: trig sends out pulses, and echo receives them. led is our LED pin int trigPin 7 int echoPin 8 int led 13 //turn on the serial monitor so that we can read exact distance //you'll see in the setup that trig is an output (sending pulses) //and echo is an input (receiving them) //led will output light void setup() { Serial.begin (9600); pinMode(trigPin, OUTPUT); pinMode(echoPin, INPUT); pinMode(led, OUTPUT); } //the sensor is triggered by a high pulse of 10 microseconds //setting it on low first helps to create a nice clean initial read! //according to arduino reference, the speed of sound is 29 microseconds per centimeter, //and since the ultrasonic waves goes out and back, to find the distance of the object sensed, //we take half of the distance! that's what the "distance =..." line is doing void loop() { long duration, distance; digitalWrite(trigPin, LOW); delayMicroseconds(2); digitalWrite(trigPin, HIGH); delayMicroseconds(10); digitalWrite(trigPin, LOW); duration = pulseIn(echoPin, HIGH); distance = (duration/2) / 29.1; //this if/else statement makes so that if an object is within this distance, the LED will light! //led, HIGH means on; led, LOW means off //this code includes a situation in which to write a message, which is if the object is farther than 80cm //otherwise, if it's closer than that, it tells us the distance! if (distance <= 80) { digitalWrite(led, HIGH); } else { digitalWrite(led,LOW); } if (distance > 80 || distance <= 0){ Serial.println("Too far, can't reach"); } else { Serial.print(distance); Serial.println(" cm"); } delay(500); } |
This is a good way to get you started on several different projects. Now you have two ways to understand distance: the serial monitor, and a visual signal. In addition, you learned some good basic skills on which to build!