ESP32 Programming Micro Controllers
“
Specifications
- 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)
Product Usage Instructions
1. Introduction
This book provides guidance on programming microcontrollers in
C/C++ using Arduino. It includes examples for Arduino UNO,
Raspberry Pi Pico, and ESP32.
2. Getting Started
To begin using this book effectively, ensure you have the
necessary hardware such as the mentioned microcontroller
boards.
3. Programming Steps
- Read through the relevant sections for the microcontroller
platform you are working with. - Follow the provided examples and code snippets to practice
programming in C/C++. - Experiment with different projects and challenges to enhance
your skills.
4. Troubleshooting
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
with the 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
guidance?
A: For additional support or clarifications, you can visit the
publisher’s website or community forums related to Arduino and
microcontroller programming.
“`
Programming
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
This is an Elektor Publication. Elektor is the media brand of
Elektor International Media B.V. PO Box 11, NL-6114-ZG Susteren, The Netherlands Phone: +31 46 4389444
All rights reserved. No part of this book may be reproduced in any material form, including photocopying, or
storing in any medium by electronic means and whether or not transiently or incidentally to some other use of this publication, without the written permission of the copyright holder except in accordance with the provisions of the Copyright Designs and Patents Act 1988 or under the terms of a licence issued by the Copyright Licencing Agency Ltd., 90 Tottenham Court Road, London, England W1P 9HE. Applications for the copyright holder’s permission to reproduce any part of the publication should be addressed to the publishers.
Declaration
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 Print 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, The Netherlands
Elektor is the world’s leading source of essential technical information and electronics products for pro engineers, electronics designers, and the companies seeking to engage them. Each day, our international team develops and delivers high-quality content – via a variety of media channels (including magazines, video, digital media, and social media) in several languages – relating to electronics design and DIY electronics. www.elektormagazine.com
4
Contents
Contents
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 Chapter 1 · The Arduino IDE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
Overview 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
Contents
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 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
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 / Resources
![]() |
Elektor ESP32 Programming Micro Controllers [pdf] User Manual ESP32 Programming Micro Controllers, ESP32, Programming Micro Controllers, Micro Controllers, Controllers |
