logo-mobile

ROHM

ROHM
Menu
  • Arduino –
  • Raspberry Pi –
  • Trending –
  • Others –
  • About –
  • Contact –

Arduino

3-Axis Accelerometer using TFT LCD Panel – Part 2

Device Plus Editorial Team
Published by Device Plus Editorial Team at July 9, 2017
Categories
  • Arduino
Tags
  • accelerometer
  • Arduino
  • rohm accelerometer
  • rohm sensor kit
  • tft lcd
tft lcd

Click here to read Part 1 of this article >

tft lcd

In Part 1, we were able to obtain and display values from KX022-1020 accelerometer on the TFT LCD panel. In Part 2, we’ll show how to control the TFT monitor while reading the contents of the program!

Today’s Electronic Recipe

Estimated time to complete: 60 minutes

Parts needed:

  • Arduino body (Arduino UNO R3)
  • Rohm Sensor Evaluation Kit http://www.rohm.com/web/global/sensor-shield-support
  • TFT liquid crystal panel (sainsmart 1.8)  https://www.sainsmart.com/sainsmart-1-8-spi-lcd-module-with-microsd-led-backlight-for-arduino-mega-atmel-atmega.html

※ Rohm sensor evaluation kit can be purchased from the following site!

Chip One Stop

Mouser Electronics

 

Displaying on TFT LCD monitor with Arduino

As before, we’ll be using SainSmart ST7735R TFT monitor. It’s a compact LCD display that can be used both with Arduino and Raspberry Pi. The monitor has a built-in microSD card slot, so it’s possible to store and load images, in addition to reading and writing data. In this tutorial, we will only try to display values on the TFT monitor.

Let’s get started! First, connect TFT monitor to Arduino.

tft lcd

Figure 1. SainSmart ST7735R TFT monitor

tft lcd

Figure 2. Backside of TFT monitor

TFT Pins:

  • VCC – Correction Voltage
  • GND – Ground
  • SCL – Serial Clock Line
  • SDA – Serial Data Line
  • RS / DC – Command / Data Selection
  • RES – LCD Controller Reset
  • CS – Chipselect for TF Card

After connecting the Arduino to the TFT monitor, try running the sample program.

 

Apply TFT Monitor library for Arduino

As we mentioned in Part 1, we need to make a small change to the library file (ST7735R) to use this TFT monitor with Arduino.

SainSmart 1.8 ST7735R TFT LCD Module with MicroSD LED Backlight For Arduino

Raspberry Pi library (ST7735R V0.2)

There is a download link at the bottom of the page from the above URL. Click on the link labeled “Download Link” from the page to download a complete set of libraries, sample code, documentation, etc. After the download is complete, decompress the compressed file and rewrite the necessary files.

After opening “ST7735.h” with an editor that can edit text, change the part shown in the picture below. You can also use Arduino IDE.

tft lcd

Once you made the change, compile the unzipped “TFT 18” directory with zip again, add it as a library in Arduino (or Arduino Create) Add Library, or place it under the “libraries” directory in Arduino’s installed directory and load the library.

When it’s complete, try moving “TFT 18” – “graphictest” from the sketch sample.

When you look at the sample program, you can see that it’s displayed quite smoothly.

Sample program – graphictest

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
//Pin setting
#define sclk 4
#define mosi 5
#define cs 6
#define dc 7
#define rst 8
//Color numbering
#define BLACK       0x0000
#define BLUE         0x001F
#define RED         0xF800
#define GREEN       0x07E0
#define CYAN         0x07FF
#define MAGENTA     0xF81F
#define YELLOW       0xFFE0
#define WHITE       0xFFFF
#include <ST7735.h>
#include <SPI.h>
ST7735 tft = ST7735(cs, dc, mosi, sclk, rst);
void fillpixelbypixel(uint16_t color) {
for (uint8_t x=0; x < tft.width; x++) {
   for (uint8_t y=0; y < tft.height; y++) {
     tft.drawPixel(x, y, color);
   }
}
delay(100);
}
void setup(void) {
Serial.begin(9600);
Serial.print("hello!");
tft.initR();           // initialize a ST7735R chip
Serial.println("init");
tft.writecommand(ST7735_DISPON);
  
uint16_t time = millis();
tft.fillScreen(BLACK);
time = millis() - time;
  
Serial.println(time, DEC);
delay(500);
  
//
tft.fillScreen(BLACK);
testdrawtext("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur adipiscing ante sed nibh tincidunt feugiat. Maecenas enim massa, fringilla sed malesuada et, malesuada sit amet turpis. Sed porttitor neque ut ante pretium vitae malesuada nunc bibendum. Nullam aliquet ultrices massa eu hendrerit. Ut sed nisi lorem. In vestibulum purus a tortor imperdiet posuere. ", WHITE);
delay(1000);
  
//a single pixel
tft.drawPixel(tft.width/2, tft.height/2, GREEN);
delay(500);
  
// line draw test
testlines(YELLOW);
delay(500);
  
// optimized lines
testfastlines(RED, BLUE);
delay(500);
testdrawrects(GREEN);
delay(500);
testfillrects(YELLOW, MAGENTA);
delay(500);
tft.fillScreen(BLACK);
testfillcircles(10, BLUE);
testdrawcircles(10, WHITE);
  
Serial.println("done");
delay(1000);
}
void loop() {
tft.writecommand(ST7735_INVON);
delay(500);
tft.writecommand(ST7735_INVOFF);
delay(500);
}
void testlines(uint16_t color) {
  tft.fillScreen(BLACK);
  for (uint16_t x=0; x < tft.width; x+=6) {
    tft.drawLine(0, 0, x, tft.height-1, color);
  }
  for (uint16_t y=0; y < tft.height; y+=6) {
    tft.drawLine(0, 0, tft.width-1, y, color);
  }
  
  tft.fillScreen(BLACK);
  for (uint16_t x=0; x < tft.width; x+=6) {
    tft.drawLine(tft.width-1, 0, x, tft.height-1, color);
  }
  for (uint16_t y=0; y < tft.height; y+=6) {
    tft.drawLine(tft.width-1, 0, 0, y, color);
  }
  
  tft.fillScreen(BLACK);
  for (uint16_t x=0; x < tft.width; x+=6) {
    tft.drawLine(0, tft.height-1, x, 0, color);
  }
  for (uint16_t y=0; y < tft.height; y+=6) {
    tft.drawLine(0, tft.height-1, tft.width-1, y, color);
  }
  tft.fillScreen(BLACK);
  for (uint16_t x=0; x < tft.width; x+=6) {
    tft.drawLine(tft.width-1, tft.height-1, x, 0, color);
  }
  for (uint16_t y=0; y < tft.height; y+=6) {
    tft.drawLine(tft.width-1, tft.height-1, 0, y, color);
  }
  
}
void testdrawtext(char *text, uint16_t color) {
tft.drawString(0, 0, text, color);
}
void testfastlines(uint16_t color1, uint16_t color2) {
  tft.fillScreen(BLACK);
  for (uint16_t y=0; y < tft.height; y+=5) {
    tft.drawHorizontalLine(0, y, tft.width, color1);
  }
  for (uint16_t x=0; x < tft.width; x+=5) {
    tft.drawVerticalLine(x, 0, tft.height, color2);
  }
}
void testdrawrects(uint16_t color) {
tft.fillScreen(BLACK);
for (uint16_t x=0; x < tft.width; x+=6) { tft.drawRect(tft.width/2 -x/2, tft.height/2 -x/2 , x, x, color); } } void testfillrects(uint16_t color1, uint16_t color2) { tft.fillScreen(BLACK); for (uint16_t x=tft.width-1; x > 6; x-=6) {
  tft.fillRect(tft.width/2 -x/2, tft.height/2 -x/2 , x, x, color1);
  tft.drawRect(tft.width/2 -x/2, tft.height/2 -x/2 , x, x, color2);
}
}
void testfillcircles(uint8_t radius, uint16_t color) {
for (uint8_t x=radius; x < tft.width; x+=radius*2) {
   for (uint8_t y=radius; y < tft.height; y+=radius*2) {
     tft.fillCircle(x, y, radius, color);
   }
}
}
void testdrawcircles(uint8_t radius, uint16_t color) {
for (uint8_t x=0; x < tft.width+radius; x+=radius*2) {
   for (uint8_t y=0; y < tft.height+radius; y+=radius*2) {
     tft.drawCircle(x, y, radius, color);
   }
}
}

The following are the main functions of the TFT monitor:

tft.drawPixel(x,y,color); – Dots of the specified color (color) are displayed at the specified position (x, y).

tft.drawCircle(x, y, radius, color); – Draws a circle with the radius (radius) specified at the specified position (x, y).

tft.fillRect(x1,y1, x2, y2, color); – Fills a rectangle with the width and height from the specified position 1 (x1, y1) to position 2 (x2, y2).

tft.drawString(x, y, text, color); – Displays text with the specified color (color) at the specified position (x, y).

tft.fillScreen(0x0000); – Fill the entire monitor with the specified color.

Although there are some other functions, you can pretty much display anything using only the above functions.

 

Graphing the accelerometer values

Next, let’s display the accelerometer values on the TFT monitor! In the case of the Sensor Evaluation Kit, basically, it’s not necessary to change the wiring on the TFT monitor side. All that is needed is to insert KX022-1020 accelerometer to the Sensor Shield.

tft lcd

Figure 3. Accelerometer and TFT monitor

Program for displaying numerical values of accelerometer:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#include <Wire.h>
#include <KX022.h>
#include <ST7735.h>
#include <SPI.h>
// You can use any (4 or) 5 pins
#define sclk 4
#define mosi 5
#define cs 6
#define dc 7
#define rst 8  // you can also connect this to the Arduino reset
// Color definitions
#define BLACK       0x0000
#define BLUE         0x001F
#define RED         0xF800
#define GREEN       0x07E0
#define CYAN         0x07FF
#define MAGENTA     0xF81F
#define YELLOW       0xFFE0
#define WHITE       0xFFFF
ST7735 tft = ST7735(cs, dc, mosi, sclk, rst);
KX022 kx022(KX022_DEVICE_ADDRESS_1E);
int _cnt = 0;
//Graph initial position
int _xc = 120;
int _yc = 130;
int _zc = 140;
void fillpixelbypixel(uint16_t color) {
for (uint8_t x=0; x < tft.width; x++) {
for (uint8_t y=0; y < tft.height; y++) {
tft.drawPixel(x, y, color);
}
}
delay(100);
}
void setup(void) {
byte rc;
Serial.begin(9600);
while (!Serial);
Wire.begin();
tft.initR(); // initialize a ST7735R chip
rc = kx022.init();
tft.fillScreen(BLACK);
1.DEVICE PLUS
testdrawtext("DEVICE PLUS!!", WHITE,25,50);
delay(1000);
tft.fillScreen(BLACK);
}
void loop() {
//KX022
byte rc;
float acc[3];
//2.Acquire accelerometer values
rc = kx022.get_val(acc);
if (rc == 0) {
Serial.write("KX022 (X) = ");
Serial.print(acc[0]);
Serial.println(" [g]");
Serial.write("KX022 (Y) = ");
Serial.print(acc[1]);
Serial.println(" [g]");
Serial.write("KX022 (Z) = ");
Serial.print(acc[2]);
Serial.println(" [g]");
Serial.println();
//Convert float type to char type
char xVal[10];
dtostrf(acc[0], 5, 2, xVal);
char yVal[10];
dtostrf(acc[1], 5, 2, yVal);
char zVal[10];
dtostrf(acc[2], 5, 2, zVal);
//Convert to TFT liquid crystal
//tft.fillScreen(BLACK);
tft.fillRect(0,0, 120, 60, BLACK);
testdrawtext("X:", RED, 5, 15);
testdrawtext(xVal, WHITE, 30, 15);
testdrawtext("Y:", BLUE, 5, 30);
testdrawtext(yVal, WHITE, 30, 30);
testdrawtext("Z:", GREEN, 5, 45);
testdrawtext(zVal, WHITE, 30, 45);
//3.Draw a graph
int x = int(acc[0]*100)+120;
int y = int(acc[1]*100)+130;
int z = int(acc[2]*100)+40;
tft.drawLine(_cnt-1, _xc, _cnt, x, RED);
tft.drawLine(_cnt-1, _yc, _cnt, y, BLUE);
tft.drawLine(_cnt-1, _zc, _cnt, z, GREEN);
_cnt++;
//Reset to the end of the screen
if(_cnt > 120){
_cnt = 0;
tft.fillScreen(BLACK);
}
_xc = x;
_yc = y;
_zc = z;
delay(10);
}
delay(10);
}
void testlines(uint16_t color) {
  tft.fillScreen(BLACK);
  for (uint16_t x=0; x < tft.width; x+=6) {
    tft.drawLine(0, 0, x, tft.height-1, color);
  }
  for (uint16_t y=0; y < tft.height; y+=6) {
    tft.drawLine(0, 0, tft.width-1, y, color);
  }
  
  tft.fillScreen(BLACK);
  for (uint16_t x=0; x < tft.width; x+=6) {
    tft.drawLine(tft.width-1, 0, x, tft.height-1, color);
  }
  for (uint16_t y=0; y < tft.height; y+=6) {
    tft.drawLine(tft.width-1, 0, 0, y, color);
  }
  
  tft.fillScreen(BLACK);
  for (uint16_t x=0; x < tft.width; x+=6) {
    tft.drawLine(0, tft.height-1, x, 0, color);
  }
  for (uint16_t y=0; y < tft.height; y+=6) {
    tft.drawLine(0, tft.height-1, tft.width-1, y, color);
  }
  tft.fillScreen(BLACK);
  for (uint16_t x=0; x < tft.width; x+=6) {
    tft.drawLine(tft.width-1, tft.height-1, x, 0, color);
  }
  for (uint16_t y=0; y < tft.height; y+=6) {
    tft.drawLine(tft.width-1, tft.height-1, 0, y, color);
  }
}
void testdrawtext(char *text, uint16_t color,int x,int y) {
tft.drawString(x, y, text, color);
}
void testfastlines(uint16_t color1, uint16_t color2) {
  tft.fillScreen(BLACK);
  for (uint16_t y=0; y < tft.height; y+=5) {
    tft.drawHorizontalLine(0, y, tft.width, color1);
  }
  for (uint16_t x=0; x < tft.width; x+=5) {
    tft.drawVerticalLine(x, 0, tft.height, color2);
  }
}
void testdrawrects(uint16_t color) {
tft.fillScreen(BLACK);
for (uint16_t x=0; x < tft.width; x+=6) { tft.drawRect(tft.width/2 -x/2, tft.height/2 -x/2 , x, x, color); } } void testfillrects(uint16_t color1, uint16_t color2) { tft.fillScreen(BLACK); for (uint16_t x=tft.width-1; x > 6; x-=6) {
  tft.fillRect(tft.width/2 -x/2, tft.height/2 -x/2 , x, x, color1);
  tft.drawRect(tft.width/2 -x/2, tft.height/2 -x/2 , x, x, color2);
}
}
void testfillcircles(uint8_t radius, uint16_t color) {
for (uint8_t x=radius; x < tft.width; x+=radius*2) {
   for (uint8_t y=radius; y < tft.height; y+=radius*2) {
     tft.fillCircle(x, y, radius, color);
   }
}
}
void testdrawcircles(uint8_t radius, uint16_t color) {
for (uint8_t x=0; x < tft.width+radius; x+=radius*2) {
   for (uint8_t y=0; y < tft.height+radius; y+=radius*2) {
     tft.drawCircle(x, y, radius, color);
   }
}
}

When you run the above program, a graph of the values of the accelerometer is displayed.

A summary of the program flow:

  1. Show “DEVICE PLUS !!” character in setup
  2. Acquire the value of the accelerometer and convert it to an integer
  3. Display graphs and text based on the values

We added 1 to the x-axis for each frame so that the graph is drawn from left to right.

When it goes to the edge of the 120px display, the graph is cleared with drawrect. The numbers on the upper part are updated with drawrect for each frame in the same way.

This concludes the tutorial on how to display and graph accelerometer values using TFT LCD monitor! There are quite a few side projects we can consider developing. For example, we can combine this TFT monitor and Arduino Pro Mini to make a wristwatch featuring small games, etc. It is also possible to make a data logger using, for instance, different sensors from the Sensor Evaluation Kit.

 

Click here to read Part 1 of this article >

Device Plus Editorial Team
Device Plus Editorial Team
Device Plus is for everyone who loves electronics and mechatronics.

Check us out on Social Media

  • Facebook
  • Twitter

Recommended Posts

  • 3-Axis Accelerometer using TFT LCD Panel – Part 13-Axis Accelerometer using TFT LCD Panel – Part 1
  • Arduino Explorer Rover Part 2 – Electronics & WiringArduino Explorer Rover Part 2 – Electronics & Wiring
  • 3D Cases for ROHM Sensor Evaluation Kit and RohmMultiSensor Library Update3D Cases for ROHM Sensor Evaluation Kit and RohmMultiSensor Library Update
  • Lightweight Arduino Library for ROHM Sensor Evaluation KitLightweight Arduino Library for ROHM Sensor Evaluation Kit
  • Smart Garden System using Arduino Create + ROHM Sensor Evaluation KitSmart Garden System using Arduino Create + ROHM Sensor Evaluation Kit
  • Make an LED Arduino Christmas Tree with ROHM Sensor KitMake an LED Arduino Christmas Tree with ROHM Sensor Kit
Receive update on new postsPrivacy Policy

Recommended Tutorials

  • How to integrate RFID module with Raspberry Pi How to integrate RFID module with Raspberry Pi
  • nRF24L01+ RF Module Tutorial nRF24L01+ RF Module Tutorial
  • How to Run Arduino Sketches on Raspberry Pi How to Run Arduino Sketches on Raspberry Pi
  • Setting Up Raspberry Pi as a Home Media Server Setting Up Raspberry Pi as a Home Media Server

Recommended Trends

  • SewBot Is Revolutionizing the Clothing Manufacturing Industry SewBot Is Revolutionizing the Clothing Manufacturing Industry
  • All About The Sumo Robot Competition And Technology All About The Sumo Robot Competition And Technology
  • 5 Interesting Tips to Calculating the Forward Kinematics of a Robot 5 Interesting Tips to Calculating the Forward Kinematics of a Robot
  • Go Inside the Drones That Are Changing Food Delivery Go Inside the Drones That Are Changing Food Delivery
Menu
  • Arduino –
    Arduino Beginner’s Guide
  • Raspberry Pi –
    Raspberry Pi Beginner's Guide
  • Trending –
    Updates on New Technologies
  • Others –
    Interviews / Events / Others

Check us out on Social Media

  • Facebook
  • Twitter
  • About
  • Company
  • Privacy Policy
  • Terms of Service
  • Contact
  • Japanese
  • 简体中文
  • 繁體中文
Don’t Forget to Follow Us!
© Copyright 2016-2021. Device Plus - Powered by ROHM
© 2021 Device Plus. All Rights Reserved. Muffin group