Example Code of Drawing Lines for Arduino

Last revision 2026/02/04

This article introduces an Arduino program that uses the DFRobot ST7687S Latch library to draw a red diagonal line and white horizontal and vertical lines on a display. The sample code demonstrates the use of specific coordinates to create these line segments, offering a practical example for those interested in Arduino graphics programming.

Introduction

The function of the program: draw a red line segment through the points (-64, -64) and (64, 64); taking the point (-64, 0) as the starting point, draw a white horizontal line with a width of 128; taking the point (0, -64) as the starting point, draw a white verticla line with a height of 128. The three lines meet at the centre point of the screen (0, 0).

Sample Code

#include "DFRobot_ST7687S_Latch.h"

#ifdef __AVR__
uint8_t pin_cs = 3, pin_rs = 5, pin_wr = 6, pin_lck = 7;
#else
uint8_t pin_cs = D3, pin_rs = D5, pin_wr = D6, pin_lck = D7;
#endif

DFRobot_ST7687S_Latch tft(pin_cs, pin_rs, pin_wr, pin_lck);

void setup(void)
{
  Serial.begin(115200);
  tft.begin();
  tft.fillScreen(DISPLAY_BLACK);
}

void loop(void)
{
   tft.drawLine(-64, -64, 64, 64, DISPLAY_RED);  //draw line
   tft.drawHLine(-64, 0, 128, DISPLAY_WHITE);  //draw horizontal line
   tft.drawVLine(0, -64, 128, DISPLAY_WHITE);  //draw vertical line
}

Result

Was this article helpful?

TOP