Challenge

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

The code for this project 1 is attached as images below:

Code:

#include <LiquidCrystal_I2C.h>
#include <Wire.h>
#include <stdint.h>

//Declare a global variable to store the address of the slave LCD
uint8_t address;
unsigned int flag = 0;

void setup() {
int8_t count = 0; // To get the number of devices connected to the master
Wire.begin(); // To start the I2C protocol
Serial.begin (9600); //To start serial communication
Serial.println ("I2C scanner. Scanning ...");
/*Starting from 0 to 127 addresses check what is the address of the slave
if the slave address is matched print the address*/
for (int8_t i = 0; i < 127; i++)
{
Wire.beginTransmission (i);
if (Wire.endTransmission () == 0)
{
/*If the slave address is found only enter this loop*/
Serial.print ("Found address: ");
Serial.print (i);
count++; //No:of:devices(slave connected)
address = i;//Store the address to a global variable
}
} // end of for loop
Serial.println ("\nDone.");
Serial.print ("Found ");
Serial.print (count);
Serial.println (" device.\n");
}

void loop() {
//Initializing the LCD
LiquidCrystal_I2C lcd(address, 16, 2);
//To run it only once
if(!flag)
{
//Initializing the LCD
lcd.init();
//Turn on the LCD backlight
lcd.backlight();
//CLear the LCD screen
lcd.clear();
//Setting the cursor
lcd.setCursor(0,0);
lcd.print("Surya");
}
lcd.backlight();
lcd.setCursor(0,1);
lcd.print(flag);
//incrementing the flag to display
flag++;
delay(1000);
}

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.