Project 2.1.2: LED and Ultrasonic Sensor Control¶
Description | You will learn how to create a simple circuit using a microcontroller, Two LEDs and an Ultrasonic Sensor. |
---|---|
Use case | Imagine you want to use a sensor to measure the distance of an object and turn on or off 2 LEDs alternatively based on the distance measured using Arduino. |
Components (Things You will need)¶
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
---|---|---|---|---|---|
Building the circuit¶
Things Needed:
- Arduino Uno Board = 1
- Arduino USB Cable = 1
- Breadboard = 1
- Red Jumper Wire = 1
- Black Jumper Wire = 1
- White Jumper Wire = 1
- Blue Jumper Wire = 1
- Green Jumper Wire = 1
- Brown Jumper Wire = 1
- Violet Jumper Wire = 1
- Orange Jumper Wire = 1
- Red LED = 1
- Green LED = 1
Mounting the component on the breadboard¶
Things needed:¶
- Ultrasonic Sensor = 1
- Breadboard =1
- Red LED = 1
Step 1: Take the breadboard and the ultrasonic sensor. Insert the ultrasonic sensor into the horizontal connectors on the breadboard, with the pins facing outwards as shown in the picture below
.
Step 2: Insert the red LED into the horizontal connectors on the breadboard beside the ultrasonic sensor and take note of where the positive pin (long pin) is and where the negative pin (short pin) is as shown in the picture below.
.
Step 3: Insert the LED green into the horizontal connectors on the breadboard on the other side of the ultrasonic sensor and take note of where the positive pin (long pin) is and where the negative pin (short pin) is as shown in the picture below.
.
WIRING THE CIRCUIT¶
Things Needed:¶
- Red male-to-male jumper wire = 1
- Black male-to-male jumper wire = 1
- White male-to-male jumper wire = 1
- Blue male-to-male jumper wire = 1
- Brown male-to-male jumper wire = 1
- Green male-to-male Jumper Wire = 1
- Violet male-to-male Jumper Wire = 1
- Orange male-to-male Jumper Wire = 1
Step 1: Connect one end of the red male-to-male jumper wire to the VCC pin of the Ultrasonic sensor and the other end to the 5V pin on the Arduino Uno board as shown in the picture below.
Step 2: Connect one end of the black male-to-male jumper wire to the GND pin of the Ultrasonic sensor and the other end to the GND pin on the Arduino Uno board as shown in the picture below.
.
Step 3: Connect one end of the Blue male-to-male jumper wire to the Echo pin of the Ultrasonic sensor and the other end to digital pin 12 on the Arduino Uno board as shown in the picture below.
.
Step 4: Connect one end of the white male-to-male jumper wire to the Trig pin of the Ultrasonic sensor and the other end to digital pin 11 on the Arduino Uno board as shown in the picture below.
.
Step 5: Connect one end of the brown male-to-male jumper wire to the negative pin of the LED and the other end to digital pin GND on the Arduino Uno board as shown in the picture below.
.
Step 6: Connect one end of the green male-to-male jumper wire to the positive pin of the LED and the other end to pin 13 on the Arduino Uno board as shown in the picture below.
.
Step 7: Connect one end of the orange male-to-male jumper wire to the negative pin of the LED and the other end to GND pin on the Arduino Uno board as shown in the picture below.
.
Step 8: Connect one end of the violet male-to-male jumper wire to the positive pin of the LED and the other end to pin 10 on the Arduino Uno board as shown in the picture below.
.
PROGRAMMING¶
Step 1: Open your Arduino IDE. See how to set up here: Getting Started.
Step 2: const int Echo = 12;
to define the Echo pin.
.
NB: Make sure you avoid errors when typing. Do not omit any character or symbol especially the bracket {} and semicolons; and place them as you see in the image. The code that comes after the two ash backslashes “//” are called comments. They are not part of the code that will be run, they only explain the lines of code. You can avoid typing them.
Step 3: Type const int Trig = 11;
to define the Trig spin.
.
Step 4: Press ENTER to go to the next line, type const int led = 13;
to define the pin for the led
.
Step 5: Press ENTER to go to the next line, type const int led_2 = 10;
to define the second pin for the led
.
Step 6: Press ENTER to go to the next line, type int distance;
to declare the distance variable.
.
Step 7: Type long duration;
to declare the duration variable.
.
Step 8: Type const int distance_threshold = 20;
to declare the distance threshold variable.
.
Step 9: Inside the curly brackets of the void setup () function, type pinMode(Trig, OUTPUT);
to set the Trig pin as an output.
.
Step 10: Type pinMode(Echo, INPUT);
to set the Echo pin as an input.
.
Step 11: Type Serial.begin (9600);
As shown in the picture below. This is to activate the serial monitor.
.
Step 12: Type pinMode(led, OUTPUT);
As shown in the picture below. This is to activate the serial monitor.
.
Step 13: Type pinMode(led_2, OUTPUT);
As shown in the picture below. This is to activate the serial monitor.
.
Step 14: Inside the curly brackets of the void loop (), which is where you put your code to run repeatedly. Type digitalWrite(Trig, LOW);
As shown in the picture below.
.
Step 15: Type delay (200);
to introduce a delay.
.
Step 16: Type the following code to turn ON the ultrasonic sensor. As shown in the picture below.

Step 17: Type digitalWrite (Trig, LOW);
to turn off the ultrasonic sensor. As shown in the picture below to turn OFF the ultrasonic sensor.
.
Step 18: Type the following code
duration = pulseIn (Echo, HIGH); // to read the duration from the Echo pin. As shown in the picture below.
//This is to take input from the Echo pin.
distance = duration * 0.034 / 2; // to calculate the distance. As shown in the picture below.
.
Step 19: Type the conditional statement to turn on the LED at a distance less than the threshold distance.

Step 20: Type digitalWrite (led_2, HIGH);
.
Step 21: Type
.
Step 22: Type the following code to print the distance value to the serial monitor. As shown in the picture below.
Serial.print (distance); //This code allows the serial monitor to print the values for distance.
Serial.println (“cm”); //Here a unit is given to the distance values.
delay (100);
.
Step 23: Save your code. See the Getting Started section
Step 24: Select the arduino board and port See the Getting Started section:Selecting Arduino Board Type and Uploading your code.
Step 25: Upload your code. See the Getting Started section:Selecting Arduino Board Type and Uploading your code
OBSERVATION¶
Step 1: Click on the serial monitor icon to view the distances being recorded as shown in the picture below.
.
Step 2: Put an object or your hand in front of the ultrasonic sensor and keep moving it away or closer to see the changes in the distances on the serial monitor and the LED turning ON and OFF as per the distances. As shown in the pictures below.
.
.
.
CONCLUSION¶
If you encounter any problems when trying to upload your code to the board, run through your code again to check for any errors or missing lines of code. If you did not encounter any problems and the program ran as expected, Congratulations on a job well done.