Project 1 - Interfacing a 16*2 LCD with Arduino using I2C protocol
#include
#include
LiquidCrystal lcd(12,11,5,4,3,2);
void setup()
{
Serial.begin(9600);
Wire.begin(1);
Wire.onReceive(ruso);
lcd.begin(16,2);
}
void loop()
{
delay(50);
}
void ruso(int y)
{
signed int voltage=Wire.read();
lcd.setCursor(0,0);
lcd.print("Voltage is");
//lcd.setCursor(0,1);
lcd.print(voltage);
//Serial.print("voltage value\n");
//Serial.println(5);
delay(50);
}
Here's an explanation of the code:
<!-- [if !supportLists]-->1. <!--[endif]-->Include Libraries:
<!-- [if !supportLists]-->· <!--[endif]-->#include
<!-- [if !supportLists]-->· <!--[endif]-->#include
<!-- [if !supportLists]-->2. <!--[endif]-->LCD Object Initialization:
<!-- [if !supportLists]-->· <!--[endif]-->LiquidCrystal lcd(12, 11, 5, 4, 3, 2);: This line initializes the LiquidCrystal object lcd for controlling the LCD. It specifies the pins to which the LCD data lines (RS, E, D4, D5, D6, D7) are connected.
<!-- [if !supportLists]-->3. <!--[endif]-->Setup Function:
<!-- [if !supportLists]-->· <!--[endif]-->void setup(): This is the setup function, which runs once when the Arduino is powered on or reset.
<!-- [if !supportLists]-->· <!--[endif]-->Serial.begin(9600);: Initializes the serial communication with a baud rate of 9600. This allows you to send debugging or informational messages to the Arduino's serial monitor.
<!-- [if !supportLists]-->· <!--[endif]-->Wire.begin(1);: Initializes the I2C communication with the Arduino acting as an I2C slave device with the address 1. The Arduino can receive data over the I2C bus with this setup.
<!-- [if !supportLists]-->· <!--[endif]-->Wire.onReceive(ruso);: Sets up an event handler function named ruso to be called when data is received over I2C. This function will handle incoming I2C data.
<!-- [if !supportLists]-->· <!--[endif]-->lcd.begin(16, 2);: Initializes the LCD with 16 columns and 2 rows. This specifies the dimensions of the LCD.
<!-- [if !supportLists]-->4. <!--[endif]-->Loop Function:
<!-- [if !supportLists]-->· <!--[endif]-->void loop(): This is the loop function, which continuously runs after the setup function.
<!-- [if !supportLists]-->· <!--[endif]-->delay(50);: It introduces a delay of 50 milliseconds, effectively slowing down the loop execution. This delay is typically used to prevent the loop from running too rapidly.
<!-- [if !supportLists]-->5. <!--[endif]-->I2C Data Reception Function (ruso):
<!-- [if !supportLists]-->· <!--[endif]-->void ruso(int y): This is the I2C data reception function. It is called when data is received over the I2C bus.
<!-- [if !supportLists]-->· <!--[endif]-->signed int voltage = Wire.read();: It reads the incoming byte from the I2C bus and stores it in the voltage variable. This assumes that the sender is sending a single byte representing voltage.
<!-- [if !supportLists]-->· <!--[endif]-->lcd.setCursor(0, 0);: Sets the cursor position on the LCD to the top-left corner.
<!-- [if !supportLists]-->· <!--[endif]-->lcd.print("Voltage is");: Prints the text "Voltage is" on the first line of the LCD.
<!-- [if !supportLists]-->· <!--[endif]-->lcd.print(voltage);: Prints the received voltage value on the LCD screen.
<!-- [if !supportLists]-->· <!--[endif]-->delay(50);: Introduces a delay, which is common in I2C communication to allow time for data reception.