Bend labs digital flex sensor 1-axis setI2Caddress

Hello,I use the example code provided by sparkfun to modify the I2C address of the flex sensor, but I currently encounter a problem:

  1. To modify the address of the sensor, it needs to be run many times before the address can be modified.
  2. After I modify the sensor to connect to Bluetooth, the sensor will jump back to the default address and the sensor address cannot be changed.
    Could someone please suggest how I should proceed? Thank you very much!

This is my sensor
image

And this is my use code

 /*
  Reading the one and two axis flex sensors from Bend Labs
  By: Nathan Seidle @ SparkFun Electronics
  Date: March 2nd, 2019
  License: This code is public domain but you buy me a beer if you use this
  and we meet someday (Beerware license).

  This example shows how to change the I2C address of the sensor.

  SparkFun labored with love to create this code. Feel like supporting open
  source? Buy a sensor from SparkFun!
  https://www.sparkfun.com/products/15245 (2-axis sensor)
  https://www.sparkfun.com/products/15244 (1-axis sensor)

  Hardware Connections:
  Use the SparkFun Qwiic Breadboard Jumper (https://www.sparkfun.com/products/14425)
  to connect to the RedBoard Qwiic and the following pins on the ADS:
  SCL: Yellow wire on Qwiic cable
  SDA: Blue
  VCC: Red
  GND: Black

  Single axis pinout: https://cdn.sparkfun.com/assets/9/f/8/2/d/Bendlabs_Single_Axis_Flex_Sensor_Pinout.png
  Dual axis pintout: https://cdn.sparkfun.com/assets/f/f/9/e/6/Bendlabs_Dual_Axis_Flex_Sensor_Pinout.png

  Open the serial monitor at 115200 baud to see the output
*/

#include <Wire.h>
#include "SparkFun_Displacement_Sensor_Arduino_Library.h" // Click here to get the library: http://librarymanager/All#SparkFun_ Displacement_Sensor

ADS myFlexSensor; //Create object of the ADS class

void setup()
{
  Serial.begin(115200);
  while (!Serial)
    ;
  Serial.println(F("SparkFun Displacement Sensor Example"));

  Wire.begin();

  //Scan bus looking for a sensor
  byte currentAddress;
  for (currentAddress = 1; currentAddress < 127; currentAddress++)
  {
    currentAddress = findI2CDevice(currentAddress); //Start scanning at last address
    if (currentAddress == 0)
      break; //No device found!
    if (myFlexSensor.begin(currentAddress) == true)
      break; //Device found!
  }

  if (currentAddress == 0 || currentAddress == 127)
  { 
    Serial.println("No Flex Sensors found on the I2C bus. Freezing...");
    while (1)
      ;
  }

  //Begin communication with sensor at current address
  if (myFlexSensor.begin(currentAddress) == true)
  {
    Serial.print("Flex Sensor found at address 0x");
    Serial.print(currentAddress, HEX);
    Serial.print(" / ");
    Serial.print(currentAddress); //Print decimal
    Serial.println("(decimal)");

    byte newAddress = 0;
    while (1)
    {
      while (Serial.available())
        Serial.read(); //Trash any incoming chars
      Serial.println("Enter the address you'd like to change to in decimal. Valid is 8 to 119.");
      while (Serial.available() == false)
        ; //Wait for user to send character

      newAddress = Serial.parseInt(); //Get decimal address from user
      if (newAddress >= 8 && newAddress <= 119)
        break; //Address is valid
      Serial.println("Invalid address. Please try again.");
    }

    myFlexSensor.setAddress(newAddress); //Change I2C address of this device
    //Valid addresses are 0x08 to 0x77 - 111 possible addresses!
    //Device's I2C address is stored to memory and loaded on each power-on

    delay(300); //Time required for device to record address to EEPROM and re-init its I2C

    if (myFlexSensor.begin(newAddress) == true)
    {
      Serial.print("Address successfully changed to 0x");
      Serial.print(newAddress, HEX);
      Serial.print(" / ");
      Serial.print(newAddress); //Print decimal
      Serial.println("(decimal)");
      Serial.print("Now load another example sketch using .begin(0x");
      Serial.print(newAddress, HEX);
      Serial.println(") to use this Flex Sensor");
      Serial.println("Freezing...");
      while (1)
        ;
    }
  }

  //Something went wrong, begin scanning I2C bus for valid addresses
  Serial.println("Address change failed. Beginning an I2C scan.");
}

void loop()
{
  Serial.println("Scanning...");

  byte found = 0;
  for (byte address = 1; address < 127; address++)
  {
    address = findI2CDevice(address); //Scans bus starting from given address. Returns address of discovered device.

    if (address > 0)
    {
      Serial.print("I2C device found at address 0x");
      if (address < 0x0F)
        Serial.print("0"); //Pretty print
      Serial.print(address, HEX);
      Serial.print(" / ");
      Serial.print(address); //Print decimal
      Serial.println("(decimal)");

      found++;
    }
    else
    {
      if (found == 0)
        Serial.println("No I2C devices found\n");
      break; //Done searching
    }
  }

  delay(5000);
}

//Scans the ICC bus looking for devices
//Start scanning from a given address
byte findI2CDevice(byte startingAddress)
{
  if (startingAddress == 0)
    startingAddress = 1; //Error check

  for (byte address = startingAddress; address < 127; address++)
  {
    Wire.beginTransmission(address);
    byte response = Wire.endTransmission();

    if (response == 0) //A device acknowledged us at this address!
      return (address);
  }

  return (0); //No device found
}

According to this comment in the code, the new I2C address is stored in nonvolatile memory, so the change should be permanent.

    //Valid addresses are 0x08 to 0x77 - 111 possible addresses!
    //Device's I2C address is stored to memory and loaded on each power-on

Perhaps the working code and setup that you forgot to post is incorrect, or the sensor is defective.

Yes, but I have this problem with the new batch of sensors I bought recently, and I have another question, is this sensor easy to break? Every time I use it halfway or upload the bluetooth code to the sensor, suddenly there is no flex sensor, and then it breaks down, but the voltage is not exceeded, and the voltage is also within the range of the module. What is the reason for this? Since I can't find out the reason at present, I use a general arduino UNO board for testing.

Hi, @jendyshao

Link to specs/data would help.

Thanks.. Tom... :smiley: :+1: :coffee: :australia:

Excuse me, can you please explain more clearly? I don't understand, thank you!

Hi,

We need to know the specifications or data off your sensor?
What part number is it?

Can you please tell us your electronics, programming, arduino, hardware experience?

Thanks.. Tom.. :smiley: :+1: :coffee: :australia:

This is the sensor specification I bought from bend labs through mouser:
100080101-01-EVAL

Sensor Datasheet

  1. https://cdn.sparkfun.com/assets/f/9/2/1/5/one_axis_datasheet.pdf

  2. https://www.mouser.tw/datasheet/2/983/bl_eval_quick_start_guide_v_0_1-1602054.pdf

Hi,
Why do you need to change the I2C address?
It looks like you are only connecting one I2C device to the BLE.

Thanks.. Tom.. :smiley: :+1: :coffee: :australia:

Hello,
In fact, I need to use three sensors, so I need to modify the addresses of these three sensors to three different addresses. The three addresses are 18, 30, and 40 respectively.
thanks!

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.