Elektor ESP32 Programming Micro Controllers User Manual

ESP32 Programming Micro Controllers

fepetra arahana

  • Product Name: Programming Microcontrollers in C/C++ Using
    Arduino
  • Author: Clemens Valens
  • Supported Platforms: Arduino UNO, Raspberry Pi Pico, ESP32
  • ISBN: 978-3-89576-720-3 (Print), 978-3-89576-721-0 (eBook)

Torolàlana momba ny fampiasana vokatra

1. Fampidirana

This book provides guidance on programming microcontrollers in
C/C++ using Arduino. It includes examples for Arduino UNO,
Raspberry Pi Pico, and ESP32.

2. Fanombohana

To begin using this book effectively, ensure you have the
necessary hardware such as the mentioned microcontroller
boards.

3. Dingana fandaharana

  1. Read through the relevant sections for the microcontroller
    platform you are working with.
  2. Araho ny ex nomenaamples and code snippets to practice
    programming in C/C++.
  3. Experiment with different projects and challenges to enhance
    your skills.

4. Famahana olana

If you encounter any issues or errors while programming, refer
back to the examples and ensure correctness in your code.

FAQ

Q: Can this book be used by beginners in programming?

A: Yes, this book caters to beginners and provides step-by-step
examples for easy understanding.

Q: Are there specific hardware requirements to follow along
miaraka amin'ny examples?

A: You will need the mentioned microcontroller boards such as
Arduino UNO, Raspberry Pi Pico, or ESP32 to practice the
examples.

Q: Is there online support available for additional
tari-dalana?

A: For additional support or clarifications, you can visit the
publisher’s website or community forums related to Arduino and
microcontroller programming.

“`

Fandaharana

programemmbinegdsdeerdies

Microcontrollers in

C/C++ Using Arduino

With Examples for Arduino UNO, Raspberry Pi Pico and ESP32

Clemens Valens
1

Programming Microcontrollers in C/C++ Using Arduino
With Examples for Arduino UNO, Raspberry Pi Pico and ESP32

By Clemens Valens

Publication Elektor ity. Elektor no marika media an'ny
Elektor International Media BV PO Box 11, NL-6114-ZG Susteren, Netherlands Phone: +31 46 4389444
Zo rehetra voatokana. Tsy misy ampahany amin'ity boky ity azo adika amin'ny endrika ara-materialy, anisan'izany ny fakana sary, na
mitahiry amin'ny fitaovana elektronika ary na mihelina na miantraika amin'ny fampiasana hafa an'ity boky ity, tsy nahazoana alalana an-tsoratra avy amin'ny tompon'ny zon'ny mpamorona afa-tsy araka ny fepetra voalazan'ny lalàna momba ny zon'ny mpamorona sy ny patanty 1988 na araka ny fepetran'ny fahazoan-dàlana navoakan'ny Agency Licensing Agency Ltd., 90 Tottenham Court Road, London, Angletera W1P 9HE. Ny fangatahana fahazoan-dàlana avy amin'ny tompon'ny zon'ny mpamorona hamerenana ny ampahany amin'ny famoahana dia tokony halefa any amin'ny mpamoaka.
FANAMBARANA
The author and publisher have made every effort to ensure the accuracy of the information contained in this book. They do not assume, or hereby disclaim, any liability to any party for any loss or damage caused by errors or omissions in this book, whether such errors or omissions result from negligence, accident, or any other cause.
ISBN 978-3-89576-720-3 Atao pirinty ISBN 978-3-89576-721-0 eBook
© Copyright 2026 Elektor International Media
www.elektor.com Editor: Clemens Valens Prepress Production: D-Vision, Julian van den Berg Printers: Ipskamp, Enschede, Pays-Bas
Elektor no loharano lehibe indrindra eran'izao tontolo izao amin'ny fampahalalana ara-teknika ilaina sy vokatra elektronika ho an'ny injeniera pro, mpamorona elektronika ary ireo orinasa mikasa ny handray anjara amin'izy ireo. Isan'andro, ny ekipa iraisam-pirenenay dia mamolavola sy manolotra votoaty avo lenta - amin'ny alàlan'ny fantsona media isan-karazany (anisan'izany ny gazetiboky, horonan-tsary, media nomerika, ary media sosialy) amin'ny fiteny maro - mifandraika amin'ny famolavolana elektronika sy elektronika DIY. www.elektormagazine.com
4

Hevitra ato Anatiny
Hevitra ato Anatiny
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 Chapter 1 · The Arduino IDE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
TAPITRAview of the Arduino IDE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 Installing and Launching the IDE. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 Using the Boards Manager . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 Upload Programs. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 Using the Library Manager . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 Using the Serial Monitor. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 Using the Serial Plotter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 Platform Notes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 Best Practices for Using the Arduino IDE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 Chapter 2 · Brief Introduction to C/C++ Programming . . . . . . . . . . . . . . . . . . . . . 17 Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 Variables and Constants. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 Control Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 Hello-World Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 Chapter 3 · Writing and Running Your First Arduino Program . . . . . . . . . . . . . . . . 20 The Arduino IDE Workflow . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20 Creating Your First Arduino Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20 Uploading the Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20 Running the Program on Different Target Platforms . . . . . . . . . . . . . . . . . . . . . . . . . . 21 Testing Serial Communication. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22 Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 Chapter 4 · Working with Digital Signals. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24 What Are Digital Signals? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24 Configuring Digital Pins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24 Controlling an LED . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25 Reading a Pushbutton . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
5

Programming Microcontrollers in C/C++ Using Arduino
Pushbutton-Controlled LED Sequence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 Platform Notes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28 Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28 Chapter 5 · Working with Analog Signals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30 What Are Analog Signals? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30 Measuring Analog Signals. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30 Measuring a Voltage and Displaying It on the Serial Port . . . . . . . . . . . . . . . . . . . . . . 30 Generating Analog(-like) Signals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32 Controlling LED Brightness with PWM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32 Controlling LED Brightness with a Potentiometer. . . . . . . . . . . . . . . . . . . . . . . . . . . . 33 Using True Analog Output (DAC) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33 Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34 Chapter 5 · Working with the Serial Port . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35 What Is Serial Communication? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35 Baud Rate and Data Format . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35 Opening a Serial Connection. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35 Serial.print() vs Serial.println() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36 Sending Numbers and Text. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36 Receiving Data Over Serial . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37 Controlling an LED via Serial . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37 Platform Notes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38 Common Pitfalls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39 Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39 Chapter 6 · Working with I²C . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40 What Is I²C? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40 Master and Slave Devices. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40 Pull-Up Resistors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40 I²C Pins on Common Boards. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40 Using I²C in Arduino (Wire Library) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41 Scanning the I²C Bus . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41 Writing Data to an I²C Device. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42 Reading Data from an I²C Device . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
6

Hevitra ato Anatiny
Logic Levels and Level Shifting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43 Repeated Start Conditions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44 I²C OLED ­ Hello, Display! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45 Platform Notes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46 Common Pitfalls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47 Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47 Chapter 7 · Working with SPI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48 What Is SPI? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48 SPI vs I²C . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48 SPI Pins on Common Boards. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48 Using SPI in Arduino (SPI Library). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49 SPI Modes and Timing (CPOL/CPHA) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49 Selecting a Slave Device . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50 Transferring Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51 Using SPI Transactions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51 Comparing Serial, I²C and SPI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52 Platform Notes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52 Logic Levels and Electrical Considerations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53 Common Pitfalls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53 Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53 Chapter 8 · Working with Time . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54 Why Time Matters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54 Blocking Delays with delay() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54 Measuring Time with millis() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54 Rollover and Unsigned Arithmetic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55 Measuring Short Intervals with micros() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55 Hardware Timers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56 Choosing the Right Tool . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60 Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60 Chapter 9 · Working with Interrupts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61 What Is an Interrupt? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61 Polling vs Interrupts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
7

Programming Microcontrollers in C/C++ Using Arduino
Rules for Writing ISRs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61 External Interrupts (GPIO) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62 Debouncing Interrupts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63 Sharing Data Between ISR and loop() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64 Platform Notes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65 Timer Interrupts as Periodic Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66 Common Pitfalls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66 Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66 Chapter 10 · Interacting with the Real World . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67 Transducers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67 Sensors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67 Key Sensor Characteristics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67 Interfacing Sensors with Microcontrollers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68 Examples of Common Sensors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68 Best Practices for Sensor Integration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69 Actuators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69 Safety and Power Considerations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70 Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70 Chapter 11 · Example Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71 Basic Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71 Machine Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72 Alarms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74 Robotics & Servo Motors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76 Automotive . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77 Home Automation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78 Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82 Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83
8

Documents / Loharano

Elektor ESP32 Programming Micro Controllers [pdf] Boky Torolàlana
ESP32 Programming Micro Controllers, ESP32, Programming Micro Controllers, Micro Controllers, Controllers

References

Mametraha hevitra

Tsy havoaka ny adiresy mailakao. Voamarika ireo saha ilaina *