0

I am trying to read a specific register from a device. In this case I am trying to read the value stored in register 0X1F from slave device 0x08. On the scope I see writes for

ADDRESS_SLAVE(Wire.beginTransmission), ADDRESS_TESTCOMP0 (Wire.write)

and

ADDRESS_TESTCOMP0 (Wire.requestFrom(ADDRESS_TESTCOMP0,8))

but I don't see the register values reading out. Here is my code:

#include <Wire.h>

int ADDRESS_SLAVE = 0X08;
int ADDRESS_TESTCOMP0 = 0X1F;


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

void loop() 
{
   Wire.beginTransmission(byte(ADDRESS_SLAVE));  
   Wire.write(byte(ADDRESS_TESTCOMP0));  
   Wire.endTransmission(byte(ADDRESS_TESTCOMP0)); 

   Wire.requestFrom(ADDRESS_TESTCOMP0,8);    
   char read = Wire.read();
   Serial.println(read);

   delay(1000);
}
6
  • 1
    please format your code as code ..... select the code text and click {} ..... or just indent each line by 4 more spaces
    – jsotola
    Jan 24 '19 at 22:41
  • maybe the arduino is "talking" too fast
    – jsotola
    Jan 24 '19 at 22:47
  • Remove the Wire.setClock. The arduino mega 2560 can not do 3.4MHz. Only 100 kHz and perhaps 400 kHz with short wires. The Wire.endTransmission does not take data as a parameter, only a parameter for a stop condition. The Wire.requestFrom takes the slave address as a parameter, not the register address. The second parameter of the Wire.requestFrom is the number of bytes that you want, since you want just 1 byte, make that 1 and not 8. Which device is it? I hope not a device that runs at 3.3v. Start here: arduino.cc/en/reference/wire and read the reference pages of the functions.
    – Jot
    Jan 24 '19 at 23:17
  • Wire.begin sets default clock. the setClock line is not applied
    – Juraj
    Jan 25 '19 at 5:27
  • Thank you for all the feedback. Code is still not accomplishing what I need it to and maybe I am going about it all wrong. Here is what I am trying to do:
    – fijismart
    Jan 25 '19 at 17:26
1

errors corrected

#include <Wire.h>

byte ADDRESS_SLAVE = 0X08; 
byte REGISTER_XY = 0X1F;
byte READ_LENGTH = 8;

void setup() 
{
 Wire.begin();
 Wire.setClock(400000); // set I2C 'full-speed'
 Serial.begin(9600);
}

void loop() 
{
   Wire.beginTransmission(ADDRESS_SLAVE);  
   Wire.write(REGISTER_XY);  // set register for read
   Wire.endTransmission(false); // false to not release the line

   Wire.requestFrom(ADDRESS_SLAVE,READ_LENGTH); // request bytes from register XY
   byte buff[READ_LENGTH];    
   Wire.readBytes(buff, READ_LENGTH);
   for (int i = 0; i < READ_LENGTH; i++) {
     Serial.println(buff[i], HEX);
   }
   Serial.println();
   delay(1000);
}

if you read only one register of the device, you can setup the device in setup() and in loop() only read the data from the register:

#include <Wire.h>

byte ADDRESS_SLAVE = 0X08; 
byte REGISTER_XY = 0X1F;
byte READ_LENGTH = 8;

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

  Wire.begin();
  Wire.setClock(400000); // set I2C 'full-speed'

  Wire.beginTransmission(ADDRESS_SLAVE);  
  Wire.write(REGISTER_XY);  // set register for read
  Wire.endTransmission();
}

void loop() {
   Wire.requestFrom(ADDRESS_SLAVE,READ_LENGTH); 
   byte buff[READ_LENGTH];    
   Wire.readBytes(buff, READ_LENGTH);
   for (int i = 0; i < READ_LENGTH; i++) {
     Serial.println(buff[i], HEX);
   }
   Serial.println();
   delay(1000);
}
2
  • we don't know if the register address is 8-bit or 16-bit. Perhaps the slave is a microcontroller and needs a delay. The Wire.read does not take parameters, it reads a single byte. You were thinking about Wire.readBytes? We don't know how long the wires are and we don't know what the slave device is or how it behaves.
    – Jot
    Jan 25 '19 at 20:19
  • @Juraj Wow...thank you very much Juraj! That works! It seems like I was pretty far off the mark with what I was trying to do. Do you know of an available reference I can use for these types of problems i.e. text/book?
    – fijismart
    Jan 25 '19 at 23:49

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.