ESP32 Dev Kit C

Today there is a microcontroller again and that is the ESP32. Also in a pack of three. The ESP32 is the big brother of the ESP8266. It has a dual core CPU and 520 kByte RAM. It has a lot more I / O pins that can take on almost any function.

Of course, the ESP32 has a complete WLAN implementation, like its little brother,. Another highlight of the ESP32 is its Bluetooth capability (Classic and BLE). In today's project we want to look at how you can communicate with the ESP32 from a smartphone via Bluetooth.

In addition to the ESP32 module with 4Mbyte Flash, the DevKit C Breakout Board has a CP2102 USB to serial converter. A 3.3V voltage regulator supplies the MCU. As with the ESP8266, the voltage at the inputs must not exceed the supply voltage.


So that we can test the result of our Bluetooth communication, we also need an LED, a series resistor with 220 ohms and a sensor that supplies an analog output voltage. For example a photosensitive resistor.


For the smartphone we need an app that implements a serial Bluetooth terminal. For example “Serial Bluetooth Terminal”, which can be found free of charge in the Playstore for Android. There are also various similar apps for Apple, but I couldn't test them due to the lack of suitable hardware.


The anode of the light emitting diode is connected to the 3.3V connection of the board via the 220 Ohm series resistor. We connect the cathode directly to the connection GPIO14. For the light-sensitive resistor we create a voltage divider with a 10 kOhm resistor between 3.3 V and GND. We feed the partial voltage to the analog input GPIO35. The voltage will decrease with increasing brightness, as the resistance value of the photosensitive resistor will then decrease.

The program is waiting for data from Bluetooth. If a message begins with B, the duration of the pause for flashing is expected in ms. If a message begins with R, the measured value of the analog input is sent via Bluetooth.

Before we upload the sketch in the Arduino IDE, we have to install the support for ESP32 boards, if not already done. In the Arduino IDE we go to Preferences via the File menu or simply with "Ctrl +;".

In the form we click on the symbol to the right of the input field under the point “Additional board administrator URLs”. Here we can now enter the URL for the ESP32 board manager:

https://dl.espressif.com/dl/package_esp32_index.json


After the presets have been saved, we call up the board manager via the menu Tools-> Board: -> Board manager. We enter “esp32” as the search term. We get the following entry, which we install.

  

Now we can select a board in the Tools-> Board: -> ESP32 Arduino menu. We use ESP32 Dev Module.

  

With the ESP32 package installed, we have everything we need because the “BluetoothSerial” library is included in the ESP32 package. So we can compile the sketch immediately.

Download sketch


#include "BluetoothSerial.h" // Serial bluetooth interface

#define LED 14     // GPIO for the LED
#define SENSOR 35  // GPIO for the analog input

// Create an instance for the Bluetooth interface
BluetoothSerial SerialBT;

uint32_t load;  // timestamp for the blinking
boolean stat;   // Current state of the LED
uint16_t Tim;   // Duration for the blinking

void set up() {
  Serial.begin(115200);    // Start the serial interface
  pinMode(LED,OUTPUT);     // Set the pin for the LED to output
  load = millis();         // remember the current time
  stat = false;            // current status LED off
  Tim = 1000;              // Flashing duration to 1s 
  digitalWrite(LED,!stat); // Set LED to current state
                           //! because the LED lights up at LOW
  SerialBT.begin("ESP32test"); // Bluetooth name of the ESP32
  Serial.println("The ESP32 is ready. Now connect via Bluetooth.");
}

void loop() {
  int msg;
  uint8_t i;
  char cmd;
  char val[20];
  uint16_t vali;
  if (Serial.available()) {   // Data from the serial monitor is sent to the smartphone
    SerialBT.write(Serial.read());
  }
  if (SerialBT.available()) {  // data from bluetooth 
    msg = SerialBT.read();     // read first character
    i=0;
    if (msg >= 0) cmd = msg;   // if the character was OK, it is saved as a command
    // Read character by character as long as there are or until the
    // Buffer is full
    while ((msg >= 0) && (i<20)) {         
      msg = SerialBT.read();
      if (i<20) val[i] = msg;
      i++;
    }
    // Convert the value in the buffer to a number
    vali = atoi(val);
    // Depending on the command
    switch (cmd) {
      // B change the flashing duration
      case 'B': Tim = vali; break;  
      // R read a value from the analog input and send it to the
      // send smartphone
      case 'R': vali = analogRead(SENSOR);
                Serial.println(vali);
                // convert the read value into a character string
                sprintf(val,"%i",vali);
                i=0;
                // and send character by character
                while ((i<20) && (val[i]!=0)) {
                  SerialBT.write(val[i]);
                  i++;
                }
                // Finally send CR and LF
                SerialBT.write(13);
                SerialBT.write(10);
                break;
    }
  }
  // When the flashing time has expired, reverse the LED state
  if ((millis()-load) > Tim) {
    load=millis(); // Remember the start time
    stat = !stat;
    digitalWrite(LED,!stat);
  }
  delay(25);
}

Now we still need the smartphone app. This can easily be downloaded from the Playstore and installed. In the Bluetooth setting on the smartphone, we have to pair our new device. The device is called ESP32test and can be found in the list of rarely used devices. Pairing should be easy.

Now we start the Bluetooth Terminal App. In the Devices menu item, we select our device. Now we can establish a connection. To do this, we click on the symbol with the two separate plugs at the top right. The connection establishment is logged in yellow in the terminal window.

We enter the command "B100" and send it to make the LED flash with 100ms. Then we enter "R" as a command and the ESP32 should respond with a measured value after the command has been sent.


The program is only an example of how to use Bluetooth with the ESP32. Now it's your turn!




Esp-32Specials

1 comment

Markus Walsleben

Markus Walsleben

Daich selbst einen solchen genutzten ESP32 DevKit C nutze, kann ich sagen, dass ich das hier gezeigte Pinout deutlich übersichtlicher und verständlicher finde als das Stand Heute noch beim Produkt verlinkte Pinout.
Danke dafür!

Leave a comment

All comments are moderated before being published

Recommended blog posts

  1. Install ESP32 now from the board manager
  2. Lüftersteuerung Raspberry Pi
  3. Arduino IDE - Programmieren für Einsteiger - Teil 1
  4. ESP32 - das Multitalent
  5. OTA - Over the Air - ESP programming via WLAN