The Nextion portion of this tutorial is very simple. It is a basic display with a single object—a waveform. In the video, I show how to use the “add” command natively in the Nextion display. I am using a Basic 2.8′ display. You can download the final configuration below. The Nextion IDE version I used to create the file is V1.65.1
If you want to support this website, you can purchase the display by clicking here NEXTION WEBSITE or selecting a link from below.
WARNING: You must unzip the file and move it to your documents. If you fail to do this, you might not see all the tabs.
Suppose you don’t download the file or want to create the Nextion for your display. All you do is add a waveform, stretch it to extents, and change the ch property to 3. The ch property allows multiple lines on the screen. You use the pco0, pco1, and pco2 properties to set the colors for each line.
This tutorial will display accelerometer and Gyroscope data on the Nextion Basic display. I will use the basic configuration I use in most of my tutorials. You can download the configuration below.
The default code has a line to set how often the delay loop is run at line 8. Make sure the delayLength variable is set to 1000.
// NOTE : General Async Delay
unsigned long asyncDelay = 0;// NOTE : 4,294,967,295
int delayLength = 1000;
I use the WIRE library to access the I2C port on the Arduino. This is the only library I use in this tutorial. Libraries are like Flutterbirds. Some are friendly and easy to get along with; others are hard to understand and seem more trouble than they are worth.
#include <Wire.h>
int error; // NOTE : Included for reference only
The next three code sections need to be placed in the setup section of your code. I break it into three parts so I can better describe each part.
Section 1 of the setup()
I started with an informational message and then started the I2C configuration. Then I have a second informational message about the power register. If you are not familiar with accessing an I2C device without a library, there are a few things you need to know. There can be multiple devices in parallel on the I2C bus as long as they have different addresses. For this device, the address is 0x68 in hex. The connection starts with the device’s address in the “beginTransmission” command. This command is followed with two wire write commands. The first of these commands identifies the register address, and the second contains the value you want to write to the register.
When I had the program working properly, I went back to this area of the code and commented out the four lines that write to register 0x6B. The program ran fine. You may not need to set register 0x6B, but it will not hurt to include this portion of the code.
Serial.println("STARTING I2C...");
Wire.begin();
delay(200);
Serial.println("Configure the Power Management Register 0x00");
Wire.beginTransmission(0x68);
Wire.write(0x6B);
Wire.write(0x00);
Wire.endTransmission(true);
delay(500);
Section 2 of the setup()
After the power register is configured, the accelerometer and gyroscope registers must be configured. Data will display if you don’t configure the registers, but you won’t know the scale.
We will start with the gyroscope. After configuring the power management register and the next two registers, you should familiarize yourself with the process. The gyroscope register is 0x1B. There are four steps to load a register, begin the transmission, select register 0x1B, write 0x10 to the register, and end the transmission.
Serial.println("Configure the GYRO Register 0x00");
Wire.beginTransmission(0x68);
Wire.write(0x1B);
Wire.write(0x10);// NOTE: valid values 0x00,0x08,0x10,0x18
Wire.endTransmission(true);
delay(500);
I choose 0x10 for no particular reason. Below is a chart for the four different scale settings. I recommend trying different settings to see different results.
- +/- 250 degree per second write 0x00
- +/- 500 degree per second write 0x08
- +/- 1000 degree per second write 0x10
- +/- 2000 degree per second write 0x18
This tutorial is primarily focused on the accelerometer. I don’t adjust the math later in the tutorial. The equations used are all related to the sensitivity of the accelerometer. Below is a list of the sensitivity per bit of the gyroscope.
- REG VALUE DEG/SEC SENSITIVITY
- 0x00 250 131
- 0x08 500 65.1
- 0x10 1000 32.8
- 0x18 2000 16.4
Section 3 of the setup()
Next, we have the accelerometer configuration. The accelerometer register is 0x1C. We have the same four steps as the gyroscope and the power register. Begin the transmission, select register 0x1C, write 0x08 to the register, and end the transmission.
Serial.println("Configure the ACCEL Register 0x00");
Wire.beginTransmission(0x68);
Wire.write(0x1C);
Wire.write(0x08);// NOTE: valid values 0x00,0x08,0x10,0x18
Wire.endTransmission(true);
delay(500);
Just like the gyroscope, I chose 0x08 for no particular reason. Below is a chart for the four different scale settings. I recommend trying different settings to see different results. The unit of measure is G-forces.
- +/- 2 G-forces write 0x00
- +/- 4 G-forces write 0x08
- +/- 8 G-forces write 0x10
- +/- 16 G-forces write 0x18
The chart below shows the sensitivity for each setting. If you want to convert the 16-bit data to a value in the chosen range, divide the collected data by the value in the chart. For example, if you collected a value of 14,953 and used the 4g scale, the result is 14953/8192 = 1.825.
- REG VALUE G-forces SENSITIVITY
- 0x00 +/- 2 16384
- 0x08 +/- 4 8192
- 0x10 +/- 8 4096
- 0x18 +/- 16 2048
NOW A MESSAGE FROM OUR SPONSOR
ADVENTURES GALORE! The Weekly Squib. CLICK HERE TO READ MORE
Time to Read the Data
It is time to start reading the data. If you are using the Cheap Controls default configuration, you need to move to the delay tab. We will be increasing the delay speed, so I would recommend commenting out the line that flashes onboard led. The begin transmission command selects the device you want to access. The next step is to set the register address to the accelerometer data at 0x3B. The device call is ended with the end transmission. Note the false in this command. You want to stop the command but keep the device open for the next “requestFrom” command.
//digitalWrite(13,!(digitalRead(13)));
Wire.beginTransmission(0x68);
Wire.write(0x3B);// NOTE : 0x3B for accel and 0x43 for gyro -- 0x41 for temp
Wire.endTransmission(false);//Should check for error status
The address is set. Now it is time to collect the data. You use the request from command for this. The three attributes used in this command are the device address, the number of bytes to read, and end transmissions with a true or false. In our case, we will read 6 bytes from device 0x68 and end the transmission after collecting six bytes of data.
Wire.requestFrom(0x68,6,true);
The data can only be transmitted in 8-bit bytes when using I2C. Each axis of the accelerometer and the gyroscope has a 16-bit value. This means we must collect two bytes for each of the three axes.
- ACCELEROMETER
- registers 0x3B(59) bits 9-15 accel X axis
- registers 0x3C(60) bits 1-8 accel X axis
- registers 0x3D(61) bits 9-15 accel Y axis
- registers 0x3E(62) bits 1-8 accel Y axis
- registers 0x3F(63) bits 9-15 accel Z axis
- registers 0x40(64) bits 1-8 accel Z axis
- GYROSCOPE
- registers 0x43(67) bits 9-15 gyro X axis
- registers 0x44(68) bits 1-8 gyro X axis
- registers 0x45(69) bits 9-15 gyro Y axis
- registers 0x46(70) bits 1-8 gyro Y axis
- registers 0x47(71) bits 9-15 gyro Z axis
- registers 0x48(72) bits 1-8 gyro Z axis
When collecting the data, it is possible to collect each byte and combine them into three individual variables using binary operators. The first byte is read and shifted 8 bits to the left. This makes the rightmost bits all zeros. When the next byte is read, it is combined with the first byte using an OR command. I have the XRaw set to a short. The short is a 16-bit number in the Arduino. This might need to be adjusted for a different MPU. The last four bytes( or two axes) are read doing the same routine.
short XRaw = (Wire.read() << 8 | Wire.read());
short YRaw = (Wire.read() << 8 | Wire.read());
short ZRaw = (Wire.read() << 8 | Wire.read());
After we collect the data, we need to make the data into a readable number. I will use the accelerometer. Take the raw data and cast it as a float. Divide it by the sensitivity and store the value as a float.
float XFloat = (float) XRaw / 8192;//16384 - original
float YFloat = (float) YRaw / 8192;//16384 - original
float ZFloat = (float) ZRaw / 8192;//16384 - original
Plot the values on the serial plotter in the Arduino IDE.
Serial.print("X-AXIS: "); Serial.print(XFloat); Serial.print("\t");
Serial.print("Y-AXIS: "); Serial.print(YFloat); Serial.print("\t");
Serial.print("Z-AXIS: "); Serial.print(ZFloat); Serial.print("\t");
Serial.println("\t");
For the Nextion display, the value has to be an integer and rescaled. This example uses the 4g scale. To center the graph, you must add 4 to the XFloat value. The waveform shows data from 1 to 255. If you want to center the data, you have to split it in half, which would be about 125. If you divide 125 by 4, you get 31.25. I round that to 30. Each G-force would equate 30 on the screen. You take the value of XFloat + 4 times 30. This value is written to the screen in the middle of the Nextion “add” command.
Serial2.print("add 1,0," + String(int((XFloat+4)*30)) + endChar);
Serial2.print("add 1,1," + String(int((YFloat+4)*30)) + endChar);
Serial2.print("add 1,2," + String(int((ZFloat+4)*30)) + endChar);
If you want to see the gyroscope data change the address that you set to the following
Wire.write(0x43);// NOTE : 0x3B for accel and 0x43 for gyro -- 0x41 for temp
You can download the final version below. In this version, I have it configured to the accelerometer.
FINAL SPONSOR MESSAGE
HELP SAVE THE FLUTTERBIRD AT flutterbird.org
END