Hitoshi Hasumi
A Beginner's Complete Guide to Microcontroller Programming with Ruby

Summarized using AI

A Beginner's Complete Guide to Microcontroller Programming with Ruby

Hitoshi Hasumi • October 07, 2023 • Bangkok, Thailand

In his talk 'A Beginner's Complete Guide to Microcontroller Programming with Ruby,' Hitoshi Hasumi introduces attendees at RubyConf TH 2023 to the fundamentals of programming microcontrollers using Ruby. Hasumi emphasizes the increasing importance of microcontrollers, which serve as the core of numerous electronic devices, and aims to provide a comprehensive starting point for beginners.

He organizes the session into four main parts:

  • Preparation: The speaker identifies that only three items are needed to start: a Raspberry Pi Pico, a USB cable, and a terminal emulator. The Raspberry Pi Pico is highlighted for its specifications, featuring an RP2040 chip capable of running in a 'bare metal' mode without an operating system.

  • Getting Started: Hasumi walks through the setup process for initializing the Raspberry Pi Pico using a specific .zip file from GitHub, followed by connecting the device to a PC and using a terminal emulator, such as GTKTerm or TeraTerm. He explains the use of the R2P2 shell, which allows users to execute Ruby commands, effectively enabling programming through a UNIX-like environment.

  • Exploring GPIO: The speaker demonstrates how to control GPIO pins for basic operations, like blinking an LED. He explains the significance of GPIO in microcontroller applications, giving a hands-on example of how to write simple commands to control hardware.

  • Understanding Sensors: Expanding on practical applications, Hasumi introduces readers to the Analog-to-Digital Converter (ADC) feature of the RP2040. In a live demonstration, he shows how to read temperature using a thermistor and explains the underlying electrical principles such as Ohm's Law.

Additionally, he discusses further applications of Pby, including R2P2 and PRK, a firmware for DIY keyboards, illustrating how Ruby can simplify microcontroller programming tasks. Hasumi concludes the session by inviting the audience to RubyKAG 2024 in Okinawa, Japan, and encouraging continued engagement with Ruby and microcontroller programming. The overall takeaway is an encouragement to leverage Ruby for creating microcontroller applications step-by-step, utilizing available libraries and tools for development, and fostering a community around this growing field.

A Beginner's Complete Guide to Microcontroller Programming with Ruby
Hitoshi Hasumi • October 07, 2023 • Bangkok, Thailand

A talk from RubyConfTH 2023, held in Bangkok, Thailand on October 6-7, 2023.
Find out more and register for updates for our next conference at https://rubyconfth.com/

RubyConf TH 2023

00:00:07.279 Today, I will guide you through the world of microcontroller programming with Ruby. The title of my talk is 'A Beginner's Complete Guide to Microcontroller Programming with Ruby.' My name is Hitoshi Hasumi.
00:00:12.759 Nowadays, microcontrollers are the brains behind countless electronic devices, from household appliances to robotics and even satellites in space. I hope that my talk will be the very first step for you to start thinking about using Ruby on microcontrollers. So let's get started! Before we dive in, here's what we will cover in this session: Part one will cover preparation, part two will be about getting started with microcontrollers, part three will explore Pby further, and part four will discuss Pby under the hood.
00:00:34.840 But before we get into it, let me introduce myself. My name is actually Hitoshi Hasumi; 'H' is an internet name. I am the creator of the Pby and the PRK firmware, which I will mention later. I am a committer for CIRB and Ring, and I was the first prize winner of the Fuk Ruby Award in 2020 and 2022. I was also a finalist nominee for the Ruby Prize two years ago due to my work with Pby.
00:01:20.840 Let's begin with part one, where we will cover the essentials you need to get started with microcontroller programming. You only need three things: a Raspberry Pi Pico, a USB cable, and a terminal emulator on your laptop. In short, just get a Pico. If you can manage other RP2040-based controllers, that should be okay.
00:01:43.200 The Raspberry Pi Pico is a microcontroller board featuring the RP2040 chip, which has a dual Cortex M0+ core, 264 KB of RAM, and 2 MB of flash memory. It generally runs without an OS, a method referred to as 'bare metal.' On the other hand, Raspberry Pi is a single-board computer that typically requires an operating system like Raspbian or Windows.
00:02:19.599 For terminal emulators, options include GTKTerm for Linux, TeraTerm for Windows (which is made in Japan), and possibly others for Mac OS, though I don't use Mac OS myself.
00:02:40.280 Now, let's begin with the setup, which consists of only four steps. First, download the latest RP2040-R2P2.zip file from GitHub and unzip it to a file named R2P2.uf2. The '.uf2' format is a universal binary format developed by Microsoft that targets portability. Next, connect the Pico to your PC using a USB cable while pressing the boot select button. You should find the RPI-RP2 drive in your file manager. Now, drag and drop the UF2 file into the RPI-RP2 drive. Finally, open the appropriate serial port on your terminal emulator.
00:04:05.840 What is a terminal emulator? Let me show you. Here we have the Pby and the R2P2 shell, which is a UNIX-like shell that runs on the Raspberry Pi Pico and is written in Ruby. You can use commands such as 'pwd' to display the current working directory or 'ls' to list files. If there are no files, you might type 'mkdir mydir' to create a directory. Let's try that now.
00:04:44.600 I’m executing the command to create a directory named 'mydir.' This time it's going to be created successfully. It appears that the system has a file system written in Ruby, and the R2P2 shell includes an IRB (Interactive Ruby Shell), as I am the maintainer of IRB. Let's print 'Hello, World!' and verify that it works. Yes, it's working!
00:05:30.759 Now, your Ruby script is going to be compiled into MRuby VM code and executed on the Pico. This means that Pby contains an MRuby compiler that can run on a one-chip microcontroller. As we move into part two, you are now prepared to start playing with microcontrollers. Let's dive deeper into how to get started with the microcontroller.
00:11:08.440 GPIO (General Purpose Input/Output) is the most fundamental functionality of a microcontroller for operating digital devices. For example, you can detect switches and buttons by using a GPIO pin in input mode. If you configure the GPIO pin to output mode, you can turn on an LED. Now, let’s get to the demo time. Here, I'll set up GPIO pin 25, which is connected to an onboard LED, and set its direction to output. Let's see what happens.
00:12:32.399 I will write a simple program to blink the onboard LED. If I write 'gpio 25 = 1' and then sleep for one second before writing 'gpio 25 = 0,' the LED should blink. Thank you for your patience! For those new to microcontrollers, it may seem like magic, but I will show you the equivalent circuit for blinking an LED using discrete components: an LED and a resistor. As you can see, the current flows through the LED and returns to ground, forming a closed loop.
00:14:51.440 You can control the brightness of the LED by adjusting the resistance. By changing the value of the resistor in the circuit, you can increase or decrease the current passing through the LED, thus changing its brightness. Now we are entering a study portion regarding electrical magnetism. The most important concept to know is Ohm's Law, which relates voltage, current, and resistance. Next, we will discuss the analog-to-digital converter (ADC), which converts an analog voltage to a digital value.
00:17:24.640 The RP2040 has a 12-bit ADC depth, giving it the ability to take values from 0 to 4,095. Typically, ADCs are used for applications like temperature sensors and joysticks. Let me demonstrate the temperature sensor application and show you how to read voltage from it. Currently, the temperature value is around 20°C. If I touch the chip, the temperature reading should increase, demonstrating how we can sense temperature with a thermistor, which changes its resistance based on temperature.
00:20:50.560 The resistance of a thermistor typically indicates 10 kΩ at 25°C. In the program, we can calculate the temperature using logarithmic equations. Now let's explore P by further. There are two main applications for P: R2P2, a UNIX-like shell system written in Ruby, and PRK, a keyboard firmware framework for DIY keyboards, where you can define keymap behavior. R2P2 also has a multi-line editor, which is quite handy for scripting.
00:23:06.560 In this shell, you can use traditional commands like 'date' to display the current time, and you can build small applications directly from here. R2P2 supports creating executable files for practical applications. It is built to facilitate easy communication and programming of microcontrollers. Additionally, we have a development ecosystem around Ruby for microcontrollers, with various libraries available to facilitate the porting of applications to other microcontrollers.
00:26:19.559 In conclusion, Pby is a Ruby implementation for one-chip microcontrollers, equipped with essential libraries for GPIO and ADC. You can develop microcontroller applications one step at a time using R2P2 and IRB. I’m currently working on a Bluetooth feature for Raspberry Pi Pico. As we wrap up, I want to invite you to RubyKAG 2024, which will take place in Okinawa, Japan, this coming May. It will host over 1,000 attendees and feature many technical talks.
00:28:44.799 All talks will have simultaneous interpretations available in English, making it accessible for everyone. If you enjoyed my talk today, I'd love for you to hit the QR code for more information on upcoming events. Thank you for listening, and I hope you found this information helpful.
Explore all talks recorded at RubyConf TH 2023
+12