HCSR04 with Builtin LED Module with Arduino UNO

Learn how to connect the new HCSR04 with RGB to your Arduino UNO with some simple steps! This sensor allows you to measure distance using ultrasonic pulses, with capabilities to measure objects up to 4 meters away. It is easy to use the distance measuring capabilities as it is compatible with HCSR04 libraries online. However, the sensor also comes with 6 Neopixel LEDs for an added effect to your projects. These are easily programmable with several libraries online, one of which I will show you in this tutorial.

Where to buy sensor:

Amazon Link

Step 1-) Physical Connection

The sensor comes with 5 pins that you should connect as shown in the diagram:

  • Umbrella (ground), labeled in black, goes to GND on the Arduino UNO, this is the 0V portion of the circuit.
  • Power (5V), labeled red, goes to 5V on the Arduino. This is important, do not connect it with the 3.3V
  • RGB-IN, labeled in blue, goes to pin number 2. This allows you to control the 6 onboard LEDs, two in each barrel.
  • EC (echo), labeled in orange, goes to Pin 10 on the Arduino UNO. Which emits an ultrasonic pulse when triggered.
  • TR (trigger), labeled in yellow, goes in pin 9. This receives the pulse from the trigger.

    Step 2-) Libraries and Code

    • Create a sketch in the Arduino IDE
    • Add the following library files from my Github Page, you can do this by downloading them, and going to Sketch > Add File, in the Arduino IDE.

      https://github.com/shillehbean/youtube-channel/blob/main/RGBLed.cpp

      https://github.com/shillehbean/youtube-channel/blob/main/RGBLed.h

      • Create another file with the following content
      #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);
      }

      This code turns on the LEDs and also prints ultrasonic distance readings in centimeters. Uncomment the code in the loop() for a rainbow effect. For more details please watch the video above and consider subscribing to the channel. One again, if you are interested you can buy the sensor here.

      Create a free account to access full content.

      All access to code and resources on ShillehTek.

      Signup Now

      Already a member? Sign In

      Explore More on Our Blog

      Controlling an LED with a Snap Using the KY-037 Sound Sensor and Raspberry Pi

      Controlling an LED with a Snap Using the KY-037 Sound Sensor and Raspberry Pi

      Discover how to set up, code, and activate the LED based on detected sound with the Raspberry Pi...

      Getting Started with the KY-037 Sound Sensor and Raspberry Pi: Detecting Sound Using Python

      Getting Started with the KY-037 Sound Sensor and Raspberry Pi: Detecting Sound Using Python

      In this tutorial, I’ll guide you through setting up the KY-037 sound sensor with a Raspberry Pi using...

      How to Post to Reddit Using Python

      How to Post to Reddit Using Python

      Post to reddit automatically using a Python script.

      How to Create a Time-Lapse Video with a Raspberry Pi Camera

      How to Create a Time-Lapse Video with a Raspberry Pi Camera

      Learn how to make a timelapse with your Raspberry Pi in Python.

      How to Integrate the MPU6050 with the STM32 Blue Pill

      How to Integrate the MPU6050 with the STM32 Blue Pill

      Learn how to measure acceleration with the STM32 and the MPU6050 in the Arduino IDE.

      Getting Started with STM32 Blue Pill in Arduino IDE Using a USB to TTL Converter — Write Your First Program

      Getting Started with STM32 Blue Pill in Arduino IDE Using a USB to TTL Converter — Write Your First Program

      This comprehensive tutorial will guide you through the process of setting up and programming the STM32 Blue Pill...

      Automate Task Scheduling in AWS with Lambda, Step Functions, and CloudWatch

      Automate Task Scheduling in AWS with Lambda, Step Functions, and CloudWatch

      In this tutorial, I'll show you how to automatically schedule tasks in AWS at regular intervals using AWS...

      Implementing Google reCAPTCHA in a Simple React and Node.js App

      Implementing Google reCAPTCHA in a Simple React and Node.js App

      Learn how to protect your React applications from bots and spam with Google reCAPTCHA integration! This step-by-step tutorial...

      AWS Lambda Tutorial: Using Selenium with Chromedriver in Python

      AWS Lambda Tutorial: Using Selenium with Chromedriver in Python

      In this tutorial, I will guide you through the process of running Selenium with ChromeDriver inside an AWS...

      How to Connect MLX90614 Infrared Thermometer to Raspberry Pi Pico W: MicroPython Tutorial!

      How to Connect MLX90614 Infrared Thermometer to Raspberry Pi Pico W: MicroPython Tutorial!

      Learn how to use the MLX90614 with the Raspberry Pi Pico W and get infrared values in MicroPython.

      Back to blog

      Leave a comment

      Please note, comments need to be approved before they are published.