Xinyuan-LilyGO / LilyGo-T-Relay

MIT License
70 stars 28 forks source link

T-relay-s3 (H629) 6 relay board simple coding & pin out #34

Closed Maxheadroom831 closed 2 months ago

Maxheadroom831 commented 4 months ago

Hello, I am a beginner and had trouble getting the relays to work. After I realized the six relays were controlled by a SIPO (serial in parallel out) shift register chip 74HC595 chip and not by direct pin allocations of the ESP32-S3 microprocessor . The code was easy to find an example for.

If you look at the schematic for the LilyGo-T-Relay

https://github.com/Xinyuan-LilyGO/LilyGo-T-Relay/blob/main/Schematic/T-Relay6-ESP32S3-Rev1.1.pdf

On the schematic there is circuit called Driver which show a 74HC595 chip controlling the relays. To control the 74HC595 chip there are 3 control pins. Latch , Clock and A (data) pin

They are controlled by the esp32-s3 pins as follows:

pin 5 = Latch (latch LOW keeps the 74HC595 from sending out there serial data, High sends the data out). pin 6 = Clock pin 7= A (data)

I used a 6 bit binary number to represent each of the 6 relays. i.e. B000000 = all off, B111111 = all on, B000001 = relay #1 on, B101010 = relays 6/4/2 on.

You could also use a HEX number.

...........Simple CODE........

// Define pin connections  from the esp32-s3 to 74HC595 chip.

const int latchPin = 6;
const int clockPin = 5;
const int dataPin = 7;

void setup ()
{
  // Setup pins as Outputs

  pinMode(latchPin, OUTPUT);
  pinMode(clockPin, OUTPUT);
  pinMode(dataPin, OUTPUT);

}

void loop() {

 //turn off all relays

    digitalWrite(latchPin, LOW); // to keep relays from changing while reading serial data

    shiftOut(dataPin, clockPin, MSBFIRST, B000000); // turn all relays off

    digitalWrite(latchPin, HIGH); // send out to serial data to the relays

    delay(1000);

    // Shift out the bits 1 through 6 with left most bit first
    digitalWrite(latchPin, LOW);

    shiftOut(dataPin, clockPin, MSBFIRST, B000001);  // turn on relay #1

    digitalWrite(latchPin, HIGH);

 delay(1000);

 // Shift out the bits 1 through 6
    digitalWrite(latchPin, LOW);

    shiftOut(dataPin, clockPin, MSBFIRST, B000010);  // turn on relay #2

    digitalWrite(latchPin, HIGH);

 delay(1000);

 // Shift out the bits 1 through 6
    digitalWrite(latchPin, LOW);

    shiftOut(dataPin, clockPin, MSBFIRST, B000100);  // turn on relay #3

    digitalWrite(latchPin, HIGH);

 delay(1000);

 // Shift out the bits 1 through 6
    digitalWrite(latchPin, LOW);

    shiftOut(dataPin, clockPin, MSBFIRST, B001000);  // turn on relay #4

    digitalWrite(latchPin, HIGH);

 delay(1000);

 // Shift out the bits 1 through 6
    digitalWrite(latchPin, LOW);

    shiftOut(dataPin, clockPin, MSBFIRST, B010000);  // turn on relay #5

    digitalWrite(latchPin, HIGH);

 delay(1000);

 // Shift out the bits 1 through 6
    digitalWrite(latchPin, LOW);

    shiftOut(dataPin, clockPin, MSBFIRST, B100000);  // turn on relay #6

    digitalWrite(latchPin, HIGH);

 delay(1000);
}
Llgok commented 2 months ago

@Maxheadroom831 You can refer to the following example program to write your code HT74HC595