2

I am trying to read data from the registers 6, 7, 8 and 9 of the STC3100 (Data Sheet) using an arduino uno.

I followed this tutorial: https://howtomechatronics.com/tutorials/arduino/how-i2c-communication-works-and-how-to-use-it-with-arduino/ And adjusted the device and register adress accordingly.

This isn't working and instead of returning the data on the register I'm just getting 16 over and over again.

Here is my code:

#include <Wire.h>
int ADXLAddress = 0x70; 

#define X_Axis_Register_DATAX0 0x08 

#define X_Axis_Register_DATAX1 0x09 

#define Power_Register 0x00 

int X0,X1,X_out;

void setup() {

  Wire.begin(); 

  Serial.begin(9600);
  delay(100);
  Wire.beginTransmission(ADXLAddress);
  Wire.write(Power_Register);
  Wire.write(16);  
  Wire.endTransmission();
}
void loop() {
  delay(2000);
  Wire.beginTransmission(ADXLAddress);  
  
  Wire.write(X_Axis_Register_DATAX0);
  Wire.write(X_Axis_Register_DATAX1);
  
  Wire.endTransmission();

  Wire.requestFrom(ADXLAddress,2); 
  
  if(Wire.available()<=2) {  
    X0 = Wire.read(); 
    X1 = Wire.read();   
  }
  
  Serial.print("X0= ");
  Serial.print(X0, BIN);
  Serial.print("   X1= ");
  Serial.println(X1, BIN);
}
2
  • Please include your code into your question (by editing it)
    – chrisl
    Apr 21 at 12:21
  • I'm not sure, but from reading the data sheet I would expect, that only one Register address must be written, than read the two bytes; the register address will be auto incremented, it seems. You code writes register adress 8, then 9 and I would say, you start reading from reg address 9 then auto increment to 10 (9=battery Voltage High reg 10 = Temperature low reg). The data sheet could also be interpreted as writing more than one register address without reading data, is not specified. Apr 21 at 14:45
1

The following code shows, how I would read from a STC3100. But I own none of them, so I can not try the code. I would be very surprised if it works directly. Use it as an example. Perhaps we could improve it together. ;-)

#include <Wire.h>

#define STC_ADDRESS 0x70
#define MODE_REGISTER 0x00 
#define CURRENT_LOW_REGISTER 0x06 
#define VOLTAGE_LOW_REGISTER 0x08 

int voltage, current;
int high_value = 0;

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

  Wire.begin(); 
  delay(100);
  // power on the STC
  Wire.beginTransmission(STC_ADDRESS);
  Wire.write(MODE_REGISTER);
  Wire.write(16);  
  Wire.endTransmission();
}

void loop() 
{
  Wire.beginTransmission(STC_ADDRESS); 
  Wire.write(VOLTAGE_LOW_REGISTER);
  Wire.requestFrom(STC_ADDRESS, 2); 
  delay(500);
  if( Wire.available() > 0 ) 
  {          
    voltage = Wire.read(); 
    //Serial.print("REG 8 = ");
    //Serial.println(voltage);
  }
  delay(500);
  if( Wire.available() > 0 ) 
  {  
    high_value = Wire.read();
    //Serial.print("REG 9 = ");
    //Serial.println(high_value);
    voltage += (high_value << 8);   
    //Serial.print("voltage = ");
    //Serial.println(voltage);

  }
  Wire.endTransmission();

  Wire.beginTransmission(STC_ADDRESS); 
  Wire.write(CURRENT_LOW_REGISTER);
  Wire.requestFrom(STC_ADDRESS, 2); 
  delay(500);
  if( Wire.available() > 0 ) 
  {  
    current = Wire.read(); 
    //Serial.print("REG 6 = ");
    //Serial.println(current);
  }
  delay(500);
  if( Wire.available() > 0 ) 
  {  
    high_value = Wire.read();   
    //Serial.print("REG 7 = ");
    //Serial.println(high_value);
    current += (high_value << 8);   
    //Serial.print("current = ");
    //Serial.println(current);
  }
  Wire.endTransmission();

  Serial.print("V= ");
  Serial.print(voltage);
  Serial.print("   C= ");
  Serial.println(current);
}
3
  • I tried the code you posted and currently it won't print anything if I run the whole code. If I run it so that it only reads registers 8 and 9, it will print a value (which is incorrect) once and then stop, as if it isn't looping at all.
    – John
    Apr 22 at 13:10
  • I looked over the code and found some errors. I changed the code with the fixed version. I made an error with the first #define macro that should have prevented the code from compiling. How did you compile it? Have you fixed the macro? If there is no hardware error, this version should at least read the registers. I have included some ''Serial.println` you can uncomment, to see the raw register values. If it will stop again, you can see where. Apr 22 at 13:46
  • Seems to be working well at the moment. Thank you so much for your help :)
    – John
    Apr 24 at 13:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.