Thursday, July 12, 2018

Espressif ESP32 Tutorial - Programming (Arduino IDE)

Introduction


As promised in the previous post, let's have a look at the different ways we can program our ESP32. I will start with the Arduino IDE, because I think that will be the most common option. In this post I will only be looking to get the "hello world" of IoT going, that is blinking a LED.



Once I've selected an IDE, we will do something useful with the ESP32 (stay tuned).

Arduino IDE




Installing support for the ESP32 IC is not yet available through the Boards Manager, so the
instructions on the GitHub page should be used. For the Mac OS, it is pretty straight forward.

1. Install the latest version of the Arduino IDE.
2. Open Terminal and execute the following command (copy->paste and hit enter):

mkdir -p ~/Documents/Arduino/hardware/espressif && \
cd ~/Documents/Arduino/hardware/espressif && \
git clone https://github.com/espressif/arduino-esp32.git esp32 && \
cd esp32 && \
git submodule update --init --recursive && \
cd tools && \
python get.py

3. Restart the Arduino IDE.

Support for the ESP32 for Arduino is under constant development, but once everything is installed,
the sketch writing and upload process is similar to other boards. If you are using the Jaycar board, select ESP32 Dev Module as the board type, and ensure that the correct serial port is selected.



Connect your ESP32 Dev Board to your PC via a USB cable and you should be ready to compile and upload your first program. First check that you can see the port which is connected to the ESP32.

On my MacBook Pro (early 2015) running High Sierra, the port on the Duinotech ESP32 Dev Board was not recognized. If this happens, you may need to install the drivers for the USB-serial converter for this board. It uses a CP2102 IC, and the drivers are found on the Silicon Labs CP2102 website:
https://www.silabs.com/products/development-tools/software/usb-to-uart-bridge-vcp-drivers.

After you download the correct driver version for your OS. You will need to install it. On the Mac this involves, unzipping the archive, mounting the DMG disk image and then running the Silicon Labs VCP Driver.pkg. You will need to give permission to run this driver in the Security and Privacy preference (this will pop up). You should now be able to see the port in the Arduino IDE (no need to reboot or restart the IDE).

Open up the Blink Sketch (File -> Examples -> 01. Basics -> Blink), compile and download to your ESP32.

You will probably get this error:

'LED_BUILTIN' was not declared in this scope

There are a couple of traps for new players here,

The standard Arduino boards define the LED_BUILTIN macro in the variant file but apparently the ESP32 Dev Board doesn't. For the Arduino, the built in LED is usually connected to pin 13, on the ESP32 it can be anything. The usual suspects are pins 2, 5 or 16. You can fix this error by adding the following line to the top of the sketch, above the setup() function. For example, if your LED is connected to pin 2:

int LED_BUILTIN = 2;

Unfortunately, the Duinotech ESP32 Dev Board doesn't seem to have a LED connected to a user controllable pin. It does have a blue LED which flashes when communicating via the serial port. It is difficult to be 100% sure that this LED is not controllable since I can't find a schematic for this board. Most development boards have a red power LED and a blue in built LED for user control. Anyway, I resorted to connecting an external LED with a current limiting resistor to GPIO 2.



Note that when the EP32 Dev Board is placed on a standard sized bread board, there will only be one spare row of pins, make sure that this is on the side you want to connect to.

Calculation of the current limiting resistor is done using this formula:

Resistor Value = (Vs – VF)/ IF = 160 ohms (I used 150 ohms since that is what I had)

Where:

Vs = Input Supply DC voltage = 3.3V
VF = LED Forward Voltage = 1.7V
IF = LED current = 10 mA



I measured the LED forward voltage drop (VF) using my LCR meter but you could just get it of the data sheet.

I didn't need to do this, but if you are having trouble uploading, try holding the ‘BOOT’ button while pressing and releasing the ‘RST’ button. This should put the board into boot-loader mode to allow uploads.

While you are here you may want to try some of the other ESP32 example sketches provided. For example ChipID (File -> Examples -> ESP32 -> ChipID) will display your chips MAC address in the serial monitor.



All up that wasn't too painful. I think the Arduino IDE would be a good option if you are already familiar with it and as long as the functionality that you need is available. Not all of the libraries are ESP32 compatible yet (e.g. analogWrite is not available yet although there are work arounds).

6 comments:

  1. duinotech esp32 schematic here:
    https://www.jaycar.com.au/medias/sys_master/images/images/9486646181918/XC3800-dataSheetMain.pdf

    ReplyDelete
  2. I have the same board and I can blink the red power led by using int LED_BUILTIN=1;
    My board doesn't seem to have a power led just the comm led.
    Just curious, how do you identify the pin# to use for the GPIO pin that you want to use. For instance, you used GPIO2 for your led, so did you just identify that as an output on pin 2 within the arduino ide? Or is there a way to identify a pin by GPIO designation within the IDE with something like this perhaps: digitalWrite(GPIO2,HIGH);
    Thanks...

    ReplyDelete
  3. Hi Unknown - you need to have a look at the Arduino core for ESP32 (https://github.com/espressif/arduino-esp32) to work out if they have defined any aliases for the pins. The tricky bit is working out which variant your board is.

    For example, have a look at: https://github.com/espressif/arduino-esp32/blob/master/variants/esp32/pins_arduino.h

    They don't use GPIO2, as that is more a Raspberry Pi style than Arduino. They do define things like the analog pins (A0 - A19), TX, RX (for serial), SDA and SCL (for I2C) and SS, MOSI, MISO and SS (for SPI).

    ReplyDelete
  4. Has anyone ever got the serial port (TXD0/RXD0) working on DUINOTECH ESP32 from Jaycar? I simply cannot get the port setup.

    ReplyDelete
    Replies
    1. Hi Ants - I did connect to the serial port via USB (which uses the same pins) and that worked.

      Delete