Daisy I2C

hola,
I’m trying to get a I2C on Daisy via Arduino.
The device im trying to use has support using ‘Wire’ so seems like the quickest way to get started.

the devices work both fine on Bela, and also the same code is working fine with an Arduino Nano

But with daisy it reports no i2c devices found on either I2C1 or I2C4

so I backed up, and tried to use the I2CScanner example
(Examples -> Wire ->I2cScanner)

the code mentions here that you may need to se the I2C bus, by using setSDA, set SDL with relevant pin numbers, so I tried

for I2C1

  Wire.setSCL(D11);
  Wire.setSDA(D12);

for i2c4

  Wire.setSCL(D13);
  Wire.setSDA(D14);

both in setup() before calling Wire.begin()

but I’m not really sure about the pin numbers… is this the way daisy is expecting them to be specified?
anyway still no luck :frowning:

thoughts? has anyone got i2c working using Wire with daisy yet?

Here’s some code that I used to validate the I2C when testing everything.

You’ll notice that setSCL() and setSDA don’t get called. They are automagically set to the I2C1 pins on the daisy.

That said, I’m surprised that it doesn’t work when being overwritten.

This doesn’t actually communicate with any particular device, but you should be able to see activity on both I2C pins everytime the LED changes state.

#include <Wire.h>

byte val = 0;

void setup() {
  // put your setup code here, to run once:
  Wire.begin();
  pinMode(LED_BUILTIN, OUTPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
  Wire.beginTransmission(44);
  Wire.write(byte(0x00));
  Wire.write(val);
  Wire.endTransmission();
  val = (val+1) % 255;
  delay(250);
  digitalToggle(LED_BUILTIN);
}

yeah, I tried before with just Wire.begin() and I2C1 and it didnt work, hence why I tried setting pins directly.

its a sensor, so Im actually trying to read values…

but basically, begintransmission always fails…

if we look at the example Arduino scanner code we see

   Wire.beginTransmission(address);
    error = Wire.endTransmission();

it then checks to see if error==0 , if so it ‘sees’ the device at this address…
so basically this is always failing.

I guess I can look to see what error actually is… perhaps there is some kind of bus speed error?

— EDIT –
so to confirm…


#include <Wire.h>

void setup() {
  // Initialise serial and touch sensor
  Serial.begin(9600);
  Wire.begin();
  pinMode(LED_BUILTIN, OUTPUT);
}


void loop() {
  Wire.beginTransmission(0x40);
  int error = Wire.endTransmission();
  digitalWrite(LED_BUILTIN,error>0);
  Serial.print(error,HEX);
  delay(250);
}

Im getting error code 2 from endTransmission,
https://www.arduino.cc/en/Reference/WireEndTransmission

2:received NACK on transmit of address

note: this is definitely correct address, and i2c1 is wired up correctly.

Yay… problem resolved…

I found, I needed pull-up resistors on the SCA/SCL lines… now its working 100% :slight_smile:


edit:
quick note, for others that might need this, you can indeed set SCL/SDA for a different i2c bus,
e.g. to use i2c4, just add this before Wire.begin()

  Wire.setSCL(D13);
  Wire.setSDA(D14);
3 Likes

Awesome! Glad you got it sorted.

Such an easy thing to miss. If I had a nickel for every time I’ve forgotten to add pull ups when testing stuff I’d be rich! :laughing:

Cool to see the I2C4 working with the other pin assignments as well.

The full pinout->peripheral layout is in the stm32duino repo variant in case you’re ever curious about what other peripherals are mapped in the Arduino setup.

3 Likes

yeah, just didn’t cross my mind, as neither the nano nor bela required them…

ah, that link is gold… i was tearing my hair out earlier trying to find that info!

btw: (non-arduino) is the idea to extend per_i2c with read and write functions? or should we get used to using stm32 HAL_* functions?

I’m still trying to figure, if using arduino ‘layer’ or moving to the daisy lib is the ‘right’ direction, i.e. whats the benefit of using libdaisy.

Arduino is great for getting up and running right now.

Long term though I would recommend switching to the Daisy libs if you’re comfortable with C++, since that is where we will be focusing our development energy on the platform.

2 Likes

btw: (non-arduino) is the idea to extend per_i2c with read and write functions? or should we get used to using stm32 HAL_* functions?
Adding generic I2C read/write functions is definitely planned.

The per_*.* files are meant to be a wrapper around the HAL, and eventually replace the HAL entirely.

Currently the led driver device (PCA9685) still uses the HAL functions, but the goal is to replace the need to use HAL anywhere but the per_ and sys_ files so that we can eventually strip it out entirely.

I’ll open a github issue for adding generic read/write support to the existing I2C module.


As for Arduino vs libDaisy: I’d say for simple programs there won’t be much of a difference, but the Arduino stuff is set up to be generic and work with a number of STM32 processors.

It is maintained by ST, but non-generic behavior doesn’t get added to their Core/ and must be supplied by libraries.

So beyond basic usage, and simple applications, libDaisy should allow for more control at different levels, and provide ways to do specific device-centric things a bit easier than with Arduino.

1 Like

I’m struggling too with the abstraction daisy to HAL. In the Field .cpp it appears that I2C is mapped to pins 8 &9 which doesn’t match pinout. I was trying to use I2C4 on 13 & 14. Not had scope on it yet…

Have partially solved own issue in that PB8 and PB9 ARE I2C_1 interface and delivered to Daisy pins. Haven’t worked out why the I2C library wants the port pins but others have a conversion to Daisy/Arduino nomenclature.