This is a simple "Hello World" example for the ESP32, using the ESP-IDF C++ framework.
These steps only need to be performed once.
-
Install Prerequisites:
brew install cmake ninja dfu-util
-
Clone the ESP-IDF repository:
mkdir ~/esp cd ~/esp git clone --recursive https://github.com/espressif/esp-idf.git
-
Install ESP-IDF Tools:
cd ~/esp/esp-idf ./install.sh
-
Set up Environment Variables: To make the
idf.py
command available in every terminal session, add the following alias to your shell's profile script (e.g.,~/.zshrc
or~/.bash_profile
):echo "alias get_idf='. $HOME/esp/esp-idf/export.sh'" >> ~/.zshrc
Then, in any new terminal, you can type
get_idf
to set up the environment.
To create a new project, you can copy the hello_world
example:
cp -r ~/esp/esp-idf/examples/get-started/hello_world ~/esp/my_new_project
cd ~/esp/my_new_project
-
Navigate to your project directory:
cd ~/esp/hello_world
-
Set up the environment in your new terminal:
. ~/esp/esp-idf/export.sh
Or, if you've set up the alias:
get_idf
-
Set the target chip (only needs to be done once per project):
idf.py set-target esp32
-
Build the project:
idf.py build
-
Flash and monitor the project: Connect your ESP32 board and run:
idf.py -p /dev/cu.usbserial-10 flash monitor
(Replace
/dev/cu.usbserial-10
with your device's serial port if it's different.)To exit the monitor, press
Ctrl+]
.
The main logic is in main/hello_world_main.c
.
- It starts by printing "Hello world!".
- Then, it retrieves and prints information about the ESP32 chip, including the number of cores, features (WiFi, BT, BLE), silicon revision, and flash size.
- It prints the minimum free heap size.
- Finally, it counts down from 10 and restarts the device.
Now that you have a working development environment, you can start exploring the other examples in the ~/esp/esp-idf/examples
directory. These examples cover a wide range of functionalities, from GPIO and timers to Wi-Fi and Bluetooth.
You can also start writing your own applications by modifying the hello_world_main.c
file or creating new source files in the main
directory.