---
title: "W5100S-EVB-PICO + RUS04 + Adafruit IO in Circuitpython"
url: "https://maker.wiznet.io/mark/projects/w5100s-evb-pico-rus04-adafruit-io-in-circuitpython/"
markdown_url: "https://maker.wiznet.io/mark/projects/w5100s-evb-pico-rus04-adafruit-io-in-circuitpython/md"
type: "WCC: WIZnet Created Content"
author: "Mark Yu"
editor: "WIZnet"
editor_url: "https://maker.wiznet.io/"
original_author: "Mark Yu"
license: "Apache License 2.0 (Apache-2.0)"
published: "2023-07-18"
language: "en"
hardware: ["WIZnet W5100S-EVB-Pico"]
likes: 1
views: 324
comments: 2
source: "WIZnet Makers (https://maker.wiznet.io/)"
---

# W5100S-EVB-PICO + RUS04 + Adafruit IO in Circuitpython

> W5100S-EVB-PICO + RUS04 + Adafruit IO in Circuitpython

Original author: Mark Yu

## Components

- **WIZnet W5100S-EVB-Pico** x 1 ([docs](https://docs.wiznet.io/Product/Chip/Ethernet/W5100S/w5100s-evb-pico))
- Software: **Adafruit Circuitpython** ([docs](https://docs.circuitpython.org/en/latest/README.html))

## Documents and links

- [git](https://github.com/wiznetmaker/W5100S-EVB-PICO-RUS04-CPY) (code)

## Article

## **Story**

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:**

![Structure Diagram for RUS-04 with W5100S-EVB-PICO](https://hackster.imgix.net/uploads/attachments/1611325/mark_ccc_1_3yzDOZbSjr.png?auto=compress%2Cformat&w=740&h=555&fit=max)

Structure Diagram for RUS-04 with W5100S-EVB-PICO

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)

[RUS-04 details](https://emakefun-docs.readthedocs.io/zh_CN/latest/open_source_hardware/rgb/)

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

[HCSR-04 Circuitpython Library](https://github.com/adafruit/Adafruit_CircuitPython_HCSR04/tree/f0eead4d62b48dd5b3e1e18bf0e9bb15f6a104d2)

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)

```plaintext
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)

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

![Results from Thonny - RUS-04](https://hackster.imgix.net/uploads/attachments/1611369/image_5IYCn8WKS0.png?auto=compress%2Cformat&w=740&h=555&fit=max)

Results from Thonny - RUS-04

Codes:

```plaintext
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 IO**

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

![Adafruit IO result](https://hackster.imgix.net/uploads/attachments/1612296/image_Xze0cp8SWZ.png?auto=compress%2Cformat&w=740&h=555&fit=max)

Adafruit IO result

Codes:

```plaintext
#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 control**

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

[Adafruit's Neopixel library](https://github.com/adafruit/Adafruit_CircuitPython_NeoPixel/tree/8211388d13f216a3933e7dc896c1fe054edc7bed)

**Step 5: Result**

I had made a youtube video to demonstrate how to use this module with Adafruit IO

[Embedded media](https://www.youtube.com/embed/YJOkvpCcMmQ?rel=0)

*RUS-04 with W5100S-EVB-PICO to Adafruit IO*

---

Source: https://maker.wiznet.io/mark/projects/w5100s-evb-pico-rus04-adafruit-io-in-circuitpython/
