Fading with the MOSFET Power Switch and Buck Regulator

Pages
Favorited Favorite 1

12V RGB LED Strip

In this example, we will control all three channels of the RGB LED strip. Since we've already hooked up a 12V RGB LED strip before, we will also a circuit with a potentiometer to cycle between each color and a photoresistor to turn on the LEDs whenever the light is below a certain light level. The following example code is based on the SparkFun Inventor's Kit v4.1 Night Light example.

Non-Addressable RGB LED Strip Hookup Guide

Add color to your projects with non-addressable LED strips! These are perfect if you want to control and power the entire strip with one color for your props, car, fish tank, room, wall, or perhaps under cabinet lighting in your home.

SparkFun Inventor's Kit Experiment Guide - v4.1

The SparkFun Inventor's Kit (SIK) Experiment Guide contains all of the information needed to build all five projects, encompassing 16 circuits, in the latest version of the kit, v4.1.2 and v4.1.

 

Parts Needed

Grab the following quantities of each part listed to build this circuit:

*Note: You will need a minimum of 19x M/M jumper wires. Six jumper wires were stripped wires that connect the barrel jacks together for power and reference ground.

 

Hardware Hookup

For this particular example, we will use three channels from a 12V RGB LED strip while also including a similar circuit from the SparkFun Inventor's Kit v4.1. The circuit diagram is shown below.

Three MOSFET Board Fritzing Hookup

Note: When testing the non-addressable LED strip, the pin labeled "G" was actually blue and the "B" was actually green. Depending on the manufacturer, the label may vary. Try testing the LED strip out with a power supply to determine if the letter represents the color.

Keep in mind that instead of the RedBoard with ATmega328P, we are using the IoT RedBoard with ESP32. Since the hardware is different, the following code was modified:

- analog and PWM pins were redefined in the example code
- threshold was modified due to the ADC's higher resolution
- logic is reversed due to the transistors
Danger: The IoT RedBoard with ESP32 has a system voltage of 3.3V. Thus, the logic levels is 3.3V instead of 5V on the RedBoard with ATmega328P. Thus, the analog reference voltage for the potentiometer and photoresistor is 3.3V. Make sure you are using 3.3V!

Your setup should look similar to the image below without the power supply.

Your three-board setup should look similar to this.

 

Upload Code

To upload code, insert the USB cable into the IoT RedBoard - ESP32.

Copy the following code and paste it in the Arduino IDE. If you have not already, select your Board (in this case, the SparkFun ESP32 IoT RedBoard), and associated COM port. Then hit the upload button.

/*
12V RGB LED Nightlight Example

Turns an 12V RGB strip LED on or off based on the light level read by a photoresistor.
Change colors by turning the potentiometer. This example is based off the SparkFun
Inventor's Kit v4.2 RGB Night-Light Example:

  https://learn.sparkfun.com/tutorials/sparkfun-inventors-kit-experiment-guide---v41

Note that instead of the RedBoard with ATmega328P, we are using the IoT RedBoard with ESP32.
Since the hardware is different, the following code was modified:

  - analog and PWM pins were redifined
  - threshold was modified due to the ADC's higher resolution
  - logic is reversed due to the transistors

WARNING: Since the IoT RedBoard with ESP32 has a system voltage of 3.3V, the logic levels
is 3.3V instead of 5V on the RedBoard with ATmega328P. Thus, the analog reference voltage
for the potentiometer and photoresistor is 3.3V. Make sure you are using 3.3V!

This sketch was written by SparkFun Electronics, with lots of help from the Arduino community.
This code is completely free for any use.

*/

int photoresistor = A4;          //variable for storing the photoresistor value
int potentiometer = A5;          //this variable will hold a value based on the position of the knob
int threshold = 3000;            //if the photoresistor reading is lower than this value the light will turn on
                             /*Note: The ESP32's ADC resolution is bigger. The max is 4095. In a bright room
                             with your finger covering the sensor, the threshold was about 3000. In a dimly
                             lit room, the threshold was about 1000. You will need to adjust this value when
                             installing it in a room. Just make sure to make it a little more than the thresholed
                             of the room. Try adding a button and some code  to save the threshold value! */

//LEDs are connected to these pins
int RedPin = 16;
int GreenPin = 17;
int BluePin = 25;

void setup() {
Serial.begin(115200);           //start a serial connection with the computer
Serial.println("12V RGB LED Strip Nightlight!");

//set the LED pins to output
pinMode(RedPin, OUTPUT);
pinMode(GreenPin, OUTPUT);
pinMode(BluePin, OUTPUT);

} //END SETUP

void loop() {

photoresistor = analogRead(A4);         //read the value of the photoresistor
potentiometer = analogRead(A5);         //read the value of the potentiometer

Serial.print("Photoresistor value:");
Serial.print(photoresistor);          //print the photoresistor value to the serial monitor
Serial.print("  Potentiometer value:");
Serial.println(potentiometer);          //print the potentiometer value to the serial monitor

if (photoresistor < threshold) {        //if it's dark (the photoresistor value is below the threshold) turn the LED on
//These nested if statements check for a variety of ranges and
//call different functions based on the current potentiometer value.
//Those functions are found at the bottom of the sketch.

/*Note: We divided 4095 by 7 colors and had a window of about 585. For users
        Adding more colors, try dividing 4095 by the total number and adjust
        eac condition statement*/

if (potentiometer > 0 && potentiometer <= 585)
  red();
if (potentiometer > 585 && potentiometer <= 1170)
  orange();
if (potentiometer > 1170 && potentiometer <= 1755)
  yellow();
if (potentiometer > 1755 && potentiometer <= 2340)
  green();
if (potentiometer > 2340 && potentiometer <= 2925)
  cyan();
if (potentiometer > 2925 && potentiometer <= 3510)
  blue();
if (potentiometer > 3510)
  magenta();
}
else {                                //if it isn't dark turn the LED off

turnOff();                            //call the turn off function

}

delay(100);                             //short delay so that the printout is easier to read

} //END LOOP

void red () {

//set the LED pins to values that make red
analogWrite(RedPin, 0);
analogWrite(GreenPin, 255);
analogWrite(BluePin, 255); 
}
void orange () {

//set the LED pins to values that make orange
analogWrite(RedPin, 0);
analogWrite(GreenPin, 128);
analogWrite(BluePin, 255);
}
void yellow () {

//set the LED pins to values that make yellow
analogWrite(RedPin, 0);
analogWrite(GreenPin, 0);
analogWrite(BluePin, 255);
}
void green () {

//set the LED pins to values that make green
analogWrite(RedPin, 255);
analogWrite(GreenPin, 0);
analogWrite(BluePin, 255);
}
void cyan () {

//set the LED pins to values that make cyan
analogWrite(RedPin, 255);
analogWrite(GreenPin, 0);
analogWrite(BluePin, 0);
}
void blue () {

//set the LED pins to values that make blue
analogWrite(RedPin, 255);
analogWrite(GreenPin, 255);
analogWrite(BluePin, 0);
}
void magenta () {

//set the LED pins to values that make magenta
analogWrite(RedPin, 0);
analogWrite(GreenPin, 255);
analogWrite(BluePin, 0);
}
void turnOff () {

//set all three LED pins to 0 or OFF
analogWrite(RedPin, 255);
analogWrite(GreenPin, 255);
analogWrite(BluePin, 255);
}  

What You Should See

Once the code has uploaded, disconnect the USB cable from the IoT RedBoard - ESP32. Then insert the barrel jack from a power supply to the MOSFET Power Switch and Buck Regulator's barrel jack connector. In this case, we used a 12V wall adapter power supply.

The MOSFET Power Switch & Buck Regulator with the wall adapter. Cover the photoresistor with your finger (or just turn off the lights in the room) and turn the potentiometer. You should notice the colors cycling through as the potentiometer is within certain ranges. You will probably want to disconnect the 3.3V jumper wire from the IoT RedBoard - ESP32, reconnect the USB cable, and open the Arduino Serial Monitor at 115200 baud for debugging purposes. That way you can view the serial data and adjust the threshold value based on the lighting in the room.

Three MOSFETs in Action

Now that we have ported the example from the RedBoard Qwiic with an ATmega328P to the RedBoard IoT Development Board - ESP32, try adjusting the condition statement with the potentiometer to add additional colors. Or even writing some code save the threshold value whenever a button is pressed down. You can also try to take advantage of the ESP32's wireless capabilities and adjust the color of the LED strip based on the weather.