0% found this document useful (0 votes)
7 views

ESP32_Coding_Cheatsheet_Full

Uploaded by

bharatadi05
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

ESP32_Coding_Cheatsheet_Full

Uploaded by

bharatadi05
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

ESP32 Coding Cheatsheet

1. Basic Structure of an ESP32 Program


Every ESP32 program consists of two main functions:

void setup() {
// Code runs once when the ESP32 starts
}

void loop() {
// Code runs continuously in a loop
}

2. Understanding delay(ms)
The delay(ms) function pauses the program for a specified time in milliseconds.

1 second = 1000 milliseconds

Example: Blink LED with Delay

const int LED_PIN = 2;

void setup() {
pinMode(LED_PIN, OUTPUT); // Set LED pin as output
}

void loop() {
digitalWrite(LED_PIN, HIGH); // Turn LED on
delay(1000); // Wait for 1 second
digitalWrite(LED_PIN, LOW); // Turn LED off
delay(1000); // Wait for 1 second
}

3. Digital Input and Output

const int LED_PIN = 2;


const int BUTTON_PIN = 4;

void setup() {
pinMode(LED_PIN, OUTPUT); // LED as output
pinMode(BUTTON_PIN, INPUT); // Button as input
}

void loop() {
if (digitalRead(BUTTON_PIN) == HIGH) { // Check if button is pressed
digitalWrite(LED_PIN, HIGH); // Turn LED on
} else {
digitalWrite(LED_PIN, LOW); // Turn LED off
}
}

4. Internal Pull-up and Pull-down

const int BUTTON_PIN = 4;


const int LED_PIN = 2;

void setup() {
pinMode(BUTTON_PIN, INPUT_PULLUP); // Enable internal pull-up resistor
pinMode(LED_PIN, OUTPUT);
}

void loop() {
if (digitalRead(BUTTON_PIN) == LOW) { // Button pressed (pull-up active)
digitalWrite(LED_PIN, HIGH);
} else {
digitalWrite(LED_PIN, LOW);
}
}

5. Analog Input (Reading Sensors)

const int POT_PIN = 34;

void setup() {
Serial.begin(115200); // Start serial communication
}

void loop() {
int value = analogRead(POT_PIN); // Read analog value (0-4095)
Serial.println(value); // Print value to Serial Monitor
delay(500);
}

6. PWM (Analog Output)

const int LED_PIN = 2;


const int PWM_CHANNEL = 0;
const int PWM_FREQUENCY = 5000;
const int PWM_RESOLUTION = 8;

void setup() {
ledcSetup(PWM_CHANNEL, PWM_FREQUENCY, PWM_RESOLUTION);
ledcAttachPin(LED_PIN, PWM_CHANNEL);
}

void loop() {
for (int brightness = 0; brightness <= 255; brightness++) {
ledcWrite(PWM_CHANNEL, brightness); // Adjust brightness
delay(10);
}
for (int brightness = 255; brightness >= 0; brightness--) {
ledcWrite(PWM_CHANNEL, brightness);
delay(10);
}
}

7. Serial Communication

void setup() {
Serial.begin(115200); // Start serial communication
}

void loop() {
Serial.println("Hello ESP32!"); // Print message to Serial Monitor
delay(1000);
}

8. Connecting to Wi-Fi

#include <WiFi.h>

const char* ssid = "Your_SSID";


const char* password = "Your_PASSWORD";

void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);

while (WiFi.status() != WL_CONNECTED) {


Serial.print("."); // Print dots while connecting
delay(500);
}

Serial.println("Connected to Wi-Fi");
}

void loop() {}

9. Simple Web Server

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

const char* ssid = "Your_SSID";


const char* password = "Your_PASSWORD";

WebServer server(80);
const int LED = 2;

void handleRoot() {
digitalWrite(LED, !digitalRead(LED)); // Toggle LED state
server.send(200, "text/plain", "LED Toggled");
}

void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);

pinMode(LED, OUTPUT);
server.on("/", handleRoot);
server.begin();
}

void loop() {
server.handleClient(); // Handle web requests
}

10. Deep Sleep Mode (Save Power)

const int SLEEP_TIME = 10; // Sleep time in seconds

void setup() {
esp_sleep_enable_timer_wakeup(SLEEP_TIME * 1000000); // Set wakeup timer
esp_deep_sleep_start(); // Enter deep sleep mode
}

void loop() {}

You might also like