ES-112Beginner

ESP32 Bluetooth Text on an LCD

Type a message on your phone and it shows up on a 16x2 LCD over Bluetooth, no internet or Wi-Fi needed. This project is the step-by-step guide to the Text on an LCD Bluetooth template in the app, built with an ESP32 and an I2C LCD. Go to Workshop › Devices › Add a device › Bluetooth, pick Text on an LCD and upload the code with Get the code. The Set up as a Bluetooth panel button on this page opens the wizard with this template too.

0.0(0)
ESP32 Bluetooth Text on an LCD
Type a message on your phone, tap Send and it appears on the LCD on your desk, with no internet or server. The ESP32 has Bluetooth built in, so your phone connects straight to the board, and the display's I2C module needs only four wires. A 16x2 display holds 32 characters: 16 on the top row, the rest below. The code turns accented letters such as é, ü and ñ into plain Latin ones and shows a question mark for unknown characters. The Backlight switch in the panel turns the light on and off. In the app, pick the Text on an LCD template, upload the code and find your board in the list. Learning: sending text over Bluetooth Low Energy, driving an I2C LCD, splitting text into rows and a letter conversion table.

Video

Circuit Diagram

ESP32 Bluetooth Text on an LCD - Circuit Diagram

Source Code

C++ (Arduino IDE)
1// ┌───────────────────────────┐
2// │ BlueGrays · bluegrays.com │
3// │ Bluetooth · ESP32         │
4// │ BG-Ekran                  │
5// └───────────────────────────┘
6// Text on an LCD
7// Arduino IDE board: ESP32 Dev Module
8// the BLE library comes with the ESP32 board package, nothing extra to install
9
10#include <BLEDevice.h>
11#include <BLEServer.h>
12#include <BLE2902.h>
13#include <Wire.h>
14#include <LiquidCrystal_I2C.h>  // Library Manager: 'LiquidCrystal I2C' (Frank de Brabander)
15
16const char BLE_NAME[] = "BG-Ekran";  // the board advertises with this name, the app finds it in the list by it
17
18// Pins: change them here if you wired differently
19const int SDA_PIN = 21, SCL_PIN = 22;
20const int LCD_COLS = 16, LCD_ROWS = 2;  // LCD size: columns × rows
21const uint8_t LCD_ADDRESS = 0x27;  // most I2C LCD backpacks use 0x27; try 0x3F if the screen stays blank
22
23void btWrite(const char *text);
24void onValue(const char *key, const char *value);
25void sendAll();
26void onLinkLost();
27
28// ── The app sends "key=value" lines; on "?" the board sends all its values ──
29char line[65];
30uint8_t lineLen = 0;
31unsigned long lastRxMs = 0;
32
33bool is(const char *a, const char *b) { return strcmp(a, b) == 0; }
34bool isOn(const char *v) { return v[0] == '1'; }
35
36void sendText(const char *key, const char *value) {
37  char out[72];
38  snprintf(out, sizeof(out), "%s=%s\n", key, value);
39  btWrite(out);
40}
41
42void sendInt(const char *key, long value) {
43  char text[12];
44  snprintf(text, sizeof(text), "%ld", value);
45  sendText(key, text);
46}
47
48void sendFloat(const char *key, float value) {
49  char text[16];
50  dtostrf(value, 1, 1, text);
51  sendText(key, text);
52}
53
54void handleLine(char *text) {
55  char *eq = strchr(text, '=');
56  if (eq == nullptr) {
57    if (text[strlen(text) - 1] == '?') {
58      btWrite("@v=1.0\n");  // the "@v" line tells the app the code version
59      sendAll();
60    }
61    return;
62  }
63  *eq = '\0';
64  onValue(text, eq + 1);
65}
66
67void feed(char c) {
68  lastRxMs = millis();
69  if (c == '\n' || c == '\r') {
70    if (lineLen == 0) return;
71    line[lineLen] = '\0';
72    lineLen = 0;
73    handleLine(line);
74  } else if (lineLen < sizeof(line) - 1) {
75    line[lineLen++] = c;
76  }
77}
78
79// ── Bluetooth (Nordic UART) ──
80#define UART_SERVICE "6E400001-B5A3-F393-E0A9-E50E24DCCA9E"
81#define UART_RX "6E400002-B5A3-F393-E0A9-E50E24DCCA9E"
82#define UART_TX "6E400003-B5A3-F393-E0A9-E50E24DCCA9E"
83
84BLECharacteristic *txChar = nullptr;
85volatile bool linked = false;
86volatile bool linkLost = false;
87// incoming bytes move from the Bluetooth task to loop() through a small ring buffer
88const int RX_SIZE = 256;
89volatile uint8_t rxBuf[RX_SIZE];
90volatile int rxHead = 0, rxTail = 0;
91
92class ServerEvents : public BLEServerCallbacks {
93  void onConnect(BLEServer *server) override { linked = true; }
94  void onDisconnect(BLEServer *server) override { linked = false; linkLost = true; }
95};
96
97class RxEvents : public BLECharacteristicCallbacks {
98  void onWrite(BLECharacteristic *c) override {
99    auto v = c->getValue();
100    for (size_t i = 0; i < v.length(); i++) {
101      int next = (rxHead + 1) % RX_SIZE;
102      if (next == rxTail) break;
103      rxBuf[rxHead] = (uint8_t)v[i];
104      rxHead = next;
105    }
106  }
107};
108
109// lines go out in 20-byte packets (works with every phone)
110void btWrite(const char *text) {
111  if (!linked || txChar == nullptr) return;
112  size_t n = strlen(text);
113  for (size_t i = 0; i < n; i += 20) {
114    size_t len = n - i < 20 ? n - i : 20;
115    txChar->setValue((uint8_t *)text + i, len);
116    txChar->notify();
117    delay(4);
118  }
119}
120
121void btBegin() {
122  BLEDevice::init(BLE_NAME);
123  BLEServer *server = BLEDevice::createServer();
124  server->setCallbacks(new ServerEvents());
125  BLEService *uart = server->createService(UART_SERVICE);
126  txChar = uart->createCharacteristic(UART_TX, BLECharacteristic::PROPERTY_NOTIFY);
127  txChar->addDescriptor(new BLE2902());
128  BLECharacteristic *rx = uart->createCharacteristic(UART_RX, BLECharacteristic::PROPERTY_WRITE | BLECharacteristic::PROPERTY_WRITE_NR);
129  rx->setCallbacks(new RxEvents());
130  uart->start();
131  BLEAdvertisementData adv;
132  adv.setFlags(0x06);
133  adv.setCompleteServices(BLEUUID(UART_SERVICE));
134  BLEAdvertisementData scan;
135  scan.setName(BLE_NAME);
136  BLEAdvertising *advertising = BLEDevice::getAdvertising();
137  advertising->setAdvertisementData(adv);
138  advertising->setScanResponseData(scan);
139  BLEDevice::startAdvertising();
140}
141
142void btPoll() {
143  while (rxTail != rxHead) {
144    char c = (char)rxBuf[rxTail];
145    rxTail = (rxTail + 1) % RX_SIZE;
146    feed(c);
147  }
148  if (linkLost) {
149    linkLost = false;
150    onLinkLost();
151    delay(100);
152    BLEDevice::startAdvertising();
153  }
154}
155
156// ── ESP32 ──
157// if you only see boxes, turn the blue trimmer on the back (contrast)
158LiquidCrystal_I2C lcd(LCD_ADDRESS, LCD_COLS, LCD_ROWS);
159char rows[LCD_ROWS][LCD_COLS + 1];
160bool lightOn = true;
161
162// the LCD only knows basic Latin letters: accented ones are simplified (é → e), others become ?
163const char FOLD[] = "AAAAAAACEEEEIIIIDNOOOOOxOUUUUYPsaaaaaaaceeeeiiiidnooooo/ouuuuypyAaAaAaCcCcCcCcDdDdEeEeEeEeEeGgGgGgGgHhHhIiIiIiIiIiJjJjKkkLlLlLlLlLlNnNnNnnNnOoOoOoOoRrRrRrSsSsSsSsTtTtTtUuUuUuUuUuUuWwYyYZzZzZzs";
164
165void toAscii(const char *in, char *out, size_t size) {
166  size_t n = 0;
167  const unsigned char *p = (const unsigned char *)in;
168  while (*p && n + 1 < size) {
169    unsigned char c = *p;
170    if (c < 0x80) { out[n++] = c >= 32 ? c : ' '; p++; continue; }
171    int len = c >= 0xF0 ? 4 : c >= 0xE0 ? 3 : c >= 0xC0 ? 2 : 1;
172    unsigned int code = (len == 2 && p[1]) ? ((c & 0x1F) << 6) | (p[1] & 0x3F) : 0;
173    out[n++] = (code >= 0xC0 && code <= 0x17F) ? FOLD[code - 0xC0] : '?';
174    for (int k = 0; k < len && *p; k++) p++;
175  }
176  out[n] = '\0';
177}
178
179void showRows() {
180  for (int r = 0; r < LCD_ROWS; r++) {
181    lcd.setCursor(0, r);
182    bool ended = false;
183    for (int c = 0; c < LCD_COLS; c++) {
184      if (rows[r][c] == '\0') ended = true;
185      lcd.write(ended ? ' ' : rows[r][c]);
186    }
187  }
188}
189
190// "yazi=" flows the text over the rows, "satir1=" … replaces a single row
191void setText(const char *text) {
192  char flat[LCD_COLS * LCD_ROWS + 1];
193  toAscii(text, flat, sizeof(flat));
194  int len = strlen(flat);
195  for (int r = 0; r < LCD_ROWS; r++) {
196    int start = r * LCD_COLS;
197    int n = len > start ? min(LCD_COLS, len - start) : 0;
198    memcpy(rows[r], flat + start, n);
199    rows[r][n] = '\0';
200  }
201}
202
203void onValue(const char *key, const char *value) {
204  if (is(key, "yazi")) {
205    setText(value);
206    showRows();
207  } else if (strncmp(key, "satir", 5) == 0) {
208    int r = atoi(key + 5) - 1;
209    if (r < 0 || r >= LCD_ROWS) return;
210    toAscii(value, rows[r], LCD_COLS + 1);
211    showRows();
212  } else if (is(key, "isik")) {
213    lightOn = isOn(value);
214    if (lightOn) lcd.backlight(); else lcd.noBacklight();
215  }
216}
217
218void sendAll() {
219  char flat[LCD_COLS * LCD_ROWS + 1];
220  snprintf(flat, sizeof(flat), rows[1][0] ? "%-16s%s" : "%s", rows[0], rows[1]);
221  if (flat[0]) sendText("yazi", flat);
222  sendInt("isik", lightOn);
223}
224
225void onLinkLost() {
226}
227
228void setup() {
229  Serial.begin(115200);
230  Wire.begin(SDA_PIN, SCL_PIN);
231  lcd.init();
232  lcd.backlight();
233  setText("BlueGrays");
234  strncpy(rows[1], BLE_NAME, LCD_COLS);
235  rows[1][LCD_COLS] = '\0';
236  showRows();
237  btBegin();
238  Serial.print(F("BlueGrays Bluetooth: "));
239  Serial.println(BLE_NAME);
240}
241
242void loop() {
243  btPoll();
244}
245

FAQ

Does it work with an iPhone?

Yes. The ESP32's built-in Bluetooth Low Energy (BLE) talks to both iPhone and Android. An iPhone cannot connect to classic Bluetooth modules like the HC-05 and HC-06, and this project does not need them. Do not try to pair the board in your phone's Bluetooth settings; BLE boards may not show up there, which is normal. The BlueGrays app makes the connection.

Can it show accented letters like é, ü or ñ?

Not as accented letters. Before the text goes to the display, the code turns accented Latin letters into plain ones: é becomes e, ü becomes u, ñ becomes n, ç becomes c. Type café and the display shows cafe, so the text stays readable. Characters that are not in the conversion table (emoji, Cyrillic or Japanese characters, symbols such as ° and €) come out as a question mark. The LCD preview in the panel shows what the text will look like before you send it.

Can I use a 20x4 LCD?

Yes. The wizard and the code screen have an LCD type choice: 16x2 or 20x4. Pick 20x4 and the app generates the code for that size. The wiring is the same: four wires on the same pins. Do not edit the column and row numbers by hand in code you got for 16x2, because other parts of the code depend on the size too. The right way is to change the LCD type in the app and get the code again. This guide was built with a 16x2 display.

How many characters fit, and does long text scroll?

A 16x2 display holds 32 characters: two rows of 16. The text field in the panel does not accept more. The first 16 characters go on the top row and the rest on the bottom row; the code does not look at words, so a word can be cut in two at the end of a row. The text does not scroll, it stays where it is. To push a word down to the bottom row, add spaces in front of it; the LCD preview shows the split before you send. A new text replaces the old one.