Example Code for Arduino-LCD Result Display
Last revision 2026/01/15
In actual application, we may use the sensor outdoor, so here we specially provide a case for you. Li-ion batteries will be the power supply and an LCD will be used to display the measured distance information.
Hardware Preparation
- DFRduino UNO R3 x 1
- IO Sensor Expansion Shield V7.1 x 1
- Gesture&Touch Sensor (SEN0285) x 1
- LCD1602 Module x 1
- PC x 1
- Connectors
Software Preparation
- Arduino IDE (V1.6.8 above)
- Download and install the Gesture and Touch Library
- Download and install the LCD1602 Library (About how to install the library?)
Wiring Diagram
Other Preparation Work
Change the RGBaddr value based on the LCD hardware version:
| Module | Version | RGBAddr |
|---|---|---|
| LCD1602 Module | V1.0 | 0x60 |
| LCD1602 Module | V1.1 | 0x6B |
| LCD1602 RGB Module | V1.0 | 0x60 |
| LCD1602 RGB Module | V1.1 | 0x2D |
Sample Code
/*!
* @file simpleGesture.ino
* @brief Sensor event will print on your serial monitor
* @n for esp32, rx_pin = D5, tx_pin = D6
* @copyright Copyright (c) 2010 DFRobot Co.Ltd (http://www.dfrobot.com)
* @license The MIT License (MIT)
* @author PengKaixing([email protected])
* @version V1.0.0
* @date 2022-03-25
* @url https://github.com/DFRobot/DFRobot_Gesture_Touch
*/
#include "DFRobot_Gesture_Touch.h"
#include <Wire.h>
#include "DFRobot_RGBLCD1602.h"
/*
Change the RGBaddr value based on the hardware version
-----------------------------------------
Moudule | Version| RGBAddr|
-----------------------------------------
LCD1602 Module | V1.0 | 0x60 |
-----------------------------------------
LCD1602 Module | V1.1 | 0x6B |
-----------------------------------------
LCD1602 RGB Module | V1.0 | 0x60 |
-----------------------------------------
LCD1602 RGB Module | V1.1 | 0x2D |
-----------------------------------------
*/
DFRobot_RGBLCD1602 lcd(/*RGBAddr*/0x60 ,/*lcdCols*/16,/*lcdRows*/2); //16 characters and 2 lines of show
#ifdef __AVR__
SoftwareSerial mySerial(/*RX*/10, /*TX*/11);
#elif defined ESP_PLATFORM
// ESP32:IO16 <--> TX:sensor
// ESP32:IO17 <--> RX:sensor
HardwareSerial mySerial(1);
#endif
// init sensor object, request write and read function
DFRobot_Gesture_Touch DFGT(&mySerial);
void setup()
{
Serial.begin(115200);
// initialize the LCD and master IIC
lcd.init();
delay(20);
// suggest default value
DFGT.setGestureDistance(20);
// enable all functions
DFGT.enableFunction(DFGT_FUN_ALL);
// disable function test
DFGT.disableFunction(DFGT_FUN_RIGHT | DFGT_FUN_LEFT);
// enable function test
// DFGT.enableFunction(DFGT_FUN_RIGHT | DFGT_FUN_LEFT);
// set auto sleep time out, in sleep mode, something approach will wake it up
// DFGT.setSleep(4);
Serial.println("simple Gesture!");
/**
* @brief set cursor position
* @param col columns optional range 0-15
* @param row rows optional range 0-1,0 is the first row, 1 is the second row
*/
lcd.setCursor(0, 0);
lcd.print("State:");
}
void loop()
{
lcd.setCursor(0, 1);
int8_t rslt = DFGT.getAnEvent(); // get an event that data saved in serial buffer
if(rslt != DF_ERR) {
switch(rslt) {
case DFGT_EVT_BACK: {
lcd.print("Back ");
} break;
case DFGT_EVT_FORWARD: {
lcd.print("Forward ");
} break;
case DFGT_EVT_RIGHT: {
lcd.print("Right ");
} break;
case DFGT_EVT_LEFT: {
lcd.print("Left ");
} break;
case DFGT_EVT_PULLUP: {
lcd.print("Pull up ");
} break;
case DFGT_EVT_PULLDOWN: {
lcd.print("Pull Down ");
} break;
case DFGT_EVT_PULLREMOVE: {
lcd.print("Pull and Remove ");
} break;
case DFGT_EVT_TOUCH1: {
lcd.print("Touch1 ");
} break;
case DFGT_EVT_TOUCH2: {
lcd.print("Touch2 ");
} break;
case DFGT_EVT_TOUCH3: {
lcd.print("Touch3 ");
} break;
case DFGT_EVT_TOUCH4: {
lcd.print("Touch4 ");
} break;
case DFGT_EVT_TOUCH5: {
lcd.print("Touch5 ");
} break;
default: break;
}
}
}
Result
The LCD will display the current sensor state. For example:
State:
Pull Down
Was this article helpful?
