Where I discuss in detail, adding a second serial port to the Uno, soft_serial, and how to use it.
While I was developing code for a robotic arm, I realized it would be best, if I used one serial port for communicating with the arm and another serial port to accept commands for the arm. Thus I created a second serial port, called soft_serial.
The port didn’t need to be fast, as it would be used as a communication link between someone on a keyboard and the Uno. It needed to do the following:
Where I discuss how to improve on the serial input of C and the ATmega328P and adding a second serial port, soft_serial.
In the example serialio_string (code on GitHub or below), I demonstrate the problem with reading text from the serial port. If you use scanf()
, it appears to work well, except you can easily over-run the buffer. For example, the program asks for “up to 7 char”, however, it will accept as many as you are willing to type. More than likely, after about 20 characters, the microcontroller will crash.
Where I demonstrate how to use additional tools such as the serial monitor, tio, along with VS Code on your PC to develop code on your Raspberry Pi for the Arduino Uno (ATmega328P).
Even though VS Code has a serial monitor, in fact, in has many serial monitor extensions, I’ve found tio to be the best. The VS Code version has failed to connect too many times, while tio has been rock solid. It also has a nice configuration method and is easy to connect/disconnect.
Where I demonstrate how to use VS Code on your PC to develop code on your Raspberry Pi for the Arduino Uno (ATmega328P).
Now that you have the best setup for developing code for the Arduino Uno, how do you use it?
You will be using two programs on your PC to interact with the Raspberry Pi, VS Code and your terminal program (Windows Terminal, macOS Terminal/iTerm/Warp) also known as your CLI. The former is your coding environment and the latter provides administrative capabilities along with some applications like a serial monitor (tio).
Where I setup the Standard C tool chain for the ATmega328P on the Raspberry Pi, however I use VS Code on my Mac (or Windows) for development and connect via SSH to the Raspberry Pi.
This is the best setup, period. Why? It provides the most simple tool chain installation, a stable, known platform and enables hardware debugging, should you want to do so.
Linux has always been the preferred platform for open-source embedded development, as the Standard C tools are native to Linux. macOS is second, as its Unix bloodline, enables it to resemble a Linux system quite easily. Windows is a distant third, as the hoops to jump through to build the tool chain are a pain (Note: WSL2 still can’t easily interact with the serial port).
Updated: Where I discuss a simplified approach to development automation using make in AVR_C, add the ability to use Arduino tools.
The previous explanation provided the Makefile from Elliot Williams and can be viewed here. I have made several significant changes to the file since:
DEPTH
variable to each local makefile (the one in the examples folder)Here are detailed instructions as to how to use make, the Makefile and env.make to configure and execute your development process.
Where I discuss the various methods of increasing execution speed in Forth and demonstrate the ease in doing so.
In this entry, I showed the relative speeds of board/language combinations from a ARM M0 running CircuitPython to a Pi Pico running C. While execution speed is important in embedded microcontrollers, development speed is equally important along with ease of development. A great attribute of Forth is its ability to combine all three. In this entry, I’ll show how you can start with a simple, fast example and make it significantly faster without great difficulty.
Where I discus debouncing buttons in Flashforth as well as indexing arrays.
Elliott Williams had a great post several years ago as to the best solution to debouncing buttons. The technique worked extremely well in C, and I wanted to attempt it in Forth. It actually works better in Forth, as Forth easily handles the 16-bit data to de-bounce properly. I’ll work through the technique in FlashForth as well as spend some time at the end as to the execution timing of the technique.
Where I describe in detail how to compile a new version of FlashForth.
Its helpful to be able to compile a new version of FlashForth. A simple reason is to increase the baudrate from 38400 to 250000, increasing your productivity. It isn’t difficult, however the tool chain can be a bit complex.
You will need to install both the MPLAB IDE and the XC8 compiler for this exercise. You will also need a hardware programmer such as the Atmel ICE or Microchip SNAP. And finally, you will definitely need the source code of which to compile, Flashforth on github.
UPDATED: This page contains a both a Forth hardware abstraction level (HAL) much like the m328def.inc file for AVR programming in C and an example of debouncing buttons.
This HAL provides the words:
The Arduino pins are defined by bit port so one can use the following commands using the Arduino pin number. This is true for pins D0-D13.
high ( bit port -- ) set a Arduino pin high
low ( bit port -- ) set a Arduino pin low
toggle ( bit port -- ) toggle output value on an Arduino pin
output ( bit port -- ) set Arduino pin as output
input ( bit port -- ) set a Arduino pin as input
pullup ( bit port -- ) set Arduino pin as input_pullup
read ( bit port -- f ) read a Arduino pin, returns bit value
For example:
Why the microcontroller datasheet is so important to programming in Forth.
The ATmega328P datasheet is critical to understanding how to program the 328P. Exploring it with Forth is the joy of Forth as Forth allows you to interactively test commands, ports, timers, ADC’s etc of the 328P. And once you are convinced you have the correct set of commands to make what you want to happen, you can codify it into a word that becomes part of the vocabulary of Forth.
Where I use Forth to develop a better understanding of the ATmega328P PWM capabilities. Revised from original post on April 17, 2021
Pulse Width Modulation (PWM) is a technique used to control analog circuits using digital signals. It is the capability to change either the frequency or duty cycle of a digital signal. The former is the number of times the signal switches from low to high in a given period of time and the latter, is how long it is either high or low.
Where I describe how to use Forth with an Arduino Uno and make it easier to program the Uno in Forth.
The Arduino Uno benefits from an incredible software framework. Between the programs already developed by the Arduino organization, and the hundreds of libraries which were created by others, you can find a program which does what you need. This makes it extremely easy to hit the ground running with a project. FlashForth isn’t like this.
Where I demonstrate your first embedded application, much like the C Language “Hello, World” program, blink.
To begin to understand how to program in Forth, I’ll iterate over several versions of blink.
First, let’s interactively light the built-in LED. On an Uno, we know it is pin 13, which corresponds to Port B, bit 5 on our microcontroller, the ATmega328P.
This is lesson one, all references in Forth, are references to the ATmega328P, which is the microcontroller on the Uno, and not the pins on the Uno. This is why the data sheet for the ATmega328P is so important. I’ll call out the specific references as we go, however, the more you use FlashForth, the more you will come to read and understand the ATmega328P datasheet. As you begin to write more Forth programs, you will be able to create the words necessary to reference the Uno, just as you did with the Arduino framework.
UPDATED: Where I describe how to use FlashForth, a phenomenal version of Forth for the ATmega328P to learn how to use Forth in an embedded system.
This video demonstrates the ease of loading FlashForth on to an Arduino Uno, and replacing it with Optiboot, when you wish to use the Arduino software framework, again.
Where I use examples from “The C Programming Language”, Kernighan & Ritchie, to demonstrate string copies, using pointers and how to check for buffer overflows.
I find it very helpful to periodically review/read “The C Programming Language”, Kernighan and Ritchie (K&R). In this past review, I ran across some examples provided on pages 105-6 (Second Edition) as to using pointers to copy strings. There were 4 examples provided, with the last commented as “the idiom should be mastered”, which is an implication of “this is a programming best practice”. Perhaps, for the code in question is quite simple and extremely powerful, that said, it leads to the issue of buffer overflows, for which I provide a mechanism to resolve.
Where I demonstrate how to reload Optiboot on an ATmega328P (Arduino Uno) and reload the Catarina bootloader on to an ItsyBitsy 32U4 board as well.
Sometimes a board’s bootloader can become corrupted or you have consciously overwritten the bootloader (See Forth on this site), and you want to reload it. This page demonstrates how to do this with the ATmega328P (Uno) and board with the AVR 32U4 (Adafruit ItsyBitsy 32U4). It also discusses the different bootloaders available for the 32U4.
Where I expand on creating a web server application on a Pi Pico W by incorporating web sockets.
In the first part of this entry, I introduced the ability to develop an HTTP-based web server using solely HTML to control the Pi Pico pins. I focused on HTTP protocol as you can write everything in the relatively simple language, HTML and it works. I used checkbox or radio button inputs to a form to control pins on the Pico. This is a far more simple approach than using JavaScript. It does introduce some significant delays as each form submission must make a complete round trip (i.e. a page reload).
Where I discuss various serial monitor applications and why I believe CoolTerm is the best one.
The serial port is an invaluable, if not mandatory tool, for developing programs in embedded computing. Given the popularity of software development for embedded computers, one would think there would be a clear answer or solution. For several years, I wasn’t able to determine the best solution. However, after more work on my part to understand a specific solution, I can conclusively say use this multiplatform, free solution: CoolTerm
Where I demonstrate an enhanced on-board shell for RP2040 (Pi Pico and Pi Pico W) using MicroPython, upysh2.py.
While Thonny can be a bit cumbersome if you already know Python, it does have advantages in being able to provide fundamental file management capability. Its helpful to be able to copy files, list files and folders on the Pico, delete files and rename them. Its biggest issue is that a hardware reset will disconnect the board and reconnecting with the board, causes the board to soft reset. Which prevents you from easily running a main.py program via reset. I have also tested this using the new Arduino Lab for MicroPython and it suffers from the same problem. Very disappointing.