Skip to content

Arduino Uno HCSR04 RGB: Distance readings with LEDs | ShillehTek

October 22, 2023

Video Tutorial (Optional)

Watch first if you want to follow the full build in real time.

Project Overview

In this project, you connect an Arduino UNO to an HCSR04 ultrasonic distance sensor with built-in RGB (NeoPixel) LEDs to measure distance (up to about 4 meters) and display LED effects while printing distance readings to the Serial Monitor.

The ultrasonic distance portion is compatible with common HCSR04 libraries and patterns, and the onboard RGB LEDs can be controlled with a simple LED library.

  • Time: 20 to 40 minutes
  • Skill level: Beginner
  • What you will build: An Arduino UNO distance meter that prints centimeters over Serial and drives the sensor’s 6 built-in RGB LEDs

Parts List

From ShillehTek

  • Breadboard - quick solderless wiring for the Arduino UNO and sensor
  • Jumper wires - connect sensor pins to Arduino UNO headers

External

  • Arduino UNO
  • HCSR04 ultrasonic sensor with built-in RGB LEDs (NeoPixel style) - Amazon Link
  • USB cable for Arduino UNO
  • Arduino IDE

Note: Power this sensor from 5V (not 3.3V) as shown in the wiring step.

Step-by-Step Guide

Step 1 - Wire the HCSR04 RGB sensor to the Arduino UNO

Goal: Make the correct 5V power and signal connections so the Arduino UNO can read distance and control the RGB LEDs.

What to do: Connect the 5 sensor pins as shown in the diagram and pin list below.

Arduino UNO wired to an HCSR04 ultrasonic sensor with built-in RGB LED module showing the pin connections diagram
Wiring diagram for Arduino UNO to HCSR04 RGB ultrasonic sensor.
  • Umbrella (GND), labeled black: to GND on the Arduino UNO (0V ground)
  • Power (5V), labeled red: to 5V on the Arduino UNO (do not use 3.3V)
  • RGB-IN, labeled blue: to digital pin 2 (controls the 6 onboard LEDs)
  • EC (echo), labeled orange: to digital pin 10
  • TR (trigger), labeled yellow: to digital pin 9

Expected result: The sensor is powered from 5V and ready for code upload, with RGB-IN on pin 2, TR on pin 9, and EC on pin 10.

Step 2 - Add the LED library files and upload the Arduino sketch

Goal: Compile and run a sketch that controls the sensor’s built-in RGB LEDs and prints ultrasonic distance readings (cm) to the Serial Monitor.

What to do: In the Arduino IDE, create a new sketch, then add these library files from GitHub (download them and use Sketch > Add File...).

Create another file in your sketch with the following content (this is the main Arduino program). Upload it to your Arduino UNO, then open the Serial Monitor at 9600 baud.

Code:

#include "RGBLed.h"
// Set the pin nr wired to the ultrasonic sensor's pin marked "RGB" or "RGB_IN":
const int RGBpin = 2;
const int trigPin = 9;
const int echoPin = 10;
const long RED    = 0xFF0000;
const long ORANGE = 0xFF8800;
const long YELLOW = 0xFFFF00;
const long GREEN  = 0x00FF00;
const long BLUE   = 0x0000FF;
const long PURPLE = 0xFF00FF;
// Put the colours in an array:
const long RAINBOW[6] = {RED, ORANGE, YELLOW, GREEN, BLUE, PURPLE};
int index = 0;
// The ultrasonic sensor has 6 built-in leds in total: 3 in each cylinder.
RGBLed ultrasonicRGB(RGBpin, 6);
// defines variables
long duration;
int distance;
void setup() {
// Set the 3 leds in the first cylinder to one colour,
// passing values from 0 to 255 for red, green, and blue.
for(int ledNr = 1; ledNr <= 3; ledNr++) {
ultrasonicRGB.setColor(ledNr, 200, 255, 0);  //yellow
}
// Set the leds in the other cylinder individually:
ultrasonicRGB.setColor(4, 255, 0, 0);  //red
ultrasonicRGB.setColor(5, 0, 255, 0);  //green
// Alternatively you can pass a hex colour value:
ultrasonicRGB.setColor(6, 0x0000FF);  //blue
// Apply the changes:
ultrasonicRGB.show();
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
Serial.begin(9600); // Starts the serial communication
}
void loop() {
// // Shift the rainbow array's index across [0] to [5], red to purple:
// index += 1;
// if(index > 2) {index = 0;}
// // Set the colours for all leds, shifting at each loop:
// for(int ledNr = 1; ledNr <= 6; ledNr++) {
// int colourNr = (index + ledNr % 2) % 6;
// ultrasonicRGB.setColor(ledNr, RAINBOW[colourNr]);
// }
// ultrasonicRGB.show();
// // Change the colours with a certain interval.
// // The smaller the delay, the faster the animation.
// delay(150);
// 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.0343 / 2;
// Prints the distance on the Serial Monitor
Serial.print("Distance: ");
Serial.println(distance);
}

You can uncomment the rainbow code inside loop() for a rainbow LED effect. For more details, watch the video above.

Expected result: The built-in LEDs turn on as configured in setup(), and the Serial Monitor prints Distance: <value> readings in centimeters.

Conclusion

You now have an Arduino UNO reading distance from an HCSR04 ultrasonic sensor with built-in RGB LEDs, printing the distance in centimeters over Serial, while also driving the onboard LED colors (with an optional rainbow effect).

Want the parts for this build? Grab your prototyping essentials from ShillehTek.com. If you want help customizing this project for your product or demo, check out our IoT consulting services.