# Arduino\_SDK Quick Start

### Arduino\_SDK Quick Start

### #The sample code's using board <a href="#woqom" id="woqom"></a>

Arduino Mega-2560 development board

### Where to download

Please check the below link to download the sample code.

<https://github.com/WITMOTION/WitHighModbus_HWT9073485>

### Routine Introduction

This routine introduces how to use the serial port 1 of the MEGA-2560 platform to connect the HW-97 module, and then perform high-precision Modbus protocol with the serial port of WitMotion HWT9053-485 sensor, and then directly print data through the serial port, receive sensor data and communicate with the sensor;

Before viewing this routine, please read the relevant sensor manual to understand the protocol used by the sensor and the basic functions of the sensor.

### How to wiring

Please prepare HWT9053-485 sensor, a 485 to TTL transfer module, MEGA-2560 development board and a serial port 3 in 1 adapter. Please reference to the below wiring:

<figure><img src="https://4238809933-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F7KeiQ2sbyWknusoV5r8U%2Fuploads%2FiRGsGfRTkHuhXtgGy7WN%2F0.png?alt=media" alt=""><figcaption></figcaption></figure>

* Copy the file wit\_c\_sdk to the library folder of Arduino's libraries

Paste path: arduino-1.8.15\libraries

<figure><img src="https://4238809933-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F7KeiQ2sbyWknusoV5r8U%2Fuploads%2F955Oz7FhjAxeQqbpxbFW%2Fimage.png?alt=media&#x26;token=177be3de-e656-4cce-8867-d7848a810ac4" alt=""><figcaption></figcaption></figure>

After pasting successfully：

<figure><img src="https://4238809933-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F7KeiQ2sbyWknusoV5r8U%2Fuploads%2FzIpNjCC4ndfAl4aKAXJU%2Fimage.png?alt=media&#x26;token=985924e4-2f36-4134-a57b-410a1f781df8" alt=""><figcaption></figcaption></figure>

* Use the Arduino software to download the program to the development board

<figure><img src="https://4238809933-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F7KeiQ2sbyWknusoV5r8U%2Fuploads%2Fw8kqWxNuAhaqSxENuFVz%2F3.png?alt=media&#x26;token=b6c5f763-f78a-4a90-9623-6ab222dd57f2" alt=""><figcaption></figcaption></figure>

<figure><img src="https://4238809933-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F7KeiQ2sbyWknusoV5r8U%2Fuploads%2Fk6zvQZN8owkFafQYAPFk%2F4.png?alt=media&#x26;token=dd097441-68ce-4537-b25f-9c1f604a9bc7" alt=""><figcaption></figcaption></figure>

Path：**wit\_c\_sdk\_arduino\examples\wit\_c\_sdk\_modbus**

<figure><img src="https://4238809933-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F7KeiQ2sbyWknusoV5r8U%2Fuploads%2FlcQbhDTesrBTSrDE3n4E%2Fimage.png?alt=media&#x26;token=baddf76c-9863-420a-8344-c707b0246e2c" alt=""><figcaption></figcaption></figure>

* Selection of development boards, processors, and serial ports

  <figure><img src="https://4238809933-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F7KeiQ2sbyWknusoV5r8U%2Fuploads%2FRYYEJ6qsOY7B8T9Y2Hba%2Fimage.png?alt=media&#x26;token=e1236ce0-384b-4b2c-8025-47ccd917ccbd" alt=""><figcaption></figcaption></figure>
* Upload and burn program

<figure><img src="https://4238809933-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F7KeiQ2sbyWknusoV5r8U%2Fuploads%2FaY83iHgmC0qBOgTdhnaK%2Fimage.png?alt=media&#x26;token=d6db185b-0fc8-49dd-be81-441e1bf6e005" alt=""><figcaption></figcaption></figure>

<figure><img src="https://4238809933-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F7KeiQ2sbyWknusoV5r8U%2Fuploads%2FmqBEKi2BjRm4AmcfcSLN%2F2.png?alt=media&#x26;token=98a3e9ca-727b-4a1e-aaeb-a7b82808f176" alt=""><figcaption></figcaption></figure>

**Reason for uoloaded item error：**

The serial port selection is wrong, the serial port is not selected, or the serial port is occupied

<figure><img src="https://4238809933-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F7KeiQ2sbyWknusoV5r8U%2Fuploads%2F0NCU7lOdDkBx8bkHBIrk%2Fimage.png?alt=media&#x26;token=77f5f6d1-90e9-436f-b8c3-b18c61742b96" alt=""><figcaption></figcaption></figure>

**The above prompts show that the program is successfully compiled and downloaded to the development board.**

* Open the serial port debugging assistant, power on, and the following information will be displayed:

  <figure><img src="https://4238809933-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F7KeiQ2sbyWknusoV5r8U%2Fuploads%2Fg6X8iOZnu4UXSfKHCWqe%2Fimage.png?alt=media&#x26;token=320a2875-ba0a-4e8d-ad2d-a33aa6cdaef5" alt=""><figcaption></figcaption></figure>

You can send corresponding instructions to configure the module through the prompt information.

### Initialization

&#x20;

Modbus is a one-question-one-answer interactive form, so the module needs to be continuously inquired before the data can be returned.

For the introduction of API functions, please read the WIT\_C\_SDKAPI function documentation.

<figure><img src="https://4238809933-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F7KeiQ2sbyWknusoV5r8U%2Fuploads%2FGMuxiHV6XV7DbmWvLWFW%2F11.png?alt=media" alt=""><figcaption></figcaption></figure>

Serial.begin(115200); //Initialize the print data serial port

RS485\_IO\_Init(); //485-pin configuration initialization

WitInit(WIT\_PROTOCOL\_905x\_MODBUS, 0x50); //Initialize the high-precision modbus protocol and set the device address

WitSerialWriteRegister(SensorUartSend); //Registers a callback function that writes out data

WitRegisterCallBack(CopeSensorData); //Register to get the sensor data callback function

WitDelayMsRegister(Delayms); //Register millisecond delay function

AutoScanSensor(); //Register millisecond delay function

### Receive the sensor's data

### Get data

We will create an array to store the read data into the array, and read the corresponding data directly according to the index. Since the module used this time is a high-precision modbus protocol, you need to provide the data start address and the amount of data to be read when you need to ask. For example, the example calls WitReadReg(AX,16) every 300ms; the starting address is AX (acceleration X value) and continuously reads 16 data volumes, and the final effect is to read the 3D acceleration, 3D angular velocity, 3D angle, and 3D magnetic field of the module. The data into the specified index array and temperature. Finally print it out.

<figure><img src="https://4238809933-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F7KeiQ2sbyWknusoV5r8U%2Fuploads%2Fu7sCb1ioVjBt9sGpeG6P%2F12.png?alt=media" alt=""><figcaption></figcaption></figure>

<figure><img src="https://4238809933-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F7KeiQ2sbyWknusoV5r8U%2Fuploads%2FadeYJ4JVgeqn23Bf3Ljr%2F13.png?alt=media" alt=""><figcaption></figcaption></figure>

<figure><img src="https://4238809933-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F7KeiQ2sbyWknusoV5r8U%2Fuploads%2FXc8y1bUeC7umstUsnigW%2F14.png?alt=media" alt=""><figcaption></figcaption></figure>

<figure><img src="https://4238809933-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F7KeiQ2sbyWknusoV5r8U%2Fuploads%2Ff6iBfyJm5YqpKr52RiUP%2F15.png?alt=media" alt=""><figcaption></figcaption></figure>

WitReadReg(AX, 16);//Interval reading module, the address starts from AX (acceleration X) to read 16 bytes of data. Refer to REG.H register list.

### Setting Sensor

The parameters of the module can be set through the function CmdProcess(); Such as acceleration calibration, magnetic field calibration and modifying baud rate etc.

<figure><img src="https://4238809933-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F7KeiQ2sbyWknusoV5r8U%2Fuploads%2FE2wobGB5wUPKA53GfsjF%2F16.png?alt=media" alt=""><figcaption></figcaption></figure>

<figure><img src="https://4238809933-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F7KeiQ2sbyWknusoV5r8U%2Fuploads%2FcokUDC1aOrfI01z9WkUs%2F17.png?alt=media" alt=""><figcaption></figcaption></figure>

* Open the serial port assistant, send the command B\r\n, and then observe the phenomenon.

  <figure><img src="https://4238809933-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F7KeiQ2sbyWknusoV5r8U%2Fuploads%2FDFILYtPJk6mcxdeOxgNv%2Fimage.png?alt=media&#x26;token=261a84d6-0ffc-45de-8cdd-e66230879307" alt=""><figcaption></figcaption></figure>

After sending B\r\n, the module will search for the device again, and prompt what baud rate is found. The window displays information prompting that the 115200 baud rate search is successful, and compares the sending instructions described in the HELP prompt information. The baud rate indicates that the setting is correct. Use other commands as needed.

### Accelemeter Calibration

Some common API function interfaces are defined in the file wit\_c\_sdk.c, which only needs to be called.

<figure><img src="https://4238809933-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F7KeiQ2sbyWknusoV5r8U%2Fuploads%2FBeD7kR3YYWgKPuIAmsMs%2F19.png?alt=media" alt=""><figcaption></figcaption></figure>

After sending a\r\n through the serial port assistant, after waiting for about 5 seconds, the X and Y axis angles will be calibrated to a state of about 0 degrees. Acceleration X, Y calibration is about 0g, Z is about 1g.

<figure><img src="https://4238809933-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F7KeiQ2sbyWknusoV5r8U%2Fuploads%2FSoaScLbWbnWOVfVBO6tZ%2Fimage.png?alt=media&#x26;token=d7e34818-eb28-4c48-a569-b3834c0cc54f" alt=""><figcaption></figcaption></figure>

### Magnetic field calibration

The magnetic field calibration needs to be sent to start the calibration first, then rotate around the three axes of the sensor and then end the calibration. First call WitStartMagCali(); to start magnetic field calibration, then rotate the sensor on three axes, and then call WitStopMagCali(); to end the calibration.

### More

Please reference to the sensor's datasheet.
