"Arduino Uno to Mega I2C Communication: Control LED with Button""I2C using just a few wires.
Автор: MKRC LABS
Загружено: 2025-03-18
Просмотров: 130
"In this video, I'll show you how to use I2C communication to control an LED on an Arduino Mega from an Arduino Uno. We'll cover the wiring, code, and basic principles of I2C. This tutorial is perfect for beginners who want to learn about serial communication and expand their Arduino skills. #Arduino #I2C #Electronics"
Wiring Instructions:Master (Uno):Connect SDA (A4) and SCL (A5) to the corresponding pins on the Mega.Connect a button to pin 6 with one terminal connected to ground and the other to pin 6 via a pull-up resistor.Slave (Mega):Connect SDA (pin 20) and SCL (pin 21) to the Uno's corresponding pins.Use onboard LED at pin 13 for both slaves.Explanation:The master reads the button state and sends a command (1 or 0) via I2C to either slave depending on the button's state.Each slave listens for its specific address and toggles its onboard LED based on the received command.This setup ensures proper communication between the master and multiple slaves using the I2C protocol.
You want to set up I2C communication where an Arduino Uno (Master) sends data to an Arduino Mega (Slave), and based on the received data, the Mega controls its onboard LED (pin 13). Additionally, you want to use Software I2C on pin 6 for the Uno instead of the default hardware I2C (SDA/SCL).
Added an empty loop()Even though the slave primarily reacts to receiveEvent(), the Arduino framework requires a loop() function.The loop() contains a small delay(100); to prevent potential watchdog timer resets.
//Code Master //
/* * I2C communication Arduino UNO as master Kamran Choudhryhttps://www.youtube.com/@MKRCLabshttp... Arduino sketch demonstrates the use of an Arduino Uno as a master device in an I2C (Inter-Integrated Circuit) communication setup. The experiment aims to showcase how the master can send data to one or more slave devices and receive responses. By utilizing the Wire library, we will establish a simple communication protocol that can be applied in various applications like sensor networks, data logging, and more. This video will guide viewers through the setup process and explain the code step-by-step.*/#include (Wire.h) // here use bigger smaller sign remove bracketconst int buttonPin = 6; // Button connected to Pin 6void setup() { pinMode(buttonPin, INPUT_PULLUP); // Set button pin as input with pull-up resistor pinMode(13, OUTPUT); // Set pin 13 as an output (LED pin) digitalWrite(13, LOW); // Turn off the LED on pin 13 Wire.begin(); // Initialize I2C communication as Master}void loop() { // Read button state bool buttonState = digitalRead(buttonPin); // Send button state to Mega Wire.beginTransmission(8); // Address of Mega (Slave) Wire.write(buttonState); // Send button state (0 or 1) Wire.endTransmission(); delay(100); // Small delay for stability}
// Code Slave//
/* Kamran Choudhry / @mkrclabs • Hoverboard hack how to hack Hover board m... */#include (Wire.h)// here use bigger smaller sign remove bracketconst int ledPin = 13; // LED connected to pin 13void setup() { pinMode(ledPin, OUTPUT); // Set LED as output Wire.begin(8); // Initialize as Slave with address 8 Wire.onReceive(receiveEvent); // Register receive event}void loop() { // Loop does not need to do anything, but it must exist delay(100);}void receiveEvent(int bytes) { while (Wire.available()) { // Ensure there's data to read int data = Wire.read(); // Read incoming data if (data == 1) { digitalWrite(ledPin, LOW); // Turn off LED } else if (data == 0) { digitalWrite(ledPin, HIGH); // Turn on LED } }}
IssuesSDA and SCL pins may be swapped or not properly connected. On Arduino Uno, SDA is on A4 and SCL is on A5; on Arduino Mega, SDA is pin 20 and SCL is pin 21.I2C communication requires pull-up resistors (typically 4.7kΩ–10kΩ) on both SDA and SCL lines. Without them, communication may fail.Each I2C device has a unique address. Using an incorrect address will prevent communication. An I2C scanner sketch can help identify the correct address.Different boards or devices may operate at different voltage levels (e.g., 3.3V vs. 5V). Using a logic level shifter is necessary to avoid compatibility issues.Multiple devices sharing the same address will cause conflicts. Use an I2C multiplexer or change the device address if possible.Long wires can introduce noise and communication errors. Keep wires under 30 cm for reliable operation.Faulty pins on the Arduino board can disrupt communication.Defective sensors, modules, or Arduino boards may cause issues. Testing with replacement components can help identify faults.Clock Speed Mismatch:The default I2C clock speed (100kHz) might not be supported by certain devices. Adjusting the clock speed using Wire.setClock() can resolve compatibility issues.Missing or incorrect code (e.g., failing to include Wire.h or improper initialization) can lead to communication failure.Solutions
Доступные форматы для скачивания:
Скачать видео mp4
-
Информация по загрузке: