TFTColorPicker

With three sensors, change the color of the TFT screen.

TFT Color Picker

GLCD colorDemo

This example for the Arduino TFT screen reads the input of three analog sensors, using the values to change the screen's color.

Color on the TFT screen is handled as 8-bit numbers (0-255). However, the library scales these values to 5-bits (32 levels) for red and blue, 6-bits (64 levels) for green.

Hardware Required

  • Arduino Uno

  • Arduino TFT screen

  • breadboard

  • hookup wire

  • three 10-kilohm potentiometers

Circuit

Connect power and ground to the breadboard.

GLCD logo1

Attach the three pots to the breadboard, connecting their outside legs to power and ground. the center legs should connect to A0-A2.

GLCD picker2

Connect the LCD screen to the breadboard. The headers on the side of the screen with the small blue tab and arrow should be the ones that attach to the board. Pay attention to the orientation of the screen, in these images, it is upside down.

GLCD picker3

Connect the BL and +5V pins to power, and GND to ground. Connect CS-LD to pin 10, DC to pin 9, RESET to pin 8, MOSI to pin 11, and SCK to pin 13. If you're using a Leonardo, you'll be using different pins. see the getting started page for more details.

GTFT picker4 Click the image for a larger version

Code

To use the screen you must first include the SPI and TFT libraries.

1#include <SPI.h>
2#include <TFT.h>

Define the pins you're going to use for controlling the screen, and create an instance of the TFT library named

TFTscreen
. You'll reference that object whenever you're working with the screen.

1#define cs 10
2#define dc 9
3#define rst 8
4
5TFT TFTscreen = TFT(cs, dc, rst);

In

setup()
, initialize the screen and clear the background. Start serial communication as well.

1void setup() {
2
3 Serial.begin(9600);
4
5 TFTscreen.begin();
6
7 TFTscreen.background(255, 255, 255);
8}

In

loop()
, read the values from the pots, mapping them to values between 0-255. with
background()
, set the mapped background colors and print the values to the serial monitor.

1void loop() {
2
3 int redVal = map(analogRead(A0), 0, 1023, 0, 255);
4
5 int greenVal = map(analogRead(A1), 0, 1023, 0, 255);
6
7 int blueVal = map(analogRead(A2), 0, 1023, 0, 255);
8
9 TFTscreen.background(redVal, greenVal, blueVal);
10
11 Serial.print("background(");
12
13 Serial.print(redVal);
14
15 Serial.print(" , ");
16
17 Serial.print(greenVal);
18
19 Serial.print(" , ");
20
21 Serial.print(blueVal);
22
23 Serial.println(")");
24
25 delay(33);
26
27}

The complete sketch is below :

1/*
2
3 TFT Color Picker
4
5 This example for the Arduino screen reads the input of
6
7 potentiometers or analog sensors attached to A0, A1,
8
9 and A2 and uses the values to change the screen's color.
10
11 This example code is in the public domain.
12
13 Created 15 April 2013 by Scott Fitzgerald
14
15 http://www.arduino.cc/en/Tutorial/TFTColorPicker
16
17 */
18
19// pin definition for the Uno
20#define cs 10
21#define dc 9
22#define rst 8
23
24// pin definition for the Leonardo
25// #define cs 7
26// #define dc 0
27// #define rst 1
28
29#include <TFT.h> // Arduino LCD library
30#include <SPI.h>
31
32TFT TFTscreen = TFT(cs, dc, rst);
33
34void setup() {
35
36 // begin serial communication
37
38 Serial.begin(9600);
39
40 // initialize the display
41
42 TFTscreen.begin();
43
44 // set the background to white
45
46 TFTscreen.background(255, 255, 255);
47
48}
49
50void loop() {
51
52 // read the values from your sensors and scale them to 0-255
53
54 int redVal = map(analogRead(A0), 0, 1023, 0, 255);
55
56 int greenVal = map(analogRead(A1), 0, 1023, 0, 255);
57
58 int blueVal = map(analogRead(A2), 0, 1023, 0, 255);
59
60 // draw the background based on the mapped values
61
62 TFTscreen.background(redVal, greenVal, blueVal);
63
64 // send the values to the serial monitor
65
66 Serial.print("background(");
67
68 Serial.print(redVal);
69
70 Serial.print(" , ");
71
72 Serial.print(greenVal);
73
74 Serial.print(" , ");
75
76 Serial.print(blueVal);
77
78 Serial.println(")");
79
80 // wait for a moment
81
82 delay(33);
83
84}

Contribute to Arduino

Join the community and suggest improvements to this article via GitHub. Make sure to read out contribution policy before making your pull request.

Missing something?

Check out our store and get what you need to follow this tutorial.

Suggest Changes

The content on docs.arduino.cc is facilitated through a public GitHub repository. You can read more on how to contribute in the contribution policy.