I have to connect to an Arduino Mega, 9 I2C sensors (in particular humidity and temperature sensors).
In order to connect multiple I2C devices (that have the same I2C address) I purchased a PCA9548A. This should allow us to connect 8 sensors.
As I wrote, I need to connect 9 sensors. In your opinion, is it correct to connect 8 sensors to the PCA9548A and the last one directly to the Arduino? In this way, to read the values, I'll need to use 8 'connections' to the PCA9548A and 1 connection to the last sensor. Does this make sense?
I would like to share this video that I used to take an idea: Usare più display I2C con il Multiplexer TCA9548A - Video 427 - YouTube
(I'm sorry but the language of it is in Italian).
Post a link to the sensor.
If it has an address select pin, a multiplexer is not necessary. Use output pins to set the I2C address such that only one is selected at the default address at any one time.
This is the link to the shop: GY-21 HTU21 moisture and temperature sensor – AZ-Delivery
This should be the link to the datasheet: https://cdn-shop.adafruit.com/datasheets/1899_HTU21D.pdf
Unless you are going to use a 2nd I2C port for the sensor directly connected to the Arduino (either hardware or software I2C), then it could not be on the same I2C bus as the multiplexer. Although you can disconnect all channels of the multiplexer to access the directly connected sensor, you will still be seeing the directly connected sensor whenever you are accessing a sensor through the multiplexer.
The sensor datasheet you linked is not an I2C sensor, and does not have an address. Data transfer is similar to SPI.
The datasheet provided by AZ-delivery is incomplete. Have you got one sensor to work? If so, post the details (code, etc.).
Odd that the website, and the data sheets, refer to it as an I2C device and give the address as 0x40, although I am not familiar enough with I2C to tell if the waveforms comply with I2C standards.
Evidently there are 3 different interfaces available for that sensor, I2C, PWM and SDM.
OP: please double check which version you have and post the details. If you do have the I2C version, then there is no option to manipulate the address, and you would need an I2C multiplexer.
However, I2C was designed for communication between ICs on a single PCB, and is problematic when used with connections longer than a few cm.
If you are thinking of something like a greenhouse, with sensors in various places, expect great difficulties with wiring I2C sensors.
This is the link to my Amazon order: https://www.amazon.it/gp/product/B07VF8Q7G5/ref=ppx_od_dt_b_asin_title_s00?ie=UTF8&psc=1
I'm trying to find the sketch example that I used to try it.
My idea is to connect the multiplexer to Arduino with a cable of a maximum length of 30/40 cm. From the multiplexer to the sensor I'll have a cable with a length around 1mt to 2.5 mt.
About this I read that the Arduino I2C max length distance is 50 feet (15 mts):
RS232
Cable length is one of the most discussed items in RS232 world. The standard has a clear answer, the maximum cable length is 50 feet, or the cable length equal to a capacitance of 2500 pF. The latter rule is often forgotten. This means that using a cable with low capacitance allows you to span longer distances without going beyond the limitations of the standard. If for example UTP CAT-5 cable is used with a typical capacitance of 17 pF/ft, the maximum allowed cable length is 147 feet.
Ref: RS232 Specifications and standard - Lammert Bies 2
Don't confuse RS232 with I2C. Those are completely different protocols, and RS232 would be a vastly better choice.
Even 1 m wires will be problematic with I2C connections. You can read about those problems, and some of the workarounds in many posts on this forum.
There is this excellent intro provided by a forum member: How to make a reliable I2C bus · Koepel/How-to-use-the-Arduino-Wire-library Wiki · GitHub
OK, Thanks. Can you point me on some valid workarounds about it, please?
See above.
My advice is to not use I2C for connections longer that a few cm, and to avoid I2C sensors that do not have address control.
I chose this humidity and temperature sensor for the high reliability detections as also for the very small size of the board. I need to put them inside hives.
I have also tried the DHT22 sensor.. but, in my opinion, this is too big for this scope. Do you know a similar no I2C sensor with small size and good reliability?
Can you tell more about your project ?
Are there motors or mains voltage that give electric noise and nasty peaks ?
If the hives are in field, then the I2C bus can reach 2 meters, and probably more with proper cables and pullup resistors and slow speed.
Are the hives in a row ? How far apart ?
What is the Arduino Mega going to do with the data ? Store it or transmit it ? How is it powered ?
Is it possible to use a small Arduino board for each hive ?
If you wonder how this is done for real, then I see a Arduino board (or small ATtiny microcontroller) for each sensor and a RS-485 connection to a central unit. But maybe we can make it work with less components.
From my experience, the DHT11 is 100% inaccurate and the DHT22 is 40% inaccurate for the humidity ![]()
The HTU21D seems a good sensor. You may assume that you have the I2C version, it is the most common one.
There are software I2C libraries that can create many I2C buses, however your Arduino Mega is a 5V board and the sensors are 3.3V sensors, so you would need a level shifter for each of the sensors and a level shifter weakens the signal, so the maximum length for the I2C bus would be reduced. (I mean when a software I2C library is used for many I2c buses)
Yes, It does.
Since the first 8 sensors addresses are "hidden" within the messages to the PCA9548A which has a default address of 0x70 any messages to your lone sensor at 0x40 will not conflict in any way.
You were quoting this post here
From the same post :
The control message to the multiplexer only sets the multiplexer channel(s) ON/OFF, and has nothing to do with the communications to the sensors, so you will still have a conflict with the direct connected sensor.
The PCA9548A can be used as a level shifter - the maximum voltage passed by the multiplexer is limited by the Vcc, but external pull-up resistors on each I2C bus set the actual HIGH voltage of each bus independently.
I meant with a software I2C library and many I2C buses. The multiplexer can do 8 sensors, a software library can do more. Here are 30 ![]()
This is a prime candidate for a Nano, or something even smaller at each location, and an RS485 bus unless you want to go wireless. Thing is, you need power at each location, so why not just run a 4-wire cable for the RS485 and run 8V on the cable for the Nanos Vin; caveat, you want to be sure your sensors don't want too much power from the Nano 5V rail.
Up to 31 nodes, with very little work; aggregate the data with a Mega if you want to, or just push it to Serial Monitor. Heck, the Mega has 4 serial ports, so you can run 3 strings of 31 nodes, if you want to. That's my view, anyway.
reducing the clock-frequency of the I2C-Bus
using special wires with low capacitance (no I have no idea where to get them from)
using long-distance tranceiver-chips 2 chips for each sensor
probably cheaper to use a very small microcontroller as a I2C-to-serial-interface
https://de.aliexpress.com/item/1005004992682193.html?spm=a2g0o.productlist.main.3.10127192nRVhqg&algo_pvid=80afa4a7-a766-4092-8977-59d4fcfbd5ba&algo_exp_id=80afa4a7-a766-4092-8977-59d4fcfbd5ba-1&pdp_ext_f={"sku_id"%3A"12000031268165370"}&pdp_npi=3%40dis!EUR!1.47!1.29!!!!!%40211beeec16774489510585193d070d!12000031268165370!sea!DE!812311978&curPageLogUid=nbKfZeec06Kx
Thank you for the question.
Let me give you a brief overview about the project and some important notes about it.
First of all I would like to highlight two important aspects: in the remote place where I'll put the system I don't have electric power as also Internet connection.
For this reason I'll use a battery with solar panels in order to solve the power supply problem. About the connection I'll use the LORA protocol.
About these two points I studied a lot and I solved them using:
- An Arduino Solar shield with solar panels and a battery.
- An Arduino LORA shield to send the data to my LORA Gateway.
Let me say that I already tested these two solutions and I can say that I am quite satisfied about the obtained results.
These two main problems, as also the possibility to reduce the costs, are at the base of the project organization that I'll present below: in my apiary I have multiple hives and I chose to use only one central point (with Arduino, solar panels, battery and LORA shield) to monitor multiple hives at the same time.
In a few words, the idea is this: at regular intervals (15 minutes) I'll wake up the Arduino (that otherwise it will be in sleep mode).
For each hive I'll read the sensors values. In particular, for each hive I'll have:
- One humidity and temperature sensor inside the hives.
- I builded a weight scale to take the weight of the hive. About this I used, for each hive, 4 load cells and one HX711 amplifier.
Back to the flow, For each hive I'll retrieve the values that I'll send with LORA (one message for each hive).
Currently, my idea is to connect 8 hives to the single Arduino.
Now, let me give you some information about the components' disposition.
Into the central box, as I said, I'll have an Arduino Mega with 2 shields (solar shield and LORA shield). In order to put Arduino to sleep and wake up it at regular intervals, I'm using a Real Time Clock RTC (DS3231) to receive the wake up interruptions.
As for humidity and temperature sensors, as I asked before, I would like to use the GY-21 HTU21 sensors that, as we discussed, are I2C sensors.
At a 30 cm distance length, I have a box with where I'll have the I2C multiplexer and all the clamps to connect the cables to all hives (weight scales and humidity/temperature sensors).
Just to recap:
Arduino to I2C multiplexer distance: 30 cm
I2C multiplexer to humidity/temperature sensors: around 1m/3m
Arduino (passing by clamps) to HX711: 1m/3m
HX711 to load cells: 20/30 cm