How to Write to and read from I2C registers

It is my first time to use Arduino IDE, I have some C and C++ experiences, however I have 20 years LabVIEW experience., which might helps me to understand IDE quickly. Currently I am working on a Sparkfun RedBoard Qwiic, which is compatible with Arduino IDE. The board uses ATmega328P chip. I have a I2C sensor device, so I just need to use I2C to get data from the sensor device via the Redboard.

The Wire library is a major tool for I2C communication. There is a sample code, SRFRange finder, preloaded in the IDE. It is not clear in reading data from data register. For example, the SRF10 device address is 112 (0x70), the data address is 2 (0x02) for high bit and 3 (0x03) for low bit, total of 16-bit data. the sample code does not specify the data address 0x02 and 0x03 (see the attached full code), how can it read data from the right data registers via the device address 112?

// step 4: request reading from sensor
Wire.requestFrom(112, 2); // request 2 bytes from slave device #112

// step 5: receive reading from sensor
if (2 <= Wire.available()) { // if two bytes were received
reading = Wire.read(); // receive high byte (overwrites previous reading)
reading = reading << 8; // shift high byte to be high 8 bits
reading |= Wire.read(); // receive low byte as lower 8 bits
Serial.println(reading); // print the reading

For my sensor, there are 4 channels, each channel uses 2 of 16-bit registers, making a high resolution 32-bit data. so I need to specify which registers to be read for a channel.

Could some one tell me how to code for reading CH2 from register 0x02 and 0x03 using device register 0x2A (decimal 42)?

Thank you

SRF10Range finder.txt (2.03 KB)

SRF10 Ultra sonic range finder.pdf (245 KB)

Welcome,

Here is a library for this sensor GitHub - neildhar/Arduino_SRF10: Library to use SRF10 sensor with Arduino over I2C

Thanks, I have downloaded the library and will go through it.
Gu

The Arduino Uno Rev3 can use pin 13 as input for any signal.
The Sparkfun Redboard is a little behind, because it has a led at pin 13, without a buffer to the led and the circuit to switch between USB power and barrel jack power is simplified.

When you connect a I2C device to a Arduino board, then the first thing to do is to run a I2C Scanner sketch.

I assume that the library is working, but don't get confused by this line: while(i2cBus->available() < 1);.
That is nonsense-code. Please don't put that in your own sketch.

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