Red light reading 3 I2C sensors

Hi,

I’m trying to read 3 sensor via I2C. I hook them with a hub to read the from the same grove pin port.

I’m using the following code, but a red ligh appears, and I don’t know why

Thanks for your help

// This #include statement was automatically added by the Particle IDE.
#include <SHT1x.h>
#include "Adafruit_Sensor.h"
#include "Adafruit_BME280.h"
#include "Adafruit_SI1145.h"
#include "math.h"


// Specify data and clock connections and instantiate SHT1x object
#define dataPin  D0
#define clockPin D1

SHT1x sht1x(dataPin, clockPin);
Adafruit_SI1145 uv = Adafruit_SI1145();

#define BME_SCK D4
#define BME_MISO D3
#define BME_MOSI D2
#define BME_CS D5

#define SEALEVELPRESSURE_HPA (1013.25)

Adafruit_BME280 bme; // I2C

void setup() {
  Serial.begin(9600);

}

void loop() {
    digitalWrite(D0, HIGH);
    digitalWrite(D1, HIGH);
    Serial.print("Temperature = ");
    Serial.print(bme.readTemperature());
    Serial.println(" *C");

    Serial.print("Pressure = ");

    Serial.print(bme.readPressure() / 100.0F);
    Serial.println(" hPa");

    Serial.print("Approx. Altitude = ");
    Serial.print(bme.readAltitude(SEALEVELPRESSURE_HPA));
    Serial.println(" m");

    Serial.print("Humidity = ");
    Serial.print(bme.readHumidity());
    Serial.println(" %");

    Serial.println();
    digitalWrite(D0, LOW);
    digitalWrite(D1, LOW);
    delay(2000);


    digitalWrite(D0, HIGH);
    digitalWrite(D1, HIGH);    
    Serial.println("===================");
    Serial.print("Vis: "); Serial.println(uv.readVisible());
    Serial.print("IR: "); Serial.println(uv.readIR());
    float UVindex = uv.readUV();
    UVindex /= 100.0;  
    Serial.print("UV: ");  Serial.println(UVindex);
    digitalWrite(D0, LOW);
    digitalWrite(D1, LOW);
    delay(2000);
  
    float temp_c;
    float temp_f;
    float humidity;
    
    digitalWrite(D0, HIGH);
    digitalWrite(D1, HIGH);
    // Read values from the sensor
    temp_c = sht1x.readTemperatureC();
    humidity = sht1x.readHumidity();
    Serial.print("temp");   Serial.println(temp_c);
    Serial.print("hum");
    Serial.println(humidity);
    digitalWrite(D0, LOW);
    digitalWrite(D1, LOW);
    delay(5000);
}

What code does the red light blink?

When using I2C you sould not tamper with D0/D1 since these are “owned” by the hardware I2C interface once you executed Wire.begin().
You can’t just mix and match HW I2C with some sort-of SW I2C lookalike.

Also have you made sure the pull-up resistors on the bus are correct?
You need one set of pull-up (2k2 ~ 4k7), but with multiple sensor boards - if each is bringing their own set in parallel - you might get too strong pull-up.

Maybe you start off with the example codes for each sensor individually and if all work fine seperately merge the codes for one extra sensor at the time.

The SHT1x library doesn’t use Wire at all. Could functions like the below be the cause of the problem?

void SHT1x::sendCommandSHT(int _command, int _dataPin, int _clockPin)
{
    int ack;
    
    // Transmission Start
    pinMode(_dataPin, OUTPUT);
    pinMode(_clockPin, OUTPUT);
    digitalWrite(_dataPin, HIGH);
    digitalWrite(_clockPin, HIGH);
    digitalWrite(_dataPin, LOW);
    digitalWrite(_clockPin, LOW);
    digitalWrite(_clockPin, HIGH);
    digitalWrite(_dataPin, HIGH);
    digitalWrite(_clockPin, LOW);
    
    // The command (3 msb are address and must be 000, and last 5 bits are command)
    shiftOut(_dataPin, _clockPin, MSBFIRST, _command);
    
    // Verify we get the correct ack
    digitalWrite(_clockPin, HIGH);
    pinMode(_dataPin, INPUT);
    ack = digitalRead(_dataPin);
    if (ack != LOW) {
    //Serial.println("Ack Error 0");
    }
    digitalWrite(_clockPin, LOW);
    ack = digitalRead(_dataPin);
    if (ack != HIGH) {
    //Serial.println("Ack Error 1");
    }
}

That’s exactly the essence of my post :wink:

Just because the device uses a Grove connector does not mean that it uses I2C and mixing I2C with anything but real/proper I2C devices on the same pins will cause you troubles.

1 Like

As @ScruffR pointed out, you shouldn’t tamper with the D0/D1 lines that are used for the I2C communication. Unfortunately, the SHT1x library also tampers with those lines in a way that’s not compatible with I2C. So, to fix your code, try these steps,

  1. Remove all the digitalWrite() calls to D0 and D1 (What did you think they were for anyway?)
  2. Move the SHT1x sensor to a different pair of pins other than D0 and D1. Change the line, SHT1x sht1x(dataPin, clockPin) to pass the new pins that you’re hooked up to, instead of dataPin and clockPin
  3. Remove the four defines of BME_SCK, BME_MISO, BME_MOSI, and BME_CS. They are only needed if you’re using SPI instead of I2C
3 Likes

Hi all,

Sorry for the delay but I was out of my office during some days.

@Ric I did what you suggest to me and now is working fine the two I2C sensors, the problem arrives when I try to add SHT1x library to the script, when I compile I have the following error

I have no idea how to solve it, thank’s in advance

Eduard
Processing 2sensors.ino
Checking library Adafruit_BME280…
Checking library Adafruit_SI1145…
Installing library Adafruit_SI1145 0.0.1 to lib/Adafruit_SI1145 …
Installing library Adafruit_BME280 1.1.4 to lib/Adafruit_BME280 …
Library Adafruit_SI1145 0.0.1 installed.
Library Adafruit_BME280 1.1.4 installed.
make -C …/modules/electron/user-part all
make[1]: Entering directory '/firmware/modules/electron/user-part’
make -C …/…/…/user
make[2]: Entering directory '/firmware/user’
Building cpp file: src/2sensors.cpp
Invoking: ARM GCC CPP Compiler
mkdir -p …/build/target/user/platform-10-msrc/
arm-none-eabi-gcc -DSTM32_DEVICE -DSTM32F2XX -DPLATFORM_THREADING=1 -DPLATFORM_ID=10 -DPLATFORM_NAME=electron -DUSBD_VID_SPARK=0x2B04 -DUSBD_PID_DFU=0xD00A -DUSBD_PID_CDC=0xC00A -DSPARK_PLATFORM -g3 -gdwarf-2 -Os -mcpu=cortex-m3 -mthumb -DINCLUDE_PLATFORM=1 -DPRODUCT_ID=10 -DPRODUCT_FIRMWARE_VERSION=65535 -DUSE_STDPERIPH_DRIVER -DDFU_BUILD_ENABLE -DSYSTEM_VERSION_STRING=0.6.0 -DRELEASE_BUILD -I./inc -I…/wiring/inc -I…/system/inc -I…/services/inc -I…/communication/src -I…/hal/inc -I…/hal/shared -I/rtos/FreeRTOSv8.2.2/FreeRTOS/Source/include -I/rtos/FreeRTOSv8.2.2/FreeRTOS/Source/portable/GCC/ARM_CM3 -I…/hal/src/electron -I…/hal/src/stm32f2xx -I…/hal/src/stm32 -I…/platform/shared/inc -I…/platform/MCU/STM32F2xx/STM32_USB_Host_Driver/inc -I…/platform/MCU/STM32F2xx/STM32_StdPeriph_Driver/inc -I…/platform/MCU/STM32F2xx/STM32_USB_OTG_Driver/inc -I…/platform/MCU/STM32F2xx/STM32_USB_Device_Driver/inc -I…/platform/MCU/STM32F2xx/SPARK_Firmware_Driver/inc -I…/platform/MCU/shared/STM32/inc -I…/platform/MCU/STM32F2xx/CMSIS/Include -I…/platform/MCU/STM32F2xx/CMSIS/Device/ST/Include -I…/dynalib/inc -Isrc -I./libraries -Isrc -Isrc -Ilib/Adafruit_SI1145/src -Ilib/Adafruit_BME280/src -I. -MD -MP -MF …/build/target/user/platform-10-msrc/2sensors.o.d -ffunction-sections -fdata-sections -Wall -Wno-switch -Wno-error=deprecated-declarations -fmessage-length=0 -fno-strict-aliasing -DSPARK=1 -DPARTICLE=1 -DSTART_DFU_FLASHER_SERIAL_SPEED=14400 -DSTART_YMODEM_FLASHER_SERIAL_SPEED=28800 -DSPARK_PLATFORM_NET=UBLOXSARA -fno-builtin-malloc -fno-builtin-free -fno-builtin-realloc -DLOG_INCLUDE_SOURCE_INFO -DUSE_SPI=SPI -DUSE_CS=A2 -DUSE_SPI=SPI -DUSE_CS=A2 -DUSER_FIRMWARE_IMAGE_SIZE=0x20000 -DUSER_FIRMWARE_IMAGE_LOCATION=0x8080000 -DMODULAR_FIRMWARE=1 -DMODULE_VERSION=4 -DMODULE_FUNCTION=5 -DMODULE_INDEX=1 -DMODULE_DEPENDENCY=4,2,102 -D_GNU_SOURCE -D_WINSOCK_H -DLOG_MODULE_CATEGORY="“app”" -fno-exceptions -fno-rtti -fcheck-new -std=gnu++11 -c -o …/build/target/user/platform-10-msrc/2sensors.o src/2sensors.cpp
2sensors.ino:2:19: fatal error: SHT1x.h: No such file or directory
compilation terminated.
…/build/module.mk:261: recipe for target ‘…/build/target/user/platform-10-msrc/2sensors.o’ failed
make[2]: *** […/build/target/user/platform-10-msrc/2sensors.o] Error 1
make[2]: Leaving directory ‘/firmware/user’
…/…/…/build/recurse.mk:11: recipe for target ‘user’ failed
make[1]: Leaving directory '/firmware/modules/electron/user-part’
make[1]: *** [user] Error 2
…/build/recurse.mk:11: recipe for target ‘modules/electron/user-part’ failed
make: *** [modules/electron/user-part] Error 2

Are you sure you added the SHT1x library to your project? I copied your code, made the corrections I suggested, and added the three libraries to the project, and it compiled just fine (I get the exact same error message that you posted if I delete the SHT1x library from my project).