Controlling 12V DC motor with ESP32 DEV module on adafruitio

Hello all,

I am Ajay Singh, a beginner at Arduino. I am trying to control a 12 V PWM-controlled motor. I am using the ESP-32S LEDC method to modify the PWM. I am using the adafruitIO platform to control PWM using Wi-Fi.

I have attached the power and ground wires to the 12 V power supply. The blue is connected to one of the PWM ports (pin 16) of the board. I am able to change the PWM that I checked with the multimeter. But the fan keeps running at full speed without any effect of PWM on it.
I have not connected the yellow wire (rpm wire) anywhere. Any help would be highly appreciated. Below is the code and image of the hardware. Thanks in advance!

#include "config.h"

/************************ Example Starts Here *******************************/

// this should correspond to a pin with PWM capability
#define fanPin 16
// setting PWM properties
const int freq = 25000;
const int ledChannel = 0;
const int resolution = 10;   //0 = 0%, 1024=100%

// set up the 'analog' feed
AdafruitIO_Feed *dcFan = io.feed("dcFan");
int count=0;
unsigned long start_time;
int rpm;
void IRAM_ATTR isr() {
}
void setup() {

  //Adafruit IO controls are set for 10 bit direct control of both fans 0 = 0%, 1024 = 100%
  ledcAttachPin(fanPin, ledChannel);
  ledcSetup(ledChannel, freq, resolution);
  
  
  // start the serial connection
  Serial.begin(115200);

  attachInterrupt(22, isr, RISING);

  // wait for serial monitor to open
  while(! Serial);

  // connect to io.adafruit.com
  Serial.print("Connecting to Adafruit IO");
  io.connect();

  // set up a message handler for the 'dcFan' feed.
  // the handleMessage function (defined below)
  // will be called whenever a message is
  // received from adafruit io.
  dcFan->onMessage(handleMessage);

  // wait for a connection
  while(io.status() < AIO_CONNECTED) {
    Serial.print(".");
    delay(500);
  }

  // we are connected
  Serial.println();
  Serial.println(io.statusText());
  dcFan->get();
}

void loop() {

  // io.run(); is required for all sketches.
  // it should always be present at the top of your loop
  // function. it keeps the client connected to
  // io.adafruit.com, and processes any incoming data.
  io.run();

}

// this function is called whenever an 'dcFan' message
// is received from Adafruit IO. it was attached to
// the analog feed in the setup() function above.
void handleMessage(AdafruitIO_Data *data) {

  // convert the data to integer
  int reading = data->toInt();

  Serial.print("received <- ");
  Serial.println(reading);

  /*int frequency =  ledcReadFreq(ledChannel);
  Serial.print("Fan frequency: ");
  //Serial.print(reading_float);
  Serial.println(frequency);
*/
  int duty = ledcRead(ledChannel);
  Serial.print("fan duty cycle: ");
  //Serial.print(reading_float);
  Serial.println(duty);
  // write the current 'reading' to the led
  ledcWrite(ledChannel, reading); // ESP32 analogWrite()
}
type or paste code here

What driver are you using for the motor (voltage & current) ?
it’s not svisubleniin the photo..

Thank you for your response. I am sorry I am not using any driver. Do I need one, if I am already able to modify the PWM on ESP32 PIN, which is directly connected to the fan's blue wire?

Does the PWM line also need to be 12 volts?

Don’t connect the ESP to anything until you send us the data sheet for the motor.

No, the range for PWM is 0-5 VDC. You can find the datasheet at this link: https://www.digikey.com/en/products/detail/delta-electronics/BFB1012EF-0018H/10244640

Have you been able to vary the speed without adafruitio?
That has to be got right before going on with the IoT aspect.

Have you confirmed with an oscilloscope that you are sending a 25kHz signal to the fan?

No, I am directly using it with adafruitio. Can you elaborate it bit more on the reason why I should not go for it at first?

I have not confirmed it with oscillocope, but I have checked the voltage level with multimeter and it is showing voltage between 1 to 3 V when I am modifying the duty cycle.

the data sheet.

Page 7 is important for what you’re trying to achieve.

5V PWM seems ok, but the rest is important too.
Remember the ESP is a 3V3 controller..

You would then have a better idea, when there's a problem, whether it's the fan controlling thing or the adafruitio thing.

From the datasheet it appears the yellow wire is PWM and blue is tach out. It will run max speed if the yellow wire is left disconnected.

Maybe I'm reading it wrong.

1 Like

In such case, I would quickly check the Motor functionality using an Arduino UNO which can supply 25 kHz (0-5V) PWM signal at DPin-9. Be sure that the GND-pin of 12V supply is connected with GND-pin of UNO.
Test Sketch for Arduino UNO (compiled and not tested):

#define OC1A 9

void setup()
{
  Serial.begin(9600);
  pinMode(OC1A, OUTPUT); //Ch-A
  //-------------------------------------------------
  TCCR1A = 0x00;   //reset
  TCCR1B = 0x00;   //TC1 reset and OFF
  //fOC1A/B = clckSys/(N*(1+ICR1); Mode-14 FPWM; OCR1A controls duty cycle
  // 25000 Hz = 16000000/(1*(1+ICR1) N=1,(8,64,256,1024) ==> ICR1 = 639
  TCCR1A |= (1 << WGM11); //Mode-14 Fast PWM
  TCCR1B |= (1 << WGM13) | (1 << WGM12);    //Mode-14 Fast PWM
  TCCR1A |= (1 << COM1A1) | (0 << COM1A0);  //Non-invert: HIGH-LOW

  ICR1 = 639;   // TOP for 25 kHz frequnecy
  OCR1A = 320;  //= 50% duty cycle
  TCNT1 = 0;
  
  TCCR1B |= (1 << CS10);//TC1 statrt with N = 1;
}

void loop(){}

I have checked the data sheet and you have read it correctly!

Oh, I see, thank you very much. It's working now. Actually, I just goggled about the wire configuration, and yellow wire is tech wire generally. But for this motor YELLOW is PWM. Thanks a lot again!

Happy to help

2 Likes

Hello again, I am having some trouble adjusting PWM. So, what's happening is that sometimes the PWM signal gets changed, but sometimes it won't. I tested it once, and it worked fine the voltage on the terminal and the speed of the motor changed. But the next time I try it again, I get a 0.8 v PWM signal which doesn't change.
I read about ESP32S and they said that some pins are active low and I need to initialize these pins to active low in the setup function of the code. But, I haven't tried it yet. Any idea/suggestion what can be wrong here?

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.