Controlling your high-voltage Fan with an Touchscreen and Arduino MEGA!

In this article we will control your high-voltaged Fan with an 2.8 inch TFT Touchscreen (Elecrow & Seeed) and Arduino Mega!
We implement a few relays and tranistors to switch high voltages with low currents.
I have a particular type of fan with 3 phases with everyone his own speed.

Read the tutorial below or watch the youtube video.

Project materials:

1x Arduino MEGA
Click here for the Amazon productlink

arduino-mega

1x 2.8 inch TFT touchscreen
Click here for the Amazon productlink

touchscreen-2-8

3x 12V Relays 
Click here for the Amazon productlink

phoenix-relais-12v

3x Relay socket
Click here for the Amazon productlink

phoenix-relais-socket

3x Protection diode
Click here for the amazon productlink

phoenix-relais-beveiliging

3x BC237B Transistor
Click here for the amazon productlink

3x 27K ohm resistor
Click here for the amazon productlink
E-Projects 100EP51427K0 27k Ohm Resistors, 1/4 W, 5% (Pack of 100)

Some wires
Click here for the Amazon productlink

wires.jpg

Any type a Fan with 3 different speed levels.

Ventilator.png

Wiring Diagram:

Click here for the larger image

Assamble the Touchscreen on your Arduino mega

Wiring Fan + Touchscreen.png
Connections

Arduino 12V – 12V breadboard
Arduino GND – 0V breadboard
Arduino pin 40 – resistor 2   27K ohm
Arduino pin 41 – resistor 3   27K ohm
Arduino pin 42 – resistor 1    27k ohm
Resistor 1 – BC237B  (Basis) “NORMAL”
Resistor 2 – BC237B  (Basis) “SLOW”
Resistor 3 – BC237B  (Basis) “FAST”
BC237B (Emitter)”NORMAL” – 0V breadboard
BC237B (Emitter) “SLOW” – 0V breadboard
BC237B (Emitter)”FAST” – 0V breadboard
Relay 1 A1 – 12V breadboard
Relay 1 A2 – BC237B (Collector) “SLOW”
Relay 2 A1 – 12V breadboard
Relay 2 A2 – BC237B (Collector) “NORMAL”
Relay 3 A1 – 12V breadboard
Relay 3 A2 – BC237B (Collector) “FAST”
Relay 1 N.O Common(11) – 230V
Relay 2 N.O Common(11) – 230V
Relay 3 N.O Common(11) – 230V
Relay 1 N.O (14) – “SLOW SPEED” 230V FAN
Relay 2 N.O (14) – “NORMAL SPEED”230V FAN
Relay 3 N.O (14) – “FAST SPEED” 230V FAN
External 12V Supply – 12V breadboard
External 0V Supply – 0V breadboard
External 230V Supply – 230V
External 0V (High voltage) – 0V FAN
External PE – PE FAN

Arduino Code

Download the SeeedTouchScreen and TFTv2 library and import them in your arduino software.

Click here for the SeeedTouch library link

Click here for the TFTv2 libarary

Copy the code below & upload it to your Arduino.

// CreaTechWeb: Controlling your high-voltaged Fan with an Touchscreen and Arduino MEGA!
// https://www.createchweb.com/controlling-your-high-voltage-fan-with-an-touchscreen-and-arduino/ 



#include <stdint.h>
#include <SeeedTouchScreen.h> 
#include <TFTv2.h>
#include <SPI.h>


#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) // mega
#define YP A2   // must be an analog pin, use "An" notation!
#define XM A1   // must be an analog pin, use "An" notation!
#define YM 54   // can be a digital pin, this is A0
#define XP 57   // can be a digital pin, this is A3 

#endif

//Measured ADC values for (0,0) and (210-1,320-1)
//TS_MINX corresponds to ADC value when X = 0
//TS_MINY corresponds to ADC value when Y = 0
//TS_MAXX corresponds to ADC value when X = 240 -1
//TS_MAXY corresponds to ADC value when Y = 320 -1

#define TS_MINX 116*2
#define TS_MAXX 890*2
#define TS_MINY 83*2
#define TS_MAXY 913*2

int RelaySlow = 40;     // Digital output for the relay "Slow"
int RelayNormal = 41;   // Digital output for the relay "Normal"
int RelayFast = 42;     // Digital output for the relay "Fast"

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

void setup(void) {
 
    TFT_BL_ON;          // turn on the background light
    Tft.TFTinit();      // init TFT library

    
      pinMode(RelaySlow, OUTPUT);     // Define RelaySlow as output
      pinMode(RelayNormal, OUTPUT);   // Define RelayNormal as output
      pinMode(RelayFast, OUTPUT);     // Define RelayFast as output

  
    Tft.fillScreen(50, 190, 30, 80,WHITE);    // Set the colors of the button "Slow" in White at start
    Tft.drawString("SLOW",95,47,2,BLUE);      // Set the colors of the Text "Slow" in Blue at start  
    
    Tft.fillScreen(50, 190, 100, 150,WHITE);  // Set the colors of the button "Normal" in White at start
    Tft.drawString("NORMAL",85,117,2,BLUE);   // Set the colors of the Text "Normal" in Blue at start  
    
    Tft.fillScreen(50, 190, 170, 220,WHITE);  // Set the colors of the button "Fast" in White at start
    Tft.drawString("FAST",95,187,2,BLUE);     // Set the colors of the Text "Fast" in Blue at start  
    
    Tft.fillScreen(50, 190, 240, 290,GREEN);  // Set the colors of the button "OFF" in Green at start
    Tft.drawString("OFF",100,257,2,BLUE);     // Set the colors of the Text "OFF" in Blue at start  
 
  
   
}

void loop(void) {
  // a point object holds x y and z coordinates
  Point p = ts.getPoint();
  p.x = map(p.x, TS_MINX, TS_MAXX, 0, 240);
  p.y = map(p.y, TS_MINY, TS_MAXY, 0, 320);
  
  // If you touch the button "Slow"
  
  if (p.x > 50 and p.x < 160 and p.y > 30 and p.y < 80){
    digitalWrite(RelayNormal, LOW);           // Turn low digital pin RelayNormal
    digitalWrite(RelayFast, LOW);             // Turn low digital pin RelayFast
    
    Tft.fillScreen(50, 190, 30, 80,GREEN);    // Set the colors of the button "Slow" in Green
    Tft.drawString("SLOW",95,47,2,BLUE);      // Set the colors of the Text "Slow" in Blue 

    Tft.fillScreen(50, 190, 100, 150,WHITE);  // Set the colors of the button "Normal" in White 
    Tft.drawString("NORMAL",85,117,2,BLUE);   // Set the colors of the Text "Normal" in Blue   
    
    Tft.fillScreen(50, 190, 170, 220,WHITE);  // Set the colors of the button "Fast" in White
    Tft.drawString("FAST",95,187,2,BLUE);     // Set the colors of the Text "Fast" in Blue   
    
    Tft.fillScreen(50, 190, 240, 290,WHITE);  // Set the colors of the button "OFF" in White
    Tft.drawString("OFF",100,257,2,BLUE);     // Set the colors of the Text "OFF" in Blue  
    
    delay (200);                              // Delay for protection against multiple 230V voltages
    digitalWrite(RelaySlow, HIGH);            // Turn high digital pin RelaySlow        
  }
  
   // If you touch the button "Normal"
   
  if (p.x > 50 and p.x < 160 and p.y > 100 and p.y < 150){
    digitalWrite(RelayFast, LOW);             // Turn low digital pin RelayFast
    digitalWrite(RelaySlow, LOW);             // Turn low digital pin RelaySlow
    
    Tft.fillScreen(50, 190, 30, 80,WHITE);    // Set the colors of the button "Slow" in White
    Tft.drawString("SLOW",95,47,2,BLUE);      // Set the colors of the Text "Slow" in Blue 
    
    Tft.fillScreen(50, 190, 100, 150,GREEN);  // Set the colors of the button "Normal" in Green
    Tft.drawString("NORMAL",85,117,2,BLUE);   // Set the colors of the Text "Normal" in Blue 
    
    Tft.fillScreen(50, 190, 170, 220,WHITE);  // Set the colors of the button "Fast" in White
    Tft.drawString("FAST",95,187,2,BLUE);     // Set the colors of the Text "Fast" in Blue 
    
    Tft.fillScreen(50, 190, 240, 290,WHITE);  // Set the colors of the button "OFF" in White
    Tft.drawString("OFF",100,257,2,BLUE);     // Set the colors of the Text "OFF" in Blue 

    delay (200);                              // Delay for protection against multiple 230V voltages
    digitalWrite(RelayNormal, HIGH);          // Turn high digital pin RelayNormal
  }
  
   // If you touch the button "Fast"
   
 if (p.x > 50 and p.x < 160 and p.y > 170 and p.y < 220){
    digitalWrite(RelayNormal, LOW);           // Turn low digital pin RelayNormal
    digitalWrite(RelaySlow, LOW);             // Turn low digital pin RelaySlow
   
    Tft.fillScreen(50, 190, 30, 80,WHITE);    // Set the colors of the button "Slow" in White
    Tft.drawString("SLOW",95,47,2,BLUE);      // Set the colors of the Text "Slow" in Blue
    
    Tft.fillScreen(50, 190, 100, 150,WHITE);  // Set the colors of the button "Normal" in White
    Tft.drawString("NORMAL",85,117,2,BLUE);   // Set the colors of the Text "Normal" in Blue 
    
    Tft.fillScreen(50, 190, 170, 220,GREEN);  // Set the colors of the button "Fast" in Green
    Tft.drawString("FAST",95,187,2,BLUE);     // Set the colors of the Text "Fast" in Blue 
    
    Tft.fillScreen(50, 190, 240, 290,WHITE);  // Set the colors of the button "OFF" in White
    Tft.drawString("OFF",100,257,2,BLUE);     // Set the colors of the Text "OFF" in Blue 
  
    delay (200);                              // Delay for protection against multiple 230V voltages
    digitalWrite(RelayFast, HIGH);            // Turn high digital pin RelayFast  
  }
  
    // If you touch the button "OFF"
    
 if (p.x > 50 and p.x < 160 and p.y > 240 and p.y < 290){
    digitalWrite(RelaySlow, LOW);             // Turn low digital pin RelaySlow      
    digitalWrite(RelayNormal, LOW);           // Turn low digital pin RelayNormal
    digitalWrite(RelayFast, LOW);             // Turn low digital pin RelayFast
    
    Tft.fillScreen(50, 190, 30, 80,WHITE);    // Set the colors of the button "Slow" in White
    Tft.drawString("SLOW",95,47,2,BLUE);      // Set the colors of the Text "Slow" in Blue
    
    Tft.fillScreen(50, 190, 100, 150,WHITE);  // Set the colors of the button "Normal" in White  
    Tft.drawString("NORMAL",85,117,2,BLUE);   // Set the colors of the Text "Normal" in Blue 
    
    Tft.fillScreen(50, 190, 170, 220,WHITE);  // Set the colors of the button "Fast" in White
    Tft.drawString("FAST",95,187,2,BLUE);     // Set the colors of the Text "Fast" in Blue
    
    Tft.fillScreen(50, 190, 240, 290,GREEN);  // Set the colors of the button "OFF" in Green
    Tft.drawString("OFF",100,257,2,BLUE);     // Set the colors of the Text "OFF" in Blue 
  }
}

End result

Control fan.png