Example Code of Drawing a Rectangle for Arduino

Last revision 2026/02/04

This article presents a step-by-step guide for drawing and filling rectangles using Arduino, featuring sample code and visual results to enhance learning and implementation.

Introduction

The function of the program: draw a rectangle with a coordinate of the top left apex(-20,-20), length 40, width 40 and blue frame at the centre of the screen, and fill the rectangle with blue.

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.drawRect(-20, -20, 40, 40, DISPLAY_BLUE);  //draw rectangle
  delay(1000);
  tft.fillRect(-20, -20, 40, 40, DISPLAY_BLUE);  //fill rectangle
  delay(1000);
  tft.fillScreen(DISPLAY_WHITE);
  delay(1000);
}

Result

Was this article helpful?

TOP