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

code manual

The document contains a series of C programs for the ESP32 microcontroller that demonstrate various functionalities including blinking LEDs, reading sensor data, and interfacing with Bluetooth. Each program is structured with a setup and loop function, showcasing tasks such as controlling LEDs based on button presses, reading light intensity, and sending data via Bluetooth. The examples serve as practical applications for learning about GPIO, serial communication, and sensor integration with the ESP32 platform.

Uploaded by

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

code manual

The document contains a series of C programs for the ESP32 microcontroller that demonstrate various functionalities including blinking LEDs, reading sensor data, and interfacing with Bluetooth. Each program is structured with a setup and loop function, showcasing tasks such as controlling LEDs based on button presses, reading light intensity, and sending data via Bluetooth. The examples serve as practical applications for learning about GPIO, serial communication, and sensor integration with the ESP32 platform.

Uploaded by

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

1.

Write a program to Blink Onboard LED(GPIO-02) Repeatedly (Infinite loop)


----------------------------------------------------------

void setup() {
// put your setup code here, to run once:
pinMode(2,OUTPUT);
}

void loop() {
// put your main code here, to run repeatedly:
digitalWrite(2,HIGH);
delay(1000);
digitalWrite(2,LOW);
delay(1000);
}

2. Write a program to Blink Onboard LED(GPIO-02) Five times only (finite loop)
-----------------------------------------------------------------------------------
----------
void setup() {
// put your setup code here, to run once:
pinMode(2,OUTPUT);
for(int i=0;i<5;i++)
{
digitalWrite(2,HIGH);
delay(1000);
digitalWrite(2,LOW);
delay(1000);
}
}

void loop() {

3. Write a program to Blink Onboard LED(GPIO-02) and External LED which is


conected at GPIO-18 Simultaneously.

-----------------------------------------------------------------------------------
----------
void setup() {
// put your setup code here, to run once:
pinMode(2,OUTPUT);
pinMode(18,OUTPUT);
}

void loop() {
// put your main code here, to run repeatedly:
digitalWrite(2,HIGH); //on
digitalWrite(18,HIGH); //on
delay(1000);
digitalWrite(2,LOW); //off
digitalWrite(18,LOW); //off
delay(1000);
}

4. Write a program to Blink Onboard LED(GPIO-02) and External LED which is


conected at GPIO-18 Alternately.
-----------------------------------------------------------------------------------
----------
void setup() {
// put your setup code here, to run once:
pinMode(2,OUTPUT);
pinMode(18,OUTPUT);
}

void loop() {
// put your main code here, to run repeatedly:
digitalWrite(2,HIGH); //on
digitalWrite(18,LOW); //off
delay(1000);
digitalWrite(2,LOW); //off
digitalWrite(18,HIGH); //on
delay(1000);
}

5. Write a Program in C to display character, integer, float and String on


Serial Monitor using UART.
-----------------------------------------------------------------------------------
--
void setup() {
// put your setup code here, to run once:
Serial.begin(9600); // Baud Rate- Bits/Sec
}
void loop() {
// put your main code here, to run repeatedly:
int val=2345;
Serial.println(123);
Serial.println(12.43);
Serial.println('B');
Serial.println("SST Tech - BIT");
Serial.println(val);
delay(3000);
}
-----------------------------------------------------------------------------------
----
6. Interface Pushbutton Switch to ESP32 (GPIO-0).
Write a Program in C to get status of switch (digital Sensor) and
display it on Serial monitor.

void setup() {

Serial.begin(9600);
pinMode(0,INPUT);
}

void loop() {
int val;
val=digitalRead(0);
Serial.println(val);
}
-----------------------------------------------------------------------------------
----------
7. Interface Pushbutton Switch (GPIO-0) and LED (GPIO-18) to ESP32 and
Write a Program in C to Control LED using Pushbutton switch.

void setup() {
Serial.begin(9600);
pinMode(0,INPUT); //Push button Switch
pinMode(18,OUTPUT); //LED
}

void loop() {
int val;
val=digitalRead(0);
Serial.println(val);
if(val==0)
digitalWrite(18,HIGH);
else
digitalWrite(18,LOW);
}
-----------------------------------------------------------------------------------
----------
8. Write a Program in C to read intensity of Light and
display it on Serial Monitor.

void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(36,INPUT); //LDR
}

void loop() {
// put your main code here, to run repeatedly:
int val;
val=analogRead(36);
Serial.println(val);
delay(1000);
}
-----------------------------------------------------------------------------------
------
9. Implement smart room lighting system.

void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(36,INPUT); //LDR
pinMode(18,OUTPUT); //LED

void loop() {
int val;
val=analogRead(36);
if(val<500)
digitalWrite(18,HIGH);
else
digitalWrite(18,LOW);
}
-----------------------------------------------------------------------------------
-------
10. Write a program to Control LED(GPIO-18) using Serial Monitor(UART).

void setup() {
pinMode(18,OUTPUT);
Serial.begin(9600);
}

void loop() {
char val;
if(Serial.available())
{
val=Serial.read();
if(val=='1')
digitalWrite(18,HIGH);
if(val=='0')
digitalWrite(18,LOW);
}
}
----------------------------------------------------------------------------
11. Write a program to Control LED2,LED18 using Serial Monitor(UART).

void setup() {
pinMode(18,OUTPUT); // External LED(GPIO-18)
pinMode(2,OUTPUT); // On Board LED(GPIO-2)
Serial.begin(9600);
}

void loop() {
char val;
if(Serial.available())
{
val=Serial.read();
switch(val)
{
case '1':
digitalWrite(18,HIGH);
break;
case '0':
digitalWrite(18,LOW);
break;
case '2':
digitalWrite(2,HIGH);
break;
case '3':
digitalWrite(2,LOW);
break;
}
}
}
-----------------------------------------------------------------------------------
-------
12. To interface Bluetooth with ESP32 and write a program to turn LED ON/OFF
when '1'/'0' is received from smartphone using Bluetooth.

// Load libraries
#include "BluetoothSerial.h"

// Check if Bluetooth configs are enabled


#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it
#endif

// Bluetooth Serial object


BluetoothSerial SerialBT;

// GPIO where LED is connected to


const int ledPin = 18;

void setup() {
pinMode(ledPin, OUTPUT);
Serial.begin(115200);
// Bluetooth device name
SerialBT.begin("ESP32test");
Serial.println("The device started, now you can pair it with bluetooth!");
}

void loop() {
char val;
if (SerialBT.available()){
val = SerialBT.read();
if (val =='1')
digitalWrite(ledPin,HIGH);
if (val =='0')
digitalWrite(ledPin,LOW);
}
}
-----------------------------------------------------------------------------------
-----
13. To interface Bluetooth with ESP32 and write a program to send LDR sensor
data to smartphone using Bluetooth.

// Load libraries
#include "BluetoothSerial.h"

// Check if Bluetooth configs are enabled


#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it
#endif

// Bluetooth Serial object


BluetoothSerial SerialBT;

// GPIO where the LDR is connected to


const int ldr = 36;

String ldrString = "";

// Timer: auxiliar variables


unsigned long previousMillis = 0; // Stores last time temperature was published
const long interval = 1000; // interval at which to publish sensor readings

void setup() {
Serial.begin(115200);
// Bluetooth device name
SerialBT.begin("koushik_BT");
Serial.println("The device started, now you can pair it with bluetooth!");
}

void loop() {
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval){
previousMillis = currentMillis;
ldrString = String(analogRead(36)) ;
SerialBT.println(ldrString);
}
}
-----------------------------------------------------------------------------------
------

You might also like