OpenWrt Forum Archive

Topic: Set up SHT2x Temp/Humidity DIR-505 i2c two or more sensors? [SOLVED]

The content of this topic has been archived on 2 Mar 2018. There are no obvious gaps in this topic, but there may still be some posts missing at the end.

First this will start as tutorial for beginners folks here, and later on will turn in to a problem seeking - asking more knowledgeable to help with a problem connecting multiply sensors on a single i2c bus?

We'll use free GPIOs form DIR-505 LED D5 that is not factory installed!

What you’ll need:
DIR-505 second hand works too
Temperature/Humidity SHT20D or SI7021-A10-GMR both the same but second one cheaper.
Two 10kΩ pull-up resistors, 100nF capacitor - I scavenger mine from an old Dell Laptop.
Some wires
Small 40-80W soldering iron with a good tip. Good soldering skills or passion to learn and may be second router wink
Flash your router with Barrier Breaker form stock D-Link firmware using emergency recovery: http://wiki.openwrt.org/toh/d-link/dir-505

Some shoots before we start: https://www.flickr.com/photos/53731093@ … res/85C893

Hardware installation and connections:

What we have on the board: LED D5 not installed. Position next to the system LED D4 two colour green/red installed, under the shield AR1131. Open the top shield carefully and solder 3 wires, check carefully after you are done! 

D5 left pin (next to the RAM chip)  +3.3Vdd - connect to sensor +Vdd pin5
D5 middle pin - Resistor R653 330Ω in series - GPIO18 - SDA connect to sensor pin1
D5 left pin ( led D4 on the left ) - Resistor R92 330Ω in series - GPIO21 -SCL connect ot sensor pin 6
GND connect to sensor Vss pin2
Connect two 10kΩ pull up resistors from GPIO18&21 to +3.3Vdd

If it’s too difficult for you to solder on LED D5 pads, then you can use SW5 mode switch instead. Set the switch to “Repeater" position, and then "Wi-Fi HotSpot” - pin2 is GPIO19 and "Router/AP" - pin5 is GPIO20 and you’ll not need pull-up resistors 10kΩ because they are installed on the board, just connect sensor to +3.3V GND and GPIO19&20 ( SDA&SCL )


Software installation:

I assume you have installed BB on your DIR-505
Connect and go to web management interface http://192.168.1.1
Setup a password once you log in, connect and setup internet connection, in my case I use ethernet wire to connect to the router and setup DIR-505 as WiFi client to connect to my main router.
Optional: If your main router uses the same subnet 192.168.1.1 you'll have to change to a different subnet example: 192.168.10.1 (Network/Interfaces/LAN-Edit change IPv4 address from /192.168.1.1 to  192.168.10.1) before connecting  to your main router.
Then reboot and log in to SSH terminal ssh root@192.168.1.1
Do a “opkg update”
"opkg install  kmod-hwmon-sht21” this will install all dependent packages as well!
"opkg install kmod-i2c-gpio-custom” this will install all dependent packages as well!
we make sure GPIOs are set on boot time: "echo "i2c-gpio-custom.ko bus0=0,18,21" > /etc/modules.d/58-i2c-gpio-custom”
Reboot your router

Make sure you’ve connected your sensor before powering on and continue:
Check if your custom GPIOs are loaded on boot:
"dmesg | grep i2c” output should be similar:
[  508.900000] i2c-gpio i2c-gpio.0: using pins 18 (SDA) and 21 (SCL)

activate your sensor ( from the sht21 driver manual we know that sensor uses 0x40 address):
"echo sht21 0x40 > /sys/bus/i2c/devices/i2c-0/new_device"

Check again with dmesg:
"dmesg | grep i2c” output should be similar:
[   11.620000] i2c-gpio i2c-gpio.0: using pins 18 (SDA) and 21 (SCL)
[ 1178.220000] i2c i2c-0: new_device: Instantiated device sht21 at 0x40

Almost ready! Check:

"cat /sys/bus/i2c/devices/0-0040/temp1_input"
25737

"cat /sys/bus/i2c/devices/0-0040/humidity1_input"
48931

Now is up to you how you’ll read, collect and visulaize this data!


Here comes the problem part and request for help to advanced OpenWRT gurus, this is my first time when I’m connecting anything different than antenna's to my router, so I’m not quite sure what I’m doing, and there are many blank’s to fill up:

I want to connect multiply sensors on one bus but for some reason I can’t initialise second sensor on the same bus.

I’m connecting the second sensor and try to initialise again:

echo sht21 0x40 > /sys/bus/i2c/devices/i2c-0/new_device
ash: write error: Invalid argument

My guess is that this device should be on different address?
But then how I can set-up the address?
I installed i2c-tools, but they are not very helpful because there is no /dev/i2c?

"i2cdetect -r 0"
Error: Could not open file `/dev/i2c-0' or `/dev/i2c/0': No such file or directory

"i2cdetect -r i2c-gpio.0"
Error: I2C bus name doesn't match any bus present!

"i2cdetect -l”  does not output anything

when I try “i2cdump -r 0 0x40 i"
Error: Chip address is not a number!

root@OpenWrt:~# ls  /dev
bus                 mtd4ro              mtdblock7           ttyS11
console             mtd5                mtdblock8           ttyS12
cpu_dma_latency     mtd5ro              network_latency     ttyS13
full                mtd6                network_throughput  ttyS14
kmsg                mtd6ro              null                ttyS15
log                 mtd7                port                ttyS2
mem                 mtd7ro              ppp                 ttyS3
mtd0                mtd8                ptmx                ttyS4
mtd0ro              mtd8ro              pts                 ttyS5
mtd1                mtdblock0           random              ttyS6
mtd1ro              mtdblock1           shm                 ttyS7
mtd2                mtdblock2           tty                 ttyS8
mtd2ro              mtdblock3           ttyATH0             ttyS9
mtd3                mtdblock4           ttyS0               urandom
mtd3ro              mtdblock5           ttyS1               watchdog
mtd4                mtdblock6           ttyS10              zero
root@OpenWrt:~#

Any ideas are welcome?

EDIT: I managed to connect one SHT20D and LM75M on one bus, they are working fine!
LM75 all address pins are connected to GND and address is 0x48, and SHT20 address is 0x40
*Also problem with i2c-tools that was not working was because I had to load the i2c-dev module: "modprobe i2c-dev"
Then I was able to use it:
i2cdetect 0
WARNING! This program can confuse your I2C bus, cause data loss and worse!
I will probe file /dev/i2c-0.
I will probe address range 0x03-0x77.
Continue? [Y/n] y
     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00:          -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: UU -- -- -- -- -- -- -- UU -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- --   

Also installing sensor ipk made the readings look nicer:

root@OpenWrt:~# sensors
sht21-i2c-0-40
Adapter: i2c-gpio0
temp1:        +23.8 C 
humidity1:     45.4 %RH

lm75-i2c-0-48
Adapter: i2c-gpio0
temp1:        +20.5 C  (high = +80.0 C, hyst = +75.0 C)

Thanks again for all that contributed to this!

Regards,

(Last edited by mveplus on 9 Oct 2015, 16:02)

The most common way for address selection of I2C devices is to connect certain address pins to Vcc, GND or let them float. The data sheet will tell what the device provides. Some devices seem to offer the opportunity to configure the device address by writing to a certain register.
In case of this device, only address 0x40 is possible. To operate multiple such devices, you need an I2C switch/hub. For example PCA9548, this will create 8 additional (virtual) I2C busses, so you could use 8 sensors. If that is not enough, you can even cascade these switches up to 7 levels (due to 3 address pins).

MBS wrote:

The most common way for address selection of I2C devices is to connect certain address pins to Vcc, GND or let them float. The data sheet will tell what the device provides. Some devices seem to offer the opportunity to configure the device address by writing to a certain register.
In case of this device, only address 0x40 is possible. To operate multiple such devices, you need an I2C switch/hub. For example PCA9548, this will create 8 additional (virtual) I2C busses, so you could use 8 sensors. If that is not enough, you can even cascade these switches up to 7 levels (due to 3 address pins).


Thanks MBS,

Yes, actually these two sensors has address that are factory programmed, and can't be changed, so either other GPIO's for second one or I2C hub/switch for more than two.
I looked also other temp sensors (LM75) that multiply sensors can be on one bus, because as you said addresses can be changed with external pins, the only thing is that they measure only temperature...

Cheers,

The discussion might have continued from here.