1

I have interfaced a height sensor and a weight sensor. The I2C address of the height and weight sensors are 0x29 and 0x2A respectively. I have merged the example of both the sensors.

This is the first time I am working with I2C and need help on this.

Do let me know about any mistakes or any sort of example which can help me to learn coding different I2C addresses, sensors, and interfacing.

#include <Wire.h>
#include "SparkFun_VL53L1X.h"
#include "SparkFun_Qwiic_Scale_NAU7802_Arduino_Library.h"

NAU7802 myScale;
SFEVL53L1X distanceSensor;

//Optional interrupt and shutdown pins.
#define SHUTDOWN_PIN 2
#define INTERRUPT_PIN 3

void setup() {
  Serial.begin(9600);
  Wire.begin(0x2A);
  if (myScale.begin() == false) {
    Serial.println("Scale not detected. Please check wiring. Freezing...");
    while (1);
  }
  Serial.println("Scale detected!");
  
  Wire.begin(0x29);
  if (distanceSensor.begin() != 0) { //Begin returns 0 on a good init
    Serial.println("Sensor failed to begin. Please check wiring. Freezing...");
    while (1) ;
  }
  Serial.println("Sensor online!");
}

// put your setup code here, to run once:

void loop() {
  distanceSensor.startRanging(); //Write configuration bytes to initiate measurement
  while (!distanceSensor.checkForDataReady()) {
    delay(1);
  }
  int distance = distanceSensor.getDistance(); //Get the result of the measurement from the sensor
  distanceSensor.clearInterrupt();
  distanceSensor.stopRanging();

  Serial.print("Distance(mm): ");
  Serial.print(distance);

  float distanceInches = distance * 0.0393701;
  float distanceFeet = distanceInches / 12.0;

  Serial.print("\tDistance(ft): ");
  Serial.print(distanceFeet, 2);

  Serial.println();
  if(myScale.available() == true) {
    long currentReading = myScale.getReading();
    Serial.print("Reading: ");
    Serial.println(currentReading);
  }
}
4
  • Do you have a specific question? Is your code not working? If it’s not working, please tell us what it’s doing wrong and what you expected it to do. There are a lot of tutorials on the web if you just need general information about I2C. Commented Dec 17, 2020 at 16:30
  • Yes the code is not working, I need help whether this is the way to use I2C protocol for two different sensor and if not then how should I proceed Commented Dec 17, 2020 at 16:32
  • What does “not working” mean? Please edit your question to include the output the program generates. Does it detect the devices? Does it not detect them? How are they wired to the CPU? What CPU are you using? You have both arduino-mega and nodemcu tags on your post. We’re not going to guess these things. Commented Dec 17, 2020 at 17:40
  • 2
    You really do need to edit your question to be more specific. The whole point is for people searching with similar problems to find applicable answers. If your title and question are not clear, then the stackexchange web sites may not be a good place for it. Guess, by any chance are your I2C wires more than, say, 6 inches long. I2C was designed to be used to connect devices on the same circuit board. Not to go distances of say a foot or more. Also, did you include pull up resistors on the I2C bus? Commented Dec 17, 2020 at 17:59

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.