Hello community !
Core : STM32 MCU based board by STMicro v2.2.0
Board : bluepill copy with original STM32F103T8C6
I'm trying since 2 days to use a GY-271 magnetic sensor with a STM32F1.
However, i can't get almost anything from my I2C bus. I tried over I2C1 and I2C2 with no results.
First, i verified the device address with an I2C scanner. I found it at 0X0D on both I2C bus. But that's all. Impossible to write or read on the device (no error generated, just nothing happening).
On I2C1, i also have a SSD1306 Oled screen at 0x68, and this guy's working fine with adafruit library. On the screen, i was able to read a random byte with Wire.requestFrom(). But on the GY-271, nothing... I always read 0, even in registers i just wrote a non-null value.
Here's the full code :
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3C ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32
#define COMPAS 0x0D
volatile uint16_t X = 0, Y = 0, Z = 0;
uint16_t tmp = 0;
boolean test = false;
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
void setup() {
delay(5000);
pinMode(PB13, INPUT);
Serial.begin(9600);
delay(500);
Serial.println("Starting the I2C interface.");
Wire.begin();
Serial.println("Starting Display.");
display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS);
display.clearDisplay();
display.display();
Wire.beginTransmission(COMPAS);
byte e = Wire.endTransmission();
Serial.print("Device ");
Serial.print(COMPAS, HEX);
if(e == 0)
Serial.println(" found.");
else {
Serial.print(" not found : ");
Serial.println(e);
}
Wire.beginTransmission(COMPAS);
Wire.write(0x00);
Wire.write(0x70);
Wire.endTransmission();
Wire.beginTransmission(COMPAS);
Wire.write(0x01);
Wire.write(0xA0);
Wire.endTransmission();
Wire.beginTransmission(COMPAS);
Wire.write(0x02);
Wire.write(0x00); // Continuous sampling
Wire.endTransmission();
Wire.beginTransmission(COMPAS);
Wire.write(0x01);
Wire.endTransmission();
Wire.requestFrom(COMPAS, 1);
while(Wire.available() < 1) ;
Serial.print("I read : ");
Serial.println(Wire.read());
attachInterrupt(digitalPinToInterrupt(PB13), readCompas, FALLING ); // supposed to call readCompas when data are ready
}
void readCompas() { // it never enters here. Do nothing if i call the function in the main()
Serial.println("READCOMPAS"); // NEVER HAPPEN ! No signal observed on DRDY pin
display.setCursor(30, 40);
display.print("READING...");
Wire.beginTransmission(COMPAS);
Wire.write(3); // 3 is the address of X position
Wire.endTransmission();
Wire.requestFrom(COMPAS, 6);
if(6<=Wire.available()) {
X = Wire.read()<<8; //X msb
X |= Wire.read(); //X lsb
Z = Wire.read()<<8; //Z msb
Z |= Wire.read(); //Z lsb
Y = Wire.read()<<8; //Y msb
Y |= Wire.read();
}
}
void loop() {
tmp = 0;
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.cp437(true);
Wire.beginTransmission(COMPAS);
Wire.write(0x01);
Wire.endTransmission();
Wire.requestFrom(COMPAS, 1);
uint8_t xxx = Wire.read();
display.setCursor(0, 0); tmp += 9;
display.print(xxx);
Serial.println(xxx); // It's always 0 !
display.setCursor(5, tmp); tmp += 9;
display.print(X);
display.setCursor(5, tmp); tmp += 9;
display.print(Y);
display.setCursor(5, tmp); tmp += 9;
display.print(Z);
display.setCursor(60, 55); test = !test;
display.print(test ? "|" : "-");
display.display();
delay(200);
}
As result on serial monitor :
Starting the I2C interface.
Starting Display.
Device D found.
I read : 0
0
0
0
0
0
0
etc...
At least, shouldn't i get the value i just wrote in the register ???
I tried to change the BUS to 1 to 2, i changed the the GY271 module (i've many of them)...
Any help welcome !
Tank you