Microcontroller
A Beginner's Complete Guide to Microcontroller Programming

Summarized using AI

A Beginner's Complete Guide to Microcontroller Programming

Hitoshi Hasumi • October 04, 2023 • Vilnius, Lithuania

In this video presentation at Euruko 2023, Hitoshi Hasumi introduces viewers to the field of microcontroller programming using Ruby, providing a comprehensive foundation for beginners. He explains the fundamental function of microcontrollers, particularly focusing on the Raspberry Pi Pico and its capabilities. The session is structured into four main parts, making it easy for attendees to grasp the concepts.

Key points covered include:

- Introduction to Microcontrollers: Microcontrollers serve as the core components in various electronic devices, and the Raspberry Pi Pico is highlighted as an ideal starting point. It's powered by the RP2040 microcontroller, which runs bare metal, meaning it doesn't require an operating system.

- Initial Setup: Users are guided through downloading the necessary firmware (RP2040 UF2 file) and connecting the Pico to a computer, emphasizing the use of terminal emulators like GTKTerm or Tera Term for interfacing.

- Basic Commands and GPIO Usage: Hasumi introduces General-Purpose Input/Output (GPIO), explaining its importance in digital operations. He demonstrates how to control an onboard LED using GPIO, showing commands for turning it on and off, and provides an analogy for understanding current flow using Ohm's law.

- Analog-to-Digital Converters (ADC): The ADC functionality is explained, particularly for reading analog values such as temperature, illustrating the connection between theory and application through a thermistor example.

- Advanced Applications: The presentation discusses advanced uses of P Ruby, including the creation of Ruby scripts for automating processes on startup and various peripheral integrations. Hasumi also touches on communication protocols like SPI and I2C used in microcontroller projects.

- Encouragement for Exploration: Hasumi concludes by inviting the audience to explore the potential of microcontroller programming with P Ruby, highlighting ongoing developments and projects available for further learning.

In summary, this presentation offers a thorough introduction for beginners interested in programming microcontrollers with Ruby, covering essential concepts, practical demonstrations, and encouraging exploration of advanced applications in the field. Key takeaways include the importance of GPIO and ADC, as well as the numerous possibilities with microcontroller programming using Ruby.

A Beginner's Complete Guide to Microcontroller Programming
Hitoshi Hasumi • October 04, 2023 • Vilnius, Lithuania

EuRuKo 2023

00:00:12.400 Today, I will guide you through the world of microcontroller programming with Ruby.
00:00:18.840 My name is Hitoshi Hasumi. It's a great honor to be a part of Euruko. Thank you very much.
00:00:25.000 Microcontrollers are now the brains behind countless electronic devices, from household appliances to robotics and even satellites in space.
00:00:30.519 Today, I will show you the very first steps of connecting the power of Ruby to microcontrollers. Here is what we will cover in this session: Part One - Preparation; Part Two - Getting Started with Microcontroller; Part Three - Exploring PB further; and Part Four - PB and the hood.
00:01:02.559 Before that, let me introduce myself. My name is Hitoshi Hasumi, and my internet name is Hosii. I am the creator of P Ruby and PK firmware that I will mention later. I am also a contributor to CES IRB.
00:01:10.080 Additionally, I was the first PR winner of the Fuk Ruby Award in 2020 and 2022. Lastly, I was a final nominee for the P Ruby Prize in 2021 due to my work on P Ruby. Let’s get started with Part One.
00:01:53.880 We will cover the essentials needed to get started with microcontrollers. You will need a Raspberry Pi Pico or another RP2040-based controller. I will explain what it is later, along with a USB cable and a terminal emulator on your laptop. In short, buy a Raspberry Pi Pico. That's it! The Raspberry Pi Pico is a microcontroller board whose MCU is RP2040, which includes a Cortex M0+ dual-core processor and 264 KB of RAM with 2 MB of flash memory.
00:02:13.920 It generally runs without an OS, also called bare metal. In contrast, the famous Raspberry Pi is a single-board computer that usually requires an OS like Raspberry Pi OS or Windows for ARM chips. For terminal emulation, popular options include GTKTerm for Linux and Tera Term for Windows. I will use GTKTerm today, which I think works for Mac as well.
00:02:36.560 Some traditional C tools may have line feed issues, so I recommend using GTKTerm or Tera Term.
00:02:44.560 Let’s begin. There are four steps to follow: First, download the latest RP2040 UF2 file from GitHub. To be exact, you can download the zip file from my GitHub page, unzip it, and keep that ready.
00:03:02.800 By the way, RP2040 stands for Ruby on Raspberry Pi. Next, connect the Pico to your PC while holding the boot button. You should see the RPI RP2 drive appear automatically in your file manager, whether that's File Explorer on Windows or Finder on Mac.
00:03:14.720 Then, drag and drop the P to UF2 file into the RPI RP2 drive. UF2 means Universal Firmware Format, which was developed by Microsoft to facilitate communication between different platforms. It is essentially a binary file.
00:03:28.840 After that, open a proper serial port on the terminal emulator. Let me demonstrate. This Raspberry Pi Pico is already installed with the UF2 file, so I just need to restart it.
00:03:51.200 Here comes P Ruby! Thank you. The RP2040 shell should start now. This is a Unix-like shell running on the Raspberry Pi. Note that every computation occurs on the microcontroller, not on my laptop. My laptop acts only as a terminal emulator. For example, there is a file system here. I can execute commands like `ls` to list files, or `pwd` to see my current directory.
00:05:02.800 Commands like `mkdir` can be used to create directories. If I run `ls -l`, I can see detailed file listings. Additionally, IRB, the interactive Ruby shell, is running within the RP2040 shell.
00:05:09.440 This allows us to execute Ruby snippets, which are compiled into M Ruby code and executed on the fly. Let me show you. When I put `p 'hello'`, it prints 'hello'. This means P Ruby includes an M Ruby compiler that can run directly on the microcontroller.
00:05:45.679 Now, let's dive deeper. First, you need to know about GPIO, which stands for General-Purpose Input Output. GPIO is the most fundamental functionality for digital operations. You can use it to detect the state of switches or buttons, determining whether they are on or off in input mode. If you change GPIO to output mode, you can, for example, light up an LED by creating voltage.
00:07:04.240 The role of GPIO in a microcontroller is akin to what STD in standard input and output means in a Unix computer. By the way, do you use debug prints? Yes, debug prints are the ultimate method! Likewise, you can visualize the internal status of the microcontroller with LEDs. For example, fast blinking can indicate something is wrong with the microcontroller, making the GPIO very fundamental to remember.
00:08:28.240 Let me show you a demo using GPIO pin 25, which is connected to the onboard LED on the Raspberry Pi. If you write a zero, it turns off the LED.
00:09:01.079 This is what will happen. If I send a command to turn the LED on or off, you will see the results of those actions in real-time.
00:09:24.720 Understanding how the RP2040 shell and IRB work can feel like magic at times. To explain what's happening behind the scenes, this is the equivalent circuit for blinking an LED. Instead of using the onboard LED, you need one LED and one resistor. In this case, a 1k ohm resistor will do.
00:09:51.399 If GPIO pin 15 connects to the resistor, the current will flow through the LED and return to the ground pin. This essentially replicates the LED blinking function you saw earlier. The voltage logic level in the RP2040 is 3.3 volts, and the LED voltage drop should be 1.8 volts based on the LED's datasheet. By using Ohm's law, we can calculate the current that flows through the circuit.
00:10:43.519 Ohm's law states the relationship between three quantities: voltage (V), current (I), and resistance (R). With this relationship, we can derive that the current through the circuit is 1.5 milliamps, which influences how bright the LED shines. By increasing the current, say to 10 milliamps, the LED will blink brighter, and if you want it to blink dimmer, you can use a larger resistor.
00:12:10.080 Another important concept is Kirchhoff's circuit laws. These laws state that the algebraic sum of currents in a network of conductors meeting at a point is zero, and the directed sum of potential differences around any closed loop is also zero.
00:12:35.080 Let’s proceed to another essential peripheral: the analog-to-digital converter (ADC). While GPIO can handle voltages as either zero or one, ADC can manage values in between by converting analog voltages into digital values. The RP2040’s ADC has a 20-bit depth, which allows it to take on values from 0 to 4095, corresponding to 0 to 3.3 volts. Typical uses of ADC include reading temperature sensors and joysticks.
00:13:18.040 Let me show you an example. To use the ADC to read a temperature sensor, you will start by requiring the ADC library and initializing your reading. For instance, if you read a voltage of 0.7 volts, you can process this within the software to display the resulting temperature in Celsius. If you hold the chip, you will see the value increase as it warms up, demonstrating how the sensor reacts to temperature changes.
00:14:48.240 This temperature reading is an illustration to show how combined circuits using discrete components can function. This can be done with a thermistor, a type of resistor whose value changes with temperature, allowing for practical temperature sensors. Though today’s session is technical, I will not demonstrate this, as it mainly serves as an introduction.
00:15:37.680 Next, let’s explore PB further with a closer look at PB applications. Two major applications exist for P Ruby: one is the P2 shell system written in P Ruby, which resembles an operating system running Ruby, and the second is the PK firmware, a keyboard firmware framework allowing you to define keymaps and keyboard behavior using Ruby. Currently, I’m using my DIY keyboard that runs on Ruby.
00:17:10.760 The community is a warm one! In the RP2, we have a multiline editor, which you already saw working in IRB. However, the code base is quite different from the IRB. There are no common lines of code, as most commands are executable and written in Ruby. You can create your own shell commands as well, such as using the 'date' command to check the system time, which can be very useful in some applications.
00:19:02.400 Moreover, text editing in the shell is possible too. You can create and edit Ruby scripts directly in the terminal, though its functionalities may still have room for improvement. For new commands, just ensure that the files you create do not overwrite existing ones.
00:20:20.240 Working with GPIO and ADC together can elevate your projects significantly. For example, if you made a program driving an LED that responds to temperature readings, you can define the GPIO pin and the interaction with the ADC readings to create an intelligent feedback system that adjusts accordingly.
00:22:57.920 Additionally, you can automate functionalities by creating scripts that run on startup. This way, when you switch on your Pico, it can perform predefined actions, like reading temperatures and adjusting outputs automatically.
00:25:20.799 Finally, let me explain some other peripherals and serial communication protocols, like SPI and I2C. These protocols are crucial for communication with various devices, including altimeters or LTE modules. I have already written libraries for those protocols to ease your exploration.
00:26:54.599 For example, I2C is a serial communication protocol used for connecting different components. You can use it with RTC modules that maintain time, even when the microcontroller is powered down. These integrations show how easy it is to keep everything synchronized.
00:28:53.840 With PB Ruby, you can build a variety of applications, as demonstrated in my earlier work with keyboards. It's important to mention that P Ruby is a Ruby implementation targeting single-chip microcontrollers, such as the RP2040. Therefore, you can develop applications step-by-step using the RP2 and IRB.
00:30:28.319 As I'm currently writing new libraries for Wi-Fi connectivity and exploring ways to broaden the reach of microcontrollers beyond the RP2040, I encourage more people to join in on this exciting journey of microcontroller programming.
00:32:12.799 If you're interested in learning more, please visit the repositories I've set up. P Ruby is the implementation of Ruby language, RP2 the shell system, and PK is the keyboard firmware. Thank you so much for your attention.
Explore all talks recorded at EuRuKo 2023
+12