Example Code of Drawing a Triangle for Arduino

Last revision 2026/02/04

This article introduces a practical example of using Arduino to draw and fill a triangle on a display, offering sample code and instructions to set up the environment and execute the graphics function using specific coordinates.

Introduction

The function of the program: draw a triangle with the coordinates of the three apexes (-20, -50), (0, 0) and(50,20)and orange frame at the centre of the screen, and fill the triangle with orange.

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_WHITE);
}

void loop(void)
{
  tft.drawTriangle(-20, -50, 0, 0, 50, 20, DISPLAY_ORANGE);  //draw triangle
  delay(1000);
  tft.fillTriangle(-20, -50, 0, 0, 50, 20, DISPLAY_ORANGE);  //fill triangle with color
  delay(1000);
  tft.fillScreen(DISPLAY_WHITE);

Result

Was this article helpful?

TOP