DGH Modules Communicate with Arduino Mega2560 Microcontroller

Arduino boards have lowered the barrier for people looking to automate projects and functions, making these pursuits more accessible than ever before! Our data acquisition modules further enhance these types of projects by expanding the connectivity possibilities, as well as providing essential data logging functionality crucial to projects that rely on triggers set off by an input value or threshold. Once you have found the perfect module for your project, the following instructions will walk you through how to set up Arduino automation with DGH modules; more specifically, the Arduino Mega2560.

A sample program and Communications Library is available from DGH Corporation for use with the Arduino Mega2560. The sample program illustrates how to set up the Mega2560 serial communications port, build a command message for the DGH module, transmit the command and receive the response message. The program transmits and receives data through the Mega2560 Serial1 port using pins #18 and #19. The Mega2560 serial pins are TTL level and therefore will require a serial communications interface driver chip in order to connect to the DGH module. Select a communications driver chip that will establish the proper signal levels for either RS-232 or the RS-485 standard.

The sample source code is heavily commented and easy to follow. It utilizes a DGHClass Communications library that includes individual functions for sending analog and digital I/O commands such as Read Data (RD), Analog Output (AO), Digital Input (DI), Digital Output (DO) and Read Events (RE). The DGHClass Library is easy to follow and may be expanded to include additional module commands, further simplifying Arduino automation.

The DGHClass Library also includes a non-blocking serial processor function that transmits commands and receives response characters. The non-blocking serial processor uses a state machine to control the serial I/O process. The state machine has four separate states: DoNothing, Transmit, Receive and MessageAvailable. The Transmit State sends a command to the module and then returns back to the main loop. The Receive State will check for available characters, store them in a response buffer and detect the end of a message. The Message Available state indicates that a valid message has been received. This state is also monitored in the program main loop() and data can be parsed when detected.

Simply set the baud rate in Setup() and place the three function calls in the main loop() of the Arduino Mega2560 program for the fastest command/response processing. See the abbreviated sample code on the right.

To download the fully commented version of the sample program and the DGHClass Library then click on the following link : DGH_AsciiProtocol.zip.

In conclusion, the sample program and library allow for Arduino automation with DGH Modules by communicating with the Mega2560 using the DGH ASCII protocol. The library performs all the important analog and digital I/O functions for controlling DGH modules. The sample program can be easily expanded to develop a more complicated application and the DGHClass library can be easily integrated into an existing application.

TESTING NOTE: When using a brand-new module from DGH, set the baud rate in the example Setup() to 300, compile the code and start communicating with the module without performing any module setup changes. Higher baud rates will be more beneficial in the final application.

#include “dgh.h”

// Define DGH Class
DGHClass DGH;

//
// Program Setup Function
//
void setup() {
// Initialize Serial1 on Mega2560 to 19,200
DGH.begin(19200);
}

// 
// Main Loop
//
void loop() {
// **************************************
// STEP 1 : Trigger Any Command
// ************************************** 
if (DGH.getCommState() == DGH_IOSTATE_NONE) 
// Transmit A Command
DGH.RD(ASCII_1);
// **************************************
// STEP 2 : ALWAYS CALL the Non-Blocking Communications Processor
// ************************************** 
DGH.processCommand();

// ************************************** 
// STEP 3 : Check for Response Message
// ************************************** 
if (DGH.msgAvailable()) {
// Process Response
if (strlen(DGH._rxbuf) > 0) {
// Have Valid Response

// Parse the Data Value
}

// Reset the Status Bit – Ready to Send Next Command
DGH.setCommState(DGH_IOSTATE_NONE);
}
}

Click outside to hide the comparison bar
Compare
×