Page 1 of 1

[SOLVED] I2C speed

Posted: Mon Mar 23, 2020 5:35 pm
by blue-man
I want to get some experience with an Blue-Pill and so i am trying to set up a little project.
This project includes to to use an AT24C64 EEPROM and read an BME280 sensor.

First i only connected the EEPROM and it was not found. :shock:
After i connected additionally the BME280 i get
Scanning...
I2C device found at address 0x50 !
I2C device found at address 0x76 !
I have not analyzed the timing or anything else yet and want to ask if maybe someone has an idea why this happens?

My assumption is that it has to do with the I2C bus speed.
But how i can find out which one is used or how it can be configured?

There is this basic documentation https://github.com/stm32duino/wiki/wiki/API#i2C
and I2C-Timing in https://github.com/stm32duino/wiki/wiki ... i2c-timing.

For an blue-pill the variant.h has to be modified in ../arduino15/packages/STM32/hardware/stm32/1.8.0/variants/PILL_F103XX ?

I2C_TIMING_SM for Standard Mode (100kHz)
I2C_TIMING_FM for Fast Mode (400kHz)
I2C_TIMING_FMP for Fast Mode Plus (1000kHz)

Is the lowest speed the standard speed?
At least the speed is not critical - it only has to work and it would be fine if the EEPROM could be used standalone.

Here is the basic code:

Code: Select all

#include <Wire.h>

void scan(void) {
	byte error, address;
	int nDevices;

	Serial.println("Scanning...");
	
	nDevices = 0;
	for(address = 1; address < 127; address++ )
	{
		// The i2c_scanner uses the return value of
		// the Write.endTransmisstion to see if
		// a device did acknowledge to the address.
		Wire.beginTransmission(address);
		error = Wire.endTransmission();
		
		if (error == 0)
		{
			Serial.print("I2C device found at address 0x");
			if (address<16)
				Serial.print("0");
				Serial.print(address,HEX);
				Serial.println("  !");
		
			nDevices++;
		}
		else if (error==4)
		{
			Serial.print("Unknown error at address 0x");
			if (address<16)
				Serial.print("0");
				Serial.println(address,HEX);
		}
		
		digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
	}
	if (nDevices == 0)
		Serial.println("No I2C devices found\n");
	else
		Serial.println("done\n");
}

void setup() {
	Wire.begin(); // initialise the connection
	Serial.begin(115200);
	
	Serial.println("I2C Test");
}

void loop() {
	scan();
	delay(5000);
}
Some hints would be beautiful. :idea:

Re: I2C speed

Posted: Mon Mar 23, 2020 5:40 pm
by blue-man
Sorry - please move this thread to the correct forum - maybe "Libraries & Hardware"

Re: I2C speed

Posted: Mon Mar 23, 2020 5:52 pm
by fpiSTM
You need a Pullup resistor on each I2C line. I guess the BME have it on the hardware.

Re: I2C speed

Posted: Tue Mar 24, 2020 1:13 am
by blue-man
Yes - that's the embarrassing reason. :oops:
Using Arduino modules makes forgetting basing things.

Here it can be seen as R2 + R3 in an RTC module.

What's about using I2C_TIMING_S and I2C_TIMING_FM?

Re: I2C speed

Posted: Tue Mar 24, 2020 6:49 am
by fpiSTM
blue-man wrote: Tue Mar 24, 2020 1:13 am What's about using I2C_TIMING_S and I2C_TIMING_FM?
By default I2C speed is set to 100KHz.
For STM32F1, those definitions are not used as it does not need to be computed.
https://github.com/stm32duino/Arduino_C ... .c#L54-L58

Use this to change the speed:
https://www.arduino.cc/en/Reference/WireSetClock

Re: I2C speed

Posted: Tue Mar 24, 2020 8:42 am
by blue-man
Thank you!

The datasheet says there are two speeds available:
It supports the standard mode (Sm, up to 100 kHz) and Fm mode (Fm, up to 400 kHz).

But for the use of an AT24C64 only max. 100 kHz for 3.3V:
Bidirectional Data Transfer Protocol
100 kHz (1.8V, 2.5V, 2.7V) and 400 kHz (5V) Clock Rate