🛠 Ajouter un capteur I²C (température) sur A4/A5

Arduino Pro Mini · intermediaire · Schéma : IMG-pro-mini-intermediaire-6-promini-capteur-i2c.svg — capteur de température I²C MCP9808 relié à SDA/A4 et SCL/A5 de l'Arduino Pro Mini, emplacements de résistances de pull-up I²C visibles sur la carte

Objectives

Use the native I²C/TWI bus of the ATmega328P: on the Pro Mini, SDA is on A4 and SCL on A5, on the same row of pins as the analog inputs. The latest SparkFun revision even adds SMD footprints for soldering I²C pull-up resistors — useful for fixed installations, as many modules already include them. We read a MCP9808 temperature sensor (address 0x18) using the Wire library and display the temperature in Celsius.

Hardware

Complete Code

// Capteur temperature I2C MCP9808 sur A4 (SDA) / A5 (SCL)
#include <Wire.h>

const uint8_t ADDR = 0x18;   // adresse par defaut du MCP9808

void setup {
  Serial.begin(57600);
  Wire.begin;              // maitre I2C, broches A4/A5 fixes sur 328P
  if (Wire.requestFrom(ADDR, (uint8_t)1) == 0) {
    Serial.println(F("Capteur absent ! Verifier SDA/SCL/adresse"));
  }
}

void loop {
  Wire.beginTransmission(ADDR);
  Wire.write(0x05);                 // registre ambient temperature
  Wire.endTransmission;
  Wire.requestFrom(ADDR, (uint8_t)2);
  if (Wire.available >= 2) {
    int16_t brut = (Wire.read << 8) | Wire.read;
    if (brut & 0x8000) brut -= 0x10000;   // signe negatif
    float t = (brut & 0x0FFF) * 0.0625;   // resolution 1/16 degre
    Serial.print(F("T = "));
    Serial.print(t);
    Serial.println(F(" C"));
  }
  delay(1000);
}

Expected Verification

The serial monitor displays a consistent temperature (~20-25 °C indoors), which rises when you pinch the sensor between your fingers. The presence test in setup displays no errors if SDA/SCL are correctly on A4/A5 and the pull-ups are present. On the latest board revision, the two small footprints near A4/A5 allow soldering 0603 4.7 kΩ resistors for a clean bus in a permanent installation.

Common Errors

Other tutorials for this board

Code examples for this board

← Back to the board 🛠 All tutorials