Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GD32F303CC I2C Scanner not working #133

Open
FearL0rd opened this issue Jan 6, 2025 · 2 comments
Open

GD32F303CC I2C Scanner not working #133

FearL0rd opened this issue Jan 6, 2025 · 2 comments
Assignees
Labels
bug Something isn't working Component: I2C Regarding the I2C component good first issue Good for newcomers

Comments

@FearL0rd
Copy link

FearL0rd commented Jan 6, 2025

Hi,

I am trying to use the i2C scanner with this dev board, but the scanner always returns, "No I2C devices found." But when I decided to try using the STM board, it worked.

Any idea what could be wrong?


// --------------------------------------
// i2c_scanner
//
// Version 1
//    This program (or code that looks like it)
//    can be found in many places.
//    For example on the Arduino.cc forum.
//    The original author is not know.
// Version 2, Juni 2012, Using Arduino 1.0.1
//     Adapted to be as simple as possible by Arduino.cc user Krodal
// Version 3, Feb 26  2013
//    V3 by louarnold
// Version 4, March 3, 2013, Using Arduino 1.0.3
//    by Arduino.cc user Krodal.
//    Changes by louarnold removed.
//    Scanning addresses changed from 0...127 to 1...119,
//    according to the i2c scanner by Nick Gammon
//    http://www.gammon.com.au/forum/?id=10896
// Version 5, March 28, 2013
//    As version 4, but address scans now to 127.
//    A sensor seems to use address 120.
//
// This sketch tests the standard 7-bit addresses
// Devices with higher bit address might not be seen properly.
//



/* Example pinmap for Bluepill I2Cs (by Testato)

 I2C-1 standard pins: PB7(sda) PB6(scl)
 Use it by "Wire" without pin declaration
  Wire.begin();

 I2C-1 alternative pins: PB9(sda) PB8(scl)
 Remap the first I2C before call begin()
  Wire.setSDA(PB9);
  Wire.setSCL(PB8);
  Wire.begin();

 I2C-2: PB11(sda) PB10(scl)
 Remap the second I2C before call begin()
  Wire.setSDA(PB11);
  Wire.setSCL(PB10);
  Wire.begin();

 If you want to use the two I2Cs simultaneously, create a new instance for the second I2C
  TwoWire Wire2(PB11,PB10);
  Wire2.begin();

*/


#include <Wire.h>

  TwoWire Wire2(PB11,PB10,1);

void setup()
{
    delay(10000);
    Serial.begin(9600);
    Wire2.begin();
    Wire.begin();
    Serial.println("\nI2C Scanner");
}


void loop()
{
    byte error, error2, 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.println(address, HEX);

            nDevices++;
        }
        else if (error == 4) {
        Serial.print("Unknown error at address 0x");
        if (address < 16)
          Serial.print("0");
        Serial.println(address, HEX);
        }

        Wire2.beginTransmission(address);
        error2 = Wire2.endTransmission();

        if(error2 == 0) {
            Serial.print("I2C B device found at address 0x");
            if (address < 16)
            Serial.print("0");
            Serial.println(address, HEX);

            nDevices++;
        }
        else if (error2 == 4) {
        Serial.print("Unknown error at address 0x");
        if (address < 16)
          Serial.print("0");
        Serial.println(address, HEX);
        }


    }
    if (nDevices == 0)
    Serial.println("No I2C devices found");
    else
    Serial.println("done");

    delay(5000);           // wait 5 seconds for next scan
}
@maxgerhardt
Copy link
Member

Thanks, I will verify whether this works or not on hardware and debug it.

@maxgerhardt maxgerhardt self-assigned this Jan 7, 2025
@maxgerhardt maxgerhardt added bug Something isn't working good first issue Good for newcomers Component: I2C Regarding the I2C component labels Jan 7, 2025
@FearL0rd
Copy link
Author

FearL0rd commented Jan 7, 2025

Thanks, I will verify whether this works or not on hardware and debug it.

max,

I have added these to Wire.h, and now I can set the ports using ( Wire.setSDA(PB7); Wire.setSCL(PB6);), and everything is working fine.

Can you include those in the build?

		void setSCL(uint32_t scl)
		{
		  _i2c.scl = DIGITAL_TO_PINNAME(scl);
		};
		void setSDA(uint32_t sda)
		{
		  _i2c.sda = DIGITAL_TO_PINNAME(sda);
		};
		void setSCL(PinName scl)
		{
		  _i2c.scl = scl;
		};
		void setSDA(PinName sda)
		{
		  _i2c.sda = sda;
		};		

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working Component: I2C Regarding the I2C component good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

2 participants