How to use Wire1 on RP2040 board?

Continuing the discussion from Issues with I2C Communication on Waveshare RP2040-Zero:

Hi could you put the code where you used Wire1 to know how why it didn't work for me

have a look at RPi Pico RP2040 I2C scanner whic scans all the Wire and Wire1 interfaces for I2C devices

running on a RPi pico RP2040 with

  1. a GY-91 on default I2C0 SDA GP4 and SCL GP5
  2. a BMP280 on I2C1 SDA GP14 and SCL GP15

results

I2C Scan
Wire0 SDA: 0 SCL: 1
No I2C devices found

Wire1 SDA: 2 SCL: 3
No I2C devices found

Wire0 SDA: 4 SCL: 5
I2C device found at address 0x68
I2C device found at address 0x76
Scan Complete

Wire1 SDA: 6 SCL: 7
No I2C devices found

Wire0 SDA: 8 SCL: 9
No I2C devices found

Wire1 SDA: 10 SCL: 11
No I2C devices found

Wire0 SDA: 12 SCL: 13
No I2C devices found

Wire1 SDA: 14 SCL: 15
I2C device found at address 0x76
Scan Complete

Wire0 SDA: 16 SCL: 17
No I2C devices found

Wire1 SDA: 18 SCL: 19
No I2C devices found

Wire0 SDA: 20 SCL: 21
No I2C devices found

Wire1 SDA: 26 SCL: 27
No I2C devices found

All Scans Successful!

The problem I have is that I want to use gpio 26 and 27 of the i2c to be able to connect in gpio4 and 5 another SPI device but nothing has worked I can't enable pins 26 and 27 and when connecting the i2c device it uses pins 4 and 5 and pin 4(MISO) is used by the SPI device and creates conflict.
I'm connecting a RTC clock module by i2c and a microsd card reader module by SPI but the pins that by default are enabled the gpio4 is shared by i2c and SPI and creates conflict what can I do?

which version of the RP2040 are you using?
not sure what you are attempting to connect to the RP2040

  1. an I2C and a SPI device
  2. two SPI devices
  3. I2C and two SPI devices?

what else do you want to connect?

what pins are you attempting to use for what devices?

Versión waveshare rp2040 Zero

  1. One i2c and one SPI devices

pins 26 and 27 are I2C1 SDA and SCL and are therefore associated with Wire1
here is an example of a BMP280 connected to pins 26 and 27 of a RPi Pico RP2040

// Interfacing RPi pico RP2040 with BMP280 temperature and pressure sensor.

#include <Wire.h>             // include Wire library, required for I2C devices
#include <Adafruit_Sensor.h>  // include Adafruit sensor library
#include <Adafruit_BMP280.h>  // include adafruit library for BMP280 sensor

// define device I2C address: 0x76 or 0x77 (0x77 is library default address)
#define BMP280_I2C_ADDRESS 0x76

// define I2C1 RP2040 pins for BMP280
#define SDA 26  
#define SCL 27  

Adafruit_BMP280 bmp280(&Wire1);

void setup() {
  Serial.begin(115200);
  delay(3000);
  Serial.println(F("RPi pico RP2040 + BMP280"));
  Wire1.setSDA(SDA);     // setup SDA and SCL pins
  Wire1.setSCL(SCL);
  Wire1.begin();
  if (!bmp280.begin(BMP280_I2C_ADDRESS)) {
    Serial.println("Could not find a valid BMP280 sensor, check wiring!");
    while (1)
      ;
  }
  Serial.println("Found BMP280 sensor!");
}

void loop() {
  // get temperature, pressure and altitude from library
  float temperature = bmp280.readTemperature();    // get temperature
  float pressure = bmp280.readPressure();          // get pressure
  float altitude_ = bmp280.readAltitude(1013.25);  // get altitude (this should be adjusted to your local forecast)
  // print data on the serial monitor software
  // 1: print temperature
  Serial.print("Temperature = ");
  Serial.print(temperature);
  Serial.println(" °C");
  // 2: print pressure
  Serial.print("Pressure    = ");
  Serial.print(pressure / 100);
  Serial.println(" hPa");
  // 3: print altitude
  Serial.print("Approx Altitude = ");
  Serial.print(altitude_);
  Serial.println(" m");

  Serial.println();  // start a new line
  delay(2000);       // wait 2 seconds
}

a run gives

RPi pico RP2040 + BMP280
Found BMP280 sensor!
Temperature = 23.30 °C
Pressure    = 997.76 hPa
Approx Altitude = 129.76 m

Temperature = 23.61 °C
Pressure    = 997.82 hPa
Approx Altitude = 129.25 m

Temperature = 23.62 °C
Pressure    = 997.83 hPa
Approx Altitude = 129.21 m

ok I'll try it thanks

Add the elements of your code and connect the RTC to pins 26 and 27 and I get this:

C:\Users\aaa\Documents\relojRTCparaWaveshareRP2040Zero\relojRTCparaWaveshareRP2040Zero.ino: In function 'void loop()':
C:\Users\aaa\Documents\relojRTCparaWaveshareRP2040Zero\relojRTCparaWaveshareRP2040Zero.ino:42:23: error: request for member 'now' in 'rtc', which is of non-class type 'RTC_DS3231()'
 42 |  DateTime fecha = rtc.now();
    |                       ^~~

exit status 1

Compilation error: request for member 'begin' in 'rtc', which is of non-class type 'RTC_DS3231()'

think you need to post the whole code

This code works ok, SDA on gpio04,SCL on gpio05 , how to change SDA to gpio26 and SCL to gpio27?

#include "RTClib.h"

int  LED_BUILTIN =16;


RTC_DS3231 rtc;
String daysOfTheWeek[7] = { "Domingo", "Lunes", "Martes", "Miercoles", "Jueves", "Viernes", "Sabado" };
String monthsNames[12] = { "Enero", "Febrero", "Marzo", "Abril", "Mayo",  "Junio", "Julio","Agosto","Septiembre","Octubre","Noviembre","Diciembre" };


void printDate(DateTime date)
{
  Serial.print(date.year(), DEC);
  Serial.print('/');
  Serial.print(monthsNames[date.month()-1]);
  Serial.print('/');
  Serial.print(date.day(), DEC);
  Serial.print(" (");
  Serial.print(daysOfTheWeek[date.dayOfTheWeek()]);
  Serial.print(") ");
  Serial.print(date.hour(), DEC);
  Serial.print(':');
  Serial.print(date.minute(), DEC);
  Serial.print(':');
  Serial.println(date.second(), DEC);
  Serial.println("___________________________________________________");
}

void setup(){
Serial.begin(9600);

  if (!rtc.begin()) {
    Serial.println(F("No se encuentra RTC"));
    //while (1);
  }
  if (rtc.lostPower()) {
    rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
  }
  

}

void loop(){
DateTime hoy = rtc.now();
printDate(hoy);
delay(500);
}

it is always a good idea to test each device in a separate program in case devices, libraries, etc corrupt each other or cause other problems
when all are working start building the complete system

e.g. testing DS32341 on a RPi pico

// RPi pico RP2040 Date and time functions using a DS3231 RTC 
//  connected via I2C1 and Wire1 using pins 26 SDA and 27 SCL 

#include <Wire.h>
#include "RTClib.h"

// define I2C1 RP2040 pins for DS3231
#define SDA 26  
#define SCL 27  

RTC_DS3231 rtc;

char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};

void setup () {

#ifndef ESP8266
  while (!Serial); // for Leonardo/Micro/Zero
#endif

  Serial.begin(115200);

  delay(3000); // wait for console opening
    Serial.println(F("RPi pico RP2040 + DS3231 RTC"));
  Wire1.setSDA(SDA);    // setup SDA and SCL pins
  Wire1.setSCL(SCL);
  Wire1.begin();

  if (! rtc.begin(&Wire1)) {
    Serial.println("Couldn't find RTC");
    while (1);
  }

  if (rtc.lostPower()) {
    Serial.println("RTC lost power, lets set the time!");
    // following line sets the RTC to the date & time this sketch was compiled
    rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
    // This line sets the RTC with an explicit date & time, for example to set
    // January 21, 2014 at 3am you would call:
    // rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
  }
      Serial.println("RTC initialised!");

}

void loop () {
    DateTime now = rtc.now();
    
    Serial.print(now.year(), DEC);
    Serial.print('/');
    Serial.print(now.month(), DEC);
    Serial.print('/');
    Serial.print(now.day(), DEC);
    Serial.print(" (");
    Serial.print(daysOfTheWeek[now.dayOfTheWeek()]);
    Serial.print(") ");
    Serial.print(now.hour(), DEC);
    Serial.print(':');
    Serial.print(now.minute(), DEC);
    Serial.print(':');
    Serial.print(now.second(), DEC);
    Serial.println();
    
    Serial.print(" since midnight 1/1/1970 = ");
    Serial.print(now.unixtime());
    Serial.print("s = ");
    Serial.print(now.unixtime() / 86400L);
    Serial.println("d");
    
    // calculate a date which is 7 days and 30 seconds into the future
    DateTime future (now + TimeSpan(7,12,30,6));
    
    Serial.print(" now + 7d + 30s: ");
    Serial.print(future.year(), DEC);
    Serial.print('/');
    Serial.print(future.month(), DEC);
    Serial.print('/');
    Serial.print(future.day(), DEC);
    Serial.print(' ');
    Serial.print(future.hour(), DEC);
    Serial.print(':');
    Serial.print(future.minute(), DEC);
    Serial.print(':');
    Serial.print(future.second(), DEC);
    Serial.println();
    
    Serial.println();
    delay(3000);
}

a run gave

RPi pico RP2040 + DS3231 RTC
RTC lost power, lets set the time!
RTC initialised!
2024/5/2 (Thursday) 18:11:39
 since midnight 1/1/1970 = 1714673499s = 19845d
 now + 7d + 30s: 2024/5/10 6:41:45

2024/5/2 (Thursday) 18:11:42
 since midnight 1/1/1970 = 1714673502s = 19845d
 now + 7d + 30s: 2024/5/10 6:41:48

2024/5/2 (Thursday) 18:11:45
 since midnight 1/1/1970 = 1714673505s = 19845d
 now + 7d + 30s: 2024/5/10 6:41:51

2024/5/2 (Thursday) 18:11:48
 since midnight 1/1/1970 = 1714673508s = 19845d
 now + 7d + 30s: 2024/5/10 6:41:54

2024/5/2 (Thursday) 18:11:51
 since midnight 1/1/1970 = 1714673511s = 19845d
 now + 7d + 30s: 2024/5/10 6:41:57

2024/5/2 (Thursday) 18:11:54
 since midnight 1/1/1970 = 1714673514s = 19845d
 now + 7d + 30s: 2024/5/10 6:42:0

..........

2024/5/2 (Thursday) 18:19:51
 since midnight 1/1/1970 = 1714673991s = 19845d
 now + 7d + 30s: 2024/5/10 6:49:57

2024/5/2 (Thursday) 18:19:54
 since midnight 1/1/1970 = 1714673994s = 19845d
 now + 7d + 30s: 2024/5/10 6:50:0

2024/5/2 (Thursday) 18:19:57
 since midnight 1/1/1970 = 1714673997s = 19845d
 now + 7d + 30s: 2024/5/10 6:50:3

The code worked perfectly, thank you very much, have a nice day.

if the code of post 11 was the answer mark it as the solution to help other users with the same problem

Hello to the above code I added an ldr resistor to read its values on the pin gpio29 and the reads are altered by the SDA and SCL pins because when disconnecting the RTC module the readings are normal, how can I fix this?

Hello, the solution was to change the SDA pins to gpio 6 and SCL to gpio 7 and so I got the correct readings of the LDR resistor thanks

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.