Skip to content

ViSi-Genie Magic: Arduino Data Logger Application

Introduction

In this codebase example, the Arduino host sends simulated sensor data to the display module. The display module then logs the data to a text file on its uSD card. The display module also plots the sensor data on a scope widget. The host can also read the contents of the text file by sending the correct command to the display module. After receiving the read command, the display module will then send back to the host the contents of the text file in packets of data (255 bytes per packet). The host will receive these packets and will print them to the Serial Monitor of the Arduino IDE. Buttons were added to the interface so that the user can control when the logging starts and ends. The reading of data from the text file on the uSD card is also initiated with a button press.

This codebase example uses an Arduino Uno as the host and a uLCD-32PTU as the display module. The Uno uses a software serial port to communicate with the display module. It uses the hardware serial port Serial0 to communicate with the Serial Monitor of the Arduino IDE. The Uno prints to the Serial Monitor information regarding the status of the program, e.g. when data logging has started or stopped. The data being logged to or read from the uSD card is also printed on the serial monitor.

Prerequisites

This codebase example assumes the reader can program the 4D Systems display module using Workshop4 IDE ViSi-Genie environment. Beginners are advised to read the following aplication notes.

Instructions

Hardware Setup

  1. This codebase example uses the 4D Arduino Adaptor Shield II to interface the uLCD-32PTU to the Arduino Uno.

    4d-arduino-adaptor-shield

  2. The RX pin (of jumper J3) of the 4D Arduino Adaptor Shield is connected to pin 10 of the Uno. The TX pin (of jumper J4) is connected to pin 11 of the Uno.

    adaptor-shield-setup-zoomout

  3. This effectively connects the serial port of the display module to the software serial port being used by the Uno (as configured in the attached Arduino sketch). For more information, refer to the 4D Arduino Adaptor Shield datasheet and to the application note ViSi-Genie: Connecting a 4D Display to an Arduino.

    adaptor-shield-setup-zoomin

Programming the Display and Host

Follow the procedure below to run this example. The reader is also advised to watch the accompanying video.

  1. Extract the zip project file.

  2. Extract the content/s of the Workshop4 Project zip file to a folder.

  3. Open the project in Workshop4 IDE. !!! note that Workshop4 Pro is needed for this codebase example.

  4. Compile the project source code and download or upload the program to the target display module (a uLCD-32PTU in this codebase example). Copy the supporting files to a uSD card mounted to the PC. Properly unmount the uSD card from the PC and mount it to the uLCD-32PTU.

  5. Navigate to the Arduino sketch file to a folder.

  6. Open the sketch in the Arduino IDE, compile it, and upload the program to your target Arduino board (an Arduino Uno for this codebase example).

  7. Properly connect the Arduino Uno to the uLCD-32PTU.

    Note

    This codebase example, the 4D Arduino Adaptor Shield (rev 2.0) was used.

  8. Open the Serial Monitor of the Arduino IDE.

How The Program Works

Below are some pointers on how the program running on the Arduino host works.

Writing the Scope Object

In the sketch, values for the integer variable sensorValue are generated using the function random(...).

sensorValue = random(-80,80);

The lower and upper limits of the generated values correspond to that of the scope object on the display module.

Logging Data to the uSD Card of the Display Module

The user-defined function for logging data to the uSD card is:

void logDataToUSD(String filename, int counter, int value);

This function will log to the file "filename" the formatted data:

counter s value mV\n

Data Logging Frequency

Frequency of data logging was set to at least every 5 seconds so as not to "overwhelm" the software serial port (note that the main loop writes to the scope object at least every 100 ms).

lastLog = millis() + 5000;

The logging frequency can be increased by decreasing the literal constant value "5000" in the line above, more especially when using a hardware serial port to communicate with the display module.

Reading Data from the uSD Card of the Display Module

The user-defined function for initiating a data read is:

void ReadFromUSD(String filename);

The Function

void myGenieMagicHandler(uint8_t index, uint8_t length);

will then handle the receiving and printing of the data to the Serial Monitor.

Modifying/Improving this Example

This example was configured such that users can run it on a minimal hardware setup - an Arduino Uno which uses its single hardware serial port for communicating with the Serial Monitor of the Arduino IDE and a software serial port for communicating with the display module. The software serial port however has its limitations which compromise the performance of applications that make intensive use of the serial communication link, such as this codebase example. For this reason, users who intend to use or improve further this codebase example for bigger projects are encouraged to use a hardware serial port of the Arduino host when interfacing it to the display module. This way, the baudrate can be increased further which would mean better update frequency of the scope object. Uncaught events from the buttons on the display module would also be avoided.

Note

The Arduino Uno has only one hardware serial port, while the Mega and Due have four hardware serial ports available. Visit the Arduino website for more information.

To use a hardware serial port (e.g. Serial1) to communicate with the display module, modify lines 66 and 67 of the attached sketch like as shown below:

Serial1.begin(9600);  
genie.Begin(Serial1);

The baudrate can also be increased up to 200000 bps.

Serial1.begin(200000);  
genie.Begin(Serial1);

Note

The baudrate configuration of the attached ViSi-Genie project should also be updated accordingly to match that of the Arduino sketch/program. Of course the TX and RX lines of the display module should also be properly connected to the RX and TX pins of the host's selected serial port. All lines related to the use of the software serial port can now be removed from the Arduino sketch. For more information, refer to the application note ViSi-Genie: Connecting a 4D Display to an Arduino.

When using a hardware serial port to communicate with the display, the user can also improve the update rate of the scope object further by experimenting with line 112 of the attached sketch. For instance, the line

lastRun = millis() + 100;

could be changed to

lastRun = millis() + 50;

A major revision of the attached sketch would be needed for users who intend to interface the display to the hardware serial port Serial0 of the Arduino host. As mentioned above, this example uses Serial0 to print information to the Serial Monitor of the Arduino IDE. Hence, Serial0 cannot be used to communicate with the display module, unless the sketch is modified.

Caution

For this codebase example, do not use the ViSi-Genie-Arduino library method "WriteOjbect(...)" inside the function "myGenieMagicHandler(...)". Doing so would cause the software serial port to "slow down".

Attachment

Project File