0

members

Easy Basics: Project 075y ESP32 Development board - PWM with Arduino IDE

of Acoptex.com in ESP8266 ESP-32

Basics: Project 075y

Project name: ESP32 Development board - PWM with Arduino IDE

Tags: EESP32 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, PWM with Arduino IDE

Attachments: testsketch, 2ledstestsketch

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. Resistor 2 pcs (220 Ohm each)

5.LED 2 pc (can be of different color)

 

6. Breadboard 1 pc

General

We will learn how to generate PWM signals with the ESP32 development board using Arduino IDE, how to get the same PWM signal on different GPIOs same time.

The ESP32 development board has a LED PWM controller with 16 independent channels that can be configured to generate PWM signals with different properties.

If you want to dim a LED with PWM using the Arduino IDE:

  1. Choose a PWM channel. There are 16 channels from 0 to 15.
  2. Set the PWM signal frequency. For a LED, a frequency of 5000 Hz is good to use.
  3. Set the signal’s duty cycle resolution from 1 to 16 bits.  We use 8-bit resolution to control the LED brightness using a value from 0 to 255.
  4. Specify on which GPIO or GPIOs the signal will be available. For that we use the function: ledcAttachPin(GPIO, channel) The two arguments are - GPIO that will output the signal and the channel that will generate the signal.
  5. Use the function: ledcWrite(channel, dutycycle) to control the LED brightness using PWM. The two arguments are - the channel that is generating the PWM signal and the duty cycle.

 

Understanding the ESP32 Development board with WiFi and Bluetooth

You can read more about it here.

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 DEVKITV1with 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

You can use any pin you want if it can be as an output. All pins that can be as outputs may be used as PWM pins.

1. Wiring for testsketch

2. Wiring for 2ledstestsketch

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. Installing ESP32 add-on in the Arduino IDE (Windows 10 OS)

  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.

2. Uploading sketch

  1. Do wiring.
  2. Plug the ESP32 development board to your PC and wait for the drivers to install (or install manually any that might be required).
  3. Open Boards manager. Go to Tools -> Board -> Boards Manager… (in our case it’s the DOIT ESP32 DEVKIT V1)
  4. 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)
  5. Open testsketch ,compile and upload it 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).
  6. Press the ESP32 on-board EN button to reboot it.
  7. When your ESP32 development board restarts you will see that your LED brightness will increase and decrease in a loop.
  8. Open 2ledstestsketch now, compile and upload it 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).
  9. Press the ESP32 on-board EN button to reboot it.
  10. When your ESP32 development board restarts you will see that all LEDs brightness will increase and decrease simultaneously in synchronized mode.  It means that all GPIOs output the same PWM signal.

Code

1. Testsketch

We define the pin the LED is attached to and set the PWM signal properties: frequency of 5000 Hz, choose channel 0 to generate the signal and resolution of 8 bits. Of course you can have other properties to generate different PWM signals.

In the setup() we need to configure LED PWM with the properties we have defined earlier by using the ledcSetup() function that accepts as arguments - the ledChannel, the frequency and the resolution: ledcSetup(ledChannel, freq, resolution);

Next we need to choose the GPIO we will get the signal from. For that use the ledcAttachPin() function, ledPin is GPIO 16, ledChannel is 0: ledcAttachPin(ledPin, ledChannel);

In the loop we will vary the duty cycle between 0 and 255 to increase the LED brightness and between 255 and 0 to decrease the LED brightness.

To set the brightness of the LED we use the ledcWrite() function: ledcWrite(ledChannel, dutyCycle);

As we are using 8-bit resolution the duty cycle controlled using a value from 0 to 255.  We use the channel that is generating the signal in the ledcWrite() function.

2. 2ledstestsketch

You can get the same signal from the same channel in different GPIOs. To achieve that  you need to attach those GPIOs to the same channel in the setup().

This is the same code as the previous but with some small modifications. We have defined one more variable for new LED, that refer to GPIO 23.

In the setup() we have added the following line to assign another GPIO to channel 0. This means that we will get the same signal that is being generated on channel 0 on both GPIOs (GPIO16 and GPIO23): ledcAttachPin(ledPin2, ledChannel);

Summary

We have learnt how to generate PWM signals with the ESP32 development board using Arduino IDE, how to get the same PWM signal on different GPIOs same time.

Libraries

  • No libraries required

Sketch

  • See attachments on the begining of this project


Other projects of Acoptex.com
Easy CBASIC Lesson 9 Compiling and debugging in Visual Studio of Acoptex.com in Csharp 10-04-2019
Easy Basics: Project 081d ESP8266 ESP-12E module and Nextion HMI display of Acoptex.com in ESP8266 ESP-12 24-03-2019

« Go back to category
Is this project fake? Report it!   
Recommend to a friend
Published at 07-11-2018
Viewed: 337 times