Sensor , I2C,1 master and 2 Slave , display problem

Hi All,

First, I hope to be in the right section of the forum

I have a simple question but i can't understand and solve it.

I have made a small assembly that uses the I2C protocol. In this one, there are two STM32F103C8T6 boards which are slaves and one BLE nano Atmega board which is the master and i use a sensor which is connected to the Atmega board. I have named each STM32: MCU_1 and MCU_2.

I use a mobile phone with an application that i made on Inventor2 to display on the screen the status of each board and the sensor.

My problem is :

For example, when I disconnect pin B6 of the MCU_2 board, the phone displays MCU2= OFF if I reconnect it the phone displays MCU2= ON . The same with MCU1, i think you have understood the reasoning.

But my problem is here:

If MCU1 & MCU2 are connected and i disconnect the signal or the ground of the sensor, the phone displays MCU1 & MCU2 = Off !!

My goal is that, if the sensor is disconnected ant that both MCU are connected , the phone displays MCU1 & MCU2 = On, and sensor = Off.

I can't understand what the problem is. Is it my code in the Atmega board, or the I2C system or the application that i made with Inventor2.

My Atmega code :

#include <Wire.h>
#define SENSOR_PIN 10
#define SLAVE 4 // STM32F103C8T6 = MCU1
#define SLAV 6  // STM32F103C8T6 = MCU2
int refreshDelay = 0, VInx = 0.0, Venx = 0, save, COMMA, COMMB, comm, reset;
String coma = "no";
String comb = "no";
unsigned int counttick = 0;

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

void(* resetFunc) (void) = 0;  // declare reset fuction at address 0

void loop() {
  unsigned long highTime = pulseIn(SENSOR_PIN, HIGH);
  unsigned long lowTime = pulseIn(SENSOR_PIN, LOW);
  unsigned long pulsetime = highTime + lowTime;

   Wire.requestFrom(SLAVE, 1);
   volatile int COMMA = Wire.read();
   Wire.requestFrom(SLAV, 1);
   volatile int COMMB = Wire.read();
   delay(100);
   volatile int comm = COMMA + COMMB;

  Serial.print(comm);
  if(comm == 48){
    coma = "yes";
    comb = "no";
    delay(100);
  }
  else if(comm == 51){
    coma = "no";
    comb = "yen";
    delay(100);
  }
    else if(comm == -2){
    coma = "no";
    comb = "no";
    delay(100);
}else if(comm == 101){
    coma = "yes";
    comb = "yen";
    delay(100);
  }

  if (pulsetime > 20100 || pulsetime < 6400) {
    P_ETH = 60;
    if (counttick >= 2) {
      Serial.print("[");
      Serial.print("=");
      Serial.print(pulsetime);
      Serial.print(",");
      Serial.print("-,");
      Serial.print("-,");
      Serial.print(",");
      Serial.print(VInx, 1);
      Serial.print("]");
    } else if (counttick < 2) {
      counttick++;
    }
    return;
  }
  long Venx = int((1000000 / pulsetime) - 50);
  save = Venx;
  
  if (Venx <= 10) {
    Venx = 65;
   } else if (Venx >= 85) {
    Venx = 85;
  }
  
  float frequency = float(1000000 / pulsetime);
  float dutyCycle = 100 * (highTime / float(lowTime + highTime));
  float totalTime = float(1.0 / frequency);
  float period = float(100 - dutyCycle) * totalTime;
  int temperature = 40.25 * 10 * period - 81.25;
  int temperatureF = temperature * 1.8 + 32;
  if (temperatureF < -39 || temperatureF > 250) {
  return;
  }

  Serial.print("[");
  Serial.print(temperature);
  Serial.print(",");
  Serial.print(Venx);
  Serial.print(",");
  Serial.print(coma);
  Serial.print(",");
  Serial.print(comb);
  Serial.print(",");
  Serial.print(VInx, 1);
  Serial.print("]");
  Serial.println();
  Wire.beginTransmission(SLAVE);
  Wire.write(Venx);
  Wire.endTransmission();
  Wire.beginTransmission(SLAV);
  Wire.write(Venx);
  Wire.endTransmission();

  delay(refreshDelay);
  counttick = 0;
 reset ++;
}

Thank you in advance for your help.

Ps: If you see that my code is not correctly constructed, please also tell me!

1 Like

Wire.read() returns unsigned bytes, comm never can become negative.

None of your local variables really is volatile.

What's the purpose or intention behind breaking wires? If you want to detect the presence of an I2C device use begin/endTransmission as in the i2c_scanner example.

A complete circuit diagram were nice.

It's not clear how you power the devices. 12V is a bit high for the Nano, it will hardly be able to power the other boards.

Thank you DrDiettrich for your interest in my problem.

I will answer you point by point.

It is not me who made the code but a friend of mine. I don't have the skills to do this.

My goal is to detect the presence of one of the devices like MCU1 or MCU2 OR the sensor. If they are connected, the phone will show ON and if one of the devices is not connected, the display of the
If they are connected, the phone will display ON and if one of the devices is not connected, the device display will be off on the phone's display.

Both STM32 boards are powered by a stabilised power supply which provides them with 3.3v. The BLE card is powered by another power supply which provides 12v.

Does this answer your questions?

Thank you for your interest.

How are you getting the information from the nano to the phone? All I see is serial communications.

Nano 33 BLE : https://store.arduino.cc/products/arduino-nano-33-ble
It has a MPM3610 DC/DC-converter (with internal mosfets, inductor, diodes). The 12V is no problem for this board.

The I2C bus has three wires: SDA, SCL, GND.
I don't see that GND in the picture.

To detect the presence of a I2C device, use the I2C scanner as DrDiettrich wrote.
To detect a single I2C device, all you need is this:

Wire.beginTransmission( 0x04);
int error = Wire.endTransmission();
if( error == 0)
  Serial.println( "Okay, found 0x04");
else
  Serial.println( "I even looked under the carpet, but could not find it");

The Nano 33 BLE has its own Category : https://forum.arduino.cc/c/hardware/nano-family/nano-33-ble/159
It is possible to change the category of a topic that you started.

Are you sure that those STM boards work in I2C Slave mode ? Can you find an example that I can look at ?

The picture of the "RF-Nano" is confusing, please don't show something that you don't have.
Note to everyone else, please don't call this a "Nano" board. Call it a "PurpleButterfly" or "I_Like_Cake" board, but not a "Nano".