1

I am a new Arduino IDE user and have a simple question below

For I2C transmission speed setting, there are serial begin() and wire.setClock(). Some sample codes just use Serial.begin() without Wire.seClock(), some use both as shown below. What is the real differences between them? When to use just serial and when to use both of them? Thanks.

void setup() {
  Serial.begin(9600);          // start serial communication at 9600bps

  Wire.begin();                // join i2c bus (address optional for master)

  Wire.setClock(400000); //Increase  I2C clock rate to 400 kHz
}
2

Serial has nothing to do with Wire/I2C communication. The examples only have this, because mostly they use Serial out- and input for interaction in the example codes (like showing the data of an I2C sensor on the Serial Monitor).

If you don't want to use the Serial Montior/Serial communication, then you can leave all the calls to Serial out.

1

Thank you for the answers, however, I am still confused as some sample codes included in the Arduino IDE do not have wire.setClock(), how can it work for I2C communication.

for example: I2C SRF10 or SRF08 Devantech Ultrasonic Ranger Finder

#include <Wire.h>

void setup() {

Wire.begin(); // join i2c bus (address optional for master)

Serial.begin(9600); // start serial communication at 9600bps }

int reading = 0;

void loop() {

// step 1: instruct sensor to read echoes

Wire.beginTransmission(112); // transmit to device #112 (0x70)

// the address specified in the datasheet is 224 (0xE0)

// but i2c adressing uses the high 7 bits so it's 112

Wire.write(byte(0x00)); // sets register pointer to the command register (0x00)

Wire.write(byte(0x50)); // command sensor to measure in "inches" (0x50)

// use 0x51 for centimeters // use 0x52 for ping microseconds

Wire.endTransmission(); // stop transmitting

// step 2: wait for readings to happen delay(70); // datasheet suggests at least 65 milliseconds

// step 3: instruct sensor to return a particular echo reading

Wire.beginTransmission(112); // transmit to device #112

Wire.write(byte(0x02)); // sets register pointer to echo #1 register (0x02)

Wire.endTransmission(); // stop transmitting

// 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

}

delay(250); // wait a bit since people have to read the output :) }

0

Serial.begin starts the communication on USB or RX/TX pins. It has nothing to do with Wire library and I2C communication.

I2C clock speed determines the speed of communication on the I2C bus. Wire.begin() among other settings, sets the I2C clock speed to some default value.

Wire.setClock() allows to set a different clock speed. This should be supported by all devices on the I2C bus or clock speed can be changed before communicating with a specific device on the bus.

Wire.setClock should be called after Wire.begin because Wire.begin sets the clock speed to some default.

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.