ESP32 Programming Micro Controllers
“
Thông số kỹ thuật
- 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)
Hướng dẫn sử dụng sản phẩm
1. Giới thiệu
This book provides guidance on programming microcontrollers in
C/C++ using Arduino. It includes examples for Arduino UNO,
Raspberry Pi Pico, and ESP32.
2. Bắt đầu
To begin using this book effectively, ensure you have the
necessary hardware such as the mentioned microcontroller
bảng.
3. Các bước lập trình
- Read through the relevant sections for the microcontroller
platform you are working with. - Theo dõi người yêu cũ được cung cấpamples and code snippets to practice
programming in C/C++. - Experiment with different projects and challenges to enhance
your skills.
KHAI THÁC. Xử lý sự cố
If you encounter any issues or errors while programming, refer
back to the examples and ensure correctness in your code.
Câu hỏi thường gặp
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
với người cũampBạn?
A: You will need the mentioned microcontroller boards such as
Arduino UNO, Raspberry Pi Pico, or ESP32 to practice the
exampđồng nghiệp.
Q: Is there online support available for additional
hướng dẫn?
A: For additional support or clarifications, you can visit the
publisher’s website or community forums related to Arduino and
microcontroller programming.
“`
Lập trình
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
Đây là một ấn phẩm Elektor. Elektor là thương hiệu truyền thông của
Elektor International Media BV PO Box 11, NL-6114-ZG Susteren, Hà Lan Điện thoại: +31 46 4389444
Đã đăng ký Bản quyền. Không phần nào của cuốn sách này được phép sao chép dưới bất kỳ hình thức tài liệu nào, kể cả sao chụp hoặc
lưu trữ trong bất kỳ phương tiện nào bằng phương tiện điện tử và dù là tạm thời hay ngẫu nhiên đối với một số mục đích sử dụng khác của ấn phẩm này mà không có sự cho phép bằng văn bản của người giữ bản quyền, ngoại trừ tuân theo các quy định của Đạo luật Bằng sáng chế và Thiết kế Bản quyền năm 1988 hoặc theo các điều khoản của giấy phép do Copyright Licensing Agency Ltd. cấp, 90 Tottenham Court Road, London, Anh W1P 9HE. Đơn xin phép chủ sở hữu bản quyền sao chép bất kỳ phần nào của ấn phẩm phải được gửi tới nhà xuất bản.
Tuyên ngôn
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 In sách điện tử ISBN 978-3-89576-721-0
© Bản quyền 2026 Elektor International Media
www.elektor.com Editor: Clemens Valens Prepress Production: D-Vision, Julian van den Berg Printers: Ipskamp, Enschede, Hà Lan
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
Nội dung
Nội dung
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 Chapter 1 · The Arduino IDE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
Quaview 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
Nội dung
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
Tài liệu / Tài nguyên
![]() |
Elektor ESP32 Programming Micro Controllers [tập tin pdf] Hướng dẫn sử dụng ESP32 Programming Micro Controllers, ESP32, Programming Micro Controllers, Micro Controllers, Controllers |
