Note
Hello, welcome to the SunFounder Raspberry Pi & Arduino & ESP32 Enthusiasts Community on Facebook! Dive deeper into Raspberry Pi, Arduino, and ESP32 with fellow enthusiasts.
Why Join?
Expert Support: Solve post-sale issues and technical challenges with help from our community and team.
Learn & Share: Exchange tips and tutorials to enhance your skills.
Exclusive Previews: Get early access to new product announcements and sneak peeks.
Special Discounts: Enjoy exclusive discounts on our newest products.
Festive Promotions and Giveaways: Take part in giveaways and holiday promotions.
👉 Ready to explore and create with us? Click [here] and join today!
Lesson 34: TT Motor
In this lesson, you’ll learn how to control a motor with the ESP32 Development Board and an L9110 motor control board. We’ll cover defining and initializing motor pins, setting them as outputs, and adjusting the motor’s speed using the analogWrite function. This project is ideal for those seeking to grasp motor control and pulse-width modulation (PWM) on the ESP32 platform, providing a hands-on demonstration of output operations in a microcontroller environment.
Required Components
In this project, we need the following components.
It’s definitely convenient to buy a whole kit, here’s the link:
Name |
ITEMS IN THIS KIT |
LINK |
|---|---|---|
Universal Maker Sensor Kit |
94 |
You can also buy them separately from the links below.
Component Introduction |
Purchase Link |
|---|---|
ESP32 & Development Board (ESP32 Board) |
|
- |
|
- |
|
Wiring
Code
Code Analysis
The first part of the code defines the motor control pins. These are connected to the L9110 motor control board.
// Define the motor pins const int motorB_1A = 26; const int motorB_2A = 25;
The
setup()function initializes the motor control pins as output using thepinMode()function. Then it usesanalogWrite()to set the speed of the motor. The value passed toanalogWrite()can range from 0 (off) to 255 (full speed). Adelay()function is then used to pause the code for 5000 milliseconds (or 5 seconds), after which the motor speed is set to 0 (off).void setup() { pinMode(motorB_1A, OUTPUT); // set motor pin 1 as output pinMode(motorB_2A, OUTPUT); // set motor pin 2 as output analogWrite(motorB_1A, 255); // set motor speed (0-255) analogWrite(motorB_2A, 0); delay(5000); analogWrite(motorB_1A, 0); analogWrite(motorB_2A, 0); }