Feather RP2040, how to use Wire for I2C? (and not Wire1)

Please tell us which board you are using.
For CircuitPython issues, ask in the Adafruit CircuitPython forum.

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
jrwst36
 
Posts: 39
Joined: Sun Apr 17, 2011 8:22 am

Feather RP2040, how to use Wire for I2C? (and not Wire1)

Post by jrwst36 »

Hello,
On the RP2040 the STEMMA and SDA SCL pins are connected to the Wire1 I2C(1). This is causing havoc with my library. The easiest thing for me would simply be to use Wire I2C(0). But can't figure out how to do that. The guide shows that there are multiple SDA0 and SCL0 pins. I've connected it to the first two pins listed (A2 and A3). That doesn't work without specific code, so I tried.

Code: Select all

Wire.setPins(A2, A3); // SDA, SCL
But that throws an error.

Can someone show me how to use the A2 and A3 pins for SDA0 and SCL0? It's driving me nuts.

User avatar
jrwst36
 
Posts: 39
Joined: Sun Apr 17, 2011 8:22 am

Re: Feather RP2040, how to use Wire for I2C? (and not Wire1)

Post by jrwst36 »

For anyone interested, I finally figured it out. First, it seems that the RP2040 uses the TwoWire library by default so some of the standard Wire commands don't work. I you do want to use Wire (as opposed to Wire1) in think you have to specify which pins to use. Or at least I did. Here's how I did it for the BME688:

Code: Select all

#include <Wire.h>
#include <Adafruit_Sensor.h>
#include "Adafruit_BME680.h"
Adafruit_BME680 bme; // I2C

void setup() {
  Serial.begin(9600);
  
  // Here's where the Wire pins are set for the TwoWire library
  Wire.setSDA(28); // this is the A2 pin
  Wire.setSCL(29); // this is the A3 pin

  ////////////////////
  // Begin the BME sensor
  while (!bme.begin()) {
    Serial.println("Could not find the BME680 sensor, checking again");
    delay(1000);
  }
 }
 
 void loop() {
  
  ////////////////////
  // BME reads
  if (! bme.performReading()) {
    Serial.println("BME reading failed");
    return;
  }

  float bme_temp = bme.temperature; // Celcius
  float bme_pres = bme.pressure / 100.0; // hPa
  float bme_humd = bme.humidity; // %
  float bme_gasR = bme.gas_resistance/ 1000.0; //KOhms

  Serial.print(bme_temp); Serial.print(", ");
  Serial.print(bme_pres); Serial.print(", ");
  Serial.print(bme_humd); Serial.print(", ");
  Serial.println(bme_gasR);

  delay(1000);
}

User avatar
freddyboomboom
 
Posts: 267
Joined: Wed Feb 16, 2022 7:55 pm

Re: Feather RP2040, how to use Wire for I2C? (and not Wire1)

Post by freddyboomboom »

On the Raspberry Pi Pico (& Pico W) since most pairs of GPIO pins can be setup as i2c, you generally have to specify which pair of pins you are using for each i2c bus you're using. And that means you can have more than one i2c bus defined, as well.

Locked
Please be positive and constructive with your questions and comments.

Return to “Feather - Adafruit's lightweight platform”