0

Background: I am trying to test the communication of 2 raspberry pi pico using i2c. Firstly, i sent a string from one and receive it from the second one. Both of them print the results in the serial monitor. I am planning to use 2 usb cables and open 2 serials to see the results. I am writing in the Arduino IDE using the Arduino-pico core.

However, when i upload a sketch, the raspberry pi disappears from my computer. If i plug it out and then plug it it, the computer doesn't recognize it. I have to reset it.

Question: I want to understand why this happens to find a work-around and avoid the same problem in the future.

Programms:

The slave code is as follows:

#include <Wire.h>

void setup() {
  Serial.begin(115200);
  while(!Serial)
  
  Wire1.setSDA(4);
  Wire1.setSCL(5);
  Wire1.begin(0x30);
  Wire1.onReceive(recv);
}

static char buff[100];
static int printNow =0;

void loop() {
  if (printNow){
  Serial.println("Slave Received : ");
  Serial.println(buff);
  printNow= 0;
  }
  
  delay(1);
}

// These are called in an **INTERRUPT CONTEXT** which means NO serial port
// access (i.e. Serial.print is illegal) and no memory allocations, etc.

//// Called when the I2C slave gets written to
void recv(int len) {
  int i;
  // Just stuff the sent bytes into a global the main routine can pick up and use
  for (i = 0; i < len; i++) {
    buff[i] = Wire1.read();
  }
  buff[i] = 0;
  printNow = 1;
}

The master code is as follows:

#include <Wire.h>

void setup() {
  Serial.begin(115200);
  while(!Serial)
  
  Wire.setSDA(4);
  Wire.setSCL(5);
  Wire.begin();
  
}
static int buff[100];

void loop() {
  int i = 0;
  char txt[50];
  int printNow = 0;

  while(Serial.available() ){
    txt[i] = Serial.read(); //int
    if ( txt[i] == '\n' ){
      printNow = 1;
      txt[i] = '\0';
    }
    i++;
  }
  i--; //reset the last increment
  if (printNow){
        Serial.println("Master sent this text: ");
        Serial.println(txt);
  }

  // Write a value over I2C to the slave
  Serial.println("Sending...");
  Wire.beginTransmission(0x30);
  Wire.write(txt, (i-1) ); // not sending the terminating 0
  Wire.endTransmission();

} 

1 Answer 1

0

So i found out that the problem was because i was trying to use Wire1 on pins that do not support wire1. If you see the pinout, GP4,GP5 are for i2c0, that means Wire and not Wire1.

I didn't expect that this coding error would get the rp stuck. I would appreciate if somebody could explain why.

2
  • 2
    you posted an answer to the question here ... it is no place to be asking further questions
    – jsotola
    Sep 6 at 16:46
  • What jsotola means is if you have a new question, ask a new question. You may include a link to this question if you think it is relevant. Please take the tour to understand better how the site works.
    – goldilocks
    Oct 6 at 19:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.