Table of Contents

Setting up Arduino IDE

Download and Install Arduino IDE from here

Once the installation is complete, open the Arduino IDE and go to FilePreferences (Alternatively, press Ctrl/Cmd + , )

In the Additional Boards Manager URLs text box, insert this link https://raw.githubusercontent.com/espressif/arduino-esp32/gh-pages/package_esp32_index.json as shown in the following screenshot and click OK

Make sure you have an active Internet connection to proceed further

Then go to ToolsBoard: xxxxxxBoards Manager. A new Boards Manager Window will pop up. Let the Boards list get refreshed for a while. Make sure you are connected to the Internet so you get all the latest microcontroller boards list. In the Search text box, type ‘esp32’. You will see the board package as shown in the following image. Click on it and install the same.

The download and installation of this board package takes a while, so be prepared to wait patiently. Once the installation is complete, click Close. The installation should have added a new boards section ESP32 Arduino to your boards list.

From the Boards list, select ESP32 Dev Module as your board.

Next connect the ESP32 module to your Computer and download the Device Driver

For Linux, Mac and some versions of Windows, the driver will get installed automatically. But if you get a notification saying Device not configured, download and install the driver manually.

When you plug the ESP32 module to your computer using a USB cable, you should be able to see a new COM port (Windows) / Serial port (Linux/Mac).

To check this, go to ToolsPortCOMx or dev/serial.

Select the port that shows up only when you connect the ESP32 Dev board.

Now your Arduino IDE is ready to program ESP32 microcontroller.

P1: Blinking the in-built LED

LED is a digital output peripheral. Following the Arduino IDE’s Basic example programs to blink an LED, we have the following in setup() function

pinMode(LED_BUILTIN, OUTPUT);

Explanation: Since we are going to have the LED pin as OUTPUT throughout our program, it is enough to execute the above instruction only once. Hence add it to setup()

Now let’s program to switch the LED pin HIGH and LOW alternatively with a 1000 millisecond (1 second ) delay.

digitalWrite(LED_BUILTIN, HIGH);
delay(1000);
digitalWrite(LED_BUILTIN, LOW);
delay(1000);

In this tutorial, we will not be configuring ESP32 Dev Board as one of the pre-confgured boards to which LED_BUILTIN is mapped to a pin. So let’s replace the LED_BUILTIN with 2 since many of the ESP32 Dev Boards have an on board LED connected to GPIO2

digitalWrite(2, HIGH);
delay(1000);
digitalWrite(2, LOW);
delay(1000);

Similarly, change the instruction in setup() also to

pinMode(2, OUTPUT);

Finally, the entire program should be looking as follows

void setup() {
    pinMode(2, OUTPUT);
}
void loop() {
    digitalWrite(2, HIGH);
    delay(1000);
    digitalWrite(2, LOW);
    delay(1000);
}

Your blink program is ready to be uploaded now. Make sure you have selected ESP32 Dev Module as your Board and the correct COM/Serial Port then click on Upload icon (or Ctrl/Cmd + U ) from the menu bar. Let the program compile and get uploaded. This may take a few minutes for the first time.

Once the uploading is done, you should be able to see the LED blinking on the ESP32 board.

Exercise: Vary the delay and see how the blink speed varies.
Also if you have an extra LED and a resistor of range 200-500 ohms, try connecting an external LED to the board and make it blink using the following circuit diagram. Make sure you modify the program to blink at the correct GPIO

P2: Fetch Temperature and Humidity values from DHT Sensor

In this exercise, we will read the Temperature and Humidity values using DHT11 Sensor. Most often, manufacturers of sensors or the Opensource Community develops libraries that allow microcontrollers to interface with various sensors.

Here, we will make use of a couple of such libraries from Adafruit. To find and install the required libraries, navigate to SketchInclude LibraryManage Libraries from the Menu bar.

Find and install the following libraries:

  1. Adafruit Unified Sensor by Adafruit [This library acts as a base library to all the sensor specific libraries]
  2. DHT sensor library by Adafruit

Alternatively, you can download these libraries as .zip file from here and import it to your Arduino IDE using SketchInclude LibraryAdd .zip Library and selecting the downloaded library files

Copy the following program to your Arduino IDE, compile it to see if everything works perfectly

#include "DHT.h"

#define DHTPIN 2
#define DHTTYPE DHT11   // DHT 11

DHT dht(DHTPIN, DHTTYPE);

void setup() {
    Serial.begin(9600);
      dht.begin();
}

void loop() {
    float h = dht.readHumidity();
    float t = dht.readTemperature();
    float f = dht.readTemperature(true);

    if (isnan(h) || isnan(t) || isnan(f)) {
        Serial.println(F("Failed to read from DHT sensor!"));
        return;
    }

    Serial.print(F("Humidity: "));
    Serial.print(h);
    Serial.print(F("%  Temperature: "));
    Serial.print(t);
    Serial.print(F("°C "));
    Serial.println(f);

    delay(2000); //Wait for a couple of seconds before fetching the values again
}

Connect the ESP32 and DHT11 Sensor module as shown below then upload the program to ESP32

Sometimes the program does not upload to the ESP32 Dev Board when the Power pins (Vin, 3v3, Gnd) is connected to an external module. Make sure you disconnect any wires from those pins if you face this problem

Your DHT11 module may have different names for the pins, just know that

  • +,VCC,V,5V needs to be connected to 3v3(preferably) or VIN of the ESP32 Dev Board
  • -, GND, G needs to be connected to GND (any) of the ESP32 Dev Board
  • S, SIG, OUT needs to be connected to the GPIO you define in your program (D2 in this case)

If you are using DHT22 module, replace the 4th line of the above program to

#define DHTTYPE DHT22

P3: Controlling LED from Smartphone using MQTT

P4: Monitor Temperature on Smartphone using MQTT

Last modified: October 10, 2020

Author

Comments

Write a Reply or Comment

Your email address will not be published.