Hi! I wonder if any one can help with this problem please?
I have built a supply voltage checker and display. The code is intended to display the measured voltage on an LCD and OLED displays at the same time. (The idea was to send it to my son to show how both worked whether he used an LCD or OLED display.) In practice only the LCD displays the voltage UNLESS I comment out all the bits about the LCD screen, and the Oled works as designed.
I am guessing there is some sort of address conflict but an i2s address checker shows two devices at 0x27 and 0x3C and by disconnecting them, they are the LCD and OLED displays.
I cannot get the Oled to display as well. Any suggestions would be very helpful. Here is my code:
/* Battery_Checker.io
This sketch displays the supply voltage up to a maximum on 25 Volts. It is written as a called procedure so you can copy
it into another sketch or add additonal code for other functions.
The following libraries are required:
LiquidCrystal I2C by Marco Schwarz or the Arduino Sensor Kit if the OLED screen is to be used.
ArduinoSensorkit
Three LEDs are required: Red; Blue and Green, to indicate over voltage; under voltage and sufficient voltage from the supply.
If it is rerquired for the Arduino to be shut down to protect it from over voltage supply, pin D7 must be connected to the
relay shown in the circuit diagram in Battery_Checker.pdf.
IMPORTANT: This sketch works with a 20x4 or a 16x2 character LDC screen via an I2C interface, or the Arduino Sensor Kit OLED
screen. Be sure to uncomment out the screen required and comment out the unrequired screens.
The following options are included:
1. Minimum voltage warning (useful for LiPo and lead-acid batteries that must not be discharged below a specified voltage.)
2. Over voltage warning.
3. Optional add on circuit to power down the attached Arduino device to protect excessively discharging a battery and
excess supply voltage from damaging the Arduino.
LCD and OLED Connections: Battery Test Connections: LED Connections:
GND - GND R1 4.7K between A3 and supply voltage. Red LED from D2 via 330R to Ground.
Vcc - 5V R2 1.2K between GND and A3. Blue LED from D3 via the same 330R to Ground.
SDA - Pin A4 (use the Arduino's regulated 5V for tests.) Green LED from D4 via the same 330R to Ground.
SCL - pin A5
Measure the voltage between the supply and ground with a digital multimeter and adjust the fraction (100/100) in line 49 until
the LCD display value agrees with the multimeter.
*/
#include <Arduino_SensorKit.h> // Required to run the OLEDscreen.
#include <LiquidCrystal_I2C.h> // Required to run the LCD screens via the I2C piggy back board.
// LiquidCrystal_I2C lcd = LiquidCrystal_I2C(0x27, 20, 4); //Addresses the 20x4 character LCD.
LiquidCrystal_I2C lcd = LiquidCrystal_I2C(0x27, 16, 2); //Addresses the 16x2 character LCD.
float MinSupplyVoltage = 9; // This is the miminum regulatable voltage for an Arduino Uno. Adjust for the battery used.
float MaxSupplyVoltage = 13; // !! piut back to 20V This is the maximum external input voltage for the Arduino Uno. Adjust for the specific model.
unsigned int SupplyVoltage = 0;
float correctionFactor = (100/100); // Change the values of this fraction to compensate for inaccuracies in R1 and R2.
void setup()
{
pinMode(2, OUTPUT); // Red LED on pin D2.
pinMode(3, OUTPUT); // Blue LED on pin D3.
pinMode(4, OUTPUT); // Green LED on pin D4.
pinMode(7, OUTPUT); // From pin D7 to supply relay.
lcd.init(); // Initialises the LCD.
lcd.backlight(); // Turns on the backlight.
lcd.begin(16, 2); // Tells the lCD it is a 16x2 display. (you'd think it would know!)
// lcd.begin(20, 4); // Tells the LCD it is a 20x4 display.
lcd.clear(); // Clears the screen, just in case.
//Uncomment this section if an OLED screen is to be used AND comment out the section above.
Oled.begin();
Oled.setFlipMode(true);
//
}
void loop()
{
//... Replace this line with the code you want to run...
checkBattery(); // This calls the checkBattery function below. Once the function has competed it returns to continue from the line below.
//... and replace this line with any other code you want to run...
delay(100); // Just because every sketch has one somewhere!
}
void checkBattery()
{
digitalWrite(2, LOW); // Ensures all of the LEDs and the relay are off.
digitalWrite(3, LOW);
digitalWrite(4, LOW);
digitalWrite(7, LOW);
SupplyVoltage = analogRead(A3); // Reads the supply voltage.
SupplyVoltage = correctionFactor * (map(SupplyVoltage, 0, 1023, 0, 250)); // The last value 250' is 'Maximum accepted input voltage * 10.)
if (SupplyVoltage / 10 > MaxSupplyVoltage) { // The maximum external supply for the Arduino Uno is 20V, so set to 19V.
digitalWrite(2, HIGH); //RED
delay(500);
digitalWrite(7, HIGH); // Opens the NC (normally closed) relay on the supply.
digitalWrite(2, LOW); // This line is redundant if the relay depowers the Arduino. Left in in case it fails!
}
if (SupplyVoltage / 10 >= MinSupplyVoltage and SupplyVoltage <= MaxSupplyVoltage ) ; {
digitalWrite(4, HIGH); //GREEN
}
if (SupplyVoltage / 10 < MinSupplyVoltage) {
digitalWrite(3, HIGH); //BLUE
}
lcd.setCursor(0, 0); //Sets the start position on the LCD screen. The format is (column, row).
lcd.print("Battery: ");
if (SupplyVoltage < 100) {
lcd.print(" ");
}
if (SupplyVoltage < 10) {
lcd.print(" ");
}
lcd.print((SupplyVoltage / 10), DEC); // The command DEC ensures the value is decimal.
lcd.print(".");
lcd.print((SupplyVoltage) % 10, DEC);
lcd.print("v");
// Uncomment this section if an OLED screen is to be used AND comment out the LCD code above.
Oled.clearDisplay(); // !! May not be needed !!
Oled.setFont(u8x8_font_chroma48medium8_r);
Oled.setCursor(0, 3);
Oled.print("Batt: ");
Oled.print(SupplyVoltage/10);
//
}
with many thanks