1. Home
  2. RG Guides
  3. RG Arduino Library Guide
  4. LCD-I2C

LCD-I2C

Displaying RG-15 Data On A LCD Screen

  • This example shows how to take the serialized data from the RG device and show it on a 16×2 LCD Screen.
  • Before starting this make sure you have completed the RG Serial Output tutorial.
  • Materials: Arduino, RG-15, mUSB cable, 8 connector wires (ground x2, 5v, V+, Rx, Tx, SDA, SCL), 16×2 LCD screen with IC2 Interface installed, and Breadboard.
  • Note: If using the Arduino 33 IoT nano you must jump the VUSB on back of board. See here

Installation

  1. Please install the LCD Library.
  2. In Arduino IDE go to File->Examples and find RG15Examples (near bottom).
  3. For this tutorial we will be using LCDScreen.

Build

Arduino nano 33 IoT:

RG Connectors:
Color Arduino nano Arduino Mega Arduino UNO RG-15
Red 5v 5v 5v V+
Green Ground(4) GND GND G
Orange Rx(1) 10 2 SO
Yellow Tx(0) 11 3 SI
LCD Connectors:
Color Arduino nano Arduino Mega Arduino UNO LCD-IC2
Red 5v 5v 5v VCC
Black Ground(~4) GND GND GND
Orange A4 SDA SDA SDA
Yellow A5 SCL SCL SCL
33IOTWiring Note that the RG has the same pins from the last tutorial, everything done is built upon the serial output example. Uno Diagram LCD Pinout The LCD screen requires 5v and MUST be powered by the arduino and not an external power supply as I2C requires this.

Code

This is a follow along for the examples provided in the Hydreon Arduino library. If you just want it working and don’t need a detailed explanation, run the example after getting to this step and It will work as is. Verify it is working by comparing your screen output with the images found in the conclusion step near the bottom. The LCD screen should post clear and clean information about the RG sensor data. Using the LCD library values can easily be printed on the screen. First we need to assign the LCD address:
LiquidCrystal_I2C lcd(0x27, 16, 2);

This line represents a 16×2 LCD screen on address 0x27 Note: If 0x27 does not work for you, please try finding your IC2 address by scanning using this. Remember if you switch address off of 0x27 you must wire your arduino differently then the tutorial. The next step is to initialize the LCD screen:
void setup() {
 lcd.begin();
  ...

  
  mySerial.begin(9600);
  rg15.setStream(&mySerial);
}

The above code is pretty simple, initialize LCD screen and then set the stream for the RG-15. createChar() is also in setup and will look confusing right now but it will be explained later. Lets take apart the loop:
void loop() {
...
      if(rg15.eventAcc > 0) {
	      lcd.clear();
	      display("Event" , rg15.eventAcc, true);
	      //Accumulation since last pole display.
	      //Only shown if it is greater then 0.
	      if(rg15.acc > 0) {
	          lcd.setCursor(12,1);
	          lcd.print("+");
	          lcd.print(rg15.acc * 1000, 0); 
	          lcd.print("mI");
	      }

      //IPH display
      lcd.setCursor(8,0);
      lcd.print(rg15.rInt,3);
      lcd.print("IPH");

      delay(5000); //wait a bit before changing screens.
      
    }
}

The LCD screen will have two screens:
  1. Displays total accumulation and an icon of the current weather
  2. Displays Event Accumulation, IPH, and Accumulation since last read.
The code above is for the second screen. If event accumulation gets detected then a rain event is present and we then display in order:
  1. Event Accumulation.
  2. If rg15.acc is present(accumulation since last poll) then we display in bottom left.
  3. Display IPH on top right of screen.
The next step displays the total screen which presents regardless of rain conditions:
 if(rg15.eventAcc > 0 or !lastCycleSunny) { // Total accumulation screen.
      //if the last screen frame shown was a sunny frame and it is still sunny,
      //we should not refresh the screen.
      lcd.clear();
      display("Total" , rg15.totalAcc, true);
      lastCycleSunny = !(rg15.eventAcc > 0);
      (lastCycleSunny) ? displaySun() : displayRain();
    }
display() is a helper function that takes parameters and prints it on screen. We will see the code in the next step.
void display(String name, float value, boolean unit) {
  lcd.setCursor(0,0);
  lcd.print(name);
  lcd.setCursor(0, 1);
  lcd.print(value, 3);
  if (unit) {
    lcd.print(rg15.unit[0]);
    lcd.print(rg15.unit[1]);
  }
}

lcd.setCursor(row, col) sets internal cursor to position of (row,col). Think of the cursor the same way it would act in a word document on a computer. When lcd.print() is called the start position is at (row, col). lcd.print() writes a character at the current cursor position. The RG-15 supports metric and imperial measurements we have to make sure that we write the correct unit:
if (unit) {
        lcd.print(rg15.unit[0]);
        lcd.print(rg15.unit[1]);
}

The RG library uses a char array instead of a string to represent a unit so unit[0] and unit[1] are both printed in order.

Custom Rain Drop and Sun Icon

To make custom characters hex arrays are created to represent which pixels should be on. The example provides 7 hex arrays to create an image of a sun and create an icon of rain:
uint8_t drop[8]  = {0x4, 0x4, 0xe, 0xe, 0x1f, 0x1f, 0x1f, 0xe};

//sun symbol binary data for cosmetic purposes.
uint8_t sun0[8] = {0x4,0x2,0x1,0x0,0x0,0x3,0x0,0x0};
uint8_t sun1[8] = {0x4,0x4,0x0,0xe,0x1f,0x1f,0x1f,0xe};
uint8_t sun2[8] = {0x0,0x8,0x10,0x0,0x0,0x1c,0x0,0x0};

uint8_t sun3[8] = {0x2,0x4,0x0,0x0,0x0,0x0,0x0,0x0} ;
uint8_t sun4[8] = {0x4,0x4,0x8,0x0,0x0,0x0,0x0,0x0};
uint8_t sun5[8] = {0x18,0x04,0x0,0x0,0x0,0x0,0x0,0x0};
Register the hex arrays with lcd.createChar():
 //make drop icon.
  lcd.createChar(0, drop);
  //make sun icon.
  lcd.createChar(1, sun0);https://rainsensors.com/wp-admin/tools.php
  lcd.createChar(2, sun1);
  lcd.createChar(3, sun2);
  
  lcd.createChar(4, sun3);
  lcd.createChar(5, sun4);
  lcd.createChar(6, sun5);
  
  lcd.home();
  //end sun icon
After assigning the custom character addresses we can call then with the lcd.write() function to display rain and sun these helper functions are in the example:
void displayRain() {
  lcd.setCursor(11, 0);
  lcd.write(0);
  lcd.setCursor(9, 1);
  lcd.write(0);
  lcd.setCursor(0,0);
}

void displaySun() {
  lcd.setCursor(10,0);
  lcd.write(1);
  lcd.write(2);
  lcd.write(3);
  lcd.setCursor(10,1);
  lcd.write(4);
  lcd.write(5);
  lcd.write(6);
  lcd.setCursor(0,0);
}

Conclusion

When the RG boots, it should show a sun Icon on the screen. This icon should only be shown when no rain event is detected. The screen should look something like this: NoRainTotal When raining the total screen should show up like this: RainTotal Once it starts raining(you can simulate rain by tapping on the RG lense) the device should show images like this. Event no Acc Event with acc The screen should be switching to a different statistic every 5 seconds from Event Acc to Total Acc. It’s also normal to not see anything in top right all the time as we only show accumulation when it is greater than 0.
Was this article helpful to you? Yes 1 No

How can we help?