Outils pour utilisateurs

Outils du site


issue167:micro_this_micro_that

Ceci est une ancienne révision du document !


March 2021 - Welcome to the first of the “Micro This Micro That” series of articles. Hopefully I will be able to keep up with doing two articles each month. So what is Micro This Micro That? The goal is to provide readers with information and projects to demonstrate MicroPython and CircuitPython compatible microcontroller boards and sensors. Occasionally, I might throw in a Raspberry Pi board to show the differences between Python, MicroPython and CircuitPython. These articles will have code, schematics, diagrams and more – trying to provide real world programming techniques for microcontrollers. I’ll attempt to point out the similarities and differences between CircuitPython, MicroPython, and “Full grown” Python. Believe me, there are TONNES of differences. I might even try to include an Arduino project here and there, but my main thrust will be on MicroPython and CircuitPython. Before we get into any of the projects, let’s take a look at exactly what a microcontroller actually is. From the AllAboutCircuits website, we get a very clear explanation of this…

“Microcontrollers are small, versatile, inexpensive devices that can be successfully implemented and programmed not only by experienced electrical engineers but also by hobbyists, students, and professionals from other disciplines.” Today, there are many different types of Microcontrollers out there. The line of Arduino products is a prime example of Microcontrollers. The group at Adafruit also have many products as well as the folks at Sparkfun. Most recently, the Raspberry Pi Foundation announced and released the Raspberry Pi Pico board that is based on their brand new RP2040 chip, which is their first Microcontroller product. The response to the RPi Pico has been astounding, and there have been many companies that have announced products based on the RP2040 chip. The most exciting thing for those of us who program in Python is that the RPi Pico can not only be programmed using C/C++, like the Arduino, but with both MicroPython and CircuitPython. Many of the products from Adafruit can be programmed using CircuitPython, but the Pico lets you choose between all three.

Let’s look at MicroPython, in general, for a moment. MicroPython is the brainchild of MicroPython.org and its founder Australian programmer and physicist Damien George, who created it to support their brand of Microcontrollers. The MicroPython language is a special subset of Python 3.4, originally designed to run on their own pyboard Microcontrollers. While it’s a really neat little board with many features, the price is about 30 USD. That puts it out of the reach for many developers who have a shoestring budget for hobby work. As cool as that sounds, there are some limitations. If you normally program using Python 3.7+, you will find things like the “f-strings” will throw syntax errors.

At this writing, libraries like numpy, pandas and matplotlib are not currently available in the forms you are used to. However, a small subset of numpy is currently under development, and matplotlib shouldn’t be far behind. This however, exposes a problem with MicroPython. There are at least eight different implementations of MicroPython (one for each different basic board type) out there, and drivers and libraries for one board don’t necessarily work on another, and, in many instances, isn’t even available on the board you are trying to develop for. To make matters worse, CircuitPython, while a different port of MicroPython, is not compatible, and is coded, for the most part, differently than MicroPython. Libraries don’t move from one to another.

For years, I’ve told my son, and many others “If you live or work on the bleeding edge, you WILL get cut!”, and believe me, that’s SO very true when dealing with Microcontrollers. I’ll explain more in a little bit. All that having been said, I won’t only deal with the RPi Pico board in this series of articles. Yes, I will concentrate on it for a few articles, but I also intend on talking about the Adafruit FeatherS2 microcontroller and some of the Arduino boards, and maybe some of the boards from Sparkfun and others as my budget allows.

Raspberry Pi Pico As I write this, the RPi Pico has been available for a little over one month. As you can see in the image below, it’s a tiny little thing that’s about 2” x 1”. You don’t actually need to have any external LEDs or sensors to see the board work, since there is an addressable LED already on the board. You don’t even need to solder the header pins for our first two projects. Of course, you’ll also need a computer to talk to and power the RPi Pico. You can use any type of computer. Many people use a Raspberry Pi 3 or 4, and I use my normal Linux Desktop. I am sure that it also will work on a Windows or Mac PC.

In addition, you will need a copy of Thonny IDE. If you are using a Raspberry Pi as your connection to the Pico, you probably already have Thonny. Just make sure that it’s version 3.3.3 or better. If you are using a Linux or other OS PC, you probably will need to download it. You need version 3.3.3 or greater. The latest version (as of this writing) is 3.3.5. To install it on your Linux machine, you can simply use wget in a terminal… wget -O thonny-latest.sh https://thonny.org/installer-for-linux If you don’t have wget, you can install it by using: sudo apt install wget Once you have the file, change the permission to executable, then run it from the terminal command prompt.

Now start Thonny, and get your RPi Pico and Micro USB cable ready. On the Pico, hold down the white BOOTSEL button and, while holding it down, plug in the cable. Make sure the other end is connected to your computer. In a moment, you will see a new file manager window pop open. Once you see this window, you can release the BOOTSEL button. Now back to Thonny, and select Run|Select Interpreter from the main menu. You should see a window pop up that looks something like this: Click on the [Install] button; you should see it start to download the MicroPython for the Pico. When it’s finished, you will see a window like this… Now we can start programming!

Project #1 In project #1, we’ll do a very simple program that simply blinks the onboard LED. The code is below. Now before I explain the code, click on the Run button in Thonny and see what happens. You should see the LED near the USB port come on for about 3 seconds and then go off for three seconds, and start all over again. Now I’ll explain the code. First, you will need to import two libraries, machine and utime. The machine library will almost always be needed, and utime is a MicroPython time library similar to the “big brother Python” time library, only smaller and designed just for MicroPython.

Next, we create an object by pointing to the pin number and telling it whether it’s supposed to be an input or an output pin. We’ll call this object “led_onboard”. Notice that we are using the GPIO number, not the physical pin number. Other systems like CircuitPython use the physical pin instead of the GPIO number. We’ll get more into that another time, but for now, just remember that when you are using MicroPython, you will want to use the GPIO designation. Next, we create a forever loop that sets the value to 1 (high) or 0 (low). When the pin switches to high, the voltage (in this case) goes to 3.3 volts which turns on the LED. When the pin switches to low, the voltage goes to 0 volts. Once we set the value to 1, we sleep for three seconds then set the value 0 and sleep 3 more seconds, then start it all over again. Be sure to save your code on your main machine, just in case something happens to your Pico.

Project #2 In project #2, we’ll make the onboard LED do something a little bit different. Rather than just blinking on and off, we’ll use the PWM (Pulse Width Modulation) function. We’ve done this a long time ago on the Raspberry Pi. I won’t go too deeply into the code with you right now, we’ll revisit this in a future article, plus, as I said, we did a similar project on the Raspberry Pi a few years ago. I just think that the throbbing LED is cool and especially with the onboard led. You should be able to find the code presented here at my github repository at: https://github.com/gregwa1953/MicroThisAndMicroThat

Since we have used the onboard LED for these two projects, there were no external components required. However, for our upcoming projects, we’ll need a breadboard, LEDs, jumper wires, resistors, etc. One word of warning though. ALL voltages for the RPi Pico are 3.3 volts. Many older sensors and displays are 5 volt, and will cause problems with the Pico, up to and including burning out the Pico. You should consider getting a Logic Level Converter that will safely change the voltages from 5 volts to 3 volts. Here are two sources that can get you started in your search… https://www.sparkfun.com/products/12009 https://www.adafruit.com/product/757 Also, before you start interfacing devices to your Pico, you should get your hands on the pinout of the board. You can download a copy of the RPi Pico pinout for your easy reference… https://datasheets.raspberrypi.org/pico/Pico-R3-A4-Pinout.pdf

One final thought. Earlier I said “If you live or work on the bleeding edge, you WILL get cut!”. The RPi Pico has only been out a little over a month as I write this. The MicroPython for the Pico (http://micropython.org/download/rp2-pico/) seems to be updated on a daily basis and is missing many of the “standard” modules that other boards enjoy. CircuitPython has other things that are missing for the Pico, and is being worked on as fast as the programmers can get the code out. There are so many displays and sensors out there that have no current support on the Pico either under MicroPython or CircuitPython, or have support on one, but not the other. There is very limited support for using WiFi on the Pico at this point. I suppose this is due to the fact that the Raspberry Pi group never thought that anyone would need networking support on a board this small and low cost.

This is all to be expected this early in the process. Sales of the Pico are so swift, that many retailers haven’t gotten a single stick and are still in a “back order” mode, or they have gotten some, but sold out within a day or two. The Arduino RP2040 device is coming with WiFi support, but still doesn’t have a release date or price. Sparkfun and Adafruit have boards coming out based on the RP2040, but still haven’t been released. If your display or sensor is extremely old or had limited sales, you might have to “bite the bullet” and consider getting a newer version that does have support, or look into writing your own driver library. I’m certain that in the next month or so, many of these issues will be solved. I don’t think that anyone thought that the Pico or the RP2040 would take off as strongly as it has. The price of the Pico makes it perfect for someone who wants to break into Microcontroller programming and experimentation. Until next time, as always; stay safe, healthy, positive and creative!

issue167/micro_this_micro_that.1617295475.txt.gz · Dernière modification : 2021/04/01 18:44 de d52fr