Getting started with ESP
Today we are going to look at the world of ESP. An ESP is essentially a micro-controller like Arduino or Pico, the main difference is you now also have Bluetooth and Wifi built in. Why? Well why would you really want to add an extra module for this when using it in IOT applications?
Types of esp:
ESP32-WROVER
ESP32-WROVER 4MB Module with Bluetooth and WiFi
ESP32 is a powerful, generic WiFi-BT-BLE MCU module that targets a wide variety of applications, ranging from low-power sensor networks to the most demanding tasks, such as voice encoding, music streaming and MP3 decoding. At the core of this module is the ESP32-D0WDQ6 chip*, same as ESP-WROOM-32 module
Compared to ESPWROOM-32, ESP32-WROVER has an additional SPI Pseudo static RAM (PSRAM) of 32 Mbits. As such, ESP32- WROVER features both 4 MB external SPI flash and 4 MB external PSRAM.
There are two CPU cores that can be individually controlled, and the clock frequency is adjustable from 80 MHz to 240 MHz.
ESP8266
ESP8266 ESP-12F Remote Serial Port WIFI Transceiver Wireless Module
The new four-layer board design, the new revision antenna RF performance optimization, compared to the communication from the ESP-12E increased by 30% to 50%! Semi-hole chip technology, the whole IO leads, with metal shielding shell, has passed FCC & CE & RoHS certification, built-board PCB antenna, 4M bytes Flash.
It features a L106 32-bit RISC microprocessor core based on the Tensilica Xtensa Diamond Standard 106Micro running at 80 MHz. With external QSPI flash of up to 16 MiB is supported (512 KiB to 4 MiB typically included).
ESP32 module
NodeMCU-32S ESP32 WiFi+Bluetooth Development Board
This is the NodeMCU development board based on ESP32, features WiFi+Bluetooth connectivity, onboard CP2102 USB to UART converter and keys. What’s more, all the I/O pins of ESP-WROOM-32 module are accessible via the extension headers. Like the ESP32-WROVER the module also the ESP32-D0WDQ6 chip at its core. The main benefits of using the module is you now have the USB converter included. Allowing for much easier setup and programming.
ESP32 CAM
ESP32-CAM WiFi Module ESP32 serial Development Board 5V Bluetooth with OV2640 Camera Module
Much like the other ESP32 versions listed, most of the core details remain the same. The only thing that changes: you now also have a onboard camera. I will look more closely at this module in a later blog.
Just some of the techy details:
- Low-power dual-core 32-bit CPU for application processors
- Main frequency up to 240MHz, computing power up to 600 DMIPS
- Built-in 520 KB SRAM, external 4M PSRAM
- Supports interfaces such as UART/SPI/I2C/PWM/ADC/DAC
- Support OV2640 and OV7670 cameras, built-in flash
- Support image WiFi upload
- Support TF card
Ai-Thinker modules
ESP-01 module
This is the first series of modules made with the ESP8266 by the third-party manufacturer Ai-Thinker and remains the most widely available. They are collectively referred to as “ESP-xx modules”. To form a workable development system they require additional components, especially a serial TTL-to-USB adapter (sometimes called a USB-to-UART bridge) and an external 3.3 volt power supply. Novice ESP8266 developers are encouraged to consider larger ESP8266 Wi-Fi development boards like the NodeMCU which includes the USB-to-UART bridge and a Micro-USB connector coupled with a 3.3 volt power regulator already built into the board. When project development is complete, those components are not needed and these cheaper ESP-xx modules are a lower power, smaller footprint option for production runs.
Now that you have selected your preferred ESP module, it is time to start programming it. But where to start.
The ESP32 can be programmed using many different development environments. Code can be written in C++ (like the Arduino) or in MicroPython. These are my 2 preferred methods, and will be discussed here. I will not be re-inventing the wheel by rewriting any of the numerous blogs out there on how to use your device, instead, I will be putting them together so you can see how you can use different guides on a single project.
Arduino IDE
When we think about using a micro-controller for a project we usually consider an Arduino. It’s inexpensive, easy to use and has a generous number of digital I/O ports, and a few analog inputs as well. But the Arduino, for all of its wonderful benefits, is lacking in a number of areas. The first one is speed, the popular Arduino AVR series of boards run at 16 MHz. That’s certainly fast enough to build thousands of applications, but it’s a bottleneck for others.
The Arduino certainly has enough digital outputs and inputs to satisfy most requirements, and its analog inputs are also useful. But adding features like WiFi and Bluetooth requires external components.
Add Boards Manager Entry
Here is what you need to do to install the ESP32 boards into the Arduino IDE:
- Open the Arduino IDE. Make sure that you are at version 1.8 or higher, if not then update your IDE with the latest version.
- Click on the File menu on the top menu bar.
- Click on the Preferences menu item. This will open a Preferences dialog box.
- You should be on the Settings tab in the Preferences dialog box by default.
- Look for the textbox labeled “Additional Boards Manager URLs”.
- If there is already text in this box add a coma at the end of it, then follow the next step.
- Paste the following link into the text box – https://raw.githubusercontent.com/espressif/arduino-esp32/gh-pages/package_esp32_index.json
- Click the OK button to save the setting.
Next, you will need to use the new entry to actually add the ESPO32 boards to your Arduino IDE. You do that by following this procedure:
- In the Arduino IDE click on the Tools menu on the top menu bar.
- Scroll down to the Board: entry (i.e. Board: Arduino/Genuino Uno).
- A submenu will open when you highlight the Board: entry.
- At the top of the submenu is Boards Manager. Click on it to open the Boards Manager dialog box.
- In the search box in the Boards Manager enter “esp32”.
- You should see an entry for “esp32 by Espressif Systems”. Highlight this entry and click on the Install button.
- This will install the ESP32 boards into your Arduino IDE
If you go back into the Boards: sub-menu you should now see a number of ESP32 boards. You’ll need to select the board that matches (or is equivalent to) the ESP32 board you have purchased.
Experiment Hookup
Before we can upload a test program we will need to hookup our ESP32 board.
The following diagram shows the connections we’ll need to perform all of the experiments and demos included in this article. Note that the ESP32 pins are specified by their GPIO names and not pin numbers, as different ESP32 boards will have different pin numbers.
You can use pretty well any LED, for a dropping resistor any values between 150 and 470 ohms will work well. Any momentary-contact push button will suffice.
The small gold square in the diagram is a Touchpad. You can use any metallic object that you can connect a wire to, a small piece of PCB would work great. I just used a small piece of bare wire on my breadboard.
Now that we are all hooked up it’s time to try our first program.
Hello World – Blink for ESP32
When using a new development environment or programming for a new device it is traditional to create a “Hello World” program. Far be it from me to dispense with tradition!
The “Hello World” for micro-controllers is the Blink sketch which, as I’m sure you already know, simply flashes an LED on and off. While “Hello World” programs and sketches are usually of little practical use they serve a number of functions:
They familiarize you with the operation of your development library. In this particular case they let you practice uploading programs to the ESP32 which, as you will soon see, is done a bit differently than an Arduino. They get you familiar with the programming syntax. In this case we are using the same C++ that the Arduino uses, so you are likely already familiar with the programming syntax.
They let you verify that your hardware is working correctly.
Here is the classic Arduino Blink sketch, rewritten for the ESP32 module.
// LED on GPIO2
int ledPin = 2;
void setup()
{
// Set LED as output
pinMode(ledPin, OUTPUT);
// Serial monitor setup
Serial.begin(115200);
}
void loop()
{
Serial.print("Hello");
digitalWrite(ledPin, HIGH);
delay(500);
Serial.println(" world!");
digitalWrite(ledPin, LOW);
delay(500);
}
Aside from using GPIO pin 2 (instead of Arduino digital output pin 13) and adding the serial monitor it’s no different from the Arduino Blink sketch.
Uploading the program
After you load the sketch into your Arduino IDE it will need to be compiled and then transferred to the ESP32 board. With an Arduino you just have to hit the Upload button and both of these steps will proceed automatically. With the ESP32, however, there is an additional step.
You begin by doing exactly the same thing you would with an Arduino – hit the Upload button (the one with the right-pointing arrow). This will start the compiler. You’ll probably notice that it takes longer to compile ESP32 programs than it does for Arduino sketches.
Watch the status screen at the bottom of the Arduino IDE. When the compiling process is finished it will print “Connecting”, followed by a string of periods. When you see this you’ll need to press the BOOT button (sometimes labeled IO0) and hold it down until you see the upload progressing, which you’ll know is happening when you see the upload progress displayed in percentage. After the program is uploaded you will need to press the ENABLE or RESET button to start it (some boards don’t require this step).
And if all goes well you’ll be rewarded with a flashing LED!
If you are looking for some more starter project using your ESP32 and the Arduino IDE take a look here
Micropython with ESP.
Next we will look at what is arguably the lesser of the used ways to program your ESP, but as a python developer, is always my go-to. However I should mention that there is still limited support in the ever-growing micropython community. The are sometimes features that I would like to use, but are not available in micropython yet. This is when it becomes nessisary for me to start looking at what is available in C++.
Let’s have a look at the easiest way I have found to use Micropython on ESP devices. Start by having a look at the following video by TechToTinker
https://www.youtube.com/watch?v=elBtWZ_fOZU
We will use Thonny IDE. Thonny is a very strong IDE tool to use when using Micropython on any compatible device. With popularity growing leaps and bounds in the last few years. I will not be boring you and going through all the steps as shown in the video above. I am going to assume that you have already installed Thonny, and flashed the firmware; and run the LED example as described.
I will only be looking at what I always find most interesting: Connecting to WiFi.
I will use the code and examples provided by Saravanan AL in this video
The first thing we will create is our ConnectWifi.py script. Open Thonny and connect your ESP32 if it is not still done after running the previous examples.
Type in the following code
def connect():
import network
ssid = "my_wifi"
password = "my_password"
station = network.WLAN(network.STA_IF)
if station.isconnected() == True:
print ("Already Connected")
return
station.active(True)
station.connect(ssid, password)
while station.isconnected() == False:
pass
print ("Connection Successful")
print (station.ifconfig())
You will need to change “my_wifi” and “my_password” to your own network configuration. Click on save and save the script on the ESP device as ConnectWifi.py. You can ofcourse change the name, but then you also need to change the name in you main.py script as well.
We started by creating a connect function and importing the network protocol. Then we simply pass in our network details. Then a station object is created to initiate the connection. Finally we print out our connection information, ie. the IP and gateway that you are connected to.
Speaking of which, let’s create our main.py.
print('RUN : main.py')
import ConnectWifi
ConnectWifi.connect()
Again save the script as main.py on the ESP device.
So Why main.py? By default if you want your script to run at start up on any micropython device, then you need to save the script as main.py, or you need to create a main.py that calls the script you want to run; this is the safest way to do it. If anything goes wrong, simply remove the pesty script and main.py will be calling nothing, giving you a chance to fix it.
So what did we do here: All we did was import our ConnectWifi script and call the connect function. As you can see starting script ConnectWifi is what we actually want to use, but we call it from main.
If all goes well then you will see what IP your network has assigned to your ESP. Success. Now was that not more exciting than seeing an LED blink?