In this project, we use a classic Arduino Nano to read analog values from three potentiometers. Each potentiometer represents one color component: Red (R), Green (G), and Blue (B). The result of the mixing is displayed in real-time on a 2.0" TFT LCD screen.
TFT LCD Display (SPI):
Potentiometers (Center pin):
To function, you need the Adafruit GFX and Adafruit ILI9341 libraries. The code reads analog inputs and converts them into the color format for the display.
// Project: Arduino Nano RGB Color Picker with TFT Display (ST7789)
// Author: Borut Bukovnik - vadnica.org
#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_ST7789.h>
#define TFT_CS 10
#define TFT_DC 8
#define TFT_RST 9
// Initialization for the ST7789 driver
Adafruit_ST7789 tft = Adafruit_ST7789(TFT_CS, TFT_DC, TFT_RST);
const int pinR = A0;
const int pinG = A1;
const int pinB = A2;
void setup() {
// Initialization of the ST7789 display (width: 240, height: 320)
tft.init(240, 320);
tft.setRotation(2);
tft.fillScreen(ST77XX_BLACK);
tft.invertDisplay(true);
tft.setCursor(50, 10);
tft.setTextColor(ST77XX_WHITE, ST77XX_BLACK);
tft.setTextSize(2);
tft.print("COLOR PICKER");
}
void loop() {
int valR = analogRead(pinR);
int valG = analogRead(pinG);
int valB = analogRead(pinB);
uint8_t r = map(valR, 0, 1023, 0, 255);
uint8_t g = map(valG, 0, 1023, 0, 255);
uint8_t b = map(valB, 0, 1023, 0, 255);
uint16_t color = tft.color565(r, g, b);
// Square with color preview
tft.fillRect(60, 40, 120, 120, color);
char buffer[40];
sprintf(buffer, "R:%3d G:%3d B:%3d", r, g, b);
tft.setCursor(20, 180);
tft.setTextColor(ST77XX_WHITE, ST77XX_BLACK);
tft.setTextSize(2);
tft.print(buffer);
sprintf(buffer, "HEX: 0x%04X ", color);
tft.setCursor(65, 210);
tft.setTextColor(ST77XX_YELLOW, ST77XX_BLACK);
tft.print(buffer);
delay(50);
}