stevemarple / SoftWire

Software I2C implementation for Arduino and other Wiring-type environments
GNU Lesser General Public License v2.1
136 stars 31 forks source link

Not available on rp2040 (raspberry pie Pico). #31

Open xiezhoubin opened 2 years ago

xiezhoubin commented 2 years ago

Problem: I use mpu6050 sensor and raspberry pie Pico development board. I can make sure the wiring is correct. However, the same code can run on Arduino uno and esp32, and can correctly read mpu6050 data. After the raspberry pie Pico runs, it cannot correctly read data.(Raspberry pie enables Pico to read normally using hardware IIC.)

arduino for rp2040: https://github.com/earlephilhower/arduino-pico

Picture: image Raspberry pie Pico failed to read with software IIC: image Arduino uno successfully read with software IIC: image

Code:

include

SoftWire Wire(12, 13); // Raspberry pie Pico(SDA=12,SCL=13) ,ARDUINO UNO(SDA=2,SCL=3)

const int MPU6050_addr = 0x68; int16_t AccX, AccY, AccZ, Temp, GyroX, GyroY, GyroZ; uint8_t swTxBuffer[32]; uint8_t swRxBuffer[32];

void setup() { Wire.setTxBuffer(swTxBuffer, sizeof(swTxBuffer)); Wire.setRxBuffer(swRxBuffer, sizeof(swRxBuffer)); Wire.begin(); Wire.beginTransmission(MPU6050_addr); Wire.write(0x6B); Wire.write(0); Wire.endTransmission(true); Serial.begin(9600); }

void loop() { Wire.beginTransmission(MPU6050_addr); Wire.write(0x3B); Wire.endTransmission(false); Wire.requestFrom(MPU6050_addr, 14, true); AccX = Wire.read() << 8 | Wire.read(); AccY = Wire.read() << 8 | Wire.read(); AccZ = Wire.read() << 8 | Wire.read(); Temp = Wire.read() << 8 | Wire.read(); GyroX = Wire.read() << 8 | Wire.read(); GyroY = Wire.read() << 8 | Wire.read(); GyroZ = Wire.read() << 8 | Wire.read();

Serial.print("AccX = "); Serial.print(AccX); Serial.print(" || AccY = "); Serial.print(AccY); Serial.print(" || AccZ = "); Serial.print(AccZ); Serial.print(" || GyroX = "); Serial.print(GyroX); Serial.print(" || GyroY = "); Serial.print(GyroY); Serial.print(" || GyroZ = "); Serial.print(GyroZ); Serial.print(" || Temp = "); Serial.println(Temp / 340.00 + 36.53); delay(500); }