Adafruit TSC2046 Touchscreen Arduino Library
Public Member Functions | List of all members
Adafruit_TSC2046 Class Reference

Class for interfacing with a TSC2046 touchscreen controller. More...

#include <Adafruit_TSC2046.h>

Public Member Functions

void begin (int spiChipSelect, SPIClass *the_spi=&SPI, uint32_t xResistance=400, uint32_t spiFrequency=2L *1000L *1000L)
 Initialize this TSC2046 using SPI. You must call this method before calling Adafruit_TSC2046::getPoint. More...
 
void setVRef (float vRef)
 Indicates the voltage connected to the TSC2046's "VRef" pin, if any. Use -1 if nothing is connected to the VRef pin. See the documentation for Adafruit_TSC2046::begin's vRef parameter for more information. More...
 
void setTouchedThreshold (float rTouchThreshold)
 Sets the threshold for Adafruit_TSC2046::isTouched. More...
 
TSPoint getPoint ()
 Gets the coordinates of the the current touch on the touchscreen. Use Adafruit_TSC2046::isTouched to determine if the touchscreen is being touched in the first place. More...
 
bool isTouched ()
 Determines if the touchscreen is currently being touched. The X and Y coordinates returned by Adafruit_TSC2046::getPoint are meaningless if this is false. More...
 
void enableInterrupts (bool enable)
 Enables or disables interrupts that fire when the touchscreen is touched. When an interrupt fires, the IRQ pin on the TSC2046 is pulled LOW. That pin can be connected to an interrupt-enabled Arduino pin to run code when the TSC2046 detects a touch. See [here] for information on using Arduino interrupts. More...
 
float readTemperatureC ()
 Reads the temperature measurement in degrees Celsius. More...
 
float readTemperatureF ()
 Reads the temperature measurement in degreese Fahrenheit. More...
 
float readBatteryVoltage ()
 Reads the voltage on the "VBat" pin, in volts. More...
 
float readAuxiliaryVoltage ()
 Reads the voltage on the "AUX" pin, in volts. More...
 
float effectiveVRef ()
 Gets the effective reference voltage, which is 2.5V if no external reference voltage value was provided in Adafruit_TSC2046::begin or Adafruit_TSC2046::setVRef, or the value of the vRef argument of those functions otherwise. More...
 

Detailed Description

Class for interfacing with a TSC2046 touchscreen controller.

Notable methods: Adafruit_TSC2046::begin, Adafruit_TSC2046::isTouched, and Adafruit_TSC2046::getPoint.

Note

The way power works with the TSC2046 is a bit unintuitive. When interrupts are enabled (which is the default, and only changed with Adafruit_TSC2046::enableInterrupts), the TSC2046 is put into an "auto power-down" mode, in which the TSC2046 automatically powers down at the end of a read, and automatically powers back up at the beginning of a read. According to the datasheet there's no delay for that and the power-up is instant. Because of that, this library leaves the TSC2046 in that mode by default.

In that mode, however, TSC2046 interrupts are enabled, meaning the IRQ pin goes LOW when the touchscreen is touched. If you have the IRQ pin connected to a pin on your Arduino that has interrupts enabled and you have enabled interrupts on that pin, and you don't want interrupts from this chip, you should call enableInterrupts(false). This will prevent the TSC2046 from powering down between reads, which uses more power, but will prevent the IRQ pin from going LOW if the touchscreen is touched and potentially causing an unwanted hardware interrupt.

Member Function Documentation

◆ begin()

void Adafruit_TSC2046::begin ( int  spiChipSelect,
SPIClass *  the_spi = &SPI,
uint32_t  xResistance = 400,
uint32_t  spiFrequency = 2L * 1000L * 1000L 
)

Initialize this TSC2046 using SPI. You must call this method before calling Adafruit_TSC2046::getPoint.

Parameters
xResistanceThe resistance in Ohms between X- and X+ on the TSC2046 breakout. With a multimeter set to measure resistance, place one probe on the pin-hole labled "X-" on your TSC2046 breakout board, and place the other probe on the pin-hole labled "X+". Your multimeter should show you a number in ohms (Ω), the unit for resistance. Pass that number as this parameter. If your multimeter gives you a value in kilohms (kΩ), divide that number by 1000 to get Ohms and pass that value.
If you do not have a multimeter or otherwise don't have a measurement, 400 (400Ω) is a reasonable value to use here.
spiChipSelectThe pin number on your board that you have connected to the SPI CS (Chip Select) pin on the TSC2046. Defaults to the SS pin if not provided, which is also the default chip select pin of the default SPI interface.
the_spiThe SPI interface to use when communicating to this touchscreen. Defaults to SPI, the default SPI interface on Arduino boards. This is often connected to pins labeled SCK, MOSI, and MISO on the physical board. For example, on Arduino Uno the MISO of the default SPI interface is pin 12.
spiFrequencyThe clock frequency for the SPI peripheral. Defaults to 2 MHz if not specified. Must not be higher than 2 MHz, per the TSC2046 datasheet.

◆ setVRef()

void Adafruit_TSC2046::setVRef ( float  vRef)

Indicates the voltage connected to the TSC2046's "VRef" pin, if any. Use -1 if nothing is connected to the VRef pin. See the documentation for Adafruit_TSC2046::begin's vRef parameter for more information.

Parameters
vRefThe voltage in volts of the supply connected to the TSC2046's VRef pin, if any. -1 (the default if you don't call this function) indicates that nothing is connected to the TSC2046's VRef pin. Connecting VRef to a voltage higher than 2.5V increases the accuracy of non-touchscreen reads (temperature, battery voltage, and auxiliary voltage), and also directly determines the maximum voltage value that can be measured by Adafruit_TSC2046::readAuxiliaryVoltage. It has no effect on touchscreen coordinate reads.
The TSC2046's VRef pin should either be connected to the same supply as the TSC2046's Vin pin, or not connected at all (Vin should be connected to a 5V or 3.3V supply from the Arduino). If you do not connect the VRef pin, either don't call this function at all, or pass -1 to this argument.

◆ setTouchedThreshold()

void Adafruit_TSC2046::setTouchedThreshold ( float  rTouchThreshold)

Sets the threshold for Adafruit_TSC2046::isTouched.

Parameters
rTouchThreshold

The resistance value (technically in Ohms) to use as the threshold for Adafruit_TSC2046::isTouched. Any pressure readings that are higher than the value provided here are considered "not touching" (remember that the pressure readings get LOWER as the physical pressure increases, see TSPoint::z). Also note that regardless of the threshold value, resistances of 0 and nonfinite numbers (like infinity) are always considered not touching.

If not set, the default value is 100000 (100kΩ).

◆ getPoint()

TSPoint Adafruit_TSC2046::getPoint ( )

Gets the coordinates of the the current touch on the touchscreen. Use Adafruit_TSC2046::isTouched to determine if the touchscreen is being touched in the first place.

Returns
The X, Y, and Z (pressure) coordinates as a TSPoint object.
See also
TSPoint.

◆ isTouched()

bool Adafruit_TSC2046::isTouched ( )

Determines if the touchscreen is currently being touched. The X and Y coordinates returned by Adafruit_TSC2046::getPoint are meaningless if this is false.

You can also change the threshold used to determine if the touchscreen is being touched with Adafruit_TSC2046::setTouchedThreshold.

Returns
True if the touchscreen is being touched, false if it is not.

◆ enableInterrupts()

void Adafruit_TSC2046::enableInterrupts ( bool  enable)

Enables or disables interrupts that fire when the touchscreen is touched. When an interrupt fires, the IRQ pin on the TSC2046 is pulled LOW. That pin can be connected to an interrupt-enabled Arduino pin to run code when the TSC2046 detects a touch. See [here] for information on using Arduino interrupts.

Note
On the TSC2046 side, interrupts are enabled by default, because disabling interrupts on this chip requires more power. If you do not want interrupts from the TSC2046 to run, it's recommended to instead either simply not connect the IRQ pin, or mask the interrupt on the Arduino processor. See the note on power in the class documentation for more information.
Parameters
enableTrue to enable interrupts, false to disable them.

◆ readTemperatureC()

float Adafruit_TSC2046::readTemperatureC ( )

Reads the temperature measurement in degrees Celsius.

Returns
The temperature in degrees Celsius (°C).
See also
Adafruit_TSC2046::readTemperatureF.

◆ readTemperatureF()

float Adafruit_TSC2046::readTemperatureF ( )

Reads the temperature measurement in degreese Fahrenheit.

Returns
The temperature in degrees Fahrenheit (°F).
See also
Adafruit_TSC2046::readTemperatureC.

◆ readBatteryVoltage()

float Adafruit_TSC2046::readBatteryVoltage ( )

Reads the voltage on the "VBat" pin, in volts.

The TSC2046 allows you to connect the positive voltage terminal of a battery to the "VBat" pin, and then monitor its voltage. The battery voltage can be (inclusively) between 0V and 6V, regardless of the voltage supply provided to Vin/Vcc.

Returns
The voltage of the connected battery, in volts.

◆ readAuxiliaryVoltage()

float Adafruit_TSC2046::readAuxiliaryVoltage ( )

Reads the voltage on the "AUX" pin, in volts.

The TSC2046 allows you to measure the voltage of whatever you connect to the AUX pin, however the voltage cannot be higher than the voltage reference. See the documentation for the vRef parameter of Adafruit_TSC2046::begin for more information, but in summary if you don't connect anything to the "VRef" pin on your TSC2046, the maximum auxiliary voltage you can read is 2.5V. If you want to be able to read higher voltages, connect the same pin you connected to "Vin" on the TSC2046 to the VRef pin on the TSC2046, and then pass the voltage of those pins to the vRef paramter of Adafruit_TSC2046::begin, or to Adafruit_TSC2046::setVRef.

Alternatively, if you want to measure voltages higher than the reference voltage, see Adafruit_TSC2046::readBatteryVoltage, which can read up to 6V.

Returns
The voltage on the AUX pin, in volts.

◆ effectiveVRef()

float Adafruit_TSC2046::effectiveVRef ( )

Gets the effective reference voltage, which is 2.5V if no external reference voltage value was provided in Adafruit_TSC2046::begin or Adafruit_TSC2046::setVRef, or the value of the vRef argument of those functions otherwise.

You probably don't need to call this function unless you're doing math directly on reads from this class.

Returns
The effective reference voltage in volts.
See also
Adafruit_TSC2046::setVRef.

The documentation for this class was generated from the following files: