This is a application that I used RUS04 with WIZnet's W5100S-EVB-PICO to communicate with adafruit IO based on circuitpython environment.
Since this is a module that I found from China, thus I need to do some modification to make this application happened.
After doing related research and the help of ChatGPT, I had easily to make it happen.
The followings are the details of my project.
Structure Diagram:From the above image, it showed the connection between the RUS-04 with W5100S-EVB-PICO.
It only required 4 wiring to create this connection.
1. VCC to 5V
2. GND to GNG
3. IO to GPIO 0 (GP0)
4. RGB to GPIO 2 (GP2)
After the connection has been created, I need to explain a bit more about the RUS-04 module.
RUS-04 module is a module based on HCSR-04 to develop a single pin IO control to handle the trigger part and the Echo part of the sonar section.
In HCSR-04, it required to have a trigger pin to provide a trigger signal to the module and it will send out a wave to detect the distance between the object and the module through the echo pin.
Since the required time for trigger pin will not be overlap with the echo pin, this module has combine two pin into single pin in a same operting pattern with HCSR-04. Thus, it reduce a pin with the same performance.
Thus, the extra pin has been created to communicate with the LED's inside the module.
Those LEDs are similiar to Neopixel LED lights, therefore I used neopixel library to control those LEDs.
For details, please refer to the link below. (Chinese)
Step 1: How to modify the Circuitpython library of HCSR-04 to be useful for RUS-04 module?
From the beginning of this story, this application will be used circuitpython.
The best thing of circuitpython is the library of products to easily create different kinds of application and it is good for beginners to develop their solution.
After having some research, I had found the library for HCSR-04 and I had used its python file to have my modification for my project.
From the codes, I found that the codes are mainly based on pulsein module to collect pulses from the sonar module.
By modifying the pins setup of the library, I could easily use this libary to collect information from the sonar module.
Modification: (setup)
def __init__(
self, trigger_pin: Pin, *, timeout: float = 0.1
) -> None: #remove the echo pin during setup
"""
:param trigger_pin: The pin on the microcontroller that's connected to the
``Trig`` pin on the HC-SR04.
:type trig_pin: microcontroller.Pin
:param float timeout: Max seconds to wait for a response from the
sensor before assuming it isn't going to answer. Should *not* be
set to less than 0.05 seconds!
"""
self._timeout = timeout
self.pin = trigger_pin #Save the pin number location
"""
Modification: (collect data from RUS-04)
def _dist_two_wire(self) -> float:
# Trigger section
self._trig = DigitalInOut(self.pin)
self._trig.direction = Direction.OUTPUT
self._trig.value = False
time.sleep(0.000002) # 2 micro seconds 10/1000/1000
self._trig.value = True # Set trig high
time.sleep(0.00001) # 10 micro seconds 10/1000/1000
self._trig.value = False # Set trig low
self._trig.deinit()
# Pulseio section to collect sonar data
pulselen = None
timestamp = time.monotonic()
if _USE_PULSEIO:
echo = PulseIn(self.pin) #pin has been changed to echo mode
while not echo:
# Wait for a pulse
if (time.monotonic() - timestamp) > self._timeout:
echo.pause()
raise RuntimeError("Timed out")
echo.pause()
pulselen = echo[0]
echo.deinit()
Step 2: Simple testing with example codes provided by adafruit library bundle.From the HCSR04 library, it also provideds some examples codes to allow me to test the library.
By removing the echo pin and modify the presentation of the print outs, It has the following result.
Codes:
import time
import board
import adafruit_hcsr04
sonar = adafruit_hcsr04.HCSR04(trigger_pin=board.GP0)
while True:
try:
result = sonar.distance
print(("{} cm").format(result))
except RuntimeError:
print("Retrying!")
pass
time.sleep(0.1)
Step 3: Communicate with Adafruit IOAfter having a confirmation about the codes could work normally with RUS04, I had uploaded this information to Adafruit IO to test the communication with the platform using MQTT protocol.
Codes:
#After connected to adafruit IO and subscribed the correct topic
while True:
io.loop()
try:
result = sonar.distance #collect data from RUS04
print(("{} cm").format(result))
io.publish(Test_feed, str(result)) #Publish to Adafruit IO
except RuntimeError:
print("Retrying!")
pass
time.sleep(3)
Step 4: LED controlAs I mentioned, RUS04 has a extra IO pins for controlling the neopixel lights inside the sonar module.
By using Adafruit IO's neopixel library, I could easily controls those LED lights and change the brightness of the module.
Step 5: ResultI had made a youtube video to demonstrate how to use this module with Adafruit IO
Comments