Challenge

Project 1 - Interfacing a 16*2 LCD with Arduino using I2C protocol

OVERVIEW:

The project is an example of communication between two Arduino boards using the I2C protocol. The master board reads analog input from a potentiometer and sends the converted value to the slave board. The slave board displays the received value on an LCD screen and sends back a value from another potentiometer to the master board, which is displayed on its own LCD screen. The project demonstrates basic I2C communication and simple data exchange between two Arduino boards.

MASTER CODE:

#include <Wire.h>                             // Library for I2C Communication functions
#include <LiquidCrystal.h>                    // Library for LCD display function

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);         // Define LCD Module Pins (RS,EN,D4,D5,D6,D7)

void setup() {
  lcd.begin(16, 2);                           // Initialize LCD display with 16 columns and 2 rows
  lcd.setCursor(0, 0);                        // Set cursor at the first line of display
  lcd.print(">>  MASTER  <<");                // Print "MASTER" on LCD
  delay(5000);                                // Wait for 5 seconds
  lcd.clear();                                // Clear the LCD display
  
  Serial.begin(9600);                         // Begin serial communication at 9600 baud rate
  
  Wire.begin();                               // Begin I2C communication at pin (A4, A5)
}

void loop() {
  Wire.requestFrom(8, 1);                     // Request 1 byte from slave arduino (8)
  byte MasterReceive = Wire.read();           // Receive a byte from the slave arduino and store in MasterReceive
  
  int potvalue = analogRead(A0);              // Read analog value from the POT (0-5V)
  byte MasterSend = map(potvalue, 0, 1023, 0, 100);// Convert digital value (0 to 1023) to (0 to 100)
  
  Wire.beginTransmission(8);                  // Start transmit to slave arduino (8)
  Wire.write(MasterSend);                     // Send one byte (converted POT value) to slave
  Wire.endTransmission();                     // Stop transmitting
  
  Serial.print("Master Received From Slave: "); // Print "Master Received From Slave" on Serial Monitor
  Serial.println(MasterReceive);              // Print MasterReceive on Serial Monitor
  
  lcd.clear();                                // Clear the LCD display
  lcd.setCursor(0, 0);                        // Set cursor at line one of LCD
  lcd.print("Slave_Value:");                  // Print "SlaveVal:" on LCD
  lcd.setCursor(0, 1);                        // Set cursor at line two of LCD
  lcd.print(MasterReceive);                   // Print MasterReceive (value received from Slave) on LCD
  
  delay(500);                                 // Delay for 500 milliseconds
}

SLAVE CODE:

#include <Wire.h>                // Library for I2C communication functions
#include <LiquidCrystal.h>       // Library for LCD display functions

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);   // Define LCD module pins (RS, EN, D4, D5, D6, D7)
byte slaveReceived = 0;                  // Variable to store the value received from the master

void setup() {
  lcd.begin(16, 2);              // Initialize LCD display
  lcd.setCursor(0, 0);           // Sets cursor at first line of display
  lcd.print(">>  SLAVE  <<");    // Prints "SLAVE" on the LCD
  delay(5000);                   // Wait for 5 seconds
  lcd.clear();                   // Clear the LCD display
  Serial.begin(9600);            // Begin serial communication at 9600 baud rate
  Wire.begin(8);                 // Begin I2C communication with slave address 8 at pins A4 and A5
  Wire.onReceive(receiveEvent);  // Call the receiveEvent function when the slave receives a value from the master
  Wire.onRequest(requestEvent);  // Call the requestEvent function when the master requests a value from the slave
}

void loop() {
  lcd.setCursor(0, 0);              // Sets cursor at first line of display
  lcd.print("Master_Value:");       // Prints "MasterVal:" on the LCD
  lcd.setCursor(0,1);               // Set cursor at line one of LCD
  lcd.print(slaveReceived);         // Prints the value received from the master on the LCD
  Serial.print("Slave Received From Master: ");  // Prints "Slave Received From Master:" on the serial monitor
  Serial.println(slaveReceived);    // Prints the value received from the master on the serial monitor
  delay(500);
  lcd.clear();                      // Clears the LCD display
}

void receiveEvent(int howMany) {       // This function is called when the slave receives a value from the master
  slaveReceived = Wire.read();         // Reads the value received from the master and stores it in the variable slaveReceived
}

void requestEvent() {                         // This function is called when the master requests a value from the slave
  int potValue = analogRead(A0);              // Reads the analog value from the POT (0-5V)
  byte slaveSend = map(potValue, 0, 1023, 0, 100); // Converts the digital value (0 to 1023) to (0 to 100)
  Wire.write(slaveSend);                      // Sends the converted value to the master
}

OUTPUT:

Your portfolio, next

Ship work like this
in your own course.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.