ESP 32 is a series of low-cost, low-power system on chip micro controllers with integrated Wi-FI and Bluetooth. SoC or system on chip is an integrated circuit that integrates all or most of the component of a computer. Example the CPU, RAM, IO interface, GPU, etc. This form is becoming more and more popular thanks to growing smartphone and computing industry. Apple M1 chip is a good example on the direction in which SoC’s are going.

The ESP 32 is really low cost it cost like 350 in India, and less than 10$ in the US. Now when you buy this you don’t buy the chip alone, but a chip on a development board. There are several articles (https://makeradvisor.com/esp32-development-boards-review-comparison/) on that. The board that I bought from amazon was this https://www.amazon.com/dp/B0718T232Z?psc=1&ref=ppx_yo2ov_dt_b_product_details, i also bought one with OLED display as well https://www.amazon.com/dp/B07DKD79Y9?psc=1&ref=ppx_yo2ov_dt_b_product_details.

In this article I will be talking about how to get started. We will basically will be setting up Aurdino IDE in linux, followed by flashing few sample programs like:

Note: Arduino is another chip like esp32 (costlier but similar). ESP32 is can be made working on the same IDE. ESP32 can also be programmed using circuit python (which uses micro python), mruby, etc. But since there are a lot of popular code examples available for Arduino IDE esp32, I personally recommend getting started and working with Arduino. 

The language used in Arduino is C++, and the C++ program is written to esp32 board using python (esptool.py).  

To setup Arduino:

1. Download the linux installation files from https://www.arduino.cc/en/software 2. Extract the contents of the file downloaded ( a .tar.xz) to the home folder 3. Open terminal 4. cd ~/<folder_name>`` 5. sh install.sh`

This would install Arduino IDE in your computer, after which connect the esp32 device to your computer USB port. Now do ls -l /dev/ttyUSB*. You will see the USB port number to which you have connected the board.

The output would look like this:

1
crw-rw---- 1 root dialout 188, 0 5 apr 23.01 ttyUSB0

You need to add your logged in user to the group dialout for the IDE to be able to access the board. So run the following command.

1
sudo usermod -a -G dialout <username>

You will have to log out and log back in for the settings to take hold, you could also quickly restart your computer if you wish.

Once this is done, you can open up your Arduino IDE

Arduino IDE

Next step is to add the board details to the Arduino IDE.

To do that, go to:

  1. File > Preference
  2. In the section additional boards manager URL text box, paste the following urls:
    1
    
    https://raw.githubusercontent.com/espressif/arduino-esp32/gh-pages/package_esp32_index.json, http://arduino.esp8266.com/stable/package_esp8266com_index.json
    
    ``

Now this will load a log of ESP32 and ESP8266 board details and chances are this would have your board as well.

Different Board

I selected the ESP32 Dev Module in the list.

Now, since we have our board selected, we can run some examples. When we added the preference URL, along with getting the board details it also got some examples. A good code to get started with is the wifi scan. To load the program go to file > examples > Under Heading Examples for ESP32 Dev Modules > WiFi > Wifi Scan

A window will open up with the code example.

Click the right arrow and it will start writing the code to the board.

compile_and_write_to_board.png

Once you get started you might/will (depending on the board) see this following error.

chip_download_mode_error.png

Failed to connected to ESP32: Wrong boot mode detected (0x13)! The chip needs to be in download mode.

What this means is that you need to enable the download mode on the chip, and to do that you need to click and hold the boot button on the board while the program tries to upload the program. The below image is where the button is on my board, it would most probably be the same for you as well.

boot_button.png

Now when you click the right button, you will see that it is overwriting what ever existing program it might be having.

To see the output of your work, click the magnifying glass on the top right corner of your Arduino board. serial_monitor.png

That Serial monitor, click the other button on your board and your program will now be running and it will slowly start to display all the wifi networks it can see.

There are other examples available as well feel free to play around with your board, and build something amazing.

PS: my board says ESP32-WROOM, its not that much different from ESP32 it just has more flash memory.

Code to control the onboard LED Light:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
#define ONBOARD_LED  2

void setup() {
  pinMode(ONBOARD_LED,OUTPUT);
}

void loop() {
  delay(1000);
  digitalWrite(ONBOARD_LED,HIGH);
  delay(100);
  digitalWrite(ONBOARD_LED,LOW);
}

Code to control the onboard LED light using a HTTP server

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92

#include <WiFi.h>
#include <WiFiClient.h>
#include <WebServer.h>
#include <ESPmDNS.h>

const char *ssid = "DragonStone";
const char *password = "passwordillak3t0";
const int onboardLed = 2;

WebServer server(80);


void handleRoot() {

  String index = "<html>\
  <head>\
    <meta http-equiv='refresh' content='5'/>\
    <title>ESP32 LED Controller</title>\
    <style>\
      body { background-color: #cccccc; font-family: Arial, Helvetica, Sans-Serif; Color: #000088; }\
    </style>\
  </head>\
  <body>\
    <h1>Hello from ESP32!</h1>\
    <p><a href='/on'>ON</a><br><br><br><a href='/off'>off</a>\
  </body>\
</html>";
  server.send(200, "text/html", index);
}

void handleNotFound() {
  String message = "File Not Found\n\n";
  message += "URI: ";
  message += server.uri();
  message += "\nMethod: ";
  message += (server.method() == HTTP_GET) ? "GET" : "POST";
  message += "\nArguments: ";
  message += server.args();
  message += "\n";

  for (uint8_t i = 0; i < server.args(); i++) {
    message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
  }

  server.send(404, "text/plain", message);
}

void setup(void) {
  pinMode(onboardLed, OUTPUT);
  digitalWrite(onboardLed, 0);
  Serial.begin(115200);
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);
  Serial.println("");
  pinMode(onboardLed,OUTPUT);

  // Wait for connection
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("");
  Serial.print("Connected to ");
  Serial.println(ssid);
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());

  if (MDNS.begin("esp32")) {
    Serial.println("MDNS responder started");
  }

  server.on("/", handleRoot);
  server.on("/on", []() {
    digitalWrite(onboardLed,HIGH);
    server.send(200, "text/html", "led is ON. <br><a href='/'>Go Back</a>");
  });
  server.on("/off", []() {
    digitalWrite(onboardLed,LOW);
    server.send(200, "text/html", "led is off. <br><a href='/'>Go Back</a>");
  });
  server.onNotFound(handleNotFound);
  server.begin();
  Serial.println("HTTP server started");
}

void loop(void) {
  server.handleClient();
  delay(2);//allow the cpu to switch to other tasks
}

Happy Codding