Reader small image

You're reading from  Developing IoT Projects with ESP32 - Second Edition

Product typeBook
Published inNov 2023
Reading LevelIntermediate
PublisherPackt
ISBN-139781803237688
Edition2nd Edition
Languages
Tools
Right arrow
Author (1)
Vedat Ozan Oner
Vedat Ozan Oner
author image
Vedat Ozan Oner

Vedat Ozan Oner is an IoT product developer and software architect, with an excellent blend of technical knowledge and experience. During his career, he has contributed to several IoT projects in different roles, which allowed him to discover all key aspects of developing successful IoT products in highly competitive markets. Vedat has a bachelor's degree in METU/computer engineering and holds several industry-recognized credentials and qualifications, including PMP®, ITIL®, and AWS Certified Developer. Vedat started his limited company, Mevoo Ltd, in London in 2018 to provide consultancy services to his clients as well as develop his own IoT products. He still lives in London with his family.
Read more about Vedat Ozan Oner

Right arrow

Using ESP32 Peripherals

In the previous chapter, we discussed the available tools that we can use to develop applications on ESP32. We learned about ESP-IDF, the PlatformIO IDE, and debugging and testing applications. Now, it is time to use the ESP32 peripherals in applications. Peripherals are how MCUs connect to the outer world. The ESP32 series chips provide a wide range of peripherals. Depending on the product vision, each family may have different types of peripherals and different numbers of channels of peripherals. For example, the ESP32-S2/S3 series has more GPIO pins compared to other families and features USB OTG (On-the-Go) to act as a USB host, which allows us to connect other USB devices, such as a keyboard or mouse. On the other hand, the ESP32-C family has a reduced set of peripherals to achieve much lower costs.

In this chapter, we will learn how to use some popular peripherals of ESP32 in the example applications. The topics are:

  • Driving General-Purpose...

Technical requirements

We will use Visual Studio Code and ESP-IDF command-line tools to create, develop, flash, and monitor the applications in this chapter.

As hardware, both of the development kits, ESP32-C3-DevKitM-1 and ESP32-S3 Box Lite, will be employed. The sensors and other hardware components used in this chapter are:

  • A 55 mm LED
  • A 220 resistor
  • A tactile switch
  • BME280 – temperature, humidity, pressure sensor
  • TSL2561 – ambient light sensor
  • An SD card breakout board
  • A micro-SD card
  • 4x 10K resistors

The source code for the examples is located in the repository found at this link: https://github.com/PacktPublishing/Developing-IoT-Projects-with-ESP32-2nd-edition/tree/main/ch3

Driving General-Purpose Input/Output (GPIO)

Fundamentally, a sensor is any device that generates some sort of output when exposed to a phenomenon – say, temperature, humidity, light, vibration, and so on. In an IoT application, we use sensors as data sources by connecting their output via an interface. General-Purpose Input/Output (GPIO) is the simplest form of communication interface in the list of peripherals. It provides high or low values. For instance, it is quite possible to read the status of a contact sensor by interfacing it with GPIO, since a contact sensor can only be either open or closed. Other, more complex sensors may require different types of interfaces, such as Inter-Integrated Circuit (I2C) or Serial Peripheral Interface (SPI).

Actuators are on the output side of IoT solutions. They change their state according to an analog or digital signal coming from the microcontroller and generate output to the environment. Some examples are a buzzer to make sound...

Interfacing with sensors over Inter-Integrated Circuit (I2C)

I2C is a serial communication bus that supports multiple devices on the same line. Devices on the bus use 7-bit addressing. Two lines are needed for the I2C interface: Clock (CLK) and Serial Data (SDA). The master device provides the clock to the bus. The following figure shows a typical architecture of an I2C bus:

Figure 3.3: I2C architecture

The only rule here is that each slave has to have its own unique address on the bus. In other words, two sensors with the same I2C address cannot share a common bus. As defined in the protocol, the master and slaves exchange data over the SDA line. The master sends a command and the addressed slave replies to that command. Now, let’s see an example of I2C communication on ESP32 by implementing a multisensor application.

Developing a multisensor application

The purpose of this example is to develop an application in which we read values from different I2C...

Integrating with SD cards over Serial Peripheral Interface (SPI)

Serial Peripheral Interface (SPI) is another serial communication protocol that can be used with devices. The main difference is that SPI requires at least four signal lines and one more each time when adding a new device to the bus.

Figure 3.6: SPI architecture

In this figure, the master node provides a clock over the CLK line,uses the Master-Out-Slave-In (MOSI) line to send data, and receives data over the Master-In-Slave-Out (MISO) line. It can communicate with a single slave node at a time. To select a slave node, it pulls the corresponding Chip-Select (CS) line to low.

Although SPI consumes more pin resources on an MCU, it achieves a higher data transfer rate compared to I2C. Therefore, in some applications, such as where SD-card integration is needed, it makes sense to prefer the SPI protocol over I2C.

In the next example, we will add SD card storage to our ESP32-C3-DevKitM-1 development kit...

Audio output over Inter-IC Sound (I²S)

Inter-IC Sound (I²S) is another type of data interface but for audio. Essentially, it has three lines for the following:

  • Data, Data-In (DIN), or Data-Out (DOUT)
  • Clock or bit clock (BCLK)
  • Channel select, Word Select (WS), or Left-Right Clock (LRCLK)

The interface is standard; however, the naming is not, as we see above. The data line carries stereo audio data for both the left (channel 0) and right (channel 1) channels. The channel select signal level indicates which channel’s data is currently being transferred: it is low for the left channel and high for the right channel. Finally, the clock line is a common clock for both ends provided by the master, which is usually the sending party in this type of communication.

In audio projects, we normally need a Digital-Analog Converter (DAC) to convert digital audio data to its analog counterpart and an amplifier to forward the analog output to a...

Developing graphical user interfaces on Liquid-Crystal Display (LCD)

There are several types of display technologies on the market for IoT applications, such as Liquid-Crystal Display (LCD), Organic Light-Emitting Diode (OLED) displays, Thin Film Transistor (TFT) displays, and e-paper technologies. They have pros and cons,therefore, it is necessary to select the display technology according to the requirements of the project. Some criteria can be:

  • Price tag
  • Power consumption
  • Hardware resources to drive the display (I2C vs SPI communication)
  • Driver support
  • Graphics capabilities, size, and resolution
  • Color requirements

For example, TFTs generally show more graphics capabilities and high resolution, but higher energy consumption too. If the project requirements mandate the least amount of energy consumption, a reflective display, such as an e-paper type display, would be the right choice.

In the next example, we will use ESP32-S3...

Summary

ESP32 has a diverse range of peripherals to be employed in different scenarios. In this chapter, we covered several important peripherals that provide interaction with the outer world in IoT applications. The most basic one is GPIO, which can be configured for any digital input/output needs. I2C and SPI are prominent in sensor communication. For audio output, we can use I2S. It is similar to I2C but supports stereo. We have also seen GUI development on LCD with the help of LVGL.

The next chapter will provide a list ofpopular IoT libraries with examples. When it comes to IoT applications, the use of third-party libraries is almost inevitable for every project to speed up product development and reduce costs. Therefore, it is always better to have an idea about the available options before jumping into development. We will discuss some of those libraries and use cases in the next chapter.

Questions

Here are some questions to reiterate the topics in this chapter:

  1. What would be the right peripheral to use when you need to drive an LED?
    1. ADC
    2. GPIO
    3. I2C
    4. DAC
  2. Which of the following options supports multiple clients on the same bus with addressing?
    1. I2S
    2. GPIO
    3. I2C
    4. SPI
  3. Why do SD cards use SPI communication with MCUs?
    1. Less error-prone
    2. Less resource-hungry (fewer pins to use)
    3. Higher transfer rate
    4. Higher memory capacity
  4. The I2S protocol defines a Word-Select (WS) signal:
    1. To transfer audio data
    2. To clock the bus
    3. To select the left or right channel
    4. To increase the data rate
  5. Which of the following is not a display technology generally used in IoT applications?
    1. MRI
    2. LCD
    3. TFT
    4. OLED
    ...

Further reading

Another resource for more information about embedded systems peripherals is:

Learn more on Discord

To join the Discord community for this book – where you can share feedback, ask questions to the author, and learn about new releases – follow the QR code below:

https://discord.gg/3Q9egBjWVZ

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Developing IoT Projects with ESP32 - Second Edition
Published in: Nov 2023Publisher: PacktISBN-13: 9781803237688
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €14.99/month. Cancel anytime

Author (1)

author image
Vedat Ozan Oner

Vedat Ozan Oner is an IoT product developer and software architect, with an excellent blend of technical knowledge and experience. During his career, he has contributed to several IoT projects in different roles, which allowed him to discover all key aspects of developing successful IoT products in highly competitive markets. Vedat has a bachelor's degree in METU/computer engineering and holds several industry-recognized credentials and qualifications, including PMP®, ITIL®, and AWS Certified Developer. Vedat started his limited company, Mevoo Ltd, in London in 2018 to provide consultancy services to his clients as well as develop his own IoT products. He still lives in London with his family.
Read more about Vedat Ozan Oner