Series: Tried Using Touch Panel TFT Display GT-SP with Arduino
Controlling the GT-SP Analog Meter Using a Potentiometer
Table of Contents
DownloadDownload sample project & program (for GT Design Studio & Arduino)
Hello everyone, I’m @kissaten, a beginner in electronics. In this series, I’m explaining the process of connecting a 7-inch touch display (GT-SP series “GTWV070S3A00P”) to an Arduino and working on various projects.
In this experiment, we will use an Arduino and a potentiometer to control the analog meter on the GT-SP display. By reflecting the values obtained from the potentiometer in real time on the GT-SP, we can achieve simple and effective data visualization.
Introduction to the Analog Meter Tool
GT Design Studio comes equipped with an analog meter tool. By clicking on the icon labeled “AMT” in the toolbar, you can switch to the analog meter drawing mode.
Next, adjust the width and height to set the display area. This process is the same as when drawing button objects and other elements you’ve used before. When drawn in a square area, the default analog meter will be displayed. If it’s not a square, the design will be trimmed accordingly.
You can also modify properties such as the size of the needle, the red-highlighted range, and the maximum value. These features will be introduced in the next section.
Creating a Screen in GT Design Studio
We will now place the elements in GT Design Studio.
Click the ‘AMT’ button on the toolbar and draw a square analog meter.
This time, in addition to the analog meter, we also placed a text object labeled ‘Potentiometer Value.’
Connecting the Potentiometer
A potentiometer is a variable resistor that allows you to adjust resistance by rotating or sliding, which in turn adjusts the voltage and enables control over signals in an electronic circuit. When connected to an Arduino, it allows easy reading of analog signals, and these values can be used to control other devices or systems.
In this project, we connected the OUTPUT of the potentiometer to Arduino’s A1 pin, and connected VCC and GND accordingly.
Picture by [Fritzing (CC BY-SA)]
Creating the Program to Display on the Touchscreen
This program is designed to reflect the potentiometer’s values directly onto the analog meter displayed on the touchscreen in a simple configuration.
DownloadDownload sample project & program (for GT Design Studio & Arduino)
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 |
#include <string.h> // ピン設定 | Pin assignments #define GT_DTR 4 // DTR #define GT_DSR 6 // DSR #define GT_TRDY 7 // TRDY <--未使用 | Disused void setup() { // ピン初期設定 | Pin setting pinMode(GT_DTR, INPUT); pinMode(GT_DSR, OUTPUT); pinMode(GT_TRDY, INPUT); digitalWrite(GT_DSR, LOW); // シリアル通信の初期設定 Serial.begin(38400); // 38400bps Baud rate Serial.setTimeout(100); // シリアルタイムアウト // メーターの最小値と最大値を設定 setMeterRange(0, 240); } void loop() { // センサーからの読み取り値を取得 int sensorValue = analogRead(A0); // センサー値を0-240の範囲にマッピングし、範囲内に収める <span class="hljs-type">int</span> meterValue = <span class="hljs-built_in">constrain</span>(<span class="hljs-built_in">map</span>(sensorValue, <span class="hljs-number">0</span>, <span class="hljs-number">1023</span>, <span class="hljs-number">240</span>, <span class="hljs-number">0</span>), <span class="hljs-number">0</span>, <span class="hljs-number">240</span>); // GT-SPに値を送信 sendMeterValue(meterValue); delay(100); // 100ミリ秒待つ } // GT-SPにメーターの最小値と最大値を設定 | Set min and max values for meter void setMeterRange(int minValue, int maxValue) { gtsp_ObjPrpSet_val(0, 0x1E, 4, minValue); // Set minimum value gtsp_ObjPrpSet_val(0, 0x1F, 4, maxValue); // Set maximum value } // GT-SPに値を送信 | Send value to GT-SP void sendMeterValue(int value) { gtsp_ObjPrpSet_val(0, 0x10, 4, value); } /********************** GT-SP関数 | Function for GT-SP **********************/ //////////////////////////////////////////////////// // オブジェクト制御コマンド-プロパティ読み出し(数値用)| Read property value (for Value) //////////////////////////////////////////////////// unsigned long gtsp_ObjPrpRead_val(int obj, int prp, int ln) { unsigned long val; gt_print("CMD"); // コマンドヘッダ gt_put(0xd4); // オブジェクト-プロパティ設定コマンド gt_put(obj >> 0); // オブジェクトNo. 下位バイト gt_put(obj >> 8); // オブジェクトNo. 上位バイト gt_put(prp >> 0); // プロパティNo. 下位バイト gt_put(prp >> 8); // プロパティNo. 上位バイト gt_put(ln >> 0); // データ長 最下位バイト gt_put(ln >> 8); // データ長 下位バイト gt_put(ln >> 16); // データ長 上位バイト gt_put(ln >> 24); // データ長 最上位バイト // プロパティデータ受信 while (Serial.available() == 0) {} val = gtsp_signal_read_val(); return val; } //////////////////////////////////////////////////// // データ受信(文字列用)| Receive data from GT-SP (for String) //////////////////////////////////////////////////// String gtsp_signal_read() { byte res_dl[4] = ""; unsigned long dl; char res_data_char[255] = ""; if (Serial.find("RESb", 4)) { Serial.readBytes(res_dl, 4); // データ長抽出 dl = (unsigned long)(res_dl[0] + (res_dl[1] << 8) + (res_dl[2] << 16) + (res_dl[3] << 24)); // データ長変換 Serial.readBytes(res_data_char, dl); // データ抽出 Serial.print("Received data: "); for (unsigned int i = 0; i < dl; i++) { Serial.print(res_data_char[i], HEX); Serial.print(" "); } Serial.println(); return String(res_data_char); // String型変換、リターン } return ""; } //////////////////////////////////////////////////// // データ受信(数値用)| Receive data from GT-SP (for Value) //////////////////////////////////////////////////// unsigned long gtsp_signal_read_val() { byte res_dl[4] = ""; unsigned long dl; byte res_val[4] = ""; unsigned long val; if (Serial.find("RESb", 4)) { Serial.readBytes(res_dl, 4); // データ長抽出 dl = (unsigned long)(res_dl[0] + (res_dl[1] << 8) + (res_dl[2] << 16) + (res_dl[3] << 24)); // データ長変換 Serial.readBytes(res_val, dl); // データ抽出 val = (unsigned long)(res_val[0] + (res_val[1] << 8) + (res_val[2] << 16) + (res_val[3] << 24)); // データ抽出 return val; // リターン } return 0; } //////////////////////////////////////////////////// // 1byte送信 | Send byte to GT-SP //////////////////////////////////////////////////// void gt_put(unsigned char onebyte) { while (digitalRead(GT_DTR) == HIGH) {} // busycheck Serial.write(onebyte); } //////////////////////////////////////////////////// // 文字列送信 | Send String to GT-SP //////////////////////////////////////////////////// void gt_print(String val) { int val_i; // 文字列を1文字ずつ送信 | Send string per one byte for (val_i = 0; val_i < val.length(); val_i++) { while (digitalRead(GT_DTR) == HIGH) {} // busycheck Serial.print(val.substring(val_i, val_i + 1)); } } //////////////////////////////////////////////////// // オブジェクト制御コマンド - プロパティ設定 (数値用) | Object Control Command - Property Setting (for Value) //////////////////////////////////////////////////// void gtsp_ObjPrpSet_val(int obj, int prp, int ln, unsigned long val) { gt_print("CMD"); // コマンドヘッダ | Command header gt_put(0xD3); // オブジェクト-プロパティ設定コマンド | Object-Property Setting gt_put(obj >> 0); // オブジェクトNo. 下位バイト | Object No. Lower byte gt_put(obj >> 8); // オブジェクトNo. 上位バイト| Object No. Upper byte gt_put(prp >> 0); // プロパティNo. 下位バイト | Property No. Lower byte gt_put(prp >> 8); // プロパティNo. 上位バイト| Property No. Upper byte gt_put(ln >> 0); // データ長 最下位バイト | Data length Least significant byte gt_put(ln >> 8); // データ長 下位バイト | Data length second byte gt_put(ln >> 16); // データ長 上位バイト | Data length third byte gt_put(ln >> 24); // データ長 最上位バイト| Data length Most significant byte if (ln >= 1) { gt_put(val >> 0); } // データ 最下位バイト | Data Least significant byte if (ln >= 2) { gt_put(val >> 8); } // データ 下位バイト | Data second byte if (ln >= 3) { gt_put(val >> 16); } // データ 上位バイト | Data third byte if (ln >= 4) { gt_put(val >> 24); } // データ 最上位バイト| Data Most significant byte } |
Interpreting the Program
setMeterRange(0, 240)
This function defines the display range for the analog meter on the GT-SP, set here between 0 and 240, which is the default maximum value. It ensures that the analog meter reflects values within this range based on the potentiometer’s movement.
int meterValue = constrain(map(sensorValue, 0, 1023, 240, 0), 0, 240)
This part of the code uses the map() function to convert the analog value from the potentiometer (ranging from 0 to 1023) to a range of 0 to 240. To ensure that the potentiometer’s rotation intuitively corresponds to the movement of the analog meter’s needle, the mapping direction is reversed. This means turning the potentiometer and the meter needle align properly, avoiding any opposite movement.
The constrain() function ensures that the mapped value is restricted within the 0 to 240 range, ensuring accurate and valid meter readings.
sendMeterValue(meterValue)
This function is crucial as it sends the potentiometer’s value to the analog meter on the GT-SP. It does this by utilizing the gtsp_ObjPrpSet_val() function, which sends the calculated meterValue to the analog meter’s property (Object No. 0, Property No. 0x10). This ensures that the meter on the GT-SP dynamically reflects the potentiometer’s real-time changes.
The maximum value of the analog meter is currently set to 240. To change this, you can modify both the GT-SP and the program accordingly. Alternatively, you can directly change the meter’s maximum value using a command like gtsp_ObjPrpSet_value(0, 0x1F, 240), which targets the analog meter’s max value property.
Execution Results
In this experiment, we used Arduino and a potentiometer to reflect real-time values on the GT-SP analog meter. The meter responded smoothly to the adjustments made via the potentiometer, demonstrating its effectiveness.
Next time, we will explore ways to enhance the design of the analog meter and introduce more refined display techniques.