How to control Servo Motors with an ESP32 - directly, using I2C, and over WiFi.
Article with code: https://dbot.ws/esp32srvo
We are working with the ESP32 again, today we’ll see a few different methods of controlling a servo motor using this amazing 32-bit microcontroller.
The ESP32 is well-suited to controlling analog servo motors as it has 16 PWM outputs. It’s built-in WiFi and Bluetooth open the doors to all sorts of remote control possibilities.
Although the ESP32 can’t use the Arduino Servo Library there are many replacement libraries available. I’ll be working with the ESP32Servo Library for most of these experiments.
We’ll see how to interface a servo to the ESP32 and then we’ll look at a few code examples that emulate the examples included with the Arduino.
We’ll also hook up a PCA9685 PWM controller to the ESP32 using its I2C connections. This is a great way to drive a lot of servo motors as each PCA9685 has 16 PWM outputs and you can cascade them to control up to 992 motors! I’ll only be using two motors for my demo.
One great reason to use an ESP32 over an Arduino is the built-in wireless communications. In the final example, we will control a servo motor with a web-based interface over WiFi.
Here is the Table of Contents for today's video:
00:00 - Introduction
02:51 - Servo Motors & ESP32
10:10 - Hookup & ESP32Servo Library
15:08 - Sweep Sketch
19:23 - Improved Sweep Sketch
21:36 - Knob Sketch
27:18 - ESP32 & PCA9685 -
37:46 - WiFi Web-based Servo Control
There is also an article on the DroneBot Workshop website with the code used here in case you want to try some of these experiments on your own.
"Never trust a computer you can’t throw out a window." — Steve Wozniak
I started a post here and have been advised to move it to here.
I am looking to enlarge the font on the web page when using the sketch WiFiAccessPoint.
I have been advised to read https://www.w3schools.com/html/html_css.asp.
I have read through quite a lot of it and can find no reference to using CSS in the WiFiClient library.
The format is different. I assume I need something like
client.print("<p style=Click to turn ON the LED.\">absolute size - xx-large</p><br>");
but can't see anyway of formatting it so the IDE will compile it correctly
The solution is to add the lines
client.print( "<!DOCTYPE html>" );
client.print( "<html lang=\"en\">" );
client.print( "<head><meta charset=\"UTF-8\"><meta name=\"HandheldFriendly\" content=\"True\">" );
client.print( "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.8, user-scalable=yes\"></head>" );
client.print( "<body>" );
client.print( "<H1>Arduino</H1>" );
before the lines
client.print( "Click <a href=\"/H\">here</a> to turn ON the LED.<br>" );
client.print( "Click <a href=\"/L\">here</a> to turn OFF the LED.<br>" );
and add
client.print( "</body></html>" );
after them.
17:58 - you talk about how the breadboard power supply is supplying power to the servo on the 5V rail and that the breadboard power supply will eventually power the esp32 on the 3.3V rail as well. But you don't show that in this video. Is that just a matter of putting jumpers from the gnd and 3.3V pins to the 3.3V rail?
@mace He was probabvly referring to one of these. It plugs into the rails on each side and is jumper selectable for 3.3V or 5V on each side.
https://www.amazon.ca/gp/product/B07L84QZTY/ref=ppx_yo_dt_b_search_asin_title?ie=UTF8&psc=1
First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
My personal scorecard is now 1 PC hardware fix (circa 1982), 1 open source fix (at age 82), and 2 zero day bugs in a major OS.
@zander yes. I have used them in the past myself, but which pins on the ESP32 are involved. I don’t have the board he does and it’s difficult to see in the video which pins he’s jumpered.
@mace Sorry, I don't understand what you are saying. What board are you talking about?
First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
My personal scorecard is now 1 PC hardware fix (circa 1982), 1 open source fix (at age 82), and 2 zero day bugs in a major OS.
@mace Yes
First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
My personal scorecard is now 1 PC hardware fix (circa 1982), 1 open source fix (at age 82), and 2 zero day bugs in a major OS.
17:58 - you talk about how the breadboard power supply is supplying power to the servo on the 5V rail and that the breadboard power supply will eventually power the esp32 on the 3.3V rail as well. But you don't show that in this video. Is that just a matter of putting jumpers from the gnd and 3.3V pins to the 3.3V rail?
@mace The jumpers on the breadboard power supply on the left side are set to 3.3v. Once he disconnects the USB cable he will power the board from the 3.3V rail.
First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
My personal scorecard is now 1 PC hardware fix (circa 1982), 1 open source fix (at age 82), and 2 zero day bugs in a major OS.
@zander I was referring to the ESP32 Development Board that he used. It appears there is a pin at the corner of his ESP32 Development Board which isn't connected to anything but which would attach to the 3.3V rail when it comes time to pull the USB power. I have a SparkFun ESP32 Thing Plus (U.FL) and the pinouts on it are different from the ESP32 board he's using. I'm trying to confirm the name of the pin on an ESP32 board that needs to jumper into the 3.3V rail. Thanks in advance.
@mace It is labelled 3.3V
First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
My personal scorecard is now 1 PC hardware fix (circa 1982), 1 open source fix (at age 82), and 2 zero day bugs in a major OS.
Hi @mace,
I haven't seen the board you refer to, so this is largely based on what I can see on the web .. I recommend you check it yourself before powering any boards up. Note my description is a summary of what I observe ... it is not a specification that every ESP32 board will neccesarily follow.
The ESP32 (and ESP8266) is 3.3V processor, whilst the most common power source for such boards is USB, which supplies 5V. Hence, all of the boards I have seen have a 5V or Vin input which connects to a voltage regulator on the board to supply the ESP32 with 3.3V. The boards also have a 3.3V pin, which can either be used to feed 3.3V directly to the ESP32, making the internal regulator redundant, or as power output of 3.3V to power peripherals.
(Note the latter current output will be current limited --- maybe 100 mA maximum.)
--------------------
Thus, powering an ESP32 board, without using the USB socket, is generally a choice of:
- 3.3 V into 3.3V pin
- 5V into the 5V or Vin pin.
- The Vin pin may tolerate above 5V, but this is board design dependent. Keeping to 5V maybe the safest approach.
This Sparkfun board also has LiPo battery input, but I am ignoring that here.
-----------------------------------
The Sparkfun board schematic is published at:
https://cdn.sparkfun.com/assets/6/d/c/6/c/ESP32_Thing_Plus_Schematic.pdf
and the description of how to use it at:
https://learn.sparkfun.com/tutorials/esp32-thing-plus-hookup-guide/all
The latter includes:
In addition to USB and battery connectors, the VBAT, and VUSB pins are all broken out to the sides of the board. These pins can be used as an alternative supply input to the Thing Plus. The maximum, allowable voltage input to VUSB is 5.8V, and VBAT should not be connected to anything other than a LiPo battery. Alternatively, if you have a regulated voltage source between 3.0V and 3.6V, the "3V3" line can be used to directly supply the ESP32 and its peripherals.
Note this guide includes warnings regarding a Sparkfun power supply ... reading of the whole page is recommended to see if it applies to you!
---------------------
A subtle board difference to be aware of - the boards I have previously looked (e.g. ESP32 CAM) have used the AMS1117 regulator, which can handle up to 12V power in, although above (about) 7V risks the regulator overheating, depending upon the load.
The particular Spakfun board mentioned in this thread uses the AP2112K, with a maximum input voltage rating of only 6V. Furthermore, the Sparkfun text says maximum 5.8V, and there is a warning about Sparkfun 5.1V power supply on the same page. Interpret their advice with care!
.........
Best wishes, Dave
@davee Wow, that's the University level explanation version of my street version of YES. Did you teach for a living?
First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
My personal scorecard is now 1 PC hardware fix (circa 1982), 1 open source fix (at age 82), and 2 zero day bugs in a major OS.
Waking up this ancient discussion! I'm trying to connect a PCA9685 I2C board to an ESP32-2432S028 AKA a "Cheap Yellow Display", that has very few available pins.The JST connector suggested for I2C uses pins 22 and 27, rather than 22 and 21 as Bill wrote (see below). He says that a change of pins can be declared, but not how to do it. I haven't been able to work this out, so can anyone tell me how to declare the change of pins?
The I2C connections to the ESP32 are as follows:
SDA – IO21
SCL – IO22Actually you can use any two pins on the ESP32 for your I2C connections and define them in software. GPIO pins 21 and 22 are the default if new pins are not declared.