Skip to content

Instantly share code, notes, and snippets.

@mcorrigan
Last active November 10, 2023 20:14
Show Gist options
  • Save mcorrigan/5e5d5b39c03db5748b021da0409a3e58 to your computer and use it in GitHub Desktop.
Save mcorrigan/5e5d5b39c03db5748b021da0409a3e58 to your computer and use it in GitHub Desktop.
Arduino Invertek Optidrive E3 VFD ModbusMaster Test
#include <Arduino.h>
#include <ModbusMaster.h>
#define MAX485_DE_RE 4
#define OUTPUT_MOTOR_FREQ 0x0007
#define DRIVE_STATUS 0x0006
#define DRIVE_TEMP 0x0018
ModbusMaster node;
void preTransmission()
{
digitalWrite(MAX485_DE_RE, HIGH);
}
void postTransmission()
{
digitalWrite(MAX485_DE_RE, LOW);
}
void setup()
{
Serial.begin(115200);
while(!Serial);
// Setup our software serial connection for modbus
Serial1.begin(115200);
// set the pin controlling MAX485 send/receive to be an output pin
pinMode(MAX485_DE_RE, OUTPUT);
// Init in receive mode
digitalWrite(MAX485_DE_RE, LOW);
// Modbus slave ID 1
node.begin(1, Serial1);
// Callbacks allow us to configure the RS485 transceiver correctly
node.preTransmission(preTransmission);
node.postTransmission(postTransmission);
}
void loop()
{
uint8_t result;
uint16_t data;
Serial.println("sending...");
// Query the Invertek E3 VFD for the current temperature
result = node.readHoldingRegisters(DRIVE_TEMP, 1);
// determine if the query result was successful or not
if (result == node.ku8MBSuccess) {
Serial.println("It worked!");
data = node.getResponseBuffer(0);
Serial.print("Data: ");
Serial.println(data);
}else if (result == node.ku8MBInvalidFunction){
Serial.println("Invalid Function");
}else if (result == node.ku8MBInvalidSlaveID){
Serial.println("Invalid Slave ID");
}else if (result == node.ku8MBInvalidCRC){
Serial.println("Bad CRC");
}else if (result == node.ku8MBResponseTimedOut){
Serial.println("Time out");
}else{
Serial.println("Something else...");
}
// do whatever else we need to -- or at the very least, slow down the queries while testing
delay(1000);
}
@mcorrigan
Copy link
Author

Updated the code to use Serial1 instead of SoftwareSerial since the latter was causing quite a bit if flakiness in the VFD communication.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment