HCSR04 With Built-in RGB LED RUS-04 Module

Background:

The HC-SR04 5V Ultrasonic Sensor is one of the most popular robotic sensors used to measure distance in DIY applications for Raspberry Pi and Arduino. It is very easy to use and has just 4 pins.

I found this new version of the sensor online that is essentially the same thing, except it has an RGB input that allows for a beautiful display of colors within the transmitter and receiver cylinders, containing a total of 6 LEDs (3 per cylinder). This is essentially the same sensor as the RUS-04 (which you can find with a google search).

Using the ultrasonic module was super easy as there are many examples online of how to use it with MicroPython and the Pico W because it works the same as the regular HC-SR04. However, in terms of the RGB module, it took me some digging to figure it out because nobody has used this sensor in MicroPython given that it is relatively new.

After some research, I realized that the only example of this sensor online was this gentleman who demonstrated how to use it with Arduino... but I needed it for MicroPython. I eventually found out it followed the WS2812 lighting standard so I eventually found a library I slightly modified for the Pico W which I link here:

Github Link For NeoPixel Library for Raspberry Pi Pico W

But do not be discouraged if you are not using the Pico or Arduino, WS2812 capable libraries are found all over the internet for different devices and coding languages. It is a pretty popular standard so you should not have issues if you are using other forms of the Raspberry Pi or any other controllers with GPIO capability. Even an amateur hobbyist like me could figure it out, and so could you ;)

I thought I would share the code I used and explain it, which will help people use this low-documented device in the future. I encourage hobbyists to get this sensor for a visual effect.

Other Important Notes:

Like the regular HC-SR04, this uses 5V logic, using the Pico or Pico W, which are 3.3 V devices is not recommended without a 5V power supply and voltage shifters. Otherwise, you can risk damaging your device and also having inconsistencies in your ultrasonic readings. I disregard that for the purpose of simplicity for this tutorial

If you do find this useful please like, comment, and subscribe:

    Supplies:

    1-) An HC-SR04 Module with RGB

    Amazon

    2-) Jumper Wires

    3-) Raspberry Pi Pico or other 5V devices (Raspberry Pis, Arduinos, etc)

    Step 1-) Physical Setup for RGB Module

    1-) Connect 3 pins as follows, this will be enough to get your RGB input powered. I use GPIO28 on the pico but the others should work as well.

    2-) To setup the ultrasonic portion you can view this video or article:

    https://www.youtube.com/watch?v=AwRwlCAQlH4

    https://www.upesy.com/blogs/tutorials/hc-sr04-ultrasonic-sensor-on-rpi-pico-with-micropython-tutorial

    That is all you need in terms of the physical setup

    Step 2-) Code Libraries

    1-) Get the neopixel lib I used and add it to the directory in your device saved as neopixel.py

    https://github.com/shillehbean/youtube-channel/blob/main/neopixel.py

    Based on another example I found online with a small modification

    2-) Add the following code to a file on your Pi:

    https://github.com/shillehbean/youtube-channel/blob/main/rgb_sr04_2.py

    Step 3-) How to Use Code

    There are two primary functions you can call in this code, fun() or change_color_with_distance().

    1-) fun()

    For this, you do not need the trigger or echo pins to set up it just randomly changes the colors of any of the 6 LEDs onboard the device, so it can make for some fun colors to look at.

    Also, this helps you get a better idea of how the lights are triggered. You just need to set the LED of your choice with an RGB value and that is the premise of how the whole thing works at a high level. I demonstrate this above in the photos, you can see the random assortment of colors. I only used 6 for this demonstration but you can play around with RGB values!

    2-) change_color_with_distance()

    This function is a cool way to combine the RGB with the Ultrasonic. If you have your trigger and echo pins set up properly, you can use if statements to change the color based on how far the object is. I demonstrate this in the clip attached.

    It is a nice way to know the distance of an object and can have some practical use cases in terms of the visual aspect of a robot, or help in dark spaces.

    Note that there are other things you can do with the library such as change the brightness and play around with an assortment of colors, and of course, you can create your functions as well. This is just to get you started.

    Hope this was clear, this should be all you need to get started, once again you can get the sensor here if you are interested!

    Let me know if you have any questions or concerns.

    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 a 5V Laser Diode with Raspberry Pi Pico W

    Controlling a 5V Laser Diode with Raspberry Pi Pico W

    Learn how to safely control high-power components like a ShillehTek 5V laser diode using the Raspberry Pi Pico W and...

    How to Connect and Use the HCSR501 PIR Sensor with a Raspberry Pi Pico/Pico W

    How to Connect and Use the HCSR501 PIR Sensor with a Raspberry Pi Pico/Pico W

    Learn how to set up the HCSR501 PIR sensor with a Raspberry Pi Pico to detect motion and trigger...

    Powering the Raspberry Pi Pico W with the MB102 Power Supply

    Powering the Raspberry Pi Pico W with the MB102 Power Supply

    Learn how to power your Raspberry Pi Pico W projects easily and flexibly with the MB102 Power Supply Module...

    How to Use L298N Motor Driver with Pico W

    How to Use L298N Motor Driver with Pico W

    Learn how to use the L298N motor driver to control DC motors with the Raspberry Pi Pico W in MicroPython.

    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...

    Back to blog

    Leave a comment

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