Arduino Examples
Now that we've installed the espressif boards package in Arduino, it's time to upload our first sketch to make sure everything installed and is working properly.
Thing Plus ESP32-S3 Github Repository
We've written a few example sketches for this specific board that live in the Github repository that we recommend taking a look at as they demonstrate some important hardware use cases. You can download it from the link above or you can get a ZIP of the library by clicking the button below:
esptool Upload Errors
Some users may run into this upload error related to esptool:
esptool.py v4.5.1 Serial port COM33 Connecting....
A serial exception error occurred: Write timeout Note: This error originates from pySerial. It is likely not a problem with esptool, but with the hardware connection or drivers. For troubleshooting steps visit: https://docs.espressif.com/projects/esptool/en/latest/troubleshooting.html the selected serial port For troubleshooting steps visit: https://docs.espressif.com/projects/esptool/en/latest/troubleshooting.html does not exist or your board is not connected
This error means the board is not entering ROM bootloader mode automatically, causing the upload to hang and fail. We found this relates specifically to esptool.py v4.5.1 in the current stable release of the ESP32 Boards Package (2.0.11). Users can work around this error with one of the following three fixes:
- Use the alpha release (3.0.0 alpha3) of the ESP32 Boards Package. This release updates to esptool.py v4.6.
- Download the latest release of esptool.py from the GitHub repository releases and then manually replace it in the espressif boards package folder.
- If you want to keep using the current full release and encounter this error, force the board to enter the bootloader by holding the BOOT button down, clicking the RESET button, and then releasing the BOOT button.
Board Settings
As mentioned in the previous section, the Thing Plus ESP32-S3 is not currently included in the ESP32 Boards Package so we recommend selecting "ESP32S3 Dev Module" for your board with the following settings:
- USB Mode: Hardware CDC and JTAG
- USB CDC on Boot: Enabled
- Upload Mode: UART0 / Hardware CDC
- PSRAM: QSPI PSRAM
This does require some manual definition/setup for pins in the code which the examples below demonstrate for some of the main peripheral components (Qwiic, µSD card, etc.).
RGB LED
This example uses the FastLED library to cycle the RGB LED on the Thing Plus through a rainbow. Open the code from your downloaded copy of the GitHub repository or copy the code below into a blank sketch. Make sure the FastLED Library is installed, select the board (ESP32S3 Dev Module) and the Port, and click the "Upload" button.
RGB LED Example
/*
Control Addressable RGB LED
By: Nathan Seidle
SparkFun Electronics
Date: January 13, 2024
License: MIT. Please see LICENSE.md for more information.
This example shows how to illuminate the RGB on the SparkFun ESP32 S3 Thing Plus.
Feel like supporting open source hardware?
Buy a board from SparkFun!
SparkFun ESP32-S3 Thing Plus (DEV-24408) https://www.sparkfun.com/products/24408
Select the following in the Arduino IDE:
Board: ESP32S3 Dev Module
USB Mode: Hardware CDC and JTAG
USB CDC on Boot: Enabled
Upload Mode: UART0 / Hardware CDC
PSRAM: QSPI PSRAM
Port: Select the COM port that the device shows up on under device manager
*/
#include <FastLED.h> // http://librarymanager/All#FastLED
#define LED_PIN 46 //Pin 46 on Thing Plus C S3 is connected to WS2812 LED
#define COLOR_ORDER GRB
#define CHIPSET WS2812
#define NUM_LEDS 1
#define BRIGHTNESS 25
CRGB leds[NUM_LEDS];
void setup()
{
Serial.begin(115200);
//while (Serial == false); //Wait for serial monitor to connect before printing anything
FastLED.addLeds<CHIPSET, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip);
FastLED.setBrightness(BRIGHTNESS);
}
void loop()
{
leds[0] = CRGB::Red;
FastLED.show();
delay(200);
leds[0] = CRGB::Green;
FastLED.show();
delay(200);
leds[0] = CRGB::Blue;
FastLED.show();
delay(200);
leds[0] = CRGB::Black;
FastLED.show();
delay(100);
leds[0] = CRGB::White;
FastLED.show();
delay(2000);
leds[0] = CRGB::Black;
FastLED.show();
delay(100);
}
I2C Scanner Power Control
This example shows how to toggle the peripheral power voltage supply on and off as well as scan for any I2C devices connected to the I2C bus/Qwiic connector. After uploading, open the serial monitor with the baud set to 115200 and you should see a serial printout of the Fuel Gauge at 0x32 as well as any other devices connected to the Qwiic connector like the screenshot below shows. If you have a Qwiic board plugged in you should see the power LED on it blinking every second as the bus is powered on and off.
I2C Scanner Power Control Example
/*
Enable Qwiic Power Control
By: Nathan Seidle
SparkFun Electronics
Date: January 13, 2024
License: MIT. Please see LICENSE.md for more information.
This shows how to control the power to the Qwiic connector. This example also shows the
Fuel Gauge IC at address 0x36.
Feel like supporting open source hardware?
Buy a board from SparkFun!
SparkFun ESP32-S3 Thing Plus (DEV-24408) https://www.sparkfun.com/products/24408
Select the following in the Arduino IDE:
Board: ESP32S3 Dev Module
USB Mode: Hardware CDC and JTAG
USB CDC on Boot: Enabled
Upload Mode: UART0 / Hardware CDC
PSRAM: QSPI PSRAM
Port: Select the COM port that the device shows up on under device manager
*/
#include <Wire.h>
int pin_qwiicPower = 45; //Thing Plus S3 45 is connected to the v-reg that controls the Qwiic power
void setup()
{
Serial.begin(115200);
while (Serial == false); //Wait for serial monitor to connect before printing anything
Serial.println("I2C Scanner");
//By default the Qwiic power is on. But it can be controlled.
pinMode(pin_qwiicPower, OUTPUT);
digitalWrite(pin_qwiicPower, HIGH);
qwiicPowerOff();
delay(2000); //Power LED on a Qwiic board should now be off
qwiicPowerOn();
Wire.begin();
}
void loop()
{
//Toggle the power on the Qwiic connector
Serial.println();
for (byte address = 1; address < 127; address++ )
{
Wire.beginTransmission(address);
if (Wire.endTransmission() == 0)
{
Serial.print("Device found at address 0x");
if (address < 0x10)
Serial.print("0");
Serial.println(address, HEX);
}
}
Serial.println("Done");
delay(100);
}
void qwiicPowerOff()
{
digitalWrite(pin_qwiicPower, LOW);
}
void qwiicPowerOn()
{
digitalWrite(pin_qwiicPower, HIGH);
}
SD Card Detect
The SD Card Detect example is a simple example that monitors the card detect pin on the µSD card slot. As mentioned in the Hardware Overview section, the card detect switch is normally open and closed with a card inserted so the switch goes to VDD (3.3V). This requires us to set up an internal pull-down for card detection to work properly.
After uploading, you can open the serial monitor with the baud set to 115200 and it should print out either "Card Inserted" or "No SD Card" depending on whether or not a µSD card is present.
SD Card Detect Example
/*
Detecting if an SD Card is present
By: Nathan Seidle
SparkFun Electronics
Date: January 13, 2024
License: MIT. Please see LICENSE.md for more information.
The socket detect switch is normally open, and closed when a card is inserted.
The switch is connected to VDD, so when a card is inserted, the switch is connected
to 3.3V. Therefore, we use an internal pull down.
This shows how to control the power to the Qwiic connector. This example also shows the
Fuel Gauge IC at address 0x36.
Feel like supporting open source hardware?
Buy a board from SparkFun!
SparkFun ESP32-S3 Thing Plus (DEV-24408) https://www.sparkfun.com/products/24408
Select the following in the Arduino IDE:
Board: ESP32S3 Dev Module
USB Mode: Hardware CDC and JTAG
USB CDC on Boot: Enabled
Upload Mode: UART0 / Hardware CDC
PSRAM: QSPI PSRAM
Port: Select the COM port that the device shows up on under device manager
*/
int pin_sdDetect = 48;
void setup()
{
Serial.begin(115200);
while (Serial == false); //Wait for serial monitor to connect before printing anything
Serial.println("SDIO test");
pinMode(pin_sdDetect, INPUT_PULLDOWN);
}
void loop()
{
if (Serial.available()) ESP.restart();
if (digitalRead(pin_sdDetect) == HIGH)
Serial.println("Card inserted");
else
Serial.println("No SD card");
delay(500);
}
PSRAM Test
This example demonstrates how to enable and use PSRAM for RAM requests above a 1,000 byte threshold. The setup initializes PSRAM and prints out whether or not this was successful and then prints out available PSRAM space.
PSRAM Test Example
/*
Enable PSRAM on the ESP32-S3
By: Nathan Seidle
SparkFun Electronics
Date: January 13, 2024
License: MIT. Please see LICENSE.md for more information.
This example shows how to enable PSRAM on ESP32-S3 modules that have it, and use it for
RAM requests above a certain byte threshold.
Note: Not all ESP32 modules have PSRAM built-in. The SparkFun ESP32-S3 uses the
ESP32-S3 Mini N8R2 with 8MB flash and 2MB PSRAM.
Feel like supporting open source hardware?
Buy a board from SparkFun!
SparkFun ESP32-S3 Thing Plus (DEV-24408) https://www.sparkfun.com/products/24408
Select the following in the Arduino IDE:
Board: ESP32S3 Dev Module
USB Mode: Hardware CDC and JTAG
USB CDC on Boot: Enabled
Upload Mode: UART0 / Hardware CDC
PSRAM: QSPI PSRAM
Port: Select the COM port that the device shows up on under device manager
*/
unsigned long lastHeapReport;
void setup()
{
Serial.begin(115200);
while (Serial == false); //Wait for serial monitor to connect before printing anything
if (psramInit() == false)
Serial.println("PSRAM failed to initialize");
else
Serial.println("PSRAM initialized");
Serial.printf("PSRAM Size available (bytes): %d\r\n", ESP.getFreePsram());
heap_caps_malloc_extmem_enable(1000); //Use PSRAM for memory requests larger than 1,000 bytes
}
void loop()
{
delay(20);
if (millis() - lastHeapReport > 1000)
{
lastHeapReport = millis();
reportHeap();
}
if (Serial.available()) ESP.restart();
}
void reportHeap()
{
Serial.printf("FreeHeap: %d / HeapLowestPoint: %d / LargestBlock: %d / Used PSRAM: %d\r\n", ESP.getFreeHeap(),
xPortGetMinimumEverFreeHeapSize(), heap_caps_get_largest_free_block(MALLOC_CAP_8BIT), ESP.getPsramSize() - ESP.getFreePsram());
}