STEP ONE - Setting up your computer
We are going to use the Arduino IDE (Integrated Development Environment) to write code for your ESP32 using a computer programming language called Arduino (similar to C+). Go to Arduino.cc and follow the instructions to install the Arduino Desktop IDE for your computer operating system. If you are new to Arduino read through the Learn Arduino Introduction section.
Once you are able to run the Arduino IDE you will need to add a Library. This will extend the basic Arduino IDE to work with the ESP32. Later we will add more libraries to extend the IDE to work with sensors such as the Ultrasonic module, devices like the servo motors and communications protocols used on web pages that will interact with your robot.
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
then type a comma ,
Then paste this link after the comma. http://arduino.esp8266.com/stable/package_esp8266com_index.json
10. Click OK. This will install the ESP32 boards into your Arduino IDE.
11. Almost there.
12. In the Arduino IDE click on the Tools menu on the top menu bar.
13. Scroll down to the Board: entry
14. A submenu will open when you highlight the Board: entry.
15. Choose the Boards Manager.
16. Enter esp into the search bar at the top.
17. Choose the esp32 by Espressif Systems and click install.
18. Once again choose Tools - Board.
19. Find the ESP32 Dev Module in the ESP32 Arduino submenu and click to select.
20. PUT YOUR SAFETY GLASSES ON. Connect your ESP32 to your computer using a USB cable. NOTE. Some cables are meant for charging only and will not "communicate" with the Esp32. If no COM ports show then try another cable.
21. In the Arduino IDE click on the Tools menu on the top menu bar.
22. Scroll down to Port.
23. A submenu to the right should show one active COM port. Click on it.
The Arduino IDE now knows how to send code to an ESP32 and it knows where the ESP32 is connected. Whew!
If you are using a Macintosh computer and you are having difficulty finding the Com port then try installing this driver. https://www.silabs.com/documents/public/software/Mac_OSX_VCP_Driver.zip
Silabs makes the USB to UART chip that allows you to communicate with the Esp32 chip via USB. They have made this generic driver to work on all Macs.
STEP TWO - Testing your connection (Your first program)
Open the Arduino IDE and select File-New. A new program window will open and you will see there is already some code there. That is because all Arduino programs have 2 parts. The code you write into the first part - the setup, is executed by the microcontroller once when the program starts. void setup() is the "method" used to do this. The code you write between the curly braces { and } will be executed once. The code you write into the second part - the loop, is executed over and over (repeatedly) until the microcontroller is powered down.
Lines that begin with "//" are called comments. These lines are not executed so you can write whatever you want. Comments help the reader understand what is happening in the code.
The IDE does 3 things.
It lets you write the code (you will see some hinting).
It compiles your code. A compiler takes the computer program written in whatever language and boils it down into code that the microcontroller understands (called machine code).
It loads your machine code into the microprocessor. Arduinos and ESP32s have a small program built into them called a 'boot loader' that facilitates communication between your computer and the microcontroller.
If the IDE cannot compile your code it will stop and give you some error messages in the console at the bottom.
The ESP32 microcontroller has some input/Output pins called GPIO pins. GPIO is General Purpose Input/Output. We can connect things like push buttons and LEDs to the these pins. The ESP32 is a pretty tiny chip. These pins can handle enough current to light up an LED or two but there is no way we could hook up a light bulb directly. The ESP32 would not be able to handle all that current or high voltage!
THE ESP32 DEVKIT has an LED onboard connected to GPIO2. So let's light it up!
Between the setup braces type the following:
pinMode(2, OUPUT);
Notice that the word pinMode turns orange. The IDE hinting recognizes that this is a command. The word OUTPUT turns blue. The IDE hinting recognizes that this is a keyword. In other words it recognizes that the command "pinMode" can do something with the word OUTPUT. The number 2 is in a spot reserved for a variable - in this case, the port number, GPIO2.
All right. Now we have told the ESP32 that pin 2 is going to sending or "outputting" a signal. Now in the loop section we will tell the ESP32 what to output. Between the loop braces type the following:
digitalWrite(2, HIGH);
delay(1000);
digitalWrite(2, LOW);
delay(1000);
Press the Upload button (rightward facing arrow) at the top of the IDE. If all goes as it should the code will compile and then upload to your ESP32. After a few moments you will see some messages in the console reflecting what the IDE is doing. After "Hard resetting via RTS pin . . ." your code will start running. We should see the blue LED on the DEVKIT blinking on and off.
digitalWrite is the command that sends binary (1 or 0) data to a pin (in this case pin 2). We could replace the word HIGH with the number 1. They both mean the same thing. We could also replace LOW with the number 0. TRY IT!
"delay" tells the microprocessor to wait before executing the next line of code. The variable spot is in milliseconds. So 1000 represents 1000 milliseconds or 1 second. The LED should blink at a rate of roughly 1 second on and 1 second off. If we change the number we can change the rate of blinking. TRY IT.
Your code should look like this.
We can also add comments to make the program easier to understand later on.
// ESP32 Blink program by Me, Jan. 9, 2021
void setup() {
pinMode(2, OUTPUT); // initialize digital pin 2 as an output.
}
// the loop function runs over and over again forever
void loop() {
digitalWrite(2, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(2, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
}
Save your program. Call it "Blinkesp32".
STEP THREE - How to use a breadboard (Your first circuit)
In this section we will first look at how breadboards work. Then we will see how to use the ESP32 DEVKIT on a 1/2 size breadboard.
Breadboards are used to make temporary circuits for test, development and learning. The breadboard lets you easily wire up and then adjust and change a circuit for these purposes. You can read about breadboards on page 10 of the Arduino comic. This Sparkfun article takes a few minutes to read but gives a good overview. You can skip anything to do with "binding posts" since we do not use them.
So breadboards allow us to make electrical connections between component leads. The spacing of the holes is 0.1 inches - the same spacing as the leads on the ESP32 DEVKIT, and almost every other chip or development circuit out there. We saw that if we put both leads into the same column the component will not work because it is "shorted".
DANGER WILL ROBINSON! Did someone say "short"? Yes indeed a "short circuit" could be a very bad thing. An electrical short could cause components to overheat, catch fire and even, occasionally, explode. Time for a safety lesson. Before we go on and do any wiring read through the next section on safety. Please follow these safety protocols. Work safely.