ESP32 Thing Power Control Shield Hookup Guide

Pages
Contributors: Alex the Giant
Favorited Favorite 4

Software

Now that the shield is assembled, the only thing left to do is to throw some code on the ESP32!

LED Strips

LED strips have gained a lot of popularity in the last few years, from task lighting, to computer cases, and even home theater lighting. In this section, we'll explore how you can use the ESP32 Thing with a Power Control Shield to control your LED strips.

For this example, we'll be using the non-addressable LED strips (either 1m strip or 5m strip will do the trick). Because each strip has a red, green, and blue pin, you could easily connect two strips to the shield for different patterns at the same time. In this example, VCC is set to 12V. Start by connecting the 12V black wire of the LED strip to the screw terminal labeled VCC. Then connect the red wire to Output 1, green wire to Output 2, and the blue wire to Output 3. Upload the example code below to the ESP32. Once uploaded, you will be able to see the LEDs changing colors.

language:c
// Connect the BLACK wire to VCC (12V)
#define CTRL_1 25 // Output 1 (RED)
#define CTRL_2 18 // Output 2 (GREEN)
#define CTRL_3 23 // Output 3 (BLUE)

void setup() {
  // Initialize pins
  pinMode(CTRL_1,OUTPUT);
  pinMode(CTRL_2,OUTPUT);
  pinMode(CTRL_3,OUTPUT);

  // Assign the control pins to PWM channels 0-2
  ledcAttachPin(CTRL_1, 0);
  ledcAttachPin(CTRL_2, 1);
  ledcAttachPin(CTRL_3, 2);

  // Initialize PWM Channels
  // ledcSetup(uint8_t channel, uint32_t frequency, uint8_t bit_resolution)
  ledcSetup(0,10000,8);
  ledcSetup(1,10000,8);
  ledcSetup(2,10000,8);
}

void loop() {
  int rgb[] = {255, 255, 255};

  // ledcWrite(channel, duty)
  ledcWrite(0,rgb[0]);
  ledcWrite(1,rgb[1]);
  ledcWrite(2,rgb[2]);

  for(int i=0;i<255;i+=5)
  {
    rgb[0] = 0;
    rgb[1] = 255 - i;
    rgb[2] = 255;
    ledcWrite(0,rgb[0]);
    ledcWrite(1,rgb[1]);
    ledcWrite(2,rgb[2]);
    delay(25);
  }

  for(int i=0;i<255;i+=5)
  {
    rgb[0] = i;
    rgb[1] = 0;
    rgb[2] = 255;
    ledcWrite(0,rgb[0]);
    ledcWrite(1,rgb[1]);
    ledcWrite(2,rgb[2]);
    delay(25);
  }

  for(int i=0;i<255;i+=5)
  {
    rgb[0] = 255;
    rgb[1] = 0;
    rgb[2] = 255 - i;
    ledcWrite(0,rgb[0]);
    ledcWrite(1,rgb[1]);
    ledcWrite(2,rgb[2]);
    delay(25);
  }

  for(int i=0;i<255;i+=5)
  {
    rgb[0] = 255;
    rgb[1] = i;
    rgb[2] = 0;
    ledcWrite(0,rgb[0]);
    ledcWrite(1,rgb[1]);
    ledcWrite(2,rgb[2]);
    delay(25);
  }

  for(int i=0;i<255;i+=5)
  {
    rgb[0] = 255 - i;
    rgb[1] = 255;
    rgb[2] = 0;
    ledcWrite(0,rgb[0]);
    ledcWrite(1,rgb[1]);
    ledcWrite(2,rgb[2]);
    delay(25);
  }

  for(int i=0;i<255;i+=5)
  {
    rgb[0] = 0;
    rgb[1] = 255;
    rgb[2] = i;
    ledcWrite(0,rgb[0]);
    ledcWrite(1,rgb[1]);
    ledcWrite(2,rgb[2]);
    delay(25);
  }
}

Motor Control Current Sensing

This code allows you to control a motor using the current sensor connected to output 2. For this example, we we will be using a 90 RPM micro gearmotor with a wheel. We will be connecting VCC to a 12V power supply. Prepare the micro gearmotor by soldering wires to the terminals. Then attach the wheel to the shaft. Connect the wire that is attached to the "+" pin to OUTPUT 2. Connect the other wire to OUTPUT 1. Upload the code below to the ESP32.

language:c
#define CTRL_1 25 // Output 1 (Motor -)
#define CTRL_2 18 // Output 2 (Motor +)

void setup() {
  // Initialize UART
  Serial.begin(115200);

  // Initialize pins
  pinMode(CTRL_1,OUTPUT);
  pinMode(CTRL_2,OUTPUT);
}

void loop() {
  int adcVal = 0;
  boolean runMode = 0; // 0-Stop, 1-Run

  while(runMode == 0) // Forward
  {
    // Turn motor on
    digitalWrite(CTRL_1,LOW);
    digitalWrite(CTRL_2,HIGH);
    delay(250); // delay to prevent current spike from giving a false positive on the ADC

    // Sample I-Sense 2
    adcVal = analogRead(34);
    Serial.println(adcVal);

    if(adcVal > 100)
    {
      runMode = 1;  // Reverse
    }
  }

  // Stop motor
  digitalWrite(CTRL_1,LOW);
  digitalWrite(CTRL_2,LOW);
  delay(2000);

  while(runMode == 1) // Reverse
  {
    // Turn motor on
    digitalWrite(CTRL_1,HIGH);
    digitalWrite(CTRL_2,LOW);
    delay(250); // delay to prevent current spike from giving a false positive on the ADC

    // Sample I-Sense 2
    adcVal = analogRead(34);
    Serial.println(adcVal);

    if(adcVal > 100)
    {
      runMode = 0;  // Forward
    }
  }

  // Stop motor
  digitalWrite(CTRL_1,LOW);
  digitalWrite(CTRL_2,LOW);
  delay(2000);
}

When we hold the motor with one hand, and the spinning wheel with the other, the motor will draw more current as the motor slows down. Too much current, and the motor will stop, wait for two seconds, spin in the opposite direction, and repeat. By opening a serial monitor set to 115200, we can also observe the changes in current as the motor increases the torque. Without a feedback resistor for I2 Gain, the motor draws a small enough current, that the ADC value should be close to 0. When the torque increases, the current increases as well which results in a ADC value greater than 100. In robotics and home automation, measuring the current being delivered to a motor can provide a lot of information, like being stuck.