TFTColorPicker
With three sensors, change the color of the TFT screen.
TFT Color Picker
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.
Attach the three pots to the breadboard, connecting their outside legs to power and ground. the center legs should connect to A0-A2.
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.
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.
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 102#define dc 93#define rst 84
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 Picker4
5 This example for the Arduino screen reads the input of6
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 Fitzgerald14
15 http://www.arduino.cc/en/Tutorial/TFTColorPicker16
17 */18
19// pin definition for the Uno20#define cs 1021#define dc 922#define rst 823
24// pin definition for the Leonardo25// #define cs 726// #define dc 027// #define rst 128
29#include <TFT.h> // Arduino LCD library30#include <SPI.h>31
32TFT TFTscreen = TFT(cs, dc, rst);33
34void setup() {35
36 // begin serial communication37
38 Serial.begin(9600);39
40 // initialize the display41
42 TFTscreen.begin();43
44 // set the background to white45
46 TFTscreen.background(255, 255, 255);47
48}49
50void loop() {51
52 // read the values from your sensors and scale them to 0-25553
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 values61
62 TFTscreen.background(redVal, greenVal, blueVal);63
64 // send the values to the serial monitor65
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 moment81
82 delay(33);83
84}
Suggest changes
The content on docs.arduino.cc is facilitated through a public GitHub repository. If you see anything wrong, you can edit this page here.
License
The Arduino documentation is licensed under the Creative Commons Attribution-Share Alike 4.0 license.