Arduino Example

This source code is provided as-is, without any warranties, express or implied, including but not limited to fitness for a particular purpose or merchantability. The user assumes full responsibility for testing, validation, and ensuring compliance with all applicable regulations and standards.

Coding for the IS4310 requires no dedicated library, making it easy to maintain and port to new Arduino boards or other microcontrollers.

This code reads the Modbus Slave ID and prints it to the terminal. Then, it stores a humidity variable in Modbus Holding Register address 0. This variable can be accessed by a Modbus Master device, such as a PC, PLC, or other controller.

You can download the Arduino project from the IS4310 product page.

This example uses the Kappa4310Ard Evaluation Board. Check the Kappa4310Ard product folder for more information.

#include <Wire.h>

void writeHoldingRegister(uint16_t holdingRegisterAddress, uint16_t data) {
  Wire.beginTransmission(0x11); // This is the I2C Chip Address of the IS4310. Never changes.  
  // A Holding Register address is 16-bits long, so we need to write 2 bytes to indicate the address. 
  Wire.write((holdingRegisterAddress >> 8) & 0xFF); // Send high 8-bits of the Holding Register Address we want to write. 
  Wire.write(holdingRegisterAddress & 0xFF); // Send low 8-bits of the Holding Register Address we want to write.
  // A Holding Register data register is 16-bits long. So we need to write 2 bytes to make a full Holding Register Write:
  Wire.write((data >> 8) & 0xFF); // Send high 8-bits of the data we want to write to the Holding Register. 
  Wire.write(data & 0xFF); // Send low 8-bits of the data we want to write to the Holding Register. 
  Wire.endTransmission(); 
}

uint16_t readHoldingRegister(uint16_t holdingRegisterAddress) {
  uint16_t result; // This is the variable where the read data will be saved. 
  Wire.beginTransmission(0x11); // This is the I2C Chip Address of the IS4310. Never changes. 
  // A Holding Register address is 16-bits long, so we need to write 2 bytes to indicate the address. 
  Wire.write((holdingRegisterAddress >> 8) & 0xFF);  // Send high 8-bits of the Holding Register Address we want to read. 
  Wire.write(holdingRegisterAddress & 0xFF);         // Send low 8-bits of the Holding Register Address we want to read.
  Wire.endTransmission(false);
  // A Holding Register data register is 16-bits long. So we need to read 2 bytes to make a full Holding Register Read:
  Wire.requestFrom(0x11, 2);  // From the IS4310, request 2 bytes (2 bytes make a full Holding Register).
  result = Wire.read(); // Read the first byte. 
  result = result << 8; // Make space for the second byte. 
  result = result | Wire.read(); // Read the second byte. 
  return result; // Return the read 16-bit register. 
}

void setup() {
  uint16_t ModbusSlaveID;

  Wire.begin(); // Initialize the I2C. 
  Serial.begin(9600); // Initialize  the Serial for the prints. 

  // The Modbus Slave ID is stored in the Holding Register Address 500 of the IS4310, let's read it:
  ModbusSlaveID = readHoldingRegister(500); 
  
  // Let's print the read Modbus Slave ID:
  Serial.println("");
  Serial.print("The Modbus Slave Address is: ");
  Serial.println(ModbusSlaveID);
}

void loop() {
  uint16_t humidity = 47; // Let's imagine a humidity sensor that reads a level of 47% RH. 

  // Let's write the humidity to the Holding Register Address 0:
  writeHoldingRegister(0, humidity);

  delay(1000);
}

Last updated