- Muchas notas - Fran Acién

20240415 - i2C read data from a sensor

I was a bit confused about how to read i2c data from a sensor. This arduino blog post is super self explaining.

Basically I have a BMP280 sensor. With the address 0x76, that you can read the sensor_id from register 0xD0, which the output should be 88d.

Here is a simple code to get that data:

#include <Wire.h>

int address = 0x76;

void setup() {
  Wire.begin();

  Serial.begin(9600);
  while (!Serial); // Leonardo: wait for Serial Monitor
  Serial.println("\nI2C Scanner");
}

void loop() {
  Wire.beginTransmission(address);    // Get the slave's attention, tell it we're sending a command byte
  Wire.write(0xD0);                               //  The command byte, sets pointer to register with address of 0x32
  Wire.requestFrom(address,1);          // Tell slave we need to read 1byte from the current register
  byte slaveByte2 = Wire.read();        // read that byte into 'slaveByte2' variable
  Wire.endTransmission();

  Serial.println(slaveByte2);
  delay(5000); // Wait 5 seconds for next scan
}