I2C between two microcontrollers
Build it step by step
0 of 7 doneWire the bus
A4 to A4, A5 to A5 and GND to GND between the two boards.
Add pull-ups
Two 4.7 kΩ resistors, one from SDA to 5 V and one from SCL to 5 V.
Upload the target
Flash the first sketch to the board that has the pot.
Upload the controller
Flash the second sketch to the other board and open its serial monitor.
Turn the pot
The serial monitor shows values from 0 to 1023.
Watch the LED
The target's built-in LED toggles every half second as commands arrive.
Scan the bus
Run an I2C scanner on the controller and confirm 0x42.
How do you build an I2C between two microcontrollers?
One Arduino acts as an I2C controller and another as a target that returns a sensor reading. You need Arduino Nano × 2, Potentiometer 10 kΩ, Resistor 4.7 kΩ × 2, Breadboard 830 Tie Points, Jumper wires male to male (40 pcs, 20 cm) and USB A to micro-USB cable (data) × 2. It takes 7 steps and about 60 minutes.
- Level
- Beginner
- Time
- About 60 min
- Parts
- 6
Parts list
- Development BoardsBuyArduino Nano × 2
- PassivesBuy
- PassivesBuyResistor 4.7 kΩ × 2
- PrototypingBuy
- PrototypingBuy
- Cables & WiresBuy
Tick what you already own. It saves to My Garage, so “Build with what I have” knows.
Wiring
| From | To | Why |
|---|---|---|
| Controller A4 (SDA) | Target A4 (SDA) | Data line |
| Controller A5 (SCL) | Target A5 (SCL) | Clock line |
| Controller GND | Target GND | Common ground, required |
| 4.7 kΩ pull-up | SDA to 5 V | I2C needs pull-ups |
| 4.7 kΩ pull-up | SCL to 5 V | I2C needs pull-ups |
| Pot outer pins | Target 5V and GND | Reference |
| Pot middle pin | Target A0 | The reading the target reports |
Code
Arduino IDE, board "Arduino Nano" (processor ATmega328P, or ATmega328P (Old Bootloader) for clones). Uses the Wire library. The second sketch is written as comments so both can sit in one file here. Copy it into its own sketch and remove the // marks. Run an I2C scanner on the controller to confirm that 0x42 is found.
Runs on a virtual Arduino Nano in your browser, with sliders for the sensors. Happy with it? Verify and upload to your real board from the same page (Chrome or Edge on a computer).
// ===== TARGET sketch (the board with the pot) =====
#include <Wire.h>
const byte MY_ADDR = 0x42;
volatile byte command = 0;
void onReceive(int n) {
while (Wire.available()) command = Wire.read();
if (command == 0x02) digitalWrite(LED_BUILTIN, HIGH);
if (command == 0x03) digitalWrite(LED_BUILTIN, LOW);
}
void onRequest() {
int v = analogRead(A0);
Wire.write(highByte(v));
Wire.write(lowByte(v));
}
void setup() {
pinMode(LED_BUILTIN, OUTPUT);
Wire.begin(MY_ADDR);
Wire.onReceive(onReceive);
Wire.onRequest(onRequest);
}
void loop() {}
// ===== CONTROLLER sketch (upload this to the other board) =====
// #include <Wire.h>
// const byte TARGET = 0x42;
// byte count = 0;
// void setup() { Wire.begin(); Serial.begin(9600); }
// void loop() {
// Wire.beginTransmission(TARGET); Wire.write(count % 2 ? 0x02 : 0x03); // toggle the target's LED
// Wire.endTransmission();
// Wire.requestFrom(TARGET, (byte)2);
// if (Wire.available() == 2) {
// int v = Wire.read() << 8; v |= Wire.read();
// Serial.print("Pot: "); Serial.println(v);
// }
// count++;
// delay(500);
// }Test checklist
What you should see at each check. If a reading is far off, stop and use the troubleshooting list.
| Check | Expected |
|---|---|
| Pot at one end | Reads near 0 |
| Pot at the other end | Reads near 1023 |
| Target LED | Blinks at 1 Hz |
| Unplug SDA | Controller reads nothing |
If it does not work
Scanner finds nothing
Check SDA and SCL are not swapped and that the grounds are joined.
Reads zero constantly
The pot middle pin is not on A0 on the target.
Garbage values
Wire.read order is wrong. The high byte comes first in this code.
Bus hangs
A pull-up is missing or both boards are set as the controller.
Still stuck? Find the fault step by step with your multimeter →
Common questions
What parts do I need for the I2C between two microcontrollers?
Arduino Nano × 2, Potentiometer 10 kΩ, Resistor 4.7 kΩ × 2, Breadboard 830 Tie Points, Jumper wires male to male (40 pcs, 20 cm) and USB A to micro-USB cable (data) × 2.
How long does the I2C between two microcontrollers take to build?
About 60 minutes for a beginner maker, in 7 steps.
How do I know the I2C between two microcontrollers works?
Pot at one end: Reads near 0. Pot at the other end: Reads near 1023. Target LED: Blinks at 1 Hz.
Scanner finds nothing: what should I check?
Check SDA and SCL are not swapped and that the grounds are joined.
Reads zero constantly: what should I check?
The pot middle pin is not on A0 on the target.
Garbage values: what should I check?
Wire.read order is wrong. The high byte comes first in this code.
Bus hangs: what should I check?
A pull-up is missing or both boards are set as the controller.
