Important changes to forums and questions
All forums and questions are now archived. To start a new conversation or read the latest updates go to forums.mbed.com.
5 years, 6 months ago.
Write or read from I2C not returning ACK
Hi,
I am working on a STM32L072CZ with an I2C temperature sensor (TC74-A0). The sensor is connected to pin D14 (SDA) and D15 (SCL).
TC74-A0 adress is 1001 000 (datasheet) so 7bit address is 0x48 and 8-bit adress 0x90
But when I try to write address on the bus, it doesn't send ack (write function returns 1).
Here is my code :
const int addr = 0x90; // define the I2C Address for TC74-A0 //Creating the desired objetcs I2C i2c(D14,D15); // sda, scl int main() { int temp = 0; int a,b,c; i2c.start(); // Start condition a = i2c.write(addr); b = i2c.write(0x00); i2c.start(); c= i2c.write(addr|1); temp = i2c.read(0); i2c.stop(); return 0; }
1 Answer
5 years, 6 months ago.
Make sure you have 4k7 pull-up resistors on the sda and clk lines.
I use this to confirm the device can be seen by the MCU,
// I2C - Search for devices on an I2C bus, // - Continuously cycles through each device address, checking for a response // - Prints 8-bit address for each device found // - if the program hangs, the bus isn't working (check pull-up resistors are fitted (4k7 on SDA and CLK)) // - if it doesn't find your device, check power/bus wiring etc #include "mbed.h" Serial pc(USBTX, USBRX); I2C i2c(D14, D15); // sda, clk int main() { // pc.baud(230400); // i2c.frequency(400000); // set required i2c frequency, default 100000 while (1) { pc.printf("\033[0m\033[2J\033[HI2C Searching!\n\n\n"); int count = 0; pc.printf("Starting....\n\n"); for (int address=0; address<256; address+=2) { if (!i2c.write(address, NULL, 0)) { // 0 returned is ok pc.printf("I2C address 0x%02X\n", address); count++; } } pc.printf("\n\n%d devices found\n", count); wait(2); } }
Hi Paul,
thanks for your answer, I tried with two pullup resistor (4.7k) and the output of the program is :
[0m[2J[HI2C Searching! Starting.... 0 devices found [0m[2J[HI2C Searching! Starting....
it's not finding the sensor, I don't know why (I checked wires and it's ok)
posted by 25 Apr 2019It should work, I've just tried testing an i2c device on a NUCLEO-L152RE board.
I assume you have the 3.3v version, the 5v version won't work here, it requires 4v logic levels, and definitely have those 4k7 pull-up resistors from sda and clk to the +3.3v supply?
posted by 25 Apr 2019Yes it's the 3.3V version. I place the first pullup resistor between STM's SDA pin and TC74's SDA pin and the second between STM's SCL pin and TC74's SCL pin.
posted by 26 Apr 2019I think you may have it wrong, the device SDA and CLK pins go directly to the STM mcu and the resistors connect to the +3.3Vcc supply and onto the SDA and CLK lines.
See here:
https://learn.sparkfun.com/tutorials/pull-up-resistors/all
posted by 26 Apr 2019