Esp32 S3 Code Examples
The ESP32 S3, a multiplexed computing framework, is a versatile platform designed for high-performance IoT applications. It's an evolution of the popular ESP32 series, offering enhanced capabilities such as higher CPU speeds, increased Flash memory, improved RAM, and dual-core MUS MA35D cores. To leverage these advancements, let's explore some code examples to get you started with the ESP32 S3.
The ESP32 S3 supports the arduino framework, making it easy for developers to prototype and build projects. Before we dive into the code examples, ensure you've set up your environment by installing an ESP32 S3 board in the Arduino IDE and selecting the appropriate framework (ESP32, then ESP32 S3 in the board selector).
Blinking an LED with ESP32 S3
Let's start with a classic - blinking an LED. This example will help you understand the basics of coding for the ESP32 S3.
Connect an LED to GPIO25 on your ESP32 S3 board. Here's the code to blink the LED: ```cpp #define LED_PIN 25 void setup() { pinMode(LED_PIN, OUTPUT); } void loop() { digitalWrite(LED_PIN, HIGH); delay(500); digitalWrite(LED_PIN, LOW); delay(500); } ```
Understanding the code
In this example, we define the LED_PIN as 25. In the setup function, we initialize the LED_PIN as an output. In the loop function, we toggle the LED on and off with a delay of 500 milliseconds between each state.
Modifying the blinking speed
To change the blinking speed, simply adjust the delay duration. For example, to make the LED blink faster, reduce the delay to 250 milliseconds: ```cpp void loop() { digitalWrite(LED_PIN, HIGH); delay(250); digitalWrite(LED_PIN, LOW); delay(250); } ```
Reading Sensor Data with ESP32 S3
The ESP32 S3 is compatible with various sensors, allowing you to build more complex IoT projects. Let's connect a DHT11 temperature and humidity sensor to our board and read its data.
Wiring the DHT11 sensor
The DHT11 has three pins: VCC, GND, and DATA. Connect VCC to 3V3, GND to GND, and DATA to GPIO26 on your ESP32 S3 board.
Code for reading DHT11 data
Now, let's write the code to read and display the temperature and humidity data from the DHT11 sensor using the DHT library: ```cpp #include "DHT.h" #define DHTPIN 26 #define DHTTYPE DHT11 DHT dht(DHTPIN, DHTTYPE); void setup() { Serial.begin(115200); dht.begin(); } void loop() { float h = dht.readHumidity(); float t = dht.readTemperature(); if (isnan(h) || isnan(t)) { Serial.println("Failed to read from DHT sensor! Will try again in 2 seconds."); delay(2000); return; } Serial.print("Humidity: "); Serial.print(h); Serial.print(" %\t"); Serial.print("Temperature: "); Serial.print(t); Serial.println(" °C"); delay(2000); } ```
The ESP32 S3 offers endless possibilities for IoT projects with its advanced features and extensive capabilities. From simple LED blinking to reading sensor data, you're now equipped with the basics to explore more complex projects. So, grab your ESP32 S3 board and start building! Happy coding!