0

I followed the tutorial I found here https://techtutorialsx.com/2019/03/29/esp32-arduino-controlling-a-dc-motor/ On the very interesting topic controlling a DC motor with the esp32 and Arduino I have some questions:

The first one: Where in the world did the guy find out the pin numbers for the Arduino IDE for this board? I have no idea how to find them!?

Taking in consideration that the digital IO pins of the ESP32 can only source a maximum current of 40 mA [1], we cannot directly use them to control a DC motor.

Instead, we are going to use a ULN2803A integrated circuit to actually provide the current to the motor, while the ESP32 will simply control if the motor is running or stopped.

The connection diagram to control a DC motor from the ESP32 using an ULN2803A can be seen below at figure 1. for controlling a DC motor with the ESP32, using a ULN2803A integrated circuit

The controlling of the DC motor with the ESP32 and a ULN2803A IC. In short, the ULN2803A will act as a switch, which will turn on / off the connection of the motor to GND, depending on the state of the input pin labeled as In 1. So, when the GPIO of the ESP32 is at a digital high value (VCC), the ULN2803A will connect the motor to GND, and thus the motor will be on. When the GPIO is at a digital low value (GND), then the motor will be disconnected from GND, and thus it will be off. Furthermore, the ULN2803A can be controlled by 3.3 V inputs and provide higher voltages, allowing us to control a 5 V DC motor with the ESP32.

The code

The code for this tutorial will be very simple, since we basically only need to control the status (high or low) of a digital pin of the ESP32.

So, we will start by declaring as a global variable the number of the pin that will be connected to the DC motor.

In my case I’ll be using GPIO 13. Note that for the Beetle ESP32 board, which is the one I’m using, this maps to the pin labeled as D7.

int motorPin = 13;

After that, we need to initialize the pin as output, in the Arduino setup function. To do that, we simply need to call the pinMode function.

This function receives as first argument the number of the pin and as second argument a constant indicating the operating mode. So, we will pass the as first argument the value of the pin stored in our previously declared global variable and as second argument the constant OUTPUT.

void setup() {

  pinMode(motorPin, OUTPUT);

}

We will write the rest of our code in the Arduino main loop. We will first turn on the motor, wait some seconds, turn it off, wait some more seconds and then repeat.

So, to turn the motor on, we simply need to set the value of the ESP32 digital pin to high, which will make the motor run at full speed. We can set the digital level of a pin with a call to the digitalWrite function.

This function receives as first argument the number of the pin and as second argument a constant indicating the digital level to which the pin should be set.

1

An Arduino boards package with configuration for your board has constants for pins which correspond to labels on the PCB. The constants are defined in pins_arduino.h file, which is applied only if you choose the corresponding board in Tools menu. In code you should use this constants as values for your constants, like for example const uint8_t LED1_PIN = D2;.

If the boards package doesn't have the board and you use generic settings, the you can study the schematics of the board to see which PCB pin goes to which MCU pin. Usually the logical pin numbers are labeled in schematics additionally to physical pin numbers of the chip.

Note: The linked pins_arduino.h is for FireBeetle ESP32. The Beetle ESP32 is not defined in ESP32 boards package yet.

The schematics is on the Wiki page.

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.