Touchscreen to 3 channel Relay, when plugged in all the relay is turned on

Here is my code thanks...




#include <Adafruit_GFX.h>
#include <MCUFRIEND_kbv.h>
MCUFRIEND_kbv tft;
#include <TouchScreen.h>
#define MINPRESSURE 200
#define MAXPRESSURE 1000

int R1= 13
;int R2=12;
int  R3=11;
int  R4=10 ;


;
// ALL Touch panels and wiring is DIFFERENT
// copy-paste results from TouchScreen_Calibr_native.ino
//const int XP = 8, XM = A2, YP = A3, YM = 9; //ID=0x9341
const int XP=6,XM=A2,YP=A1,YM=7; //ID=0x9341
const int TS_LEFT = 907, TS_RT = 136, TS_TOP = 942, TS_BOT = 139;
//const int TS_LEFT = 136, TS_RT = 907, TS_TOP = 139, TS_BOT = 942;

TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);

Adafruit_GFX_Button  Fon_btn, Foff_btn ,Bon_btn,Boff_btn,Lon_btn,Loff_btn,Hon_btn,Hoff_btn;

int pixel_x, pixel_y;     //Touch_getXY() updates global vars
bool Touch_getXY(void)
{
    TSPoint p = ts.getPoint();
    pinMode(YP, OUTPUT);      //restore shared pins
    pinMode(XM, OUTPUT);
    digitalWrite(YP, HIGH);   //because TFT control pins
    digitalWrite(XM, HIGH);
    bool pressed = (p.z > MINPRESSURE && p.z < MAXPRESSURE);
    if (pressed) {
        //pixel_x = map(p.x, TS_LEFT, TS_RT, 0, tft.width()); //.kbv makes sense to me
        pixel_x = map(p.x, TS_RT, TS_LEFT, 0, tft.width());
        pixel_y = map(p.y, TS_TOP, TS_BOT, 0, tft.height());

    
    }
    return pressed;
}

#define BLACK   0x0000
#define BLUE    0x001F
#define RED     0xF800
#define GREEN   0x07E0
#define CYAN    0x07FF
#define MAGENTA 0xF81F
#define YELLOW  0xFFE0
#define WHITE   0xFFFF

void setup(void)
{
    Serial.begin(9600);
    uint16_t ID = tft.readID();
    Serial.print("TFT ID = 0x");
    Serial.println(ID, HEX);
    Serial.println("Calibrate for your Touch Panel");
    if (ID == 0xD3D3) ID = 0x9486; // write-only shield
    tft.begin(ID);
    tft.setRotation(0);            //PORTRAIT
    tft.fillScreen(BLACK);
    Fon_btn.initButton(&tft,  60, 050, 100, 40, WHITE, CYAN, BLACK, "ON", 2);
    Foff_btn.initButton(&tft, 180, 050, 100, 40, WHITE, CYAN, BLACK, "Off", 2);
    Fon_btn.drawButton(false);
     Foff_btn.drawButton(false);
    Bon_btn.initButton(&tft,  60, 110, 100, 40, WHITE, CYAN, BLACK, "ON", 2);
    Boff_btn.initButton(&tft, 180, 110, 100, 40, WHITE, CYAN, BLACK, "Off", 2);
    Bon_btn.drawButton(false);
    Boff_btn.drawButton(false);
    Lon_btn.initButton(&tft,  60, 180, 100, 40, WHITE, CYAN, BLACK, "ON", 2);
    Loff_btn.initButton(&tft, 180, 180, 100, 40, WHITE, CYAN, BLACK, "Off", 2);
    Lon_btn.drawButton(false);
    Loff_btn.drawButton(false);
    Hon_btn.initButton(&tft,  60, 250, 100, 40, WHITE, CYAN, BLACK, "ON", 2);
    Hoff_btn.initButton(&tft, 180, 250, 100, 40, WHITE, CYAN, BLACK, " Off", 2);
    Hon_btn.drawButton(false);
    Hoff_btn.drawButton(false);
    digitalWrite(R1, HIGH);
    digitalWrite(R2, HIGH);
    digitalWrite(R3, HIGH);
    digitalWrite(R4, HIGH);
    pinMode(R1, OUTPUT);
    pinMode(R2, OUTPUT);
    pinMode(R3, OUTPUT);
    pinMode(R4, OUTPUT);
}

/* two buttons are quite simple
 */
void loop(void)
{
    bool down = Touch_getXY();
    Fon_btn.press(down && Fon_btn.contains(pixel_x, pixel_y));
    Foff_btn.press(down && Foff_btn.contains(pixel_x, pixel_y));
    Bon_btn.press(down && Bon_btn.contains(pixel_x, pixel_y));
    Boff_btn.press(down && Boff_btn.contains(pixel_x, pixel_y));
    Lon_btn.press(down && Lon_btn.contains(pixel_x, pixel_y));
    Loff_btn.press(down && Loff_btn.contains(pixel_x, pixel_y));
    Hon_btn.press(down && Hon_btn.contains(pixel_x, pixel_y));
    Hoff_btn.press(down && Hoff_btn.contains(pixel_x, pixel_y));
    
    if (Fon_btn.justReleased())
       Fon_btn.drawButton(false);
    if (Foff_btn.justReleased())
        Foff_btn.drawButton(false);
    if (Fon_btn.justPressed()) {
        Fon_btn.drawButton(true);
        digitalWrite(R1, HIGH);
    }
    if (Foff_btn.justPressed()) {
        Foff_btn.drawButton(true);
        digitalWrite(R1, LOW);
    }
   if (Bon_btn.justReleased())
       Bon_btn.drawButton(false);
    if (Boff_btn.justReleased())
        Boff_btn.drawButton(false);
    if (Bon_btn.justPressed()) {
        Bon_btn.drawButton(true);
        digitalWrite(R2, HIGH);
    }
    if (Boff_btn.justPressed()) {
        Boff_btn.drawButton(true);
        digitalWrite(R2, LOW);}

        if (Lon_btn.justReleased())
       Lon_btn.drawButton(false);
    if (Loff_btn.justReleased())
        Loff_btn.drawButton(false);
    if (Lon_btn.justPressed()) {
        Lon_btn.drawButton(true);
        digitalWrite(R3, HIGH);
    }
    if (Loff_btn.justPressed()) {
        Loff_btn.drawButton(true);
        digitalWrite(R3, LOW);
    }
    if (Hon_btn.justReleased())
       Hon_btn.drawButton(false);
    if (Hoff_btn.justReleased())
        Hoff_btn.drawButton(false);
    if (Hon_btn.justPressed()) {
        Hon_btn.drawButton(true);
        digitalWrite(R4, HIGH);
    }
    if (Hoff_btn.justPressed()) {
        Hoff_btn.drawButton(true);
        digitalWrite(R4, LOW);
    }
    }

Check the connection to the TV, it might be loose. That is more information then you gave us on the hardware. Post an annotated schematic showing how you have wired it and links to the hardware devices technical information.

1 Like

Initially, your pins (R1...R4) are INPUTs so when you write them HIGH, you are turning on the internal pull-up resistors, just like doing pinmode(R1, INPUT_PULLUP).

Next, you set them to OUTPUT and since the chip powers up with them initially LOW, that is what you get on the pin. Many relays are active LOW which means they turn on when the signal is LOW and turn off when the signal is HIGH.

Do a little experimenting and find out what you have.

    pinMode(R1, OUTPUT);
    pinMode(R2, OUTPUT);
    pinMode(R3, OUTPUT);
    pinMode(R4, OUTPUT);
    digitalWrite(R1, LOW);
    digitalWrite(R2, HIGH);
    digitalWrite(R3, LOW);
    digitalWrite(R4, HIGH);

This should make 2 relays on and 2 off and will tell you what state to use for on/off

Last question, when plugged in why all relay is turned on? thanksss

Because your relays should be active LOW so the logic is inverted. HIGH to OFF and LOW to ON.

After:

pinMode(R4, OUTPUT);

Add:

digitalWrite(R1, HIGH);
digitalWrite(R2, HIGH);
digitalWrite(R3, HIGH);
digitalWrite(R4, HIGH);

@brandon474, do not cross-post. Thread merge.

When i plug the power all the relay now off, but the problem is even though the relay is off the out has power

When i plug the power all the relay now off, but the problem is even though the relay is off the out has power, the tester can read power to the relay even it is turned off

Show us how the load is connected.

It should be connected between common and NO.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.