0% found this document useful (0 votes)
63 views91 pages

23SDEC02A-SKILL MANUAL-Faculty

Uploaded by

Sandeep Koppula
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
63 views91 pages

23SDEC02A-SKILL MANUAL-Faculty

Uploaded by

Sandeep Koppula
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 91

23SDEC02A EMBEDDED SYSTEM AUTOMATION - SKILL MANUAL

SKILL DEVELOPMENT PROJECT-2

EMBEDDED SYSTEM AUTOMATION


23SDEC02A

A.Y: 2024 –25

Semester: 2-2

DEPARTMENT OF ELECTRONICS AND


COMMUNICATION ENGINEERING
Vaddeswaram, Guntur District

1
Koneru Lakshmaiah Education Foundation
23SDEC02A EMBEDDED SYSTEM AUTOMATION - SKILL MANUAL

INDEX OF EXPERIMENTS

Page
S. No. Name of the Experiment Marks Faculty Sign
No.

1. Reading the default clock rate of ESP 32 Microcontroller.

2. Interrupt handling of ESP 32 Microcontroller.

3. Timer and Interrupt of ESP 32 Microcontroller.

4. Serial Communication between Two ESP 32.

5. I2C Device Scanning using ESP 32.

6. Testing of I2S microphone using ESP 32.

7. Finding the Default SPI pins of ESP 32.

8. Interfacing MCP 2515 CAN controller with ESP 32.

9. ADC read using LED dimmer using ESP 32.

10. Observation of Analog output from ESP 32.

11. Sine wave Generation using ESP 32.

12. Testing of EEPROM memory of ESP 32.

13. Scanning Bluetooth Devices using ESP 32.

Duplex Bluetooth Communication Between ESP 32 and


14.
Android Phone.

2
Koneru Lakshmaiah Education Foundation
23SDEC02A EMBEDDED SYSTEM AUTOMATION - SKILL MANUAL

Page
S. No. Name of the Experiment No. Marks Faculty Sign

15. Scanning of Wi-Fi Network using ESP 32.

Signal Strength Identification of Wi-Fi Network using ESP


16.
32.

17. Get and Set MAC address of ESP 32.

18. Get the IP address of ESP 32.

19. Assign Static IP address for ESP 32.

20. Change the host name of ESP 32.

21. Controlling the GPIO pins in ESP32 using Touch Inputs

3
Koneru Lakshmaiah Education Foundation
2023-24 EVEN SEMESTER SKILLING CONTINUOUS EVALUATION
S. Date Experiment Title Pre- In Lab Post Viva- Total Sign of
No. Lab Lab Voce (50M) Faculty
(5M) Logic Execu Result Anal (5M) (5M)
(10M) tion (10M) ysis
(10M) (5)
1.

2.

3.

Koneru Lakshmaiah Education Foundation


4.

5.

6.

7.

8
23SDEC02A EMBEDDED SYSTEM AUTOMATION - SKILL MANUAL

9.

10.

4
2023-24 EVEN SEMESTER SKILLING CONTINUOUS EVALUATION
S. Date Experiment Title Pre- In Lab Post Viva- Total Sign of
No. Lab Lab Voce (50M) Faculty
(5M) Logic Execu Result Anal (5M) (5M)
(10M) tion (10M) ysis
(10M) (5)
11.

12.

13.

Koneru Lakshmaiah Education Foundation


14.

15.

16.

17.

18
23SDEC02A EMBEDDED SYSTEM AUTOMATION - SKILL MANUAL

19.

20.

5
23SDEC02A EMBEDDED SYSTEM AUTOMATION - SKILL MANUAL

Session 01: Reading the default clock rate of ESP 32 Microcontroller

Date of the Session: / / Time of the Session: to

PREREQUISITE:
• General idea of clock frequency, ESP32 board
• General idea of basic circuit

PRE-LAB:
1) What is the role of the CPU clock speed in a microcontroller like the ESP32?
2) What is the default CPU clock speed of the ESP32, and how can it be configured?

OBJECTIVE:
To design and implement a serial communication port, checks the CPU clock rate & XTAL Freq &
APB bus clock.

COMPONENTS REQUIRED:
• ESP32
• Breadboard
• Jumper Wires Pack
• Micro usb cable

THEORY:
ESP32 CPU Speed (Frequency):

The ESP32 is a dual-core system with two Harvard Architecture Xtensa LX6 CPUs. All embedded
memory, external memory, and peripherals are located on the data bus and/or the instruction bus of
these CPUs.

CPU Clock:

Both CPUs can be clocked from various sources internally or externally. The most important circuitry
is the PLL (Phase-Locked Loop) which multiplies the input clock frequency (whether it’s an external
crystal or internal oscillator). This results in a much higher frequency signal that’s derived from the
main clock source (orders of magnitude in terms of frequency).

6
Koneru Lakshmaiah Education Foundation
23SDEC02A EMBEDDED SYSTEM AUTOMATION - SKILL MANUAL

The PLL_CLK is an internal PLL clock signal with a frequency of 320 MHz or 480 MHz. Note that
this PLL clock is divided /2 or /4 before it’s fed to any CPU. The table down below from the datasheet
shows the possible configurations for the clock.

Peripherals Clock:

The APB bus clock (APB_CLK) is derived from CPU_CLK, the division factor depends on the
CPU_CLK source as shown in the table down below. The peripherals clock signals are APB_CLK,
REF_TICK, LEDC_SCLK, APLL_CLK, and PLL_D2_CLK.

With that being said, we can conclude that setting the PLL_CLK as a clock source will help us choose
the 480MHz PLL clock output as a clock source for the CPU (the CPU_CLK will be 480/2 MHz).
While the APB bus will be clocked @ 80MHz due to this option being selected.

CIRCUIT:

7
Koneru Lakshmaiah Education Foundation
23SDEC02A EMBEDDED SYSTEM AUTOMATION - SKILL MANUAL

ESP32:

CODE:
#define GPIO_pin 5
uint32_t Freq = 0;
void setup()
{
pinMode(GPIO_pin, OUTPUT);
Serial.begin(115200);
Freq = getCpuFrequencyMhz();
Serial.print("CPU Freq = ");
Serial.print(Freq);
Serial.println(" MHz");
Freq = getXtalFrequencyMhz();
Serial.print("XTAL Freq = ");
Serial.print(Freq);
Serial.println(" MHz");
Freq = getApbFrequency();
Serial.print("APB Freq = ");
Serial.print(Freq);
Serial.println(" Hz");
}
void loop()
{
digitalWrite(GPIO_pin, 1);
digitalWrite(GPIO_pin, 0);

8
Koneru Lakshmaiah Education Foundation
23SDEC02A EMBEDDED SYSTEM AUTOMATION - SKILL MANUAL

}
PROCEDURE:
1. Choose the board, COM port, hold down the BOOT button, click upload and keep your
finger on the BOOT button pressed.
2. When the Arduino IDE starts sending the code, you can release the button and wait for the
flashing process to be completed.
3. Now, the ESP32 is flashed with the new firmware.

RESULT:

POST LAB:
Take the snapshot of Tinker CAD simulation and paste here with your REG NO on it.

9
Koneru Lakshmaiah Education Foundation
23SDEC02A EMBEDDED SYSTEM AUTOMATION - SKILL MANUAL

Session 02: Interrupt handling of ESP 32 Microcontroller

Date of the Session: / / Time of the Session: to

PREREQUISITE:
• General idea of Interrupts, ESP32 board
• General idea of basic circuit

PRE-LAB:

1. What is the primary purpose of external interrupts in microcontroller development, such as


the ESP32?
2. Explain the concept of an external interrupt trigger type. How does it influence the interrupt
behaviour?

OBJECTIVE:

• To Define an output pin (for the LED)


• Define an input pin & Attach interrupt to it
• Write the ISR function to toggle the LED pin on each RISING edge

COMPONENTS REQUIRED:

• ESP32
• Breadboard
• Jumper Wires Pack
• Micro USB Cable

THEORY:

Interrupt Latency:

Interrupt latency is the time it takes the CPU to respond to a specific interrupt signal. It’s a measure
for the response time of an interrupt and it’s desired to be as small as possible. Upon an interrupt signal
reception, the CPU suspends the current main code executions, saves the context, switches the context
to the ISR, executes the ISR code, switches the context back to main, and finally resumes the main
code execution.

10
Koneru Lakshmaiah Education Foundation
23SDEC02A EMBEDDED SYSTEM AUTOMATION - SKILL MANUAL

This takes some time and it’s desired to be as small as possible. The time between the hardware
interrupt signal and the start of ISR execution (interrupt latency) is what we’re going to measure in
this LAB.

CIRCUIT:

ESP32:

CODE:
#define Btn1_GPIO 35
#define LED1_GPIO 5

void IRAM_ATTR Ext_INT1_ISR()


{
// Toggle The LED
digitalWrite(LED1_GPIO, !digitalRead(LED1_GPIO));
}

11
Koneru Lakshmaiah Education Foundation
23SDEC02A EMBEDDED SYSTEM AUTOMATION - SKILL MANUAL

void setup()
{
pinMode(LED1_GPIO, OUTPUT);
pinMode(Btn1_GPIO, INPUT);
attachInterrupt(Btn1_GPIO, Ext_INT1_ISR, RISING);
}

void loop()
{
// Do Nothing...
}
PROCEDURE:

1. Choose the board, COM port, hold down the BOOT button, click upload and keep
your finger on the BOOT button pressed.

2. When the Arduino IDE starts sending the code, you can release the button and wait for
the flashing process to be completed.

3. Now, the ESP32 is flashed with the new firmware.

RESULT:

POST LAB:

Take the snapshot of Tinker CAD simulation and paste here with your REG NO on it.

12
Koneru Lakshmaiah Education Foundation
23SDEC02A EMBEDDED SYSTEM AUTOMATION - SKILL MANUAL

Session 03: Timer and Interrupt of ESP 32 Microcontroller

Date of the Session: / / Time of the Session: to

PREREQUISITE:
• General idea of Timer, ESP32 board
• General idea of basic circuit

PRE-LAB:
1. What is the primary purpose of timers in microcontroller development, such as the ESP32?
2. Explain the difference between hardware timers and software timers on the ESP32.

OBJECTIVE:
• Generate Timer Interrupt events in Arduino IDE.
COMPONENTS REQUIRED:
• ESP32
• breadboard
• Jumper Wires Pack
• Micro USB Cable

THEORY:

ESP32 Timers & Timer Interrupts (Arduino IDE):


In this tutorial, you’ll learn how to use ESP32 internal Timers & generate Timer Interrupt events in
Arduino IDE. We’ll discuss how ESP32 Timers work, how to configure ESP32’s Timers, and how to
generate periodic interrupts to synchronize the execution of logic within your project. And also
measure the timer between two events whether they’re external or internal events.

ESP32 Timers:

The ESP32 SoCs come with 4 hardware timers, each of which is a general-purpose 64-bit up/down
counter with a 16-bit prescaler. Except for ESP32-C3 which has only 2 timers each of which is 54 bits
instead. The ESP32 timers have the capability of auto-reloading at the end of the counting period as
well.

14
23SDEC02A EMBEDDED SYSTEM AUTOMATION - SKILL MANUAL

ESP32 Timers Functional Description:

Each ESP32 timer uses the APB clock (APB_CLK which is normally 80 MHz in frequency) as a base
clock. This clock is then scaled downby a 16-bit prescaler which generates the time-base tick time.
Therefore, we’ll be changing the value of the prescaler in order to control the timer tick time.

The 16-Bit prescaler can divide the APB_CLK by a factor from 2 to 65536. When you set the prescaler
value to be either 1 or 2, the clock divisor is 2; when you set the prescaler to 0, the clock divisor is
65536. Any other value will cause the clock to be divided by exactly that value that you’ve written to
the prescaler register.

ESP32 Timers Alarm Generation:

ESP32 timers can trigger an alarm (Event) which will cause a timer to reload and/or interrupt to occur,
depending on your configuration. The alarm event is triggered when the value you’ve stored in the
alarm register matches the current timer value. This is very useful to set periodic interrupts to execute
some pieces of logic periodically in your project as we’ll be doing hereafter.

ESP32 Timers Equation:

In order to generate periodic events with ESP32, we’ll be using the alarm event generation as well as
the timer’s prescaler in order to achieve the desired interrupt periodicity. There are 3 special cases for
the Timer equation down below, which are when the prescaler value is = 0,1, and 2. When
Prescaler=1or2, it’s going to be as follows [ TOUT = TimerTicks x (2/APB_CLK) ]. When
Prescaler=0, it’s going to be as follows [ TOUT = TimerTicks x (65536/APB_CLK) ]. Otherwise, we
can generally use the equation down below.

ESP32 Timer Example (Arduino):

Let’s say we’d like to toggle an LED every 1 ms without using a delay that blocks the CPU and does
much harm to the overall timing performance of your system. For this, we’ll use the timer’s equation
above, Given that the default APB_CLK is 80MHz or 80,000,000Hz. The desired TOUT for the
interrupt period in which we’ll toggle the LED is 1ms, So TOUT = 1ms or 0.001s. The only two
unknowns now are the Prescaler value and the TimerTicks count which we’ll set the alarm event to.
15
23SDEC02A EMBEDDED SYSTEM AUTOMATION - SKILL MANUAL

We can set the Prescaler to whichever value we want but for the sake of simplifying the calculations,
let’s set the Prescaler=80. This will leave us with only one unknown which is the TimerTicks count.
So, we’ll solve the equation for TimerTicks:

CIRCUIT:

ESP32:

CODE:

#define LED 21
hw_timer_t *Timer0_Cfg = NULL;

void IRAM_ATTR Timer0_ISR()


{
digitalWrite(LED, !digitalRead(LED));
}
16
23SDEC02A EMBEDDED SYSTEM AUTOMATION - SKILL MANUAL

void setup()
{
pinMode(LED, OUTPUT);
Timer0_Cfg = timerBegin(0, 80, true);
timerAttachInterrupt(Timer0_Cfg, &Timer0_ISR, true);
timerAlarmWrite(Timer0_Cfg, 50000, true);
timerAlarmEnable(Timer0_Cfg);
}
void loop()
{
// Do Nothing!
}

PROCEDURE:

1. Set Timer0 to generate a periodic interrupt each 50ms


2. In Timer0_IRS() which runs every 50ms, do a LED toggle
3. Keep repeating…

OUTPUT:

POST LAB:

Take the snapshot of Tinker CAD simulation and paste here with your REG NO on it.

17
23SDEC02A EMBEDDED SYSTEM AUTOMATION - SKILL MANUAL

Session 04: Serial Communication between Two ESP 32

Date of the Session: / / Time of the Session: to

PREREQUISITE:
• General idea of ESP32 board
• General idea of UART

PRE-LAB:
1. Identify the pins commonly used for UART communication on the ESP32.
2. What is the purpose of the TXD and RXD pins in UART?
3. Give the significance of the baud rate in UART communication.
4. Define the terms "data bits," "stop bits," and "parity" in the context of UART.
5. What is flow control in UART, and why is it necessary?

OBJECTIVE:

To perform UART or serial communication between two ESP32 boards using UART
hardware library of Arduino IDE

REQUIRED COMPONENTS:

• Two ESP32 development boards


• One 5mm LED
• One 220-ohm resistor
• Connecting Wires
• Breadboard

CIRCUIT:

• Connect TX2 pin of master ESP32 board with RX2 pin of slave ESP32 board. Likewise,
connect RX2 pin of master ESP32 board with TX2 pin of slave ESP32 board.

• Anode pin of LED is connected with digital pin15 (slave) through a 220-ohm current
limiting resistor. The cathode pin is grounded.

18
Koneru Lakshmaiah Education Foundation
23SDEC02A EMBEDDED SYSTEM AUTOMATION - SKILL MANUAL

• Also make sure both ESP32 boards have their grounds in common.

Master ESP32 Arduino Sketch


Open your Arduino IDE and go to File > New to open a new file. Copy the code given below
and save it.

CODE:
#include <HardwareSerial.h>

19
Koneru Lakshmaiah Education Foundation
23SDEC02A EMBEDDED SYSTEM AUTOMATION - SKILL MANUAL

HardwareSerial SerialPort(2); // use UART2

void setup()
{
SerialPort.begin(15200, SERIAL_8N1, 16, 17);
}
void loop()
{
SerialPort.print(1);
delay(5000);
SerialPort.print(0);
delay(5000);
}

How the Code Works?

Now let’s see how above code works.


First include the hardware serial library using #include files. This line includes the
HardwareSerial.h library.
#include <HardwareSerial.h>
A serial object of the HardwareSerial library, named as “SerialPort” is declared here. We
specify the UART port number as a parameter inside it. We are using UART2 in this case.

HardwareSerial SerialPort(2); // use UART2


Inside the setup() function, we will open the serial communication of port UART2 using
SerialPort.begin (BaudRate, SerialMode, RX_pin, TX_pin).

void setup()
{
SerialPort.begin(15200, SERIAL_8N1, 16, 17);
}

Here the master will send the number 1 and 0 to the slave after every 5 seconds.

void loop()
{
SerialPort.print(1);

20
Koneru Lakshmaiah Education Foundation
23SDEC02A EMBEDDED SYSTEM AUTOMATION - SKILL MANUAL

delay(5000);
SerialPort.print(0);
delay(5000);
}

Slave ESP32 Arduino Sketch


Open your Arduino IDE and go to File > New to open a new file. Copy the code given below
and save it.
#include <HardwareSerial.h>

HardwareSerial SerialPort(2); // use UART2

char number = ' ';


int LED = 15;

void setup()
{
SerialPort.begin(15200, SERIAL_8N1, 16, 17);
pinMode(LED, OUTPUT);
}
void loop()
{
if (SerialPort.available())
{
char number = SerialPort.read();
if (number == '0') {
digitalWrite(LED, LOW);
}
if (number == '1') {
digitalWrite(LED, HIGH);
}
}
}
How the Code Works?

First include the hardware serial library.

#include <HardwareSerial.h>

21
Koneru Lakshmaiah Education Foundation
23SDEC02A EMBEDDED SYSTEM AUTOMATION - SKILL MANUAL

A serial object of the HardwareSerial library, named as “SerialPort” is declared here. We


specify the UART port number as a parameter inside it. We are using UART2 in this case.

HardwareSerial SerialPort(2); // use UART2

A char variable called ‘number’ stores the data that the slave will receive from the master.

char number = ' ';

The LED is connected with the slave ESP32 GPIO15.

int LED = 15;

Inside the setup() function, we will open the serial communication of port UART2 using
SerialPort.begin (BaudRate, SerialMode, RX_pin, TX_pin). Also, set the LED pin as an output
pin using the pinMode() function. Specify the pin as the first parameter and the mode as the
second parameter.

One important point to note here is that the baud rate of both ESP32 master and slave boards
should be the same.

void setup()
{
SerialPort.begin(15200, SERIAL_8N1, 16, 17);
pinMode(LED, OUTPUT);
}

Inside the loop() function, we will check if data is present in the buffer. If some data is
available, it is stored in variable ‘number.’ If the number is ‘0’, then turn the LED will be
turned OFF. If the number is ‘1’, then the LED will be turned ON.

The master will send ‘1’ and ‘0’ with a delay of 5 seconds continuously to the slave. Hence,
the LED will stay ON for 5 seconds and then OFF for 5 seconds.

void loop()
{
if (SerialPort.available())
{

22
Koneru Lakshmaiah Education Foundation
23SDEC02A EMBEDDED SYSTEM AUTOMATION - SKILL MANUAL

char number = SerialPort.read();


if (number == '0') {
digitalWrite(LED, LOW);
}
if (number == '1') {
digitalWrite(LED, HIGH);
}
}
}

Hardware Demonstration

To see the demonstration, upload the master and sender codes to the two ESP32 boards. But,
before uploading code, make sure to select the ESP32 Dev Module from Tools > Board and
also select the correct COM port to which the board is connected from Tools > Port.

23
Koneru Lakshmaiah Education Foundation
23SDEC02A EMBEDDED SYSTEM AUTOMATION - SKILL MANUAL

The LED will turn ON and stay ON for 5 seconds before turning OFF for 5 seconds. This
continues as the master keeps on sending 1/0 to the slave.

OUTPUT:

RESULT:

24
Koneru Lakshmaiah Education Foundation
23SDEC02A EMBEDDED SYSTEM AUTOMATION - SKILL MANUAL

Session 05: I2C Device Scanning using ESP 32

Date of the Session: / / Time of the Session: to

PREREQUISITE:
• General idea of ESP32 board
• General idea of I2C

PRE-LAB:

1. Identify the pins on the ESP32 commonly used for I2C communication.
2. What is the purpose of the SDA and SCL pins in the I2C interface?
3. What is the purpose of I2C device scanning?
4. How does the ESP32 discover and identify I2C devices on the bus?
5. Describe the role of the Wire library in Arduino IDE for I2C communication.

OBJECTIVE:

To perform I2C Device Scanning using ESP 32

REQUIRED COMPONENTS:

• ESP32 development board


• MPU6050 3-axis accelerometer
• I2C LCD interface module
• Connecting Wires
• Breadboard

CONNECTIONS:

25
Koneru Lakshmaiah Education Foundation
23SDEC02A EMBEDDED SYSTEM AUTOMATION - SKILL MANUAL

CODE:
#include <Wire.h>

#define I2C_Freq 100000


#define SDA_0 18
#define SCL_0 19

TwoWire I2C_0 = TwoWire(0);

void setup()
{
Serial.begin(115200);
I2C_0.begin(SDA_0 , SCL_0 , I2C_Freq);
}

void loop()
{
byte error, address;
int nDevices;
Serial.println("Scanning...");
nDevices = 0;
for(address = 1; address < 127; address++ )
{
// The i2c_scanner uses the return value of
// the Write.endTransmisstion to see if
// a device did acknowledge to the address.
I2C_0.beginTransmission(address);
error = I2C_0.endTransmission();
if (error == 0)
{
Serial.print("I2C device found at address 0x");
if (address<16)
Serial.print("0");
Serial.print(address,HEX);
Serial.println(" !");
nDevices++;
}
else if (error==4)
{
Serial.print("Unknown error at address 0x");
if (address<16)

26
Koneru Lakshmaiah Education Foundation
23SDEC02A EMBEDDED SYSTEM AUTOMATION - SKILL MANUAL

Serial.print("0");
Serial.println(address,HEX);
}
}
if (nDevices == 0)
Serial.println("No I2C devices found\n");
else
Serial.println("done\n");
delay(5000); // wait 5 seconds for next scan
}
Test #1 Setup & Results

For the first test, I’ve connected an MPU6050 IMU sensor to the SCL & SDA lines of I2C1
as you can see in the image down below. The I2C address for this slave device is 0x68 as
stated in the datasheet.
We can change only one bit of that address so there could be 2 MPU6050 sensors on the same
bus at maximum. Anyway, we should expect to see that address after running the example on
the “Tera Term” serial terminal on my PC.

OUTPUT:

27
Koneru Lakshmaiah Education Foundation
23SDEC02A EMBEDDED SYSTEM AUTOMATION - SKILL MANUAL

RESULT:

Test #2 Setup & Results

In the second test, I’ve just added another I2C module to the bus lines which is the
I2C_LCD interface IO expander (PCF8574T). This module has an I2C slave address of
0x27 as a default address if no solder bridge on the module’s board is touched at all.

You can still play around with those bridges to change the device address to allow many
I2C_LCD devices on the same bus

FINAL RESULT:

28
Koneru Lakshmaiah Education Foundation
23SDEC02A EMBEDDED SYSTEM AUTOMATION - SKILL MANUAL

Session 06: Testing of I2S microphone using ESP 32

Date of the Session: / / Time of the Session: to

PREREQUISITE:
• General idea of ESP32 board
• General idea of I2S peripherals

PRE-LAB:

1. What is I2S protocol and where it is used?


2. Identify the pins on the ESP32 commonly used for I2S microphone modules
3. What is the purpose of SD, SCK and WS connection pins in the I2S microphone
module?
4. How does the ESP32 discover and identify I2C devices on the bus?
5. Describe the role of the I2S library in Arduino IDE for I2S microphone.

OBJECTIVE:

To display the audio waveforms from the microphone using the Serial Plotter through
ESP32 in Arduino IDE.

REQUIRED COMPONENTS:

• ESP32 development board


• INMP441 Microphone Module
• Connecting Wires
• Breadboard

CONNECTIONS:

29
Koneru Lakshmaiah Education Foundation
23SDEC02A EMBEDDED SYSTEM AUTOMATION - SKILL MANUAL

CODE:
// Include I2S driver
#include <driver/i2s.h>

// Connections to INMP441 I2S microphone


#define I2S_WS 25
#define I2S_SD 33
#define I2S_SCK 32

// Use I2S Processor 0


#define I2S_PORT I2S_NUM_0

// Define input buffer length


#define bufferLen 64
int16_t sBuffer[bufferLen];

void i2s_install() {
// Set up I2S Processor configuration
const i2s_config_t i2s_config = {
.mode = i2s_mode_t(I2S_MODE_MASTER | I2S_MODE_RX),
.sample_rate = 44100,
.bits_per_sample = i2s_bits_per_sample_t(16),
.channel_format = I2S_CHANNEL_FMT_ONLY_LEFT,
.communication_format =
i2s_comm_format_t(I2S_COMM_FORMAT_STAND_I2S),
.intr_alloc_flags = 0,
.dma_buf_count = 8,

30
Koneru Lakshmaiah Education Foundation
23SDEC02A EMBEDDED SYSTEM AUTOMATION - SKILL MANUAL

.dma_buf_len = bufferLen,
.use_apll = false
};

i2s_driver_install(I2S_PORT, &i2s_config, 0, NULL);


}

void i2s_setpin() {
// Set I2S pin configuration
const i2s_pin_config_t pin_config = {
.bck_io_num = I2S_SCK,
.ws_io_num = I2S_WS,
.data_out_num = -1,
.data_in_num = I2S_SD
};

i2s_set_pin(I2S_PORT, &pin_config);
}

void setup() {

// Set up Serial Monitor


Serial.begin(115200);
Serial.println(" ");

delay(1000);

// Set up I2S
i2s_install();
i2s_setpin();
i2s_start(I2S_PORT);
delay(500);
}

void loop() {

// False print statements to "lock range" on serial plotter display


// Change rangelimit value to adjust "sensitivity"
int rangelimit = 3000;
Serial.print(rangelimit * -1);
Serial.print(" ");

31
Koneru Lakshmaiah Education Foundation
23SDEC02A EMBEDDED SYSTEM AUTOMATION - SKILL MANUAL

Serial.print(rangelimit);
Serial.print(" ");

// Get I2S data and place in data buffer


size_t bytesIn = 0;
esp_err_t result = i2s_read(I2S_PORT, &sBuffer, bufferLen, &bytesIn,
portMAX_DELAY);

if (result == ESP_OK)
{
// Read I2S data buffer
int16_t samples_read = bytesIn / 8;
if (samples_read > 0) {
float mean = 0;
for (int16_t i = 0; i < samples_read; ++i) {
mean += (sBuffer[i]);
}

// Average the data reading


mean /= samples_read;

// Print to serial plotter


Serial.println(mean);
}
}
}
How the Code works:

We start by including the ESP32 I2S driver.


We then define the connections to our microphone. If you wish, you can rewire the microphone
and change the code here.
The ESP32 has two internal I2S processors. We will be using the first one, I2S Port 0. We also
define the length of an input data buffer.
Next, we have a function called i2s_install, which sets up the I2S port parameters.
A second function, i2s_setpin, sets up the physical connection to the I2S device, which in our
case is the microphone module.

32
Koneru Lakshmaiah Education Foundation
23SDEC02A EMBEDDED SYSTEM AUTOMATION - SKILL MANUAL

In the Setup, we set up our serial connection, as we will be using the Serial Plotter to display
our audio waveforms. We then cal our two functions to set up the I2S port, and then start it
with a third built-in function.
Our Loop starts with a “false” print statement, this just causes two constants to be printed to
steady the reading on the Serial Plotter, which otherwise will dynamically change its Y-axis
scale.
We then read data from the module and place it in our data buffer. If the data is good, we read
it out and display it on the Serial Plotter

OUTPUT:

RESULT:

33
Koneru Lakshmaiah Education Foundation
23SDEC02A EMBEDDED SYSTEM AUTOMATION - SKILL MANUAL

Session 07: Finding the Default SPI pins of ESP 32

Date of the Session: / / Time of the Session: to

PREREQUISITE:
• General idea of ESP32 board
• General idea of SPI Pins, Multiple SPI Bus Interfaces, and Peripherals (Arduino IDE)

PRE-LAB:

1. What is the role of SPI in the ESP32 and how is it utilized within the
microcontroller?
2. How do SPI pins (CLK, MOSI, MISO, CS) facilitate communication with
peripheral devices?
3. What is the default GPIO pin assignments for the SPI interface (CLK, MOSI, MISO,
CS) on the ESP32?
4. Can the default SPI pins on the ESP32 be reconfigured or changed?

OBJECTIVE:

To perform SPI communication using ESP 32

REQUIRED COMPONENTS:

• ESP32 development board


• Connecting Wires
• Breadboard

THEORY:

SPI stands for Serial Peripheral Interface, and it is a synchronous serial data protocol used by
microcontrollers to communicate with one or more peripherals. For example, your ESP32
board communicating with a sensor that supports SPI or with another microcontroller.
In an SPI communication, there is always a controller (also called master) that controls
the peripheral devices (also called slaves). Data can be sent and received simultaneously. This

34
Koneru Lakshmaiah Education Foundation
23SDEC02A EMBEDDED SYSTEM AUTOMATION - SKILL MANUAL

means that the master can send data to a slave, and a slave can send data to the master at the
same time.

You can have only one master, which will be a microcontroller (the ESP32), but you can have
multiple slaves. A slave can be a sensor, a display, a microSD card, etc., or another
microcontroller. This means you can have an ESP32 connected to multiple sensors, but the
same sensor can’t be connected to multiple ESP32 boards simultaneously.

SPI Interface:

For SPI communication you need four lines:

• MISO: Master In Slave Out


• MOSI: Master Out Slave In
• SCK: Serial Clock
• CS /SS: Chip Select (used to select the device when multiple peripherals are used on
the same SPI bus)
On a slave-only device, like sensors, displays, and others, you may find a different
terminology:

• MISO may be labelled as SDO (Serial Data Out)


• MOSI may be labelled as SDI (Serial Data In)

ESP32 SPI Peripherals:

The ESP32 integrates 4 SPI peripherals: SPI0, SPI1, SPI2 (commonly referred to as HSPI),
and SPI3 (commonly referred to as VSPI).
SP0 and SP1 are used internally to communicate with the built-in flash memory, and you
should not use them for other tasks.

35
Koneru Lakshmaiah Education Foundation
23SDEC02A EMBEDDED SYSTEM AUTOMATION - SKILL MANUAL

You can use HSPI and VSPI to communicate with other devices. HSPI and VSPI have
independent bus signals, and each bus can drive up to three SPI slaves.

ESP32 Default SPI Pins:

CODE:

//Find the default SPI pins for your board


//Make sure you have the right board selected in Tools > Boards
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
Serial.print("MOSI: ");
Serial.println(MOSI);
Serial.print("MISO: ");
Serial.println(MISO);
Serial.print("SCK: ");
Serial.println(SCK);
Serial.print("SS: ");
Serial.println(SS);
}

void loop() {
// put your main code here, to run repeatedly:
}

Note: make sure you select the board you’re using in Tools > Board, otherwise, you may not
get the right pins.

After uploading the code, open the Serial Monitor, RST your board and you’ll see the SPI
pins.

36
Koneru Lakshmaiah Education Foundation
23SDEC02A EMBEDDED SYSTEM AUTOMATION - SKILL MANUAL

OUTPUT:

RESULT:

37
Koneru Lakshmaiah Education Foundation
23SDEC02A EMBEDDED SYSTEM AUTOMATION - SKILL MANUAL

Session 8: Interfacing MCP 2515 CAN controller with ESP 32

Date of the Session: / / Time of the Session: to

PREREQUISITE:

• General idea of ESP32 board


• General idea of CAN Bus communication with MCP2515 module

PRE-LAB:

1. What is CAN (Controller Area Network) Bus communication, and what makes it
suitable for use in various applications?
2. How does CAN Bus differ from other communication protocols like SPI or I2C?
3. What are the primary functions of the ESP32 and MCP2515 within the CAN Bus
communication setup?
4. How does the MCP2515 interface with the ESP32 for CAN Bus communication?

OBJECTIVE:

To interface CAN controller MCP2515 module with ESP 32

REQUIRED COMPONENTS:

• ESP32
• MCP2515 module
• Connecting wires

38
Koneru Lakshmaiah Education Foundation
23SDEC02A EMBEDDED SYSTEM AUTOMATION - SKILL MANUAL

CIRCUIT:

CODE:

#include <OneWire.h>
#include <DallasTemperature.h>
#include <BluetoothSerial.h>
#include <SPI.h>
#include "mcp_can.h"
//------CAN BUS setting ---------------------------------------------------------
#define CAN0_INT 21 // Set INT to pin 50
const int SPI_CS_PIN = 5;
MCP_CAN CAN0(SPI_CS_PIN);
//----- DS18B20 ------------------
#define DQ_Pin 4

OneWire oneWire(DQ_Pin);
DallasTemperature sensors(&oneWire);

byte data[12]; // buffer for data


byte address[8]; // 64 bit device address

#define LED_BUILTIN 2
BluetoothSerial SerialBT;

39
Koneru Lakshmaiah Education Foundation
23SDEC02A EMBEDDED SYSTEM AUTOMATION - SKILL MANUAL

//--------- Flag structure --------------------------------------


typedef struct _vFlag
{
uint8_t BTFlag = 0;
uint8_t DC_Flag = 0;
uint8_t CANFlag = 0;
uint8_t I2C_Flag = 0;
uint8_t BMP180Flag = 0;
uint8_t DS18B20Flag = 0;
uint8_t JSONFlag = 0;
uint8_t LEDFlag = 1;
uint8_t sensor_Flag = 0;
uint8_t sensor1_Flag = 0;
uint8_t initial_Flag = 0;
uint8_t Tone_Flag = -1;
uint8_t IR_RECV_Flag=0;
uint8_t IR_SEND_Flag=0;
uint8_t FunctionFlag = 3;
uint8_t SendFlag = 0;
uint8_t BMPCnt = 0;
} vFlag;
vFlag *flag_Ptr;
vFlag flag;
//----------uart--------------
#define LINE_BUFFER_LENGTH 64
//--------- uart structure --------------------------------------
typedef struct _vUart
{
char c;
int lineIndex = 0;
int line1Index = 0;
int BTlineIndex = 0;
bool lineIsComment;
bool lineSemiColon;
//char *line;
char line[128];
//char line1[128];
char BTline[20];
//char R_line[20];
//char L_line[20];
String inputString;

40
Koneru Lakshmaiah Education Foundation
23SDEC02A EMBEDDED SYSTEM AUTOMATION - SKILL MANUAL

String BTinputString;
String S1inputString;
int V[16];
char ctemp[30];
char I2C_Data[80];
int DC_Spped = 50;
float Voltage[16];
int Buffer[128];
int StartCnt = 0;
int ReadCnt = 0;
int sensorValue = 0;
} vUart;
vUart *Uart_Ptr;
vUart Uart;
//-------------CAN-------------------------------
typedef struct _vCAN_t
{
long unsigned int rxId;
long unsigned int txId;
unsigned char rxlen = 8;
unsigned char txlen = 8;
unsigned char rxext = 0;
unsigned char txext = 0;
unsigned char rxBuf[20];
byte data[8];
unsigned char masknum = 0;
unsigned char maskext = 0;
long unsigned int maskId;
unsigned char filternum = 0;
unsigned char filterext = 0;
long unsigned int filterId;
} vCAN_t;

vCAN_t *can_Ptr;
vCAN_t can;

//-------------------------------------------------
void setup()
{
Serial.begin(9600);
Serial.println(F("init"));

41
Koneru Lakshmaiah Education Foundation
23SDEC02A EMBEDDED SYSTEM AUTOMATION - SKILL MANUAL

pinMode(LED_BUILTIN, OUTPUT);
SerialBT.begin("BT_BS18B20");// BTName

if (oneWire.search(address))
{
Serial.println("Slave device found!");
Serial.print("Device Address = ");
Serial.println(address[0]);
}
else
{
Serial.println("Slave device not found!");
}
//-----DS-----------
sensors.begin();
//standard frame
can.txId = 0x7D1;
can.txext = 0;
can.txlen = 8;
can.data[0] = 0x8E;
can.data[1] = 0x00;
can.data[2] = 0x00;
can.data[3] = 0x00;
can.data[4] = 0x00;
can.data[5] = 0x00;
can.data[6] = 0x00;
can.data[7] = 0x00;

}
//-----------------------------------------
void loop()
{
Serial.print(F("Main at core:"));
Serial.println(xPortGetCoreID());
while(1)
{
if(flag.LEDFlag == 1)
{
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
vTaskDelay(300);

42
Koneru Lakshmaiah Education Foundation
23SDEC02A EMBEDDED SYSTEM AUTOMATION - SKILL MANUAL

digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage


LOW
vTaskDelay(300);
}
while (Serial.available() > 0)
{
Uart.c = Serial.read();

if ((Uart.c == '\n') || (Uart.c == '\r'))


{ // End of line reached
if (Uart.lineIndex > 0)
{ // Line is complete. Then execute!
Uart.line[Uart.lineIndex] = '\0'; // Terminate string

processCommand(Uart.line); // do something with the command


SerialBT.println(Uart.line);

Uart.lineIndex = 0;
Uart.inputString = "";
}
else
{
// Empty or comment line. Skip block.
}
Uart.lineIsComment = false;
Uart.lineSemiColon = false;
Serial.println(F("ok>"));
}
else
{
//Serial.println( c );
if ((Uart.lineIsComment) || (Uart.lineSemiColon))
{
if (Uart.c == ')')
Uart.lineIsComment = false; // End of comment. Resume line.
}
else
{
if (Uart.c == '/')
{ // Block delete not supported. Ignore character.
}

43
Koneru Lakshmaiah Education Foundation
23SDEC02A EMBEDDED SYSTEM AUTOMATION - SKILL MANUAL

else if (Uart.c == '~')


{ // Enable comments flag and ignore all characters until ')' or EOL.
Uart.lineIsComment = true;
}
else if (Uart.c == ';')
{
Uart.lineSemiColon = true;
}
else if (Uart.lineIndex >= LINE_BUFFER_LENGTH - 1)
{
Serial.println("ERROR - lineBuffer overflow");
Uart.lineIsComment = false;
Uart.lineSemiColon = false;
}
else if (Uart.c >= 'a' && Uart.c <= 'z')
{ // Upcase lowercase
Uart.line[Uart.lineIndex] = Uart.c - 'a' + 'A';
Uart.lineIndex = Uart.lineIndex + 1;
Uart.inputString += (char)(Uart.c - 'a' + 'A');
}
else
{
Uart.line[Uart.lineIndex] = Uart.c;
Uart.lineIndex = Uart.lineIndex + 1;
Uart.inputString += Uart.c;
}
}
}
} //while (Serial.available() > 0)
while (SerialBT.available())
{
String BTdata = SerialBT.readString();

Serial.println(BTdata);
BTprocessCommand(BTdata);
}//while (BT.available())

if(flag.DS18B20Flag == 1)
{
vDS18B20Task();
sensors.requestTemperatures();

44
Koneru Lakshmaiah Education Foundation
23SDEC02A EMBEDDED SYSTEM AUTOMATION - SKILL MANUAL

SerialBT.print("E");
SerialBT.println(sensors.getTempCByIndex(0));
}

if (flag.CANFlag == 1)
{
Serial.print("Temperatures --> ");
sensors.requestTemperatures();
Serial.println(sensors.getTempCByIndex(0));
//--------------------------------
float f=sensors.getTempCByIndex(0);
String mystring;
mystring = String(f);
byte float_data[mystring.length()+1];
mystring.getBytes(float_data, mystring.length()+1);
//---------------------------------------------
int size = sizeof(float_data);
for(int i=0;i<size;i++)
{
can.data[i] =float_data[i];
}

flag.CANFlag = 2;
flag.LEDFlag = 0;
delay(50);
byte sndStat = CAN0.sendMsgBuf(can.txId, can.txext, can.txlen, can.data);
if (sndStat == CAN_OK)
Serial.println("CAN Message Sent Successfully!");
else
Serial.println("Error Sending CAN Message...");
flag.CANFlag = 1;
}
/**
else
{
Serial.println("Open CAN First...");
}**/

}
}
//-------------------------------------

45
Koneru Lakshmaiah Education Foundation
23SDEC02A EMBEDDED SYSTEM AUTOMATION - SKILL MANUAL

void BTprocessCommand(String data)


{

}
//----------------------------------------
void processCommand(char *data)
{
int len, xlen, ylen, zlen, alen;
int tempDIO;
String stemp;

len = Uart.inputString.length();
//---------------------------------------
if (strstr(data, "VER") != NULL)
{
Serial.println(F("ESP32_20230727"));
Serial.println(F("CAN_DS18B20"));
}
if (strstr(data, "DS18B20_ON") != NULL)
{
flag.DS18B20Flag = 1;
flag.LEDFlag=0;
Serial.println(F("DS18B20_ON"));

}
if (strstr(data, "DS18B20_OFF") != NULL)
{
flag.DS18B20Flag = 0;
flag.LEDFlag=1;
Serial.println(F("DS18B20_OFF"));
}
///----------CAN function ----------------
if (strstr(data, "CAN_ON_125") != NULL)
{
if (flag.CANFlag == 0)
{
if (CAN_OK != CAN0.begin(MCP_NORMAL, CAN_125KBPS, MCP_8MHZ))
{
Serial.println("CAN BUS Shield init fail");
Serial.println("Init CAN BUS Shield again");
flag.CANFlag = 0;

46
Koneru Lakshmaiah Education Foundation
23SDEC02A EMBEDDED SYSTEM AUTOMATION - SKILL MANUAL

}
else
{
Serial.println("CAN BUS Shield init ok!");
CAN0.setMode(MCP_NORMAL); // Set operation mode to normal so the
MCP2515 sends acks to received data.
pinMode(CAN0_INT, INPUT); // Configuring pin for /INT input
flag.CANFlag = 1;
Serial.println("init ok");
}
}
else if (flag.CANFlag == 2)
{
flag.CANFlag = 1;
Serial.println("CAN BUS message ON!");
}
}
if (strstr(data, "CAN_ON_250") != NULL)
{
if (flag.CANFlag == 0)
{
if (CAN_OK != CAN0.begin(MCP_NORMAL, CAN_250KBPS, MCP_8MHZ))
{
Serial.println("CAN BUS Shield init fail");
Serial.println("Init CAN BUS Shield again");
flag.CANFlag = 0;
}
else
{
Serial.println("CAN BUS Shield init ok!");
CAN0.setMode(MCP_NORMAL); // Set operation mode to normal so the
MCP2515 sends acks to received data.
pinMode(CAN0_INT, INPUT); // Configuring pin for /INT input
flag.CANFlag = 1;
Serial.println("init ok");
}
}
else if (flag.CANFlag == 2)
{
flag.CANFlag = 1;
Serial.println("CAN BUS message ON!");

47
Koneru Lakshmaiah Education Foundation
23SDEC02A EMBEDDED SYSTEM AUTOMATION - SKILL MANUAL

}
}
if (strstr(data, "CAN_ON_500") != NULL)
{
if (flag.CANFlag == 0)
{
if (CAN_OK != CAN0.begin(MCP_NORMAL, CAN_500KBPS, MCP_8MHZ))
{
Serial.println("CAN BUS Shield init fail");
Serial.println("Init CAN BUS Shield again");
flag.CANFlag = 0;
}
else
{
Serial.println("CAN BUS Shield init ok!");
CAN0.setMode(MCP_NORMAL); // Set operation mode to normal so the
MCP2515 sends acks to received data.
pinMode(CAN0_INT, INPUT); // Configuring pin for /INT input
flag.CANFlag = 1;
Serial.println("init ok");
}
}
else if (flag.CANFlag == 2)
{
flag.CANFlag = 1;
Serial.println("CAN BUS message ON!");
}
}
if (strstr(data, "CAN_OFF") != NULL)
{
flag.CANFlag = 2;
flag.LEDFlag = 1;
Serial.println("CAN BUS message OFF!");
}
}
//-----------------------------------------

//-------------------------------------------
void vDS18B20Task()
{
Serial.print("Temperatures --> ");

48
Koneru Lakshmaiah Education Foundation
23SDEC02A EMBEDDED SYSTEM AUTOMATION - SKILL MANUAL

sensors.requestTemperatures();
Serial.println(sensors.getTempCByIndex(0));
}

RESULT:

49
Koneru Lakshmaiah Education Foundation
23SDEC02A EMBEDDED SYSTEM AUTOMATION - SKILL MANUAL

Session 9: ADC read using LED dimmer using ESP 32

Date of the Session: / / Time of the Session: to

PREREQUISITE:

• General idea of ESP32 board


• General idea of ESP32 Analog Input Read Potentiometer

PRE-LAB:

1. What is ADC, and how does it convert an analog signal to a digital value in
microcontrollers like the ESP32?
2. What are the characteristics and capabilities of the ADC in the ESP32
microcontroller?
3. How can Pulse Width Modulation (PWM) be used to control the brightness of an
LED?
4. How would you physically connect an LED to the ESP32 to implement LED
dimming using PWM?
5. How can the analog values read from the ADC be mapped to the PWM range for
controlling LED brightness?

OBJECTIVE:

To create an LED dimmer using an ESP32 and a potentiometer to control the LED
brightness.

REQUIRED COMPONENTS:

• ESP32 development board


• Potentiometer
• LED
• 1 kΩ Resistor
• Breadboard

50
Koneru Lakshmaiah Education Foundation
23SDEC02A EMBEDDED SYSTEM AUTOMATION - SKILL MANUAL

CIRCUIT:

ESP32 Analog Input Read Potentiometer (LED Dimmer Code Example):

We start with defining and attaching The PWM GPIO pin. The pin I’ll be using a PWM
pin is GPIO5 in this example.
Then, we’ll be configuring the PWM Channel’s frequency (1kHz) & resolution (12-Bits
to be similar to the ADC_Result). And in the main loop() function, we’ll read the ESP32
ADC analog input (potentiometer – GPIO35 pin), and write the result to the PWM duty
cycle.

CODE:
#define LED_GPIO 5
#define PWM1_Ch 0
#define PWM1_Res 12
#define PWM1_Freq 1000
#define AN_Pot1 35

int AN_Pot1_Result = 0;

void setup()
{
ledcAttachPin(LED_GPIO, PWM1_Ch);
ledcSetup(PWM1_Ch, PWM1_Freq, PWM1_Res);
}

51
Koneru Lakshmaiah Education Foundation
23SDEC02A EMBEDDED SYSTEM AUTOMATION - SKILL MANUAL

void loop()
{
AN_Pot1_Result = analogRead(AN_Pot1);
ledcWrite(PWM1_Ch, AN_Pot1_Result);
delay(1);
}

OUTPUT:

RESULT:

52
Koneru Lakshmaiah Education Foundation
23SDEC02A EMBEDDED SYSTEM AUTOMATION - SKILL MANUAL

Session 10: Observation of Analog output from ESP 32

Date of the Session: / / Time of the Session: to

PREREQUISITE:

• General idea of ESP32 board


• General idea of ESP32 DAC Direct Voltage Output Mode

PRE-LAB:

1. How does Pulse Width Modulation (PWM) in the ESP32 simulate analog output?
2. What factors influence the resolution and accuracy of analog-like output using PWM
on the ESP32?
3. If using PWM to control an LED's brightness, how does adjusting the PWM duty
cycle impact the perceived brightness of the LED?
4. How would you observe and measure the analog-like output generated by the ESP32,
such as using an oscilloscope or a multimeter?
5. What aspects of the signal (frequency, duty cycle) would you examine during
observation?

OBJECTIVE:

To generate constant analog output voltage using ESP 32 DAC

REQUIRED COMPONENTS:

• ESP32 development board


• Connecting Wires
• Breadboard
• Voltmeter

THEORY:

ESP32 Analog Output DC Voltage:

53
Koneru Lakshmaiah Education Foundation
23SDEC02A EMBEDDED SYSTEM AUTOMATION - SKILL MANUAL

You can typically use the ESP32 built-in DAC to generate an analog output DC voltage which
will remain constant until you update the value of the DAC buffer register. The digital input
value is 8-Bit because the ESP32 DAC is 8-Bit in resolution and the output is an analog
voltage level on the DAC_CHANNEL pin. Use the following formula to find out the analog
output that corresponds to any digital input value.

CIRCUIT:

54
Koneru Lakshmaiah Education Foundation
23SDEC02A EMBEDDED SYSTEM AUTOMATION - SKILL MANUAL

CODE:
#define DAC_CH1 25

void setup()
{
// DO NOTHING
}

void loop()
{
// Send Output DC Voltage Raw Values To The DAC
// (0 64 128 192 255)
// Insert Delay After Each Sample Output
dacWrite(DAC_CH1, 0);
delay(3500);
dacWrite(DAC_CH1, 64);
delay(3500);
dacWrite(DAC_CH1, 128);
delay(3500);
dacWrite(DAC_CH1, 192);
delay(3500);
dacWrite(DAC_CH1, 255);
delay(3500);
}

OUTPUT:

55
Koneru Lakshmaiah Education Foundation
23SDEC02A EMBEDDED SYSTEM AUTOMATION - SKILL MANUAL

RESULT:

56
Koneru Lakshmaiah Education Foundation
23SDEC02A EMBEDDED SYSTEM AUTOMATION - SKILL MANUAL

Session 11: Sine Wave Generation using ESP 32

Date of the Session: / / Time of the Session: to

PREREQUISITE:

• General idea of ESP32 board


• General idea of sinusoidal waveform generation with controllable frequency,
amplitude, and phase.

PRE-LAB:

1. What is a sine wave, and why is it a fundamental waveform in signal processing


and electronics?
2. Does the ESP32 have built-in hardware support for generating analog signals like a
sine wave, or is it primarily capable of digital signal generation?
3. How would you observe and measure the generated sine wave's characteristics, such
as using an oscilloscope or a multimeter?
4. How does the resolution of the digital output (bits of resolution) impact the quality of
the generated sine wave?

OBJECTIVE:

• To generate sine wave with ESP32 using the built-in DAC feature.
• To obtain clean sinusoidal waveform with controllable frequency, amplitude, and
phase.

REQUIRED COMPONENTS:

• ESP32 development board


• Connecting Wires
• Digital Oscilloscope

57
Koneru Lakshmaiah Education Foundation
23SDEC02A EMBEDDED SYSTEM AUTOMATION - SKILL MANUAL

CIRCUIT:

CODE:
#include <driver/dac.h>

// Timer0 Configuration Pointer (Handle)


hw_timer_t *Timer0_Cfg = NULL;

// Sine LookUpTable & Index Variable


uint8_t SampleIdx = 0;
const uint8_t sineLookupTable[] = {
128, 136, 143, 151, 159, 167, 174, 182,
189, 196, 202, 209, 215, 220, 226, 231,
235, 239, 243, 246, 249, 251, 253, 254,
255, 255, 255, 254, 253, 251, 249, 246,
243, 239, 235, 231, 226, 220, 215, 209,
202, 196, 189, 182, 174, 167, 159, 151,
143, 136, 128, 119, 112, 104, 96, 88,
81, 73, 66, 59, 53, 46, 40, 35,
29, 24, 20, 16, 12, 9, 6, 4,
2, 1, 0, 0, 0, 1, 2, 4,
6, 9, 12, 16, 20, 24, 29, 35,
40, 46, 53, 59, 66, 73, 81, 88,
96, 104, 112, 119};

58
Koneru Lakshmaiah Education Foundation
23SDEC02A EMBEDDED SYSTEM AUTOMATION - SKILL MANUAL

// The Timer0 ISR Function (Executes Every Timer0 Interrupt Interval)


void IRAM_ATTR Timer0_ISR()
{
// Send SineTable Values To DAC One By One
dac_output_voltage(DAC_CHANNEL_1, sineLookupTable[SampleIdx++]);
if(SampleIdx == 100)
{
SampleIdx = 0;
}
}
void setup()
{
// Configure Timer0 Interrupt
Timer0_Cfg = timerBegin(0, 80, true);
timerAttachInterrupt(Timer0_Cfg, &Timer0_ISR, true);
timerAlarmWrite(Timer0_Cfg, 10, true);
timerAlarmEnable(Timer0_Cfg);
// Enable DAC1 Channel's Output
dac_output_enable(DAC_CHANNEL_1);
}
void loop()
{
// DO NOTHING
}

OUTPUT:

RESULT:

59
Koneru Lakshmaiah Education Foundation
23SDEC02A EMBEDDED SYSTEM AUTOMATION - SKILL MANUAL

Session 12: Testing of EEPROM memory of ESP 32

Date of the Session: / / Time of the Session: to

PREREQUISITE:

• General idea of ESP32 board


• General idea of ESP 32 EEPROM memory

PRE-LAB:

1. What is EEPROM, and what distinguishes it from other types of memory like RAM
and Flash memory?
2. What are the characteristics and limitations of the EEPROM emulation on the
ESP32?
3. What is the typical size or capacity of the EEPROM space available on the ESP32
for data storage?
4. How would you verify the accuracy and consistency of data read from and written
to the EEPROM?

OBJECTIVE:

To test the EEPROM memory of ESP 32 by saving the last LED state

REQUIRED COMPONENTS:

• ESP32 development board


• Connecting Wires
• Breadboard
• Push button
• LED
• Resistor

60
Koneru Lakshmaiah Education Foundation
23SDEC02A EMBEDDED SYSTEM AUTOMATION - SKILL MANUAL

CIRCUIT:

CODE:
// Include The EEPROM.h Library To Read & Write To The FLASH Memory
#include <EEPROM.h>

// Define The Number of Bytes You Need


#define EEPROM_SIZE 1
// Define The LED Output GPIO Pin & Button Input GPIO Pin
#define LED_GPIO 25
#define BTN_GPIO 2

// Global Variables For Button Reading & Debouncing


int ledState = LOW;
int btnState = LOW;
int lastBtnState = LOW;
int lastDebounceTime = 0;
int debounceDelay = 50;
int eeprom_address = 0;

void setup() {
Serial.begin(115200);
pinMode(LED_GPIO, OUTPUT);
pinMode(BTN_GPIO, INPUT);
EEPROM.begin(EEPROM_SIZE);
ledState = EEPROM.read(eeprom_address);
digitalWrite(LED_GPIO, ledState);

61
Koneru Lakshmaiah Education Foundation
23SDEC02A EMBEDDED SYSTEM AUTOMATION - SKILL MANUAL

void loop() {
int reading = digitalRead(BTN_GPIO);
if (reading != lastBtnState) {
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
if (reading != btnState) {
btnState = reading;
if (btnState == HIGH) {
ledState = !ledState;
digitalWrite(LED_GPIO, ledState);
EEPROM.write(eeprom_address, ledState);
EEPROM.commit();
Serial.println("State Changed & Saved To FLASH!");
}
}
}
lastBtnState = reading;
}
Code Explanation:
The code example does simply read the button input pin and debounces it to make sure it’s
not picking up any noise. And toggle the LED if the button is actually pressed. The LED
toggle event does also a write operation to the eeprom_address location and saves the current
ledState in the FLASH memory.

We start by including the EEPROM.h Library


// Include The EEPROM.h Library To Read & Write To The FLASH Memory
#include <EEPROM.h>
Then, we define the needed memory size of EEPROM which is only 1 byte. And also define
the GPIO pins we’ll be using.
// Define The Number of Bytes You Need
#define EEPROM_SIZE 1
// Define The LED Output GPIO Pin & Button Input GPIO Pin
#define LED_GPIO 25
#define BTN_GPIO 2

62
Koneru Lakshmaiah Education Foundation
23SDEC02A EMBEDDED SYSTEM AUTOMATION - SKILL MANUAL

We also define some global variables to save the LED state, and button state, and perform
the button debouncing logic.

// Global Variables For Button Reading & Debouncing


int ledState = LOW;
int btnState = LOW;
int lastBtnState = LOW;
int lastDebounceTime = 0;
int debounceDelay = 50;
int eeprom_address = 0;
setup()
in the setup() function, we initialize serial communication for debugging, the pin modes, and
the EEPROM with the pre-defined size.

Serial.begin(115200);
pinMode(LED_GPIO, OUTPUT);
pinMode(BTN_GPIO, INPUT);
EEPROM.begin(EEPROM_SIZE);
We also read the last saved LED state from the FLASH memory and apply it to the LED
output. This happens only at startup, hence it’s done in setup() function.

ledState = EEPROM.read(eeprom_address);
digitalWrite(LED_GPIO, ledState);
loop()
in the loop() function, it’s mostly about reading the button and debouncing it. Once, it’s clear
that the button is held HIGH and it’s not a noise, we take the LED toggle action + saving the
current LED state to the EEPROM (Flash Memory).

int reading = digitalRead(BTN_GPIO);


if (reading != lastBtnState)
{
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay)
{
if (reading != btnState)
{
btnState = reading;

63
Koneru Lakshmaiah Education Foundation
23SDEC02A EMBEDDED SYSTEM AUTOMATION - SKILL MANUAL

if (btnState == HIGH)
{
ledState = !ledState;
digitalWrite(LED_GPIO, ledState);
EEPROM.write(eeprom_address, ledState);
EEPROM.commit();
Serial.println("State Changed & Saved To FLASH!");
}
}
}
lastBtnState = reading;

OUTPUT:

RESULT:

64
Koneru Lakshmaiah Education Foundation
23SDEC02A EMBEDDED SYSTEM AUTOMATION - SKILL MANUAL

Session 13: Scanning Bluetooth Devices using ESP 32

Date of the Session: / / Time of the Session: to

PREREQUISITE:

• General idea of ESP32 board


• General idea of ESP32 Bluetooth Classic Vs ESP32 Bluetooth Low Energy (BLE)

PRE-LAB:

1. What Bluetooth capabilities does the ESP32 offer, and which Bluetooth protocols or
versions does it support?
2. How does the ESP32 detect and identify Bluetooth devices within its range?
3. How does the ESP32 handle potential errors or issues during Bluetooth device
scanning?
4. Once Bluetooth devices are scanned, how is the information about discovered
devices collected and retrieved in ESP32-based code?

OBJECTIVE:

To perform scanning of surrounding Bluetooth devices using ESP 32

REQUIRED COMPONENTS:

• ESP32 development board

THEORY:
ESP32 Bluetooth Scanner:
The ESP32 Bluetooth Scanner is also called Device Discovery which is a very simple
application that runs either Synchronously or Asynchronously and searches for all surrounding
Bluetooth devices and report their number, names, and MAC addresses. Then you can choose
which device to connect to afterward or keep rescanning in case no device is found.

65
Koneru Lakshmaiah Education Foundation
23SDEC02A EMBEDDED SYSTEM AUTOMATION - SKILL MANUAL

As stated earlier the ESP32 Bluetooth Scanner can run Synchronously (Blocking) which means
it’ll keep searching until completion and the whole application will be stuck until the
synchronous function call is executed till completion. It’s not generally a bad thing to do as it
may be required in some applications.

In order to perform Bluetooth Scanning for Device Discovery (Synchronously), use the
SerialBT.discover(BT_DISCOVER_TIME) function. For Asynchronous scanning, you should
use SerialBT.discoverAsync() function instead.

CODE:
#include <BluetoothSerial.h>

#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)


#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it
#endif

#if !defined(CONFIG_BT_SPP_ENABLED)
#error Serial Bluetooth not available or not enabled. It is only available for the ESP32 chip.
#endif

BluetoothSerial SerialBT;

#define BT_DISCOVER_TIME 10000


static bool btScanSync = true;

void setup() {
Serial.begin(115200);
SerialBT.begin("ESP32test"); //Bluetooth device name
Serial.println("The device started, now you can pair it with bluetooth!");
if (btScanSync) {
Serial.println("Starting discover...");
BTScanResults *pResults = SerialBT.discover(BT_DISCOVER_TIME);
if (pResults)
pResults->dump(&Serial);
else
Serial.println("Error on BT Scan, no result!");
}
}

void loop() {

66
Koneru Lakshmaiah Education Foundation
23SDEC02A EMBEDDED SYSTEM AUTOMATION - SKILL MANUAL

delay(100);
}

OUTPUT:

RESULT:

67
Koneru Lakshmaiah Education Foundation
23SDEC02A EMBEDDED SYSTEM AUTOMATION - SKILL MANUAL

Session 14: Duplex Bluetooth Communication Between ESP 32 and


Android Phone

Date of the Session: / / Time of the Session: to

PREREQUISITE:

• General idea of ESP32 board


• General idea of ESP32 Bluetooth send and receive data

PRE-LAB:

1. What is duplex communication in the context of Bluetooth, and why is it essential


for bidirectional data exchange?
2. What are the Bluetooth capabilities of the ESP32, particularly concerning its ability
to function as a Bluetooth device and communicate with other devices like
smartphones?
3. What communication protocols or methods can be used for exchanging data between
the ESP32 and an Android app over Bluetooth?
4. What are potential challenges or issues that might arise during Bluetooth
communication between the ESP32 and the Android phone?

OBJECTIVE:

To create an ESP32 Bluetooth Receiver Device and use Android Smartphone to control it
over Bluetooth.

REQUIRED COMPONENTS:

• ESP32 development board


• Android mobile phone
• Serial Bluetooth Terminal Android app
• Breadboard
• Connecting wires

68
Koneru Lakshmaiah Education Foundation
23SDEC02A EMBEDDED SYSTEM AUTOMATION - SKILL MANUAL

CIRCUIT:

CODE:
#include "BluetoothSerial.h"

#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)


#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it
#endif
#if !defined(CONFIG_BT_SPP_ENABLED)
#error Serial Bluetooth not available or not enabled. It is only available for the ESP32 chip.
#endif

// LED GPIO Pin Definition


#define LED_GPIO 25

// Bluetooth Serial Object (Handle)


BluetoothSerial SerialBT;

// ESP32 Bluetooth (Slave) Device Information


const char *pin = "1234"; // Change this to more secure PIN.
String device_name = "ESP32-BT-Slave";

// Bluetooth Received Byte & Message Buffer Array


String RxBuffer = "";
char RxByte;

void setup() {
pinMode(LED_GPIO, OUTPUT);
Serial.begin(115200);
SerialBT.begin(device_name); //Bluetooth device name

69
Koneru Lakshmaiah Education Foundation
23SDEC02A EMBEDDED SYSTEM AUTOMATION - SKILL MANUAL

Serial.printf("The device with name \"%s\" is started.\nNow you can pair it with
Bluetooth!\n", device_name.c_str());
SerialBT.setPin(pin);
Serial.println("Using PIN");
}

void loop() {
// Read The Received Bytes & Add Them To Message (RxBuffer) Array
if (SerialBT.available()){
RxByte = SerialBT.read();
if (RxByte != '\n'){
RxBuffer += String(RxByte);
}
else{
RxBuffer = "";
}
Serial.write(RxByte);
}
// Check The Received Message & Update Output LED State
if (RxBuffer =="led_on"){
digitalWrite(LED_GPIO, HIGH);
}
else if (RxBuffer =="led_off"){
digitalWrite(LED_GPIO, LOW);
}
delay(25);
}
Code Explanation:
First of all, include the BluetoothSerial library and check if Bluetooth is properly enabled or
not.

#include "BluetoothSerial.h"
#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it
#endif
#if !defined(CONFIG_BT_SPP_ENABLED)
#error Serial Bluetooth not available or not enabled. It is only available for the ESP32 chip.
#endif
Create an instance of the BluetoothSerial called SerialBT, and define a GPIO pin for LED
output which is going to be GPIO25 pin.

70
Koneru Lakshmaiah Education Foundation
23SDEC02A EMBEDDED SYSTEM AUTOMATION - SKILL MANUAL

// LED GPIO Pin Definition


#define LED_GPIO 25
// Bluetooth Serial Object (Handle)
BluetoothSerial SerialBT;
Set a PIN code for the ESP32 slave device “1234” and a name “ESP32-BT-Slave“. Create a
variable “RxByte” to hold the last received byte of data and an array “RxBuffer” to store the
incoming data byte by byte.

// ESP32 Bluetooth (Slave) Device Information


const char *pin = "1234"; // Change this to more secure PIN.
String device_name = "ESP32-BT-Slave";
// Bluetooth Received Byte & Message Buffer Array
String RxBuffer = "";
char RxByte;
setup()

in the setup() function, set the LED pin to output

pinMode(LED_GPIO, OUTPUT);
Start serial communication for monitoring & debugging

Serial.begin(115200);
Start the Bluetooth device with the given name and PIN code.

SerialBT.begin(device_name); //Bluetooth device name


Serial.printf("The device with name \"%s\" is started.\nNow you can pair it with
Bluetooth!\n", device_name.c_str());
SerialBT.setPin(pin);
Serial.println("Using PIN");
loop()

In the loop() function, read the incoming data bytes and push them to the RxBuffer array.

// Read The Received Bytes & Add Them To Message (RxBuffer) Array
if (SerialBT.available()){
RxByte = SerialBT.read();
if (RxByte != '\n'){
RxBuffer += String(RxByte);
}
else{
RxBuffer = "";
}

71
Koneru Lakshmaiah Education Foundation
23SDEC02A EMBEDDED SYSTEM AUTOMATION - SKILL MANUAL

Serial.write(RxByte);
}
Then check the received message buffer (RxBuffer) array, if it’s “led_on” turn the LED ON,
else if it was “led_off” turn the LED OFF. Otherwise, don’t take any action. And keep
repeating.

// Check The Received Message & Update Output LED State


if (RxBuffer =="led_on"){
digitalWrite(LED_GPIO, HIGH);
}
else if (RxBuffer =="led_off"){
digitalWrite(LED_GPIO, LOW);
}
OUTPUT:

RESULT:

72
Koneru Lakshmaiah Education Foundation
23SDEC02A EMBEDDED SYSTEM AUTOMATION - SKILL MANUAL

Session 15: Scanning of Wi-Fi Network using ESP 32

Date of the Session: / / Time of the Session: to

PREREQUISITE:

• General idea of ESP32 board


• Set up and configure the ESP32 to scan for WiFi networks

PRE-LAB:

1. What functionalities does the ESP32 offer for scanning nearby Wi-Fi networks?
2. How does the ESP32 detect and gather information about available Wi-Fi networks
within its range?
3. Outline the basic structure or algorithm of the code required to initiate Wi-Fi network
scanning using the ESP32.

OBJECTIVE:

To scan the available WiFi networks using the ESP 32 board

REQUIRED COMPONENTS:

• ESP32 development board

CODE:
#include "WiFi.h"

void setup()
{
Serial.begin(115200);

// Set WiFi to station mode and disconnect from an AP if it was previously connected.
WiFi.mode(WIFI_STA);
WiFi.disconnect();
delay(100);

Serial.println("Setup done");

73
Koneru Lakshmaiah Education Foundation
23SDEC02A EMBEDDED SYSTEM AUTOMATION - SKILL MANUAL

}
void loop()
{
Serial.println("Scan start");

// WiFi.scanNetworks will return the number of networks found.


int n = WiFi.scanNetworks();
Serial.println("Scan done");
if (n == 0) {
Serial.println("no networks found");
} else {
Serial.print(n);
Serial.println(" networks found");
Serial.println("Nr | SSID | RSSI | CH | Encryption");
for (int i = 0; i < n; ++i) {
// Print SSID and RSSI for each network found
Serial.printf("%2d",i + 1);
Serial.print(" | ");
Serial.printf("%-32.32s", WiFi.SSID(i).c_str());
Serial.print(" | ");
Serial.printf("%4d", WiFi.RSSI(i));
Serial.print(" | ");
Serial.printf("%2d", WiFi.channel(i));
Serial.print(" | ");
switch (WiFi.encryptionType(i))
{
case WIFI_AUTH_OPEN:
Serial.print("open");
break;
case WIFI_AUTH_WEP:
Serial.print("WEP");
break;
case WIFI_AUTH_WPA_PSK:
Serial.print("WPA");
break;
case WIFI_AUTH_WPA2_PSK:
Serial.print("WPA2");
break;
case WIFI_AUTH_WPA_WPA2_PSK:
Serial.print("WPA+WPA2");
break;

74
Koneru Lakshmaiah Education Foundation
23SDEC02A EMBEDDED SYSTEM AUTOMATION - SKILL MANUAL

case WIFI_AUTH_WPA2_ENTERPRISE:
Serial.print("WPA2-EAP");
break;
case WIFI_AUTH_WPA3_PSK:
Serial.print("WPA3");
break;
case WIFI_AUTH_WPA2_WPA3_PSK:
Serial.print("WPA2+WPA3");
break;
case WIFI_AUTH_WAPI_PSK:
Serial.print("WAPI");
break;
default:
Serial.print("unknown");
}
Serial.println();
delay(10);
}
}
Serial.println("");

// Delete the scan result to free memory for code below.


WiFi.scanDelete();

// Wait a bit before scanning again.


delay(5000);
}
OUTPUT:

RESULT:

75
Koneru Lakshmaiah Education Foundation
23SDEC02A EMBEDDED SYSTEM AUTOMATION - SKILL MANUAL

Session 16: Signal Strength Identification of Wi-Fi Network


using ESP 32

Date of the Session: / / Time of the Session: to

PREREQUISITE:

• General idea of ESP32 board


• General idea of get ESP32 WiFi Connection Status

PRE-LAB:

1. How does the ESP32 detect and assess the signal strength of available Wi-Fi
networks within its range?
2. What metrics or units are commonly used to represent Wi-Fi signal strength, such as
dBm (decibel-milliwatts) or RSSI (Received Signal Strength Indicator)?
3. Can the ESP32 process or filter Wi-Fi network signal strength data to focus on
specific criteria (e.g., strong signals, weak signals)?
4. How would you verify the correlation between signal strength values and the actual
signal quality or network performance?

OBJECTIVE:

To get and print the RSSI (Received Signal Strength Indicator) for the WiFi network

CODE:

#include <WiFi.h>

// Replace with your own network credentials


const char* ssid = "yourNetworkSSID";
const char* password = "yourNetworkPassword";

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

76
Koneru Lakshmaiah Education Foundation
23SDEC02A EMBEDDED SYSTEM AUTOMATION - SKILL MANUAL

Serial.println("\nConnecting");
// Keep Waiting Until Connection is Established
while(WiFi.status() != WL_CONNECTED){
Serial.print(".");
delay(100);
}
// Connected Successfully
Serial.println("\nConnected To The WiFi Network");
Serial.print("Local ESP32 IP: ");
Serial.println(WiFi.localIP());
// Print The RSSI (Received Signal Strength Indicator)
Serial.print("RRSI: ");
Serial.print(WiFi.RSSI());
Serial.println(" dBm");
}

void loop(){
// Do Nothing
}
Note:
RSSI is an estimated measure of the WiFi signal strength for a specific network (router or
access point). The return value has the following form and unit (-x dBm). Which means a lower
absolute value indicates a more powerful connection. Here’s how to judge the RSSI value and
decide on the signal strength rating:

RSSI > -30 dBm: Amazing!


RSSI < -55 dBm: Very good signal
RSSI < -67 dBm: Fairly good
RSSI < -70 dBm: Okay
RSSI < -80 dBm: Not good
RSSI < -90 dBm: Extremely weak signal (unusable)

OUTPUT:

77
Koneru Lakshmaiah Education Foundation
23SDEC02A EMBEDDED SYSTEM AUTOMATION - SKILL MANUAL

RESULT:

78
Koneru Lakshmaiah Education Foundation
23SDEC02A EMBEDDED SYSTEM AUTOMATION - SKILL MANUAL

Session 17: Get and Set MAC address of ESP 32

Date of the Session: / / Time of the Session: to

PREREQUISITE:

• General idea of ESP32 board


• General idea of get ESP32 WiFi Connection Status

PRE-LAB:

1. How is a MAC address formatted, and what information does it contain?


2. Where is the MAC address stored in the ESP32's hardware, and how is it accessed
programmatically?
3. Can the MAC address of an ESP32 be changed or set programmatically?
4. Are there any specific scenarios or applications where changing the MAC address
might be necessary or beneficial?

OBJECTIVE:

To learn how to Get the ESP32 MAC Address and how to Change the ESP32 MAC Address
in Arduino IDE

Get ESP32 MAC Address:


The MAC address of the ESP32 can be easily obtained using the WiFi library that’s already
built into the Arduino Core for ESP32. You need to call the WiFi.macAddress() function that
returns the MAC address in the form of a 6-byte array formatted as a string.

CODE:

#include <WiFi.h>
void setup(){
Serial.begin(115200);
Serial.print("\nDefault ESP32 MAC Address: ");
Serial.println(WiFi.macAddress());
}

79
Koneru Lakshmaiah Education Foundation
23SDEC02A EMBEDDED SYSTEM AUTOMATION - SKILL MANUAL

void loop(){
// Do Nothing
}
OUTPUT:

RESULT:

Change ESP32 MAC Address:


To change the ESP32 MAC address in Arduino IDE, you can use the
esp_wifi_set_mac()function from the esp_wifi.h library. Which takes the WiFi mode and the
MAC address array as arguments.

CODE:
#include <WiFi.h>
#include <esp_wifi.h>

uint8_t New_MAC_Address[] = {0x10, 0xAA, 0xBB, 0xCC, 0x33, 0xF5};

void setup(){
Serial.begin(115200);
WiFi.mode(WIFI_STA);
Serial.print("\nOLD ESP32 MAC Address: ");

80
Koneru Lakshmaiah Education Foundation
23SDEC02A EMBEDDED SYSTEM AUTOMATION - SKILL MANUAL

Serial.println(WiFi.macAddress());
esp_wifi_set_mac(WIFI_IF_STA, New_MAC_Address);
Serial.print("NEW ESP32 MAC Address: ");
Serial.println(WiFi.macAddress());
}

void loop(){
// Do Nothing
}
OUTPUT:

RESULT:

81
Koneru Lakshmaiah Education Foundation
23SDEC02A EMBEDDED SYSTEM AUTOMATION - SKILL MANUAL

Session 18: Get the IP address of ESP 32

Date of the Session: / / Time of the Session: to

PREREQUISITE:

• General idea of ESP32 board


• General idea of getting the IP of ESP 32

PRE-LAB:

1. What is an IP address?
2. Explain the purpose of subnet masks.
3. How does the ESP32 handle networking tasks?
4. What is the significance of SSID and password in WiFi configuration?

OBJECTIVE:

To get static IP address of ESP 32

REQUIRED COMPONENTS:

• ESP32 development board


• Connecting Wires
• Breadboard

CODE:

#include <WiFi.h>

// Replace with your own network credentials


const char* ssid = "yourNetworkSSID";
const char* password = "yourNetworkPassword";

void setup(){
Serial.begin(115200);
WiFi.mode(WIFI_STA);

82
Koneru Lakshmaiah Education Foundation
23SDEC02A EMBEDDED SYSTEM AUTOMATION - SKILL MANUAL

WiFi.begin(ssid, password);
Serial.println("\nConnecting to WiFi Network ..");
// Wait until connection is established
while(WiFi.status() != WL_CONNECTED){
Serial.print(".");
delay(100);
}
// Print network settings assigned by the DHCP
Serial.println("\nConnected to the WiFi network");
Serial.print("Local ESP32 IP: ");
Serial.println(WiFi.localIP());
Serial.print("Subnet Mask: " );
Serial.println(WiFi.subnetMask());
Serial.print("Gateway IP: ");
Serial.println(WiFi.gatewayIP());
Serial.print("DNS 1: ");
Serial.println(WiFi.dnsIP(0));
Serial.print("DNS 2: ");
Serial.println(WiFi.dnsIP(1));
}

void loop(){
// Do Nothing
}
OUTPUT:

RESULT:

83
Koneru Lakshmaiah Education Foundation
23SDEC02A EMBEDDED SYSTEM AUTOMATION - SKILL MANUAL

Session 19: Assign Static IP address for ESP 32

Date of the Session: / / Time of the Session: to

PREREQUISITE:

• General idea of ESP32 board


• General idea of assigning static IP address for ESP 32

PRE-LAB:

1. What specific parameters are required to configure a static IP address


2. How would you incorporate the necessary parameters (IP address, subnet mask,
gateway, DNS server) into the ESP32 code?
3. How are these parameters set or defined within the ESP32 code?
4. What are potential benefits and drawbacks of using a static IP address on the ESP32
in terms of network stability and interoperability?

OBJECTIVE:

To set a static IP address for ESP32

REQUIRED COMPONENTS:

• ESP32 development board


• Connecting Wires
• Breadboard

84
Koneru Lakshmaiah Education Foundation
23SDEC02A EMBEDDED SYSTEM AUTOMATION - SKILL MANUAL

CODE:

#include <WiFi.h>

// Replace with your own network


credentials
const char* ssid = "yourNetworkSSID";
const char* password =
"yourNetworkPassword";

IPAddress local_IP(192, 168, 1, 108); //


Desired Static IP Address
IPAddress subnet(255, 255, 255, 0);
IPAddress gateway(192, 168, 1, 1);
IPAddress primaryDNS(192, 168, 1, 1); //
Not Mandatory
IPAddress secondaryDNS(0, 0, 0, 0); //
Not Mandatory

void setup(){
Serial.begin(115200);
WiFi.mode(WIFI_STA);
// Configures Static IP Address
if (!WiFi.config(local_IP, gateway,
subnet, primaryDNS, secondaryDNS))
{
Serial.println("Configuration Failed!");
}

85
Koneru Lakshmaiah Education Foundation
23SDEC02A EMBEDDED SYSTEM AUTOMATION - SKILL MANUAL

WiFi.begin(ssid, password);
Serial.println("\nConnecting to WiFi
Network ..");
// Wait until connection is established
while(WiFi.status() !=
WL_CONNECTED){
Serial.print(".");
delay(100);
}
Serial.println("\nConnected to the WiFi
network");
// Print The IP And It Should Match The
Desired Static IP That We've Defined
Serial.print("Local ESP32 IP: ");
Serial.println(WiFi.localIP());
}

void loop(){
// Do Nothing
}
Note:
Don’t forget to change the ssid and password to match your network credentials.

OUTPUT:

RESULT:

86
Koneru Lakshmaiah Education Foundation
23SDEC02A EMBEDDED SYSTEM AUTOMATION - SKILL MANUAL

Session 20: Change the host name of ESP 32

Date of the Session: / / Time of the Session: to

PREREQUISITE:

• General idea of ESP32 board


• What is the purpose of changing hostname in ESP 32
• Why to change the WiFi network credentials, SSID, PWD and Host name

PRE-LAB:

1. What is a hostname in networking, and how is it used to identify devices within a


network?
2. What parameters or information are required to configure or change the hostname of
an ESP32 device?
3. How does changing the hostname affect the ESP32's identification and interaction
within the network environment?
4. How would you incorporate the necessary code to modify the hostname in an
ESP32-based program?

OBJECTIVE:

To get the ESP32 hostname and also to modify custom hostname in Arduino IDE.

REQUIRED COMPONENTS:

• ESP32 development board


• Connecting Wires
• Breadboard

ESP32 Get HostName: The ESP32 Hostname is the device name that other WiFi devices
will see on the network. You can get the ESP32 hostname by calling

87
Koneru Lakshmaiah Education Foundation
23SDEC02A EMBEDDED SYSTEM AUTOMATION - SKILL MANUAL

the WiFi.getHostname() function. Here is an example code for getting and printing the
ESP32 WiFi hostname.

Serial.println(WiFi.getHostname());

Here is the generic (default) hostname for my ESP32 board as seen by my router.

ESP32 HostName Change:

To change the ESP32 hostname, you need to call the WiFi.setHostname() function before
calling WiFi.mode() and then WiFi.begin() in this exact same order.

CODE:

#include <WiFi.h>

// Replace with your own network


credentials
const char* ssid = "yourNetworkSSID";
const char* password =
"yourNetworkPassword";
const char* MyHostName = "ESP32-Test";

void setup(){
Serial.begin(115200);
WiFi.setHostname(MyHostName);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
Serial.println("\nConnecting to WiFi
Network ..");
// Wait until connection is established
while(WiFi.status() !=
WL_CONNECTED){

88
Koneru Lakshmaiah Education Foundation
23SDEC02A EMBEDDED SYSTEM AUTOMATION - SKILL MANUAL

Serial.print(".");
delay(100);
}
// Print ESP32's IP & HostName
Serial.println("\nConnected to the WiFi
network");
Serial.print("Local ESP32 IP: ");
Serial.println(WiFi.localIP());
Serial.print("ESP32 HostName: ");
Serial.println(WiFi.getHostname());
}

void loop(){
// Do Nothing
}
OUTPUT:

And now my router is seeing the new ESP32 Hostname after running the example code above

RESULT:

89
Koneru Lakshmaiah Education Foundation
23SDEC02A EMBEDDED SYSTEM AUTOMATION - SKILL MANUAL

Session 21: Controlling the GPIO pins in ESP32 using Touch Inputs

Date of the Session: / / Time of the Session: to

PREREQUISITE:

• General idea of ESP32 board


• Understanding of touch sensor principles and how they interface with
microcontrollers

PRE-LAB:

1. What type of cable should be used for ESP32? What parameters or information are
required to configure or change the hostname of an ESP32 device?
2. Specify ROM size of ESP32?
3. Mention the software which is to be installed in the laptop for ESP32 board?
4. What is the operating voltage of the GPIO pins in ESP32?

OBJECTIVE:

To interface MPU6050 module with ESP32 and WEBSERVER using Arduino IDE
Programming.

REQUIRED COMPONENTS:

• ESP32 development board


• Connecting cable
• Connecting Wires

CONNECTION:

90
Koneru Lakshmaiah Education Foundation
23SDEC02A EMBEDDED SYSTEM AUTOMATION - SKILL MANUAL

CODE:
// set pin numbers
const int touchPin = 4;
const int ledPin = 16;

// change with your threshold value


const int threshold = 20;
// variable for storing the touch pin value
int touchValue;

void setup(){
Serial.begin(115200);
delay(1000); // give me time to bring up serial monitor
// initialize the LED pin as an output:
pinMode (ledPin, OUTPUT);
}

void loop(){
// read the state of the pushbutton value:
touchValue = touchRead(touchPin);
Serial.print(touchValue);
// check if the touchValue is below the threshold
// if it is, set ledPin to HIGH

91
Koneru Lakshmaiah Education Foundation
23SDEC02A EMBEDDED SYSTEM AUTOMATION - SKILL MANUAL

if(touchValue< threshold){
// turn LED on
digitalWrite(ledPin, HIGH);
Serial.println(" - LED on");
}
else{
// turn LED off
digitalWrite(ledPin, LOW);
Serial.println(" - LED off");
}
delay(500);
}

Precautions:
1. Use insulation tools while you connect the Circuit.
2. Wear Good Insulating Shoes.
3. Avoid contacting circuits with wet hands or wet materials.
4. Maintain a work space clear of extraneous material such as books, papers, and
clothes.
5. Be careful while connecting any actuators in 230V AC Mains.

RESULTS:

92
Koneru Lakshmaiah Education Foundation

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy