Advertisement

ESP32 — Getting Started the Easy Way!

 

Open-hardware platforms enable rapid prototyping and faster time-to-market of new IoT applications. The objective of this post is to give a brief introduction to another open-hardware IoT playground — the ESP32 board. The ESP32 is, in fact, a small development board based on the ESP32 IoT-enabled microcontroller, the successor of the far-famed ESP8266 microcontroller from Espressif. ESP32 is a very powerful Wi-Fi-/Bluetooth-enabled SoC with a massive GPIO count, and the development board exposes this power in a very accessible IoT module design, suitable for both beginners and encouraged users.

 

Hardware facts

The “ESP32 development board” that you usually get from eBay is a small and convenient generic breakout board with ESP-WROOM-32 module installed, exposed I/O pin headers, and a few additional supportive components. The 30-pin board (often referred to as ESP32-DOIT development board v1) includes a CP2102-based USB-to-Serial programming interface that also provides the power supply for the board. The board also has a 3.3-V onboard voltage regulator, two push buttons (enable, boot), and visual indicators (power, user). Listed below are the key components used in the ESP32-DOIT (v1) board:

 

  • Power Part: NCP1117-3.3 (3.3-V LDO positive voltage regulator)
  • USB Part: CP2102 (USB-UART Bridge)
  • Core Part: ESP-Wroom-32 Module (Wi-Fi/Bluetooth Classic/BLE module based on ESP32-D0WDQ6 chip with two Xtensa 32-bit LX6 microprocessors)

Programming environment

Because the ESP32 is the heart of the module, it should be possible to program your ESP32-DOIT board with any environment supported by the ESP32. As usual, the most practical environment is the Arduino (another offer that has a lot of support is MicroPython). One of the things that makes the board so easy to use is that it already has a micro-USB port. Besides, the board includes built-in buttons and LEDs, which is very useful for getting started without the need to connect to external hardware. Thus, to program it, you just need a USB cable, and you don’t need to solder anything to get started and create your first program. Yes, you can start playing with the board without needing to solder anything, as long as you don’t need to use the GPIOs or other peripherals. There are plenty of things to try, such as the Wi-Fi/Bluetooth functionalities or the different programming environments, just to name a few. If you are running Arduino on Windows, you can read the below instructions (if you’ve never connected an FTDI device to your computer before, you may need to install drivers for the USB-to-Serial converter):

 

 

Thereafter, plug in the ESP32-DOIT board wait for the drivers to install. Then run the Arduino IDE and select your board and the COM port (see next figure). Finally, get ready to compile and upload your first code.

 

Connecting to the ESP32 through the Arduino IDE

 

Pre-flight trials

To make sure the toolchain and board are properly set up, you can upload the simplest of “hello world” sketches — Blink! The LED attached to GPIO2 is okay for this maiden test. Just copy/paste the below code into a new Arduino sketch and upload (also watch my first test video).

 

/*
* ESP32 Hello World
* ESP-WROOM-32
* Hardware: ESP32 Development Kit v1 (DOIT Board)
* Code Written by T.K.Hareendran
* Read the tutorial first, and then proceed!
*/


const int ledPin = 2; // User LED is connected to Pin 24


void setup() {


pinMode (ledPin, OUTPUT); //GPIO2 as Output

}


void loop() {


digitalWrite (ledPin, HIGH);


delay(500);



digitalWrite (ledPin, LOW);


delay(500);

}

 

In case of programming trouble, just hold down the “boot” switch when the upload starts. After uploading the sketch, you may need to press the “enable” button to get your ESP32 board to begin running the sketch. That’s all!

 

Bonus: an easy-to-do breathing LED!

Inspired by Apple’s good old “breathing” pattern used for the sleep indicator, here is the code to generate a “breathing LED” effect. In this code, ledcWrite function is used to implement a software-fade LED. You can tinker with it if you like or just use it as it is. Take a look at the code shown below, and also remember to watch my test video.

 

/*

* ESP32 Hello World

* ESP-WROOM-32

* Hardware: ESP32 Development Kit v1 (DOIT Board)

* Breathing Onboard LED

* Adapted Code by T.K.Hareendran

* Read the tutorial first, and then proceed!

*/




#define LEDC_CHANNEL_0     0

#define LEDC_TIMER_13_BIT  13

#define LEDC_BASE_FREQ     5000

#define LED_PIN            2




int brightness = 0;

int fadeAmount = 5;

void ledcAnalogWrite(uint8_t channel, uint32_t value, uint32_t valueMax = 255) {

uint32_t duty = (8191 / valueMax) * min(value, valueMax);

ledcWrite(channel, duty);

}




void setup() {

ledcSetup(LEDC_CHANNEL_0, LEDC_BASE_FREQ, LEDC_TIMER_13_BIT);

ledcAttachPin(LED_PIN, LEDC_CHANNEL_0);

}




void loop() {

ledcAnalogWrite(LEDC_CHANNEL_0, brightness);

brightness = brightness + fadeAmount;

if (brightness <= 0 || brightness >= 255) {

fadeAmount = -fadeAmount;

}

delay(30);

}

 

And finally…

Readers with a particularly sharp eye may notice that no Wi-Fi example sketch is given here to play with the introduced IoT module. To quench their thirst, I jotted down a simple Wi-Fi scanner sketch for searching for the nearest Wi-Fi networks through the serial monitor in Arduino IDE or through a terminal program like PuTTY.

 

/*

ESP32 WiFi Scanner

Hardware: ESP-WROOM-32 based ESP32 DOIT DEV.BOARD v1

Example Sketch (Adapted): By T.K.Hareendran

*/




#include “WiFi.h”




void setup()

{

Serial.begin(115200);




WiFi.mode(WIFI_STA);

WiFi.disconnect();

delay(100);




Serial.println(“Setup Done!”);

}




void loop()

{

Serial.println(“WiFi Scanner Running...”);




int n = WiFi.scanNetworks();

Serial.println(“WiFi Scan Done”);

if (n == 0) {

Serial.println(“No WiFi Network!”);

} else {

Serial.print(n);

Serial.println(“ WiFi Networks Found!”);

for (int i = 0; i < n; ++i) {




Serial.print(i + 1);

Serial.print(“: “);

Serial.print(WiFi.SSID(i));

Serial.print(“ (“);

Serial.print(WiFi.RSSI(i));

Serial.print(“)”);

Serial.println((WiFi.encryptionType(i) == WIFI_AUTH_OPEN) ? “ “ : “*”);

delay(10);

}

}

Serial.println(““);




delay(5000);

}

 

 

Using a PuTTY terminal to connect to the ESP32

 

The ESP32 doesn’t replace the ESP8266, but it is a valuable member of the IoT-enabled microcontroller family. The added Bluetooth support is significant, as are the extra GPIOs, ADCs, and other hardware peripherals. Admittedly, there are still plenty of GPIOs, hardware peripherals, and many other cool features that we need to play with, but so far, I’m enjoying the challenge and potential of the ESP32!

 

Update

Here is a video showing the device in action!

3 Comments

Join the conversation!

Error! Please fill all fields.
Looking for the latest from TI?