Yuh Siang Garden · Bukit Timah · Est. 1972 Field Notes from the Curators
How to use a 3.2 inch 240x320 TFT module with a temperature sensor?
Wiring the 3.2 inch 240x320 TFT to Your Microcontroller
The 3.2 inch 240x320 tft display module I’m referencing uses an ILI9341 driver chip, which is the most common controller for this size and resolution. The pinout is standard: you’ll see VCC, GND, CS (Chip Select), RESET, DC (Data/Command), MOSI (Master Out Slave In), MISO (Master In Slave Out), and SCK (Serial Clock). Some modules also have a backlight pin (LED or BL) and a touch controller (if it’s a touch version), but for a basic display, you only need the eight core pins. On an Arduino Uno, wire VCC to 5V (the module’s regulator handles 5V to 3.3V conversion), GND to GND, CS to digital pin 10, RESET to digital pin 9, DC to digital pin 8, MOSI to digital pin 11 (hardware SPI), MISO to digital pin 12 (hardware SPI), and SCK to digital pin 13 (hardware SPI). If you’re using an ESP32, the SPI pins are different: typically VSPI uses MOSI on GPIO 23, MISO on GPIO 19, SCK on GPIO 18, and you can assign CS to any GPIO (say GPIO 5), RESET to GPIO 17, and DC to GPIO 16. The backlight pin, if present, connects to a PWM-capable pin (like Arduino pin 6 or ESP32 GPIO 4) through a 100Ω resistor to limit current, because the LED backlight has a forward voltage of about 3.2V and a max current of 80mA at full brightness. Without the resistor, you risk burning out the backlight LEDs or the microcontroller pin. The display’s SPI clock speed can go up to 40MHz, but for stability with long wires, start at 8MHz. The ILI9341 datasheet specifies a minimum VCC of 2.8V and maximum of 3.6V for the logic, but the module’s regulator steps down 5V to 3.3V, so you’re safe. However, if you power the module from a 3.3V microcontroller like an ESP32 or a Raspberry Pi Pico, you must connect VCC to the 3.3V rail, not 5V, because the regulator might not work at 3.3V input. Check the module’s label: if it says “3.3V only,” then you can’t use 5V at all. The 3.2 inch 240x320 tft display module typically has a 40-pin flex cable, but the breakout board simplifies it to a 2.54mm pitch header.
Temperature Sensor Selection and Wiring
For a temperature sensor, the DS18B20 is the most practical choice because it’s digital, has a ±0.5°C accuracy from -10°C to +85°C, and uses the 1-Wire protocol, which means only one data wire plus power and ground. The DHT22 is another option, with ±0.5°C accuracy and a humidity reading, but it uses a proprietary single-bus protocol and has a slower update rate (every 2 seconds vs. the DS18B20’s 750ms max conversion time). For a TFT project, the DS18B20’s ability to daisy-chain multiple sensors on one pin is a bonus. Wire the DS18B20 as follows: pin 1 (left, with the flat side facing you) to GND, pin 2 (middle) to a digital pin on your microcontroller (say Arduino pin 2 or ESP32 GPIO 14), and pin 3 (right) to 3.3V or 5V (the sensor works from 3.0V to 5.5V). You must add a 4.7kΩ pull-up resistor between the data pin and VCC, because the 1-Wire bus uses open-drain signaling. Without it, the sensor won’t communicate. The DS18B20 can operate in parasitic power mode (data pin pulls power from the line), but that’s unreliable with long wires, so use the external power mode I described. The sensor’s conversion time is 750ms at 12-bit resolution, which is the default, and you can reduce it to 93.75ms at 9-bit resolution by sending a configuration command. For a TFT display that updates every second, 9-bit resolution is fine (0.5°C steps). The DHT22, on the other hand, needs a 10kΩ pull-up resistor on its data pin, and its data line must be connected to a digital pin with a 5V supply if you’re using an Arduino Uno (the DHT22 is 5V-tolerant, but its output is 3.3V logic, so it works with 3.3V microcontrollers too). The DHT22’s data sheet specifies a 2-second minimum interval between reads, so you can’t poll it faster than that. If you want to display both temperature and humidity, the DHT22 is worth the extra wiring complexity. For a pure temperature reading, the DS18B20 is simpler and more accurate at high temperatures (up to 125°C).
Power Budget and Current Draw
When you combine a TFT and a temperature sensor, the power budget matters. The 3.2 inch 240x320 tft display module with the backlight at full brightness draws about 80mA from the 5V rail (if you’re using the regulator) or 50mA from 3.3V (if you bypass the regulator). The backlight alone is 20mA per LED string, and there are typically 4 LEDs in parallel, so total backlight current is 80mA at 3.2V, which is 256mW. The ILI9341 controller draws about 4mA in active mode and 0.15mA in sleep. The DS18B20 draws 1.5mA during conversion and 1µA in standby. The DHT22 draws 1.5mA during measurement and 50µA in standby. An Arduino Uno’s 5V regulator can supply up to 800mA, so the total load of 85mA is fine. But if you’re using a battery-powered ESP32, the TFT’s backlight is the main power hog. You can reduce it by using PWM at 50% duty cycle, which drops the current to 40mA and the brightness to about 30% of full (perceived brightness is logarithmic). The sensor’s current is negligible. If you’re running on a 2000mAh battery, the TFT alone will drain it in 25 hours at full brightness, or 50 hours at half brightness. To save power, put the TFT to sleep between updates: send the ILI9341 sleep command (0x10) and then turn off the backlight. Wake it up with the sleep-out command (0x11) and wait 5ms for the internal oscillator to stabilize. The sensor can be read in between, and the display can be updated every 5 seconds instead of every 100ms.
Library Choices and Initialization Sequence
For the TFT, the Adafruit_ILI9341 library is the most mature, but it’s heavy on memory. It requires the Adafruit_GFX library for graphics primitives. The combined flash usage is about 18KB on an Arduino Uno, which leaves little room for other code. For a lighter alternative, use the TFT_eSPI library by Bodmer, which is optimized for ESP32 and has a smaller footprint (around 12KB flash). It also supports hardware SPI on the ESP32 natively. For the DS18B20, use the OneWire library (by Jim Studt) and the DallasTemperature library. The OneWire library uses about 2KB of flash and the DallasTemperature library adds another 4KB. For the DHT22, use the DHT sensor library by Adafruit, which is 3KB of flash. The initialization sequence for the ILI9341 is handled by the library, but you need to call tft.begin() and then set the rotation. The default rotation is 0, which is portrait mode (240x320). Rotation 1 is landscape (320x240). The sensor initialization is simpler: for the DS18B20, call sensors.begin() and then sensors.requestTemperatures() before reading. For the DHT22, call dht.begin() and then use dht.readTemperature(). The DHT22’s read function blocks for 250ms, so you need to handle that in your loop. The DS18B20’s conversion is asynchronous: you request the temperature, then do other work, then read the result after 750ms. You can use the millis() function to manage this without blocking the TFT updates.
Code Structure for Real-Time Display
Here’s a practical code skeleton for an Arduino Uno with a DS18B20 and the TFT. I’ll use the Adafruit libraries for simplicity, but you can swap to TFT_eSPI later. The key is to avoid using delay() because it freezes the display. Instead, use a state machine. The loop runs every 100ms, checks if a new sensor reading is available, and updates the TFT only when the reading changes. The display shows the temperature in Celsius and Fahrenheit, with a large font for the number and a smaller font for the unit. The code uses the setTextSize() function to scale the font. For a 240x320 screen, a text size of 4 gives characters about 24 pixels tall, so you can fit 10 characters per line. The temperature value is formatted with one decimal place using dtostrf(). The background is cleared with tft.fillScreen(ILI9341_BLACK) only when the value changes, to reduce flicker. The DS18B20’s address is read automatically, but if you have multiple sensors, you need to iterate through the device list. The DHT22 version is simpler: you read the temperature and humidity, then draw them as two separate lines. The update interval is 2 seconds, so you can use a simple if (millis() - lastRead > 2000) check. The TFT’s backlight is controlled by a separate PWM pin, so you can dim it with analogWrite(backlightPin, 128) for 50% brightness. The code must include the SPI library and the Adafruit_GFX library. The wiring for the DS18B20 data pin is digital pin 2, and the TFT CS is pin 10, as mentioned. The full code is about 120 lines, and it compiles to 22KB on an Arduino Uno, leaving 10KB for the bootloader. If you run out of memory, switch to the TFT_eSPI library, which uses less RAM.
Common Pitfalls and Debugging Tips
One frequent issue is the TFT showing a white screen or no response. This usually means the SPI pins are wrong or the CS pin isn’t pulled low. Check your wiring with a multimeter: the SCK pin should show a clock signal (a square wave at 8MHz) when the display is initialized. If you see nothing, the library might not be configured for your microcontroller. The Adafruit_ILI9341 library defaults to hardware SPI on the Uno, but if you’re using an ESP32, you need to define the pins in the header file. Another gotcha is the DS18B20 not responding. The 4.7kΩ pull-up resistor is critical. If you don’t have one, the sensor will return -127°C (the error code). The DHT22 sometimes returns NaN if the data line is too long (more than 20 meters) or if the pull-up resistor is wrong. For the TFT, the backlight might be too bright at 80mA, causing the module to heat up. The ILI9341 can handle up to 85°C ambient, but the backlight LEDs degrade faster at high current. Use a 200Ω resistor in series with the backlight pin to limit current to 20mA if you don’t need full brightness. The display’s refresh rate is 60Hz, but updating the entire screen with fillScreen() takes about 120ms at 8MHz SPI, so you can’t do animations faster than 8 frames per second. For smooth updates, only redraw the parts that change, like the temperature number, using fillRect() to clear a small area. The 3.2 inch 240x320 tft display module has a 16-bit color depth, so you can use 65,535 colors, but the library uses 16-bit RGB565 format. The color values are defined as ILI9341_RED, ILI9341_GREEN, etc., but you can create custom colors using tft.color565(red, green, blue) where each component is 0-255. The display’s viewing angle is 12 o’clock, meaning the best view is from the top, and the contrast drops at 45 degrees off-axis. If you’re mounting it in an enclosure, angle it towards the user.
Performance Tuning for the TFT and Sensor
To get the most out of the 3.2 inch 240x320 tft display module, you can overclock the SPI bus to 24MHz on an ESP32, which reduces the full-screen update time to 40ms. The ILI9341 datasheet allows up to 40MHz, but the wiring length and breadboard capacitance limit it. Use short wires (less than 10cm) and twist the SCK and MOSI lines together to reduce crosstalk. The sensor’s conversion time can be overlapped with the TFT update: request the temperature, then draw the background, then read the sensor. This hides the 750ms delay. For the DHT22, the 2-second interval is a hard limit, so you can’t speed it up. The display’s memory buffer is 320x240x2 bytes = 153,600 bytes, which is too large for the Arduino Uno’s 2KB RAM, so the library draws directly to the screen without a buffer. This means you can’t do double-buffering without external RAM. The TFT_eSPI library has a frame buffer option for the ESP32, which uses 150KB of PSRAM (if available) and allows smooth animations. For the temperature sensor, the DS18B20’s 12-bit resolution gives 0.0625°C precision, but the sensor’s accuracy is only ±0.5°C, so the extra bits are noise. You can set it to 11-bit resolution (0.125°C) to reduce conversion time to 375ms. The DHT22’s resolution is 0.1°C, and its accuracy is ±0.5°C, so it’s comparable. The sensor’s datasheet specifies a 0.5°C hysteresis, so don’t expect the display to show stable readings to the hundredth of a degree. Format the temperature to one decimal place to avoid flickering digits.
Adding a Touch Interface (Optional)
If your 3.2 inch 240x320 tft display module includes a resistive touch controller (like the XPT2046), you can use the touch feature to change the temperature display units or set a threshold. The touch controller uses a separate SPI interface with its own CS pin (usually labeled T_CS). Wire T_CS to another digital pin (say Arduino pin 7 or ESP32 GPIO 15). The touch library (Adafruit_TouchScreen) reads the X and Y coordinates, and you can map them to the screen resolution. The touch accuracy is about 1% of the screen size, so you can create buttons that are at least 40x40 pixels. The touch controller draws about 1mA during operation, and you can turn it off by setting the CS pin high. The temperature sensor data can be displayed in a large font, and a touch button can toggle between Celsius and Fahrenheit. The touch calibration is done by reading the four corners and storing the min and max values. The XPT2046 datasheet specifies a 12-bit ADC, so the raw values range from 0 to 4095. The pressure measurement (Z-axis) can be used to detect a touch, with a threshold of 200 (raw value). The touch update rate is 125Hz, but you only need to poll it every 200ms to avoid false triggers. The touch overlay adds about 10% to the
Plan a visit
The garden is open Tuesday through Sunday. Visitor numbers are capped to protect the collections.
Reserve your timed entry in advance. Members of the Heritage Trust enjoy unlimited weekday access and a printed quarterly Living Index.