Good afternoon arduino community friends, the code that I will show below shows certain parameters on the 16X2 LCD screen but I need to modify it so that it works on the 16x2 screen with the i2c module but I don't know what parts have to be changed to make it work ,I would be very grateful if someone knew how to do it and, could explain and teach to me what needs to be modified and why, thank you very much in advance.
code:
#include <LiquidCrystal.h>
LiquidCrystal lcd(7,8,10,11,12,13);
int num_Measure = 128 ; // Set the number of measurements
int pinSignal = A0; // pin connected to pin O module sound sensor
int redLed = 5;
long Sound_signal; // Store the value read Sound Sensor
long sum = 0 ; // Store the total value of n measurements
long level = 0 ; // Store the average value
int soundlow = 40;
int soundmedium = 500;
void setup ()
{
pinMode (pinSignal, INPUT); // Set the signal pin as input
Serial.begin (9600);
lcd.begin(16,2);
}
void loop ()
{
// Performs 128 signal readings
for ( int i = 0 ; i <num_Measure; i ++)
{
Sound_signal = analogRead (pinSignal);
sum =sum + Sound_signal;
}
level = sum / num_Measure; // Calculate the average value
Serial.print("Sound Level: ");
lcd.print("Sound Level= ");
Serial.println (level-33);
lcd.print(level-33);
if(level-33<soundlow)
{
lcd.setCursor(0,2);
lcd.print("Intensity= Low");
digitalWrite(redLed,LOW);
}
if(level-33>soundlow && level-33<soundmedium)
{
lcd.setCursor(0,2);
lcd.print("Intensity=Medium");
digitalWrite(redLed,LOW);
}
if(level-33>soundmedium)
{
lcd.setCursor(0,2);
lcd.print("Intensity= High");
digitalWrite(redLed,HIGH);
}
sum = 0 ; // Reset the sum of the measurement values
delay(200);
lcd.clear();
}
The easiest way to get a 16X2 I2C character display (hd44780 controller) to work is to use the hd44780 library.
For an I2C LCD display to work, the I2C address and the I2C backpack to LCD pin mapping must be correct. If the library default settings for either or both are not correct the LCD will not work. You can try to figure out the right pin mapping and use an I2C scanner to find the address, but if you install and use the hd44780 library that is done automatically by the library.
Install the hd44780 library. The hd44780 library is the best available for I2C LCDs. The library is available in the Library Manager. Go to Library Manager (in the IDE, Sketch, Include Libraries, Manage Libraries) and in the Topics dropdown choose Display and in the Filter your search box enter hd44780. Select and install the hd44780 library by Bill Perry.
The class that you want to use is the hd44780_I2Cexp class. There are examples to show how to use the library. The nice thing about the hd44780 library is that it will autodetect the I2C address and the I2C backpack to LCD pin mapping.
In the examples, there is a diagnostic sketch that will help us to help you if you still have trouble with the display. Run the diagnostic sketch and post the results.
To expand on groundFungus's comment a bit:
All you need to do is either buy another lcd with an i2c lcd backpack on it, or solder an i2c lcd backpack to the LCD you already have.
Both are inexpensive and readily available on ebay.
Install the hd44780 library using the IDE library manager, but not from a zip file.
in your sketch, change this:
#include <LiquidCrystal.h>
LiquidCrystal lcd(7,8,10,11,12,13);
to this:
#include <Wire.h>
#include <hd44780.h> // main hd44780 header
#include <hd44780ioClass/hd44780_I2Cexp.h> // i2c expander i/o class header
hd44780_I2Cexp lcd; // declare lcd object: auto locate & auto config expander chip
But before you actually try it in your own sketch, run the included diagnostic sketch, I2CexpDiag, to test everything.
Your code using the hd44780 library. Note the changes to the setCursor() functions. The lines on a 16x2 are labeled 0 and 1. Tested on my Uno with 16x2 display. Display works, I can't test the rest.
//#include <LiquidCrystal.h>
//LiquidCrystal lcd(7,8,10,11,12,13);
// changed only the following 4 code lines to make compatable with hd44780 library
// see the hello world example in the hd44780_I2Cexp examples
#include <Wire.h>
#include <hd44780.h> // main hd44780 header
#include <hd44780ioClass/hd44780_I2Cexp.h> // i2c expander i/o class header
hd44780_I2Cexp lcd; // declare lcd object: auto locate & auto config expander chip
int num_Measure = 128 ; // Set the number of measurements
int pinSignal = A0; // pin connected to pin O module sound sensor
int redLed = 5;
long Sound_signal; // Store the value read Sound Sensor
long sum = 0 ; // Store the total value of n measurements
long level = 0 ; // Store the average value
int soundlow = 40;
int soundmedium = 500;
void setup ()
{
pinMode (pinSignal, INPUT); // Set the signal pin as input
Serial.begin (9600);
lcd.begin(16,2);
}
void loop ()
{
// Performs 128 signal readings
for ( int i = 0 ; i <num_Measure; i ++)
{
Sound_signal = analogRead (pinSignal);
sum =sum + Sound_signal;
}
level = sum / num_Measure; // Calculate the average value
Serial.print("Sound Level: ");
lcd.print("Sound Level= ");
Serial.println (level-33);
lcd.print(level-33);
if(level-33<soundlow)
{
lcd.setCursor(0,1); // ******** the lines are nuimbered 0 and 1 on the 16x2 display
lcd.print("Intensity= Low");
digitalWrite(redLed,LOW);
}
if(level-33>soundlow && level-33<soundmedium)
{
lcd.setCursor(0,1); // ***************
lcd.print("Intensity=Medium");
digitalWrite(redLed,LOW);
}
if(level-33>soundmedium)
{
lcd.setCursor(0,1); // *****************
lcd.print("Intensity= High");
digitalWrite(redLed,HIGH);
}
sum = 0 ; // Reset the sum of the measurement values
delay(200);
lcd.clear();
}