6

members

Easy Basics: Project 072i ESP32 Development board with SG90 Micro Servo Motor Web Server

of Acoptex.com in ESP8266 ESP-32

Basics: Project 072i

Project name: ESP32 Development board with SG90 Micro Servo Motor Web Server

Tags: ESP32 Dev Module, ESP32 development board, ESP32 Development board with WiFi and Bluetooth, ESP32-DevKitC V4 development board, ESP-WROOM-32 module with ESP32‑D0WDQ6 chip, Espressif Systems, ESP32-based development board, ESP32 modules, ESP32-WROOM-32, ESP32-WROOM-32U, ESP32-WROOM-32D, ESP32-SOLO-1, USB-UART bridge, IOT, ESP-WROOM-32 Dev Module, ESP32 DEVKITV1, Installing the ESP32 Board in Arduino IDE, Uploading sketch, Web Server, Servo motor, SG90 Micro Servo Motor, SG90 9G Micro Servo Motor, Micro Servo Motor S0009, Servo Motor S0003, Futaba S3003 Standard Servo

Attachments:  sketch1library1sketch2

In this project, you need these parts :

1. ESP32 development board with WiFi and Bluetooth and USB A / micro USB B cable 1 pc

2.Arduino IDE ( you can download it from here  )

3.Jumper cables F-M

4. SG90 Micro Servo Motor 1 pc 

General

We will learn how to create a web server with the ESP32 development board to control a servo motor. We will make slider control of servo motor shaft’s position. 

You can position the servo motor’s shaft in various angles from 0 to 180 degrees. Servo motors controlled using a pulse width modulation (PWM) signal. The PWM signal sent to the motor will determine the servo motor's shaft position. You can simply use PWM capabilities of the ESP32 development board by sending a 50Hz signal with the appropriate pulse width or you can use a library To control the servo motor.

The web server we need to make:

 

  1. Will contain a slider from 0 to 180 that you can adjust to control the servo motor’s shaft position;
  2. The current slider value and the servo motor's shaft position automatically updated on the web page -  no need to refresh the web page. We are going to use AJAX to send HTTP requests to the ESP32 development board on the background for this;
  3. Refreshing the web page doesn’t change the slider value and the shaft position.

 

There are a lot of different development boards made. You can find more information about them here

Understanding servo motors

You can read about them here.

Understanding the ESP32 Development board with WiFi and Bluetooth

We will discuss here an Espressif Systems products. Our development board is using ESP-WROOM-32 module from Espressif Systems.

Espressif offers a wide range of fully-certified Wi-Fi & BT modules powered by their own advanced SoCs.

1. Dual-core Modules with Wi-Fi & Dual-mode Bluetooth

Features

  • Two independently-controlled CPU cores with adjustable clock frequency, ranging from 80 MHz to 240 MHz
  • +19.5 dBm output at the antenna ensures a good physical range
  • Classic Bluetooth for legacy connections, also supporting L2CAP, SDP, GAP, SMP, AVDTP, AVCTP, A2DP (SNK) and AVRCP (CT)
  • Support for Bluetooth Low Energy (BLE) profiles including L2CAP, GAP, GATT, SMP, and GATT-based profiles like BluFi, SPP-like, etc
  • Bluetooth Low Energy (BLE) connects to smart phones, broadcasting low-energy beacons for easy detection
  • Sleep current is less than 5 μA, making it suitable for battery-powered and wearable-electronics applications
  • Integrates 4 MB flash
  • Peripherals include capacitive touch sensors, Hall sensor, low-noise sense amplifiers, SD card interface, Ethernet, high-speed SPI, UART, I2S and I2C
  • Fully certified with integrated antenna and software stacks

2. Single-core Modules with Wi-Fi & Dual-mode Bluetooth

Features

  • High-performance 160 MHz single-core CPU
  • +19.5 dBm output at the antenna ensures a good physical range
  • Classic Bluetooth for legacy connections, also supporting L2CAP, SDP, GAP, SMP, AVDTP, AVCTP, A2DP (SNK) and AVRCP (CT)
  • Support for Bluetooth Low Energy (BLE) profiles including L2CAP, GAP, GATT, SMP, and GATT-based profiles like BluFi, SPP-like, etc
  • Bluetooth Low Energy (BLE) connects to smart phones, broadcasting low-energy beacons for easy detection
  • Sleep current is less than 5 μA, making it suitable for battery-powered and wearable-electronics applications
  • Peripherals include capacitive touch sensors, Hall sensor, low-noise sense amplifiers, SD card interface, Ethernet, high-speed SPI, UART, I2S and I2C
  • Fully certified with integrated antenna and software stacks

3. Single-core Modules with 802.11b/g/n 2.4 GHz Wi-Fi

Features

  • High-performance 160 MHz single-core CPU
  • +19.5 dBm output at the antenna ensures a good physical range
  • Sleep current is less than 20 μA, making it suitable for battery-powered and wearable-electronics applications
  • Peripherals include UART, GPIO, I2C, I2S, SDIO, PWM, ADC and SPI
  • Fully certified with integrated antenna and software stacks

There are different development Boards made by Espressif Systems and other manufacturers. We will publish some information about Espressif Systems boards but you can also find out more information about other development boards here.

1. 2.4 GHz Wi-Fi & BT/BLE Development Boards

Features

  • PC connectivity: USB
  • Power supply options: USB (by default), or 5V/GND header pins, or 3V3/GND header pins
  • SDK: ESP-IDF source code and example applications

2. 2.4 GHz Wi-Fi Development Boards

Features

  • PC connectivity: USB
  • SDK: ESP8266 SDK source code and example applications

3. 2.4 GHz Wi-Fi + BT/BLE + Sensor Development Boards

Features

  • PC connectivity: USB
  • SDK: ESP-IOT-SOLUTION source code and example applications

You can find more information (datasheets, schematics, pins descriptions, functional desgn descriptions) about each board by pressing Getting started link close to each board here.

ESP32 chip

ESP32 is a series of low cost, low power system on a chip microcontrollers with integrated Wi-Fi and dual-mode Bluetooth. The ESP32 series employs a Tensilica Xtensa LX6 microprocessor in both dual-core and single-core variations and includes in-built antenna switches, RF balun, power amplifier, low-noise receive amplifier, filters, and power management modules. ESP32 is created and developed by Espressif Systems, a Shanghai-based Chinese company, and is manufactured by TSMC using their 40 nm process. It is a successor to the ESP8266 microcontroller.

ESP32 can perform as a complete standalone system or as a slave device to a host MCU, reducing communication stack overhead on the main application processor. ESP32 can interface with other systems to provide Wi-Fi and Bluetooth functionality through its SPI / SDIO or I2C / UART interfaces.

ESP32 is highly-integrated with in-built antenna switches, RF balun, power amplifier, low-noise receive amplifier, filters, and power management modules. ESP32 adds priceless functionality and versatility to your applications with minimal Printed Circuit Board (PCB) requirements.

ESP32 is capable of functioning reliably in industrial environments, with an operating temperature ranging from –40°C to +125°C. Powered by advanced calibration circuitries, ESP32 can dynamically remove external circuit imperfections and adapt to changes in external conditions.

Engineered for mobile devices, wearable electronics and IoT applications, ESP32 achieves ultra-low power consumption with a combination of several types of proprietary software. ESP32 also includes state-of-the-art features, such as fine-grained clock gating, various power modes and dynamic power scaling.

Functional Block Diagram:

Features of the ESP32 include the following:

Processors:

  • CPU: Xtensa dual-core (or single-core) 32-bit LX6 microprocessor, operating at 160 or 240 MHz and performing at up to 600 DMIPS
  • Ultra low power (ULP) co-processor
  • Memory: 520 KiB SRAM

Wireless connectivity:

  • Wi-Fi: 802.11 b/g/n
  • Bluetooth: v4.2 BR/EDR and BLE

Peripheral interfaces:

  • 12-bit SAR ADC up to 18 channels
  • 2 × 8-bit DACs
  • 10 × touch sensors (capacitive sensing GPIOs)
  • Temperature sensor
  • 4 × SPI
  • 2 × I²S interfaces
  • 2 × I²C interfaces
  • 3 × UART
  • SD/SDIO/CE-ATA/MMC/eMMC host controller
  • SDIO/SPI slave controller
  • Ethernet MAC interface with dedicated DMA and IEEE 1588 Precision Time Protocol support
  • CAN bus 2.0
  • Infrared remote controller (TX/RX, up to 8 channels)
  • Motor PWM
  • LED PWM (up to 16 channels)
  • Hall effect sensor
  • Ultra low power analog pre-amplifier

Security:

  • IEEE 802.11 standard security features all supported, including WFA, WPA/WPA2 and WAPI
  • Secure boot
  • Flash encryption
  • 1024-bit OTP, up to 768-bit for customers
  • Cryptographic hardware acceleration: AES, SHA-2, RSA, elliptic curve cryptography (ECC), random number generator (RNG)

Power management:

  • Internal low-dropout regulator
  • Individual power domain for RTC
  • 5uA deep sleep current
  • Wake up from GPIO interrupt, timer, ADC measurements, capacitive touch sensor interrupt

You can find ESP32 chip datasheet here, hardware design here, technical reference manual here.

Signals and connections of the SG90 mini servo motor

Servo motors have three wires: power (VCC), ground (GND) and signal.

When using a small servo like the SG90 Micro Servo Motor you can power it directly from the ESP32 development board. If you’re using more than one servo or more powerful servo motor you need to use an external power supply.

Wire | Color 

VCC | Red 

GND | Black or brown

Signal | Yellow, orange or white

Signals and connections of the ESP32 Development board with WiFi and Bluetooth

You can find more information (datasheets, schematics, pins descriptions, functional desgn descriptions) about each board (made by Espresiff Systems) by pressing Getting started link close to each board here.

Let's check our development board - ESP32 DEVKITV1 with ESP-WROOM-32 module from Espressif Systems:

Pinout diagram for the ESP Wroom 32 breakout:

ESP32-WROOM-32 - ESP32-WROOM-32 module soldered to the development board. Optionally ESP32-WROOM-32D, ESP32-WROOM-32U or ESP32-SOLO-1 module may be soldered instead of the ESP32-WROOM-32.

USB-UART Bridge - A single chip USB-UART bridge provides up to 3 Mbps transfers rates.

BOOT button - Download button: holding down the Boot button and pressing the EN button initiates the firmware download mode. Then user can download firmware through the serial port.

EN button - Reset button: pressing this button resets the system.

Micro USB Port - USB interface. It functions as the power supply for the board and the communication interface between PC and the ESP module.

TX0, TX2 - transmit pin. GPIO pin

RX0, RX2  - receive pin.  GPIO pin

3V3 (or 3V or 3.3V) - power supply pin (3-3.6V). 

GND - ground pin.

EN - Chip enable. Keep it on high (3.3V) for normal operation.

Vin - External power supply 5VDC.

Wiring

Servo motor | ESP32 development board

VCC                Vin

GND               GND

Signal             GPIO 14 (or any PWM pin)

Note: You can use any ESP32 development board GPIO, because any GPIO is able to produce a PWM signal. However we don’t recommend to use GPIOs 9, 10, and 11 that are connected to the integrated SPI flash.

Step by Step instruction

The ESP32 is currently being integrated with the Arduino IDE like it was done for the ESP8266. There’s an add-on for the Arduino IDE that allows you to program the ESP32 using the Arduino IDE and its programming language.

  1. Download and install the latest Arduino IDE Windows Installer from arduino.cc
  2. Download and install Git and Git GUI from git-scm.com
  3. Search for Git GUI, right-click the icon and select “Run as administrator
  4. Select the Clone Existing Repository option.
  5. Select source and destination. Source Location: https://github.com/espressif/arduino-esp32.git
  6. Target Directory:C:/Users/[YOUR_USER_NAME]/Documents/Arduino/hardware/espressif/esp32
  7. Do not create the espressif/esp32 folders, because they will be created automatically.
  8. Click Clone to start cloning the repository.Wait a few seconds while the repository is being cloned.
  9. Open the folder: C:/Users/[YOUR_USER_NAME]/Documents/Arduino/hardware/espressif/esp32/tools
  10. Right-click the get.exe file and select “Run as administrator“.
  11. You will see that necessary files will be downloaded and upzipped. It will take some time.
  12. When get.exe finishes, you should see the following files in the directory.
  13. Do wiring.
  14. Plug the ESP32 development board to your PC and wait for the drivers to install (or install manually any that might be required).
  15. Open Arduino IDE. 
  16. Open Boards manager. Go to Tools -> Board -> Boards Manager… (in our case it’s the DOIT ESP32 DEVKIT V1)
  17. Select COM port that the board is attached to (if you don’t see the COM Port in your Arduino IDE, you need to install the ESP32 CP210x USB to UART Bridge VCP Drivers)
  18. Compile and upload the sketch1 to your ESP32 development board. If everything went as expected, you should see a “Done uploading” message. (You need to hold the ESP32 on-board Boot button while uploading).
  19. Press the ESP32 on-board EN button to reboot it. 
  20. Open the Serial Monitor at a baud rate of 115200. The motor’s shaft will rotate to one side and then to the other side. You will see degrees in Serial Monitor.
  21. Let's build the web server now. We need to create the HTML page with slider for this project.  You can see the HTML page here.
  22. Modify sketch2 with your SSID and password data.
  23. Compile and upload the sketch2 to your ESP32 development board. If everything went as expected, you should see a “Done uploading” message. (You need to hold the ESP32 on-board Boot button while uploading).
  24. Press the ESP32 on-board EN button to reboot it.
  25. Open the Serial Monitor at a baud rate of 115200. You should see the ESP32 IP address displayed on the Serial Monitor (we have 192.168.0.116).
  26. You can access your web server, if you type your ESP32 development board IP address in your browser. In our case it is http://http://192.168.0.116. Move the slider to control the servo motor.

Code

sketch1.

This sketch rotates the servo 180 degrees to one side, and 180 degrees to the other. First we need to include the Servo library. Then we create a servo object and define servoPin. In the setup() we initialize a serial communication for debugging purposes, and attach servoPin to the servo object.

In the loop() we change the motor’s shaft position from 0 to 180 degrees, and then from 180 to 0 degrees. To set the shaft to a particular position, you just need to use the servo1.write() method in the servo object. You pass as an argument, an integer number with the position in degrees: servo1.write(pos);

HTML Page
The HTML page for this project involves creating a slider. To create a slider in HTML you use the input tag. The input tag specifies a field where the user can enter data. There are a wide variety of input types. To define a slider, use the "type" attribute with the "range" value. In a slider, you also need to define the minimum and the maximum range using the "min" and "max" attributes.
You also need to define other attributes like:
the class to style the slider
the id to update the current position displayed on the web page
And finally, the onchange attribute to call the servo function to send an HTTP request to the ESP32 when the slider moves.
You also need to define other attributes: the class to style the slider, the id to update the current position displayed on the web page and the onchange attribute to call the servo function to send an HTTP request to the ESP32 development board when the slider moves. You can open the file here. 
You also need to define other attributes like:the class to style the sliderthe id to update the current position displayed on the web pageAnd finally, the onchange attribute to call the servo function to send an HTTP request to the ESP32 when the slider moves.
You need to add some JavaScript code to your HTML file using the tags. This snippet of the code updates the web page with the current slider position:
var slider = document.getElementById("servoSlider");
    var servoP = document.getElementById("servoPos");
    servoP.innerHTML = slider.value;
    slider.oninput = function() {
      slider.value = this.value;
      servoP.innerHTML = this.value;
    }
And the next lines make an HTTP GET request on the ESP IP address in this specific URL path /?value=[SLIDER_POSITION]&.
$.ajaxSetup({timeout:1000});
function servo(pos) {
  $.get("/?value=" + pos + "&");
}
For example, when the slider is at 0, you make an HTTP GET request on the following URL: http://192.168.1.135/?value=0and when the slider is at 180 degrees, you’ll have something as follows: http://192.168.1.135/?value=180&
When the ESP32 development board receives the GET request, it can retrieve the value parameter in the URL and move the servo motor to the right position.
sketch2.
First we include the Servo library, and create a servo object called servo2. We also create a variable to hold the GPIO number the servo is connected to - GPIO 14.
Don’t forget that you need to modify the ssid and password. Then we create a couple of variables that will be used to extract the slider position from the HTTP request. In the setup() we need to attach the servo to the GPIO it is connected to with servo2.attach().
The first part of the loop() creates the web server and sends the HTML text to display the web page. We use the same method we’ve used in this web server project.
The following part of the code retrieves the slider value from the HTTP request:
//GET /?value=180& HTTP/1.1
if(header.indexOf("GET /?value=")>=0) {
  pos1 = header.indexOf('=');
  pos2 = header.indexOf('&');
  valueString = header.substring(pos1+1, pos2);
When you move the slider, you make an HTTP request on the following URL, that contains the slider position between the = and & signs:  http://your_IP_address/?value=[SLIDER_POSITION]&
The slider position value is saved in the valueString variable. Then we set the servo to that specific position using servo2.write() with the valueString variable as an argument. The valueString variable is a string, so we need to use the .toInt() method to convert it into an integer number – the data type accepted by the write() method.
servo2.write(valueString.toInt());
Summary

We have learnt how to create a web server with the ESP32 development board to control a servo motor. 

Libraries

  • All libraries attached on the begining of this project description.
  • ESP32-Arduino-Servo library included.Download, unzip  and add to libraries in our PC, for example C:\Users\toshiba\Documents\Arduino\libraries. This link you can find in Preferences of Adruino IDE program which installed in your PC. You can read more about it here. You will need to move out Servo library as it will create an error during compilation: C:\Program Files (x86)\Arduino\libraries\Servo

Sketch

  • See attachments on the begining of this project


Other projects of Acoptex.com
Medium Basics: Project 083w Sipeed Maixduino board - Using PlatformIO IDE of Acoptex.com in Sipeed Maixduino 08-08-2019

« Go back to category
Is this project fake? Report it!   
Recommend to a friend
Published at 25-08-2018
Viewed: 1465 times