Jump to content

SOLVED - 4-Wire Resistive Touch-Screen Reading with Energia

Recommended Posts

Problem solved!


Here is a basic touch screen with 4 wires, YP-XP-YN-XN as per this specification sheet. The screen has no controller for touch.


The sketch works fine on Arduino Uno but not with the Stellaris.


When I move my finger horizontally, both x and y values change. Same problem occurs when I move my finger vertically.


Only one value should be impacted. 


What's going wrong? I suspect some initialisation problem, especially with the open pin.

// Core library for code-sense
#include "Energia.h"

// Include application, user and local libraries

// Define variables and constants
uint16_t x, y, z;

#define TOUCH_XP A9     // TOUCH_XP PE4 A9 Resistor touch screen terminal (Left) analog
#define TOUCH_YP A8     // TOUCH_YP PE5 A8 Resistor touch screen terminal (Top) analog
#define TOUCH_XN PA_3   // TOUCH_XN PA3 Resistor touch screen terminal (Right) digital
#define TOUCH_YN PA_2   // TOUCH_YN PA2 Resistor touch screen terminal (Bottom) digital

void getRawTouch2(uint16_t &x0, uint16_t &y0, uint16_t &z0)
    // Read x
    // xp = +Vref
    // xn = ground
    // yp = measure
    // yn = open
    pinMode(TOUCH_YP, INPUT);
    pinMode(TOUCH_YN, INPUT);
    digitalWrite(TOUCH_YP, LOW);
    digitalWrite(TOUCH_YN, LOW);
    pinMode(TOUCH_XP, OUTPUT);
    pinMode(TOUCH_XN, OUTPUT);
    digitalWrite(TOUCH_XP, HIGH);
    digitalWrite(TOUCH_XN, LOW);
    x0  = analogRead(TOUCH_YP);
    x0 += analogRead(TOUCH_YP);
    x0  = 4095 - (x0/2);
    // Read y
    // xp = measure
    // xn = open
    // yp = +Vref
    // yn = ground
    pinMode(TOUCH_XP, INPUT);
    pinMode(TOUCH_XN, INPUT);
    digitalWrite(TOUCH_XP, LOW);
    digitalWrite(TOUCH_XN, LOW);
    pinMode(TOUCH_YP, OUTPUT);
    pinMode(TOUCH_YN, OUTPUT);
    digitalWrite(TOUCH_YP, HIGH);
    digitalWrite(TOUCH_YN, LOW);
    y0  = analogRead(TOUCH_XP);
    y0 += analogRead(TOUCH_XP);
    y0  = 4095 - (y0/2);
    // Read z
    // xp = ground
    // xn = measure
    // yp = measure
    // yn = +Vref
    pinMode(TOUCH_XP, OUTPUT);
    digitalWrite(TOUCH_XP, LOW);
    pinMode(TOUCH_YN, OUTPUT);
    digitalWrite(TOUCH_YN, HIGH);
    digitalWrite(TOUCH_XN, LOW);
    pinMode(TOUCH_XN, INPUT);
    digitalWrite(TOUCH_YP, LOW);
    pinMode(TOUCH_YP, INPUT);
    int z1 = analogRead(TOUCH_XN);
    int z2 = analogRead(TOUCH_YP);
    if (x0>0) z0 = (4095-(z2-z1));
    else z0 = 0;

// Add setup code
void setup() {
    Serial.println("Touch Test");

// Add loop code
void loop() {
    getRawTouch2(x, y, z);
    if (z>0) {
        Serial.print(x, DEC);
        Serial.print(y, DEC);
        Serial.println(z, DEC);

Thank you for your help.

Link to post
Share on other sites

Have you looked at the output waveform (e.g. how fast it responds after you set the input pins.)

Since I do not think the Stellaris Launchpad can source as much current as the Arduino (and the processor is probably running a lot faster) wondered if 

it might not have time for the readings to settle before you are taking the reading.


Could try taking several readings in succession after setting up to read each axis (possibly with small delays), just to make sure that you are getting a stable reading.  


If you have the resistance of the touchpad, might be able to calculate how long it would take to charge the system.

Link to post
Share on other sites



Thank you very much for pointing on the right direction.


The Stellaris is too fast! The touch needs more time!


Here is the modified code:

    // for x axis
    do {
        a  = analogRead(TOUCH_YP);
        b  = analogRead(TOUCH_YP);
    } while (absDiff(a,  > 8);
    x0  = 4095 - a;


inline uint16_t absDiff(uint16_t a, uint16_t  { return (a >  ? a-b : b-a; }

Stability is reached after a couple of iterations.


Problem solved :)

Link to post
Share on other sites

Rei Vilo,


Thanks, this may explain the jitter on my touch screens! T never though a processor could be too fast! :huh:


Looks like I help you and you help me.  That is the way things are supposed to be in this forum!!!!!!!!!!!!!!!!!!


The fun thing is that a work I do low level development on Muilti-Giga Hz processors where I am used to waiting, at home I deal with 8-100Mhz and forgot about waits!  My focus has always to get things to run faster! :unsure: Lve and learn! :wacko:




Link to post
Share on other sites

@@jkabat, you're welcome!


Looks like I help you and you help me.  That is the way things are supposed to be in this forum!!!!!!!!!!!!!!!!!!



Yes, that's what makes this forum —Stellarisiti and 43oh— so nice.


The library for the Kentec 3.5" on Energia works fine now with the 8-but parallel bus and the touch, both solved through the forum.

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...