1. Home
  2. RG Arduino Guide
  3. Reading Serial Data With Arduino

Reading Serial Data With Arduino

While we have an Arduino Library available this example provides a way to make your own Arduino Scratch file without using our provided one. For tutorial purposes we will be using an Arduino Uno for programming but the general principle is the same between devices.

Some helpful information for the RG device is found in this pdf https://rainsensors.com/wp-content/uploads/sites/3/2020/07/rg-15_instructions_sw_1.000.pdf.

Principle

Much like an Arduino the RG-15 sends and receives serial data. Our goal is to send all data from the RG through the Arduino and right to our computer.

In the pdf linked above it explains how sending specific characters can command the device. In polling mode the RG-15 will send data back whenever it is sent an ‘r’

Diagram

Code

Starting with a fresh file the first step is to set the stream between Arduino and RG.

First include the SoftwareSerial Library:

#Include <SoftwareSerial.h>

With SoftwareSerial we assign two pins as our Rx and Tx Pins in this case they are set to two and three:

SoftwareSerial unoSerial = SoftwareSerial(2,3);

Finally open the stream and set the baud rate with:

  unoSerial.begin(9600);

To receive data from the device we must send an ‘r’ command and then grab its response:

  unoSerial.write('r');
  unoSerial.write('\n');
  String response = unoSerial.readStringUntil('\n');
  Serial.println(response);
  delay(1000);

Serial.println won’t work however because we need to open that Serial port so add this to setup():

  Serial.begin(9600);

Full Code


#include <SoftwareSerial.h>

SoftwareSerial unoSerial = SoftwareSerial(2,3);

void setup() {
  Serial.begin(9600);
  unoSerial.begin(9600);
}

void loop() {
  unoSerial.write('r');
  unoSerial.write('\n');
  String response = unoSerial.readStringUntil('\n');
  Serial.println(response);
  delay(1000);
}
Was this article helpful to you? Yes No

How can we help?