🛠 Scanner les deux bus I2C de la Due (Wire sur 20/21, Wire1 sur 70/71)
Objectives
The Due has TWO hardware I2C controllers, with a numbering pitfall documented in the core: the Wire object corresponds to the TWI1 controller (pins 20 = SDA, 21 = SCL, near AREF) while Wire1 corresponds to TWI0 (pins 70/71, small 4-pin connector near RESET). This tutorial writes a scanner that sweeps both buses and reports the 7-bit addresses that respond — the basis of all I2C sensor troubleshooting.
Hardware
- 1 Arduino Due
- 1 or 2 I2C peripherals for testing (DS3231 RTC module, SSD1306 OLED, BME280 sensor…) — 3.3 V modules or 3.3 V tolerant
- Micro-USB cable; optionally 4.7 kΩ pull-up resistors if the module does not have them
Complete code
// Scanner Wire (TWI1: 20/21) et Wire1 (TWI0: 70/71)
#include <Wire.h>
uint8_t scanner(TwoWire &w, const char *nom) {
uint8_t trouves = 0;
SerialUSB.print(nom); SerialUSB.print(" : ");
for (uint8_t a = 1; a < 127; a++) {
w.beginTransmission(a);
if (w.endTransmission == 0) { // 0 = ACK reçu
SerialUSB.print("0x"); SerialUSB.print(a, HEX); SerialUSB.print(" ");
trouves++;
}
}
if (!trouves) SerialUSB.print("(aucun) ");
SerialUSB.println;
return trouves;
}
void setup {
SerialUSB.begin(115200);
delay(1500); // laisser le CDC s'énumérer
SerialUSB.println("=== Scan I2C Due ===");
Wire.begin; // TWI1 -> broches 20 (SDA) / 21 (SCL)
Wire1.begin; // TWI0 -> broches 70 / 71 (connecteur pres RESET)
scanner(Wire, "Wire (TWI1, broches 20/21)");
scanner(Wire1, "Wire1 (TWI0, broches 70/71)");
SerialUSB.println("Fin de scan.");
}
void loop { }
Expected verification
With a module on the classic I2C connector (20/21), the "Wire" line displays its address (e.g., 0x68 for a DS3231, 0x3C for a SSD1306) and the "Wire1" line displays (none). Moving the module to the 4-pin connector 70/71 reverses the results. With no peripheral connected, both lines display (none).
Common errors
- Believing that
Wire= TWI0: it is the opposite (Wire→TWI1 on 20/21, Wire1→TWI0 on 70/71); the SAM3X datasheets referring to TWI0/TWI1 do not match the Arduino names — hence this tutorial. - Forgetting pull-ups on SDA/SCL: without them, the scan sees nothing or sees random data; most commercial modules already include them.
- Powering an I2C module with 5 V and connecting SDA/SCL directly to the Due: 5 V levels on 3.3 V pins = risk of destruction; use 3.3 V modules.
- Using old
TWCR/TWDRAPIs inherited from AVR tutorials: the SAM core uses the modernWireAPI (byte, not int, forendTransmission). - Scanning too early after opening the monitor: the native CDC takes a moment to enumerate, hence the initial
delay(1500). - Confusing 7-bit and 8-bit addresses: the scan displays 0x3C (7 bits) while some tutorials write 0x78 (8 bits) — it is the same peripheral.
Other tutorials for this board
- Faire clignoter la LED de l'Arduino Due et apprivoiser ses deux ports USB
- Lire un potentiomètre avec le vrai ADC 12 bits de la Due
- Allumer une LED externe en respectant les broches 3,3 V et 9 mA de la Due
- Générer une vraie tension analogique avec les DAC 12 bits (broches 66 et 67)
- Dialoguer avec un GPS (ou tout module TTL) sur Serial1 sans bloquer la boucle
- Monter la Due sur un bus CAN 500 kbit/s avec un transceiver externe
Code examples for this board
clignotante.inoArduinobouton-led.inoArduinodac-onde.inoArduinoadc-12-bits.inoArduinopwm-12-bits.inoArduinogps-serial1.inoArduino