HOW TO USE MOSFET AS A SWITCH?

HOW TO USE MOSFET AS A SWITCH?-Uncategorized

In our last tutorial we have explained what MOSFET transistor is and how it works. Now we will use the acquired knowledge and apply it to a practical project. In a brief example, we will explain the connection and operation using MOSFET in order to control LED strip illumination.

INTRODUCTION

Standard LED strips require power supply of 12V to work properly. This is a high voltage which can not be provided by our Dasduino and that is why we can not connect the strip directly to Dasduino. What if we want to control the LED strip using Dasduino? How will we find the solution? The answer lies in the MOSFET transistor. Using it, we can connect Dasduino to the LED strip and control its illumination.

PROJECT DESCRIPTION

This small project is very simple to create. Let’s use our previously acquired knowledge and apply it in this case. If we connect Dasduino to the control electrode of the MOSFET, we can control the flow of MOSFET’s channel, on which one color of the RGB LED strip is connected. If we connect each color to a separate MOSFET, we can control the whole RGB LED strip. To make this project you can use any NPN or N-channel MOSFET transistor, only pay special attention to characteristics of the transistor to prevent overloading or damaging it. We have decided to use the IRF540N MOSFET(N) transistor, which can allow current flow of up to 20A. Also, pay attention to which RGB LED strip you are using and how much power supply is needed for proper work. The LED strip we have chosen is 50cm long and for it we need a 1A power supply. 3 resistors of 10kΩ are connected between the control electrode and ground of the power supply in order for signal to be LOW until Croduino changes its state.

Required components: 
Dasduino Core
• Breadboard
• RGB LED strip (12V, common anode)
• 3x IRF540N MOSFET(N) transistor
• 3x 10kΩ resistor
• Power supply (12V, DC)

HOW TO CONNECT IT?

All of the above mentioned components are connected as shown in the picture. The 12V power supply is connected to the LED strip (label +) and VIN pin of the Dasduino. Connection of Dasduino’s VIN pin and power supply is necessary only if Dasduino does not have its own power supply via USB cable. MOSFET control electrodes (Gate) are connected to Dasduino’s outputs (pin 9, 10 and 11). Also, we connect the control electrode to the power supply’s ground via 10Kohm resistor. Besides that, we connect the R, G and B LED strip connector to the drain. All we have left is the introductory (Source) connector of the MOSFET, which is connected directly to power supply’s ground. It is also necessary to connect Dasduino’s GND to the ground of the power supply.

ARDUINO CODE

The LED strip is illuminated the same way as LED diode, using digitalWrite() and analogWrite() functions. No libraries or new additional functions are required to execute the code. Program code below shows the specified colors for a certain amount of time, after which it switches to another color display. First it is red, then blue, then green after which the strip starts “mixing” these three colors and creates different colors.

#define REDPIN 10 //pin number for red color of the LED strip
#define GREENPIN 9 //pin number for green color of the LED strip
#define BLUEPIN 11 //pin number for blue color of the LED strip
#define FADESPEED 5     // by increasing this value, colors change more slowly
void osvijetliLED(int r, int g, int b) { //function which illuminates the LED strip with different intensities of red, blue and green
  analogWrite(REDPIN, r);
  analogWrite(GREENPIN, g);
  analogWrite(BLUEPIN, b);
  delay(FADESPEED);
};
void setup() {
  pinMode(REDPIN, OUTPUT); //set LED's pins as outputs
  pinMode(GREENPIN, OUTPUT);
  pinMode(BLUEPIN, OUTPUT);
  digitalWrite(REDPIN, LOW); //set the initial LED strip state to LOW
  digitalWrite(BLUEPIN, LOW);
  digitalWrite(GREENPIN, LOW);
}
void loop() {
  int r, g, b;
  digitalWrite(REDPIN, HIGH); //illuminate red color
  digitalWrite(BLUEPIN, LOW);
  digitalWrite(GREENPIN, LOW);
  delay(500);
  digitalWrite(REDPIN, LOW); //illuminate blue color
  digitalWrite(BLUEPIN, HIGH);
  digitalWrite(GREENPIN, LOW);
  delay(500);
  digitalWrite(REDPIN, LOW); //illuminate green color
  digitalWrite(BLUEPIN, LOW);
  digitalWrite(GREENPIN, HIGH);
  delay(500);
  digitalWrite(REDPIN, LOW); //turn off all the colors
  digitalWrite(BLUEPIN, LOW);
  digitalWrite(GREENPIN, LOW);
  delay(500);
  
//gradually increase green color to the maximum value
//then blue, and red
//the ultimate color will be white
  for (r = 0; r < 256; r++) {  
    for (b = 0; b < 256; b++) {
      for (g = 0; g = 0; r--) {
    for (b = 255; b >= 0; b--) {
      for (g = 255; g >= 0; g--) {
        osvijetliLED(r, g, b);
      }
    }
  }
  delay(500);
//go from white, and gradually reduce green color
//then blue, then red, until all the colors turn off
  for (r = 255; r >= 0; r--) {
    for (b = 255; b >= 0; b--) {
      for (g = 255; g >= 0; g--) {
        osvijetliLED(r, g, b);
      }
    }
  }
  delay(500);
}