Auto direction control of 485 modules using not-gate (Part3/3)

In part 2, we learned that:

  1. Only a small portion of RSS485 to TTL chips support automatic direction control. The majority of the chips need to be controlled by R̅E̅ and DE pin.
  2. The SP3485 uses much less power than the MAX13487 in a 3.3v setup.
  3. Most of the non-auto direction control modules you can buy, use a not gate as flow control.

Let’s take a closer look at how a not gate managed to control the flow direction in the XY-017 module. And then I modified the schematic a bit for easier understanding.

The “XY-017” module schematic diagram

The TX signal goes through two not-gate and to DI pin. RO data also goes through two not-gate and to RX. The two not-gate could reform the signal and sharpen the edge, especially when we are driving an LED in this module.

The following oscilloscope graph recorded the signal of 74HC04 Pin 10 (which is the also same as pin DI) versus RS485+. The left part is the MCU asking for the holding resistor value and the right part is the reply from the sensor. There is a short voltage vibration during transitioning. The transition has to be located before the reply of the sensor. The sensor takes around 1ms before replying.

For the R̅E̅ and DI, the signal is latched by C4 (20nF ceramic cap). C4 is charged via R7 and discharged via D3 (there is no current flow into Pin 13), so the discharge rate is much faster than the charging rate. When Pin 10 is high, C4 gets charged. When Pin 10 is low, C4 discharges.

Take a closer look at pin 13 and the inverted output pin 12, we see that the 74ch04 inverts the signal when the input is around 1.3V.

The charging and discharge time has to be set up within the range for a correct latching time of R̅E̅ and DE. Or otherwise, the signal will be disturbed. For a too small capacitor, I changed the capacitor from 32nF to 1nF. R̅E̅ and DE changed state when the requesting message is not yet finished.

As a result, the 485+ 485- are pulled to neutral, and the difference between 485+ and 485- becomes zero (bus idling state). Although the sensor can still understand the message and make a reply.

And here is a zoomed-in capture.

If a too large capacitor is chosen, the transition will occur during the sensor reply signal and disturb the message. As a result, the MCU cannot understand the reply. ***In later time, I will switch to a Schmitt trigger inverter to check any improvement***

Auto direction control by transistor

On the other hand, we could use a PNP transistor as a not gate to control the flow.

MCU sending data

When TX is high, R̅E̅ and DE are low, SP3485 enters receiving mode. When TX is 1, the transistor activates, R̅E̅ and DE pull low, SP3485 output enters high-Z mode (high impedance mode), which means, the output is floating. When the output is floating, the bus line B- will be pulled down to ground while A+ pulled up to Vcc. So when TX is 1, A+ line is 1 and B- line is 0.

When TX is 0, the transistor is off, R̅E̅ and DE pull high. When DE is 1 and DI is 0, B- outputs 1, and A+ outputs 0. So when TX is 0, the 485 line is 0.

MCU receiving data

When the MCU is receiving data, the TX line is pulled 1, R̅E̅ and DE are 0, SP3485 enters receiving mode and transceives the 485 data to MCU.

TXD R̅E̅ DEDI(Pull GND)BARo
MCU sending10000(Floating)1(Floating)1
MCU sending 0110100
MCU receiving 10000(Floating) 1(Floating) 1
MCU receiving10001(Floating) 0(Floating) 0

Appendix: power consumption of Sipex SP3485 and Texas Instruments SN74HC04N

The following is using a Sipex SP3485 chip with Texas Instruments SN74HC04N hex inverter. The Sipex SP3485 does not feature a low power shutdown mode. So this module cannot drop to μA level consumption, please consider Sipex SP3481 or MaxLinear SP3485 if you need a low power mode. In this circuit board, all the pull-down and pull-up resistors are changed to 100k to minimize power loss.

Average transmission current: 4.47mA (requesting: 10.8mA, replying:653.7μA)
Average idling current: 182.86μA

However, the drawback of using such a weak pull-up is that the signal will be much noisier. The good news is that RS485 calculate the difference between AB line, as a result, they compensate each other.

USB Wifi dongle for raspberry pi zero w

USB wifi adapters are not very user-friendly for Linux devices. Most of the adapters’ drivers are neither preinstalled nor will be automatically installed for your Linux OS. Fortunately, modern Raspberry Pi (such as Pi 3, 4, and zero w) models come with onboard wifi. Yet you may also find it handy to have additional wifi systems for some projects.

Environment

Hardware: Raspberry Pi Zero W, USB wifi dongle Realtek 8812CU and RTL8188FTV

Kernel: 5.10.17+

OS: Raspbian GNU/Linux 10 (buster)

Finding drivers/ firmwares for your USB dongle

I previously bought a wifi dongle running on the chip Realtek 8821CU and wanted to build a wifi repeater for my raspberry pi zero w. However, there is no easy install package for raspberry pi zero. I have tried several installers following the steps on Github.

(https://github.com/brektrou/rtl8821CU),

(https://github.com/uzh-rpg/rpg_dwa171_wifidongle),

(https://github.com/whitebatman2/rtl8821CU) and

(https://github.com/morrownr/8821cu). None of them works and I suspect that the drivers do not support armv6 CPU which the Pi zero is using. If you are planning to install it on an armv7 device, please give them a try.

Then I decided to give up and bought another dongle running on an RTL 8188 FTV chip and it works after installing the firmware.

Configure your wifi adaptor

Most of the following content is extracted from (https://www.raspberrypi.org/forums/viewtopic.php?t=44044). To sum up,


Connect your wifi USB adaptor and check your current network status by ifconfig -a

pi@raspberrypi:~ $ ifconfig -a
 lo: flags=73  mtu 65536
         inet 127.0.0.1  netmask 255.0.0.0
         inet6 ::1  prefixlen 128  scopeid 0x10
         loop  txqueuelen 1000  (Local Loopback)
         RX packets 0  bytes 0 (0.0 B)
         RX errors 0  dropped 0  overruns 0  frame 0
         TX packets 0  bytes 0 (0.0 B)
         TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
 wlan0: flags=4163  mtu 1500
         inet 192.168.0.187  netmask 255.255.255.0  broadcast 192.168.0.255
         inet6 1234::1234:1234:1234:1234  prefixlen 64  scopeid 0x20

         ether 12:34:56:12:34:56  txqueuelen 1000  (Ethernet)
         RX packets 566  bytes 76882 (75.0 KiB)
         RX errors 0  dropped 0  overruns 0  frame 0
         TX packets 363  bytes 56329 (55.0 KiB)
         TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

If there are multiple wlan such as wlan0 as well as wlan1. Congratulations!! your Pi could recognize your USB adaptor. Usually, wlan0 is your onboard wifi (if your device has one) and the other wlan are your additional wifi systems. If your device does not include onboard wifi, then wlan0 is your external wifi USB dongle.


If wlan1 does not show on ifconfig -a, like me, then run a lsusb.

pi@raspberrypi:~ $ lsusb                                                        Bus 001 Device 002: ID 0bda:f179 Realtek Semiconductor Corp.
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub

Device 002 f179 Realtek Semiconductor Corp is the wifi dongle. So at least the wifi dongle is working. Otherwise, there will be nothing about it.


Then run a lsmod to check whether your kernel modules are currently loaded.

pi@raspberrypi:~ $ lsmod
 Module                  Size  Used by
 aes_arm                16384  1
 aes_generic            40960  1 aes_arm
 cmac                   16384  1
 bnep                   20480  2
 hci_uart               40960  1
.
.
.

For me, there is nothing about my USB adaptor. So it could be concluded that the driver is not installed on my Raspberry Pi Zero W.

The firmware-packages for the more common devices are as follows:
firmware-ralink for Ralink devices (RT5370/RT3070/etc.)
firmware-realtek for Realtek devices (RTL8188CUS/etc.)
firmware-atheros for Atheros devices.

Run apt-get install <firmware-package> to install the firmware on Raspbian (renamed to Raspberry Pi OS).

Unfortunately, my Realtek 8812CU and RTL8818FTV drivers are not included in the firmware package. So I have to go for other options. Luckily, MrEngman (https://www.raspberrypi.org/forums/viewtopic.php?t=241185) wrote a script to determine your USB dongle and kernel version and install your suitable driver. Run the following commands:

$ sudo wget http://downloads.fars-robotics.net/wifi-drivers/install-wifi -O /usr/bin/install-wifi
$ sudo chmod +x /usr/bin/install-wifi
$ sudo install-wifi

I have tried my RTL8188FTV and Realteck 8821CU. The 8188 works, but not the 8821. MrEngman’s script cannot recognize my 8821.

Your wifi module is unrecognised.
**** Unable to identify your wifi module ****

If anyone knows how to install the Realtek 8821CU driver on a raspberry pi zero, please leave a comment.

If MrEngMan’s script could recognize your wifi dongle, it should return something like my 8188:

Your wifi module is Bus 001 Device 003: ID 0bda:f179 Realtek Semiconductor Corp.
And it uses the 8188fu driver.

If your driver has been installed correctly, you should now see your firmware’s kernel module listed on lsmod

pi@raspberrypi:~ $ lsmod
 Module                  Size  Used by
 rtl8188fu            1306624  0
 aes_arm                16384  1
 aes_generic            40960  1 aes_arm
 cmac                   16384  1
 bnep                   20480  2
 hci_uart               40960  1
.
.
.

As well as anadditionl wlan1 on ifconfig -a

pi@raspberrypi:~ $ ifconfig -a
 lo: flags=73  mtu 65536
         inet 127.0.0.1  netmask 255.0.0.0
         inet6 ::1  prefixlen 128  scopeid 0x10
         loop  txqueuelen 1000  (Local Loopback)
         RX packets 0  bytes 0 (0.0 B)
         RX errors 0  dropped 0  overruns 0  frame 0
         TX packets 0  bytes 0 (0.0 B)
         TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
 wlan0: flags=4163  mtu 1500
         inet 192.168.0.187  netmask 255.255.255.0  broadcast 192.168.0.255
         inet6 1234::1234:1234:1234:1234  prefixlen 64  scopeid 0x20

         ether 12:34:56:12:34:56  txqueuelen 1000  (Ethernet)
         RX packets 2026  bytes 875332 (854.8 KiB)
         RX errors 0  dropped 0  overruns 0  frame 0
         TX packets 886  bytes 117991 (115.2 KiB)
         TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
 wlan1: flags=4163  mtu 1500
         inet 192.168.0.179  netmask 255.255.255.0  broadcast 192.168.0.255
         inet6 1234::1234:1234:1234:1234  prefixlen 64  scopeid 0x20

         ether 12:34:56:12:34:56  txqueuelen 1000  (Ethernet)
         RX packets 158  bytes 44279 (43.2 KiB)
         RX errors 0  dropped 5  overruns 0  frame 0
         TX packets 36  bytes 5029 (4.9 KiB)
         TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

As well as iw dev:

pi@raspberrypi:~ $ iw dev
 phy#1
         Interface wlan1
                 ifindex 3
                 wdev 0x100000001
                 addr 12:34:56:12:34:56
                 ssid yourwifissid
                 type managed
                 txpower 12.00 dBm
 phy#0
         Unnamed/non-netdev interface
                 wdev 0x2
                 addr 12:34:56:12:34:56
                 type P2P-device
                 txpower 31.00 dBm
         Interface wlan0
                 ifindex 2
                 wdev 0x1
                 addr 12:34:56:12:34:56
                 ssid yourwifissid
                 type managed
                 channel 6 (2437 MHz), width: 20 MHz, center1: 2437 MHz
                 txpower 31.00 dBm

Reminder

I do not recommend editing /etc/network/interfaces. There is no need to edit the file for a Raspberry Pi Zero running Raspberry Pi OS lite. Rather, do your modification on /etc/wpa_supplicant/wpa_supplicant.conf. If you have installed the driver correctly, the network will automatically pop up in ifconfig -a.