Download PDF Parts List View on YouTube Download Code

Near the end of January 2021, the Raspberry Pi Foundation dropped a bombshell with the introduction of the Pico, their entry into the world of microcontrollers.

Lots of capability and a breakthrough price tag has everyone watching the Pico, and today I’m going to put this little wonder through its paces.

 

So let’s get the MicroPython out because today we’ll be programming EVERYTHING on a Raspberry Pi Pico using MicroPython. We’ll see what four dollars worth of microcontroller gets you these days!

Introduction

For the folks at the Raspberry Pi Foundation to release a microcontroller was in itself a huge announcement, after all the makers of the world’s most popular single-board computer never had expressed an interest in microcontrollers.

Raspberry Pi Pico

But not only did the announcement of the Raspberry Pi Pico come as a surprise, but the fact that it was built around exclusive Raspberry Pi silicon was also even more of a shocker. Instead of building upon the mountain of existing code and support for ESP32 or SAMD21-based designs they chose to create their own microcontroller.

So when it comes to the Pico we are all starting from scratch. 

Raspberry Pi did release a ton of technical documentation, plus a great guide called Get Started with MicroPython on Raspberry Pi Pico.  It’s available in softcover, and as a PDF download as well.

But otherwise, there is not, at the moment, a lot of “real-world” information about the Pico. Undoubtedly that will change soon, especially as Raspberry Pi has licensed other manufacturers, including Adafruit, to use the RP2040 chip in their own designs.  That should bring us more code and development tools.

Pico on Breadboard

In the meantime, let’s see what we can find out about the Pico by hooking a bunch of things up to it!

Raspberry Pi Pico

The Pico is a tiny board, approximately the same size as an Arduino Nano. Like all Raspberry Pi products the packaging is pretty minimal, in fact, it’s just a Pico inside a plastic package, which itself was cut off of a strip of packages. Sort of like packages of snacks or candy that you can buy in bulk.

Look at the Pico Board

The Pico board is all you get for your four dollars, not to say that that isn’t a great buy, but if you’re looking for fancy packaging or documentation then look elsewhere.

Unlike the similar-priced Seeeduino XIAO, there are no header pins packaged with the board, so you’ll need to supply them yourself.  It’s a well-made board that can also be used as an SMD component and soldered directly to a printed circuit board.

Top View

From the top the Pico looks like this:

Raspberry Pi Pico Layout

The most predominant feature on the board is the microUSB connector at one end. This is used for both communications and to supply power to the Pico.

An on-board LED is mounted next to the microUSB connector, it’s internally connected to GPIO pin 25. It’s worthwhile to note that this is the only LED on the entire Pico board.

The BOOTSEL pushbutton switch is mounted a bit down from the LED, it allows you to change the boot mode of the Pico so that you can load MicroPython onto it and perform drag-and-drop programming.

At the bottom of the board, you’ll see three connections, these are for a serial Debug option that we won’t be exploring today, but will be of interest to advanced developers.

In the center of the board is the brains of the whole thing, the RP2040 MCU, whose features we’ll examine in a short while.

Ground Pins

There are several ground connections on the board, eight of them plus an additional one on the 3-pin Debug connector.

Pico Ground Pins

These pins are easy to spot, they are evenly spaced and are square as opposed to rounded like the other connections.

One of the ground connections, on pin 33, is also designated the Analog Ground

Power Pins

The Pico is a 3.3-volt logic device, however, it can be powered with a range of power supplies thanks to a built-in voltage converter and regulator.

Pico Power Pins

All of the power-related pins are groped in one section on the board, close to the microUSB connector. 

  • VBUS – This is the power from the microUSB bus, 5-volts. If the Pico is not being powered by the microUSB connector then there will be no output here.
  • VSYS – This is the input voltage, which can range from 2 to 5-volts. The on-board voltage converter will change it to 3.3-volts for the Pico.
  • 3V3 – This is a 3.3-volt output, from the Pico’s internal regulator. It can be used to power additional components, providing you keep the load under 300ma.

There are also a couple of inputs that can allow you to control the power in the Pico.

  • 3V3_EN – You can use this input to disable the Pico’s internal voltage regulator, which will shut off the Pico and any components powered by it.
  • RUN – This line can enable or disable the RP2040 microcontroller, it can also reset it.

GPIO Pins

There are 26 exposed GPIO connections on the Raspberry Pi Pico board.

Pico GPIO Pins

They are laid out pretty-well in order, with a “gap” between GPIO 22 and GPIO 26 (those “missing” pins are used internally).

Pretty well all these pins have multiple functions, and you can configure up to 16 of them for PWM.

There are two I2C busses, two UARTs, and two SPI busses, these can be configured to use a wide variety of GPIO pins. 

Analog Pins

The Pico has three Analog-to-Digital Converters, plus a fourth one used internally for an on-board temperature sensor.

Pico Analog Pins

The ADC’s have a 12-bit resolution.

You can also provide an external precision voltage-reference on the ADC_VREF pin. One of the grounds, the ADC_GND on pin 33 is used as a ground point for that reference.

RP2040 Microcontroller

The Raspberry Pi Pico is based around the Foundation’s new chip, the RP2040 microcontroller.  It has some impressive specifications:

  • Dual-core 32-bit ARM Cortex-M0+
  • Runs at 48MHz, but can be overclocked to 133MHz
  • 30 GPIO pins (26 exposed)
  • Can support USB Host or Device mode
  • 8 Programmable I/O (PIO) state machines

The RP2040 is capable of supporting up to 16MB of off-chip Flash memory, although in the Pico there is only 4MB.

Raspberry Pi has a lot of plans for this chip and has licensed it to many other manufacturers as well. 

Programming the Pico

You can get started with the Pico using one of two programming languages.

Pico Programming Languages

  • MicroPython – A subset of Python, MicroPython is an interpreted language that is made specifically for microcontrollers.
  • C++ – Many microcontroller users have familiarity with C++ as it is used on the Arduino and ESP32 boards. 

Although I’m anxious to use C++ with the Pico to extract every last gram of performance from it I decided to do what most people are doing and use MicroPython.   At this early stage of development, the tools for C++ are still getting the final touches put on them, and I’m looking forward to the Pico being part of the PlatformIO and Arduino IDE board collections, which I suspect it will be soon.

Getting Started with the Pico

So let’s get started with the Pico.

When we get our Pico board it is packaged in a plastic carrier with no additional parts. Unless you have plans for surface-mounting the Pico, or if your only intention is to flash the onboard LED or use the Pico as a USB dongle, you’ll need some pins.

The Pico has 40 pins, 20 on each side. An additional three pins are used for the Debug port.

Standard male header pins come in 40-pin strips, so one strip can be halved to use as pins for the Pico. If you want to have pins on the Debug connector you’ll need to have another 3-pin mal header, either straight or 90 degrees.

Soldering a Pico

Before we can start programming our Pico we’ll need to do a bit of soldering!

Aside from the Dupont male header pins, you’re going to need a suitable soldering iron and some solder. The iron should have a fine tip, and you’ll need a cleaner sponge and a holder for it.

You’ll also need a method of holding the pins of the Pico in place while you solder them, as they need to be mounted at an exact 90-degree angle to the board so that they will fit into a solderless breadboard.

Many experiments use a solderless breadboard to hold the pins, and while this works you do run a risk of damaging the breadboard with heat a-or a solder splatter. However, if you have an older breadboard that has seen better days you could repurpose it as a pin holder!

Soldering the Pico GPIO Pins

Personally, I like to use a couple of pieces of cheap perfboard, perforated experimenters board. By “cheap” I mean the single-sided stuff with no plate-through holes, just bare copper on one side.

Two pieces of this stuff are great for holding the pins, I use this a lot whenever I have to solder up a small module or microcontroller.

Get the iron up to temperature and heat the pin-pad joint and apply the solder on the other side, never directly to the iron. Heat the parts, not the solder.

If you want to solder the 3-pin connector for the Debug (it’s optional) you should probably do that first. These pins face in the opposite direction to the GPIO pins.  I used a small post-it note pad to hold the board straight, as the Debug connector, unfortunately, doesn’t line up with the GPIO pins on a 0.1-inch grid.

After that, it was just a matter of soldering 40 pins, 20 at a time! It really doesn’t take too long, only use as much solder as necessary to avoid solder bridges and inspect your work when you are finished.

Cleaning up the Pico after Soldering

I like to clean my PCBs after I solder them, to get rid of the flux and resin that was in the core of the solder. It appears as a brownish stain around the solder connections.

This step is entirely optional, as the flux and resin have no adverse effect on the operation or lifespan of the components. It just looks better IMHO!

Pico PCB cleaning and safety supplies

If you want to clan your board you’ll need some PCB Board Cleaner or Flus Remover. As it also tends to leave a bit of a residue I use Isopropyl Alcohol to clean that up with. 

Your local electronics store is the best source for the PCB Board Cleaner, as the cost of shipping it makes it expensive to purchase online. The Isopropyl Alcohol can be found at your local drug store, be sure to get the pure alcohol-water mixture (70%) and not the one that is scented.

I use an old toothbrush and some plastic containers and perform the work in a basin. Wearing proper eye, breathing, and hand protection is essential, so have a mask, gloves, and goggles ready.

Pico board cleaning

I scrub the pins using the toothbrush dipped in PCB Cleaner, then rinse them with the toothbrush dipped in Isopropyl Alcohol. 

Pico after flux removal

Let the board air dry or use an air hose, and you’ll have a shiny new Pico ready for action!

Thonny IDE with Pico

Now that the Pico has its pins attached we can begin experimenting with it. I suggest that you place it in a solderless breadboard in anticipation of our upcoming experiments.

While there are a multitude of IDEs that we could choose to work with our new Pico, my suggestion is to use the Raspberry Pi recommended Thonny IDE.

Thonny IDE

Thonny bills itself as a “Python IDE for Beginners”, and it is available for Windows, Mac OSX and Linux.

It has also been part of the Raspberry Pi Operating System (formerly Raspbian) for ages.

I’m going to use Thonny IDE on the Raspberry Pi Operating System, running on an 8GB Raspberry Pi 4, as my development platform for today’s experiments.  You could use any platform that you can run Thonny on of course, but I wanted to keep it all in the Raspberry Pi family – plus, as Thonny was already installed on a new build of the Raspberry Pi Operating System it was pretty simple to get started!

Boot & Install MicroPython

The first thing that we need to do is to get MicroPython installed onto our Pico. 

Get the microUSB cable hooked up to your Pico and get prepared to plug the other end into your computer. Before you plug it in, depress the BOOTSEL pushbutton on the Pico.

Pico press BOOTSEL on startup

With BOOTSEL held down plug the Pico into your computer’s USB port. Hold the key down for a couple of seconds and then release it.

You should see a new drive available on your computer, the message will look different depending upon which operating system you have, but it will be similar to the one you get when you plug a USB stick into a computer.

Open the new “drive”, and you’ll see a folder named “RPI-RP2”. In this drive, you’ll see a couple of files, one of them being a web document index.htm.

Click on that web document and your browser will open, and you’ll be redirected to the Raspberry Pi Pico Getting Started page.

Click the tab for Getting Started with MicroPython.   You will see a link to download a UF2 file, which is the downloadable MicroPython file. Download this file to your computer.

pico-downPico Download Micropython

Now drag the file that you downloaded into the Pico’s RPI-RP2 folder. Once you do the folder will disappear and the Pico will be booted in MicroPython mode.

Configure Thonny IDE

With the Pico still hooked up, open the Thonny IDE, if you are using the Raspberry Pi Operating System as I did you’ll find Thonny under the Programming Tools menu.

Once Thonny is open look at the status bar on the bottom right, it will likely be displaying a version of Python. This is the version of Python currently running on your computer, which for our experiments is of no importance to us.

Click on that message and a drop-down should appear, with other environments to select. One of them should be MicroPython (Raspberry Pi Pico). Select that one.

You’ll notice a new Shell opening up at the bottom, and in that shell, you should see some text indicating that you have connected to the Pico. 

Time to start programming!

Shell Test 

The Shell is the “command line” of the Pico, and you can execute code directly here.

A simple test is to type the following (this is also a good way to see if you are properly connected to the Pico) and then press Enter:

You should be greeted with “Hello World” printing in the shell, which of course is what you told the Pico to do.

Script Test

Of course, you won’t be typing your programs directly into the shell, for one thing, once you execute them they are gone and it’s also inconvenient for any program of considerable size.

You are going to use the Editor to type your programs, which is the large text area above the shell that dominates the Thonny IDE screen.

Go into this editor and type in the same text you did earlier, to greet the world with a nice “hello”.

Click the Run button (The green one with the arrow) and you’ll be prompted to save your program first. You’ll be presented with the choice of saving it on the local computer or on the Pico, try saving on the Pico. Name your program something with a “.py” extension, like “hello.py”.

The program will save and run, and you’ll see the “Hello World” greeting in the shell. You can press the Run button to see it again, and again.

So now that you know how to write and save MicroPython programs we are ready to begin our experiments!

Working with LEDs & Switches

Basic digital I/O functions can easily be illustrated using LEDs and switches, and that’s exactly how we’ll start off our Pico adventure.

But pay attention to how we wire the switches, we’re going to be doing things a bit differently.

RGB LED

Probably the easiest output device to work with is an LED. This output device works when enough current is applied in the right direction. Although simple it can be used to illustrate I/O techniques that can be applied to other devices like relays or transistors.

I’ll be using a Common-Cathode RGB LED, but you could also use three discrete LEDs instead. Either way, you’ll also require three dropping resistors, I used 330-ohm resistors in my experiments.

If you elect to use the RGB LED like I did make certain to get a standard RGB LED and not a programmable one. If you want to learn more about using RGB LEDs check out the article RGB LEDS – Colorful Arduino Experiments.

Pushbutton Switches

This is as simple an input device as it gets. I’m using a pair of momentary-contact normally-open pushbutton switches.  

One Red and one Black, but otherwise identical.

Again we are using a simple input device to test the I/O capabilities of our little Pico. We will make it a bit more interesting by wiring our two switches differently, and we also won’t be using any pull-up or pull-down resistors.

LEDs & Switches Hookup

Here is the hookup diagram for our LED’s and switches. Remember, you can use three discrete LEDs if you don’t have a common-cathode RGB LED.

Pico LED and Switch Hookup

Note that the two switches are wired differently.

The Black pushbutton switch has one side connected to a GPIO pin on the Pico, and the other side is connected to ground.

The Red switch is reversed, it has one side connected to a GPIO pin and the other side connected to 3.3-volts, which is the operating voltage and logic voltage of the Pico.

RGB Blink

The first experiment we will do is our own variation of the “Blink” sketch used with Arduino. Yes we have technically already seen how to flash the onboard LED, but as we now have an RGB LED at our disposal we can certainly find a way to “spice up” our blink!

Open the following code in Thonny:

This is a simple script that can definitely be improved upon, but it will serve to illustrate our point very well.

We start by importing the machine and utime libraries. You’ll find that any activity involving I/O ports requires machine, while utime is required whenever we want to work with time functions.

We then define the connections to our three LED elements, note that they are referred to by GPIO number and NOT the physical pin number on the Pico.  We define all of these as machinePin.OUT, meaning these are now setup as Output pins.

The while True condition is similar to the Loop in an Arduino sketch, the code in here is executed continuously.

In this section, we address the LEDs and set them either on (value of 1) or off (value of 0). We go through a sequence, and on the final one, we print to the console. 

Pico RGB Blink end-of-loop

Then we do it all again.

Load the script to the Pico and watch the LED, you should be treated to a colorful blink!

Pico RGB Blink

Switch Test

Our next script is a very basic test of the two switches that we have wired in such a strange fashion.

The first odd thing about the wiring you will observe is that they are different, the black switch connects the input to ground while the red one connects it to 3.3-volts, for a logic HIGH.

The other interesting thing is that neither switch employs a pull-up or pull-down resistor. They obviously need them, the black switch needs a pull-up and the red one requires a pull-down in order to function correctly.

We will be adding the required resistors in the code!

Note the syntax of the lines defining the pushbuttons, you’ll see how they are defined as Inputs and how pull-down and pull-up resistors have been added as well.

In the while True: loop you‘ll also note that we are monitoring for different conditions, the Red switch triggers a HIGH input while the Black one triggers a LOW.

The tiny time delay at the end is a simple form of debouncing, you can experiment with the value if you like.

This script prints all of its results in the console, so watch it while you press the buttons.

Interrupts and Toggles

The next experiment introduces a couple of useful concepts. The first, and arguably the most important, of these two concepts is the “Interrupt”.

Interrupts

An Interrupt is pretty much like it sounds like, an event that “interrupts” the normal flow of a program.  In our case we are dealing with external hardware interrupts, meaning that a signal or change of state has occurred that needs to be addressed before the program can continue.

On the Pico we create an Interrupt as follows:

  • We define a pin as being the “interrupt input”, and we define what change of state on that point is considered to be an interrupt. On the Pico, we can use any GPIO pin for this, and we can define more than one.
  • We create an “interrupt handler” function, something we want to run when an interrupt is detected.
  • We pair that “interrupt handler” with the “interrupt input”.

Now every time that interrupt input condition occurs the Pico will stop whatever it is doing and will execute the “interrupt handler”. It will then resume where it left off.

Toggle

Not as fundamental as an Interrupt but still very useful. A “toggle” simply inverts the state of an output on the Pico.

So if the output is HIGH and we apply a toggle it goes LOW. 

We don’t need to know the current state of the output, we just know that when we apply a toggle it will change to the opposite state.

Naturally, this is an ideal function for writing yet another Blink program, so that’s what we will do. Only our Blink program runs the risk of being interrupted!

In this MicroPython script, we will be blinking the Green segment of our RGB LED, using a toggle to change its state.  Under normal operation the LED state will toggle every two seconds.

However, we can interrupt the blinking by pressing the red pushbutton. This will cause an interrupt, which will turn off the green led and then turn on the red one. It will stay on for four seconds, after which control of the program is returned so we can continue with our green blinking. 

We begin our script by importing the machine and utime libraries, as we have before.  

The LED segments and the red pushbutton are defined as in our previous scripts.  The LEDs are turned off at program startup.

Then we define a function, which is our interrupt handler called “int_handler”.  Inside this function we do the following:

  • Turn off interrupts, so we don’t get multiple ones.
  • Print “Interrupt Detected” to the Shell
  • Turn on the RED LED segment.
  • Turn off the Green LED segment.
  • Sleep for four seconds.
  • Turn off the Red segment.
  • Reestablish interrupts
  • Exit

The line after our handler function “glues” the interrupt to the pin we defined as the red pushbutton input.  One thing to notice is that it specifies “IRQ_RISING”, this means that it will trigger an interrupt if the input rises from zero (ground) to one (3.3-volts). This is in line with how our red pushbutton is wired.

In the True loop, we just blink our LED, using the ”toggle” function available to any GPIO pin defined as an output.

Pico Interrupt Test

Send it up to your Pico and observe the RGB LED, it should start blinking Green. Watch it for a while, then press the Red pushbutton. The LED should turn red and the Shell should display “Interrupt Detected”. After four seconds the green flashing will recommence.

Switch & LED Demo

Since we’ve been working with switches and LEDs we may as well combine them for one more simple script.

This one is very simple, and you should be familiar with its operation by now. We define the RGB LED and the black pushbutton using the machine library functions.

Remember, the Black pushbutton is held HIGH by the pull-up we defined, it goes LOW when the button is pressed as the other side is wired to ground.

So in the True loop, we look for a value of “0”, to indicate that the button was pressed. Once we detect this condition we cycle the LED segments through their colors.

As I said, pretty simple stuff!

Analog Input Test

Now let’s move onto analog inputs.

The Raspberry Pi Pico has three analog inputs, and they all have 12-bits of resolution. 

The three inputs are as follows:

  • GPIO 26 – ADC0 (pin 31)
  • GPIO 27 – ADC1 (pin 32)
  • GPIO 28 – ADC2 (pin 34)

There is also a fourth ADC used for the internal temperature sensor.

Potentiometer Hookup

In our test, we will use a potentiometer to present a variable voltage at the analog input, which we will then read. We will be using ADC0 for our potentiometer input, but you could also use one of the other two.

Pico Analog Hookup

Note that while I show pin 23 as being the ground that was just for convenience, you can use any Pico ground pin. There is also a special Analog Ground at pin 33 you could use.  On my breadboard, I tied pin 33 to some of the other grounds.

Potentiometer Readings

The first experiment we will do is to simply read the value we are getting at the analog input, this should fluctuate based upon the position of our potentiometer.

It’s a simple script that begins, as usual, with the import of the machine library for GPIO manipulation and the utime library for time functions.

We then define our potentiometer connection. Note how we use “ADC ” to indicate that we want to use GPIO pin 26 as an analog input.  Of course this only works on the three GPIO pins that have analog input capabilities.

In the True loop we just print the value we get from the potentiometer, then delay a couple of seconds before we do it again.

One important thing to note is the type of value we get back with the “read_u16” function is an unsigned 16-bit integer. This means it will vary between 0 and 65,535, not 4095 as you might expect from a 12-bit ADC.

This may seem strange, but as we will see in the next script it actually is useful to be able to pass values all with the same numerical data type.

Pico Analog Test

Run the script and observe the Shell, you should see the values there change as you move the potentiometer shaft.

LED PWM Control

Let’s expand upon the previous script and use the output of the potentiometer to control the brightness of the LED.

Naturally, we will be using PWM for our control, a task that is quite simple in MicroPython.

One of the key items to note in this script is the way we define “led_red”. Instead of being an output, we define it as “PWM”. 

The potentiometer is defined in exactly the same fashion as it was in the last script.

Now that we have given the output a property of “PWM” it inherits a number of other parameters. One of them is the PWM frequency, which we set to 1000 Hz.

In the true loop, we continually take the unsigned 16-bit value from the potentiometer and pass it to the LEDs duty cycle, also conveniently specified as an unsigned 16-bit integer.

This shows the value of keeping both at the same numbering scheme, no need to map the analog value, which is really 0 to 4095, to the duty cycle, which is really 0 to 100.

Pico PWM LED Test

Run the sketch and you should be able to smoothly control the brightness of the red LED segment.

Adding a Display

The next experiment we will perform will be to attach an OLED display to our Pico and, of course, print something to it.

We will be using an I2C display, so we will also see how the Pico works with I2C connections. Remember, the Pico has two I2C busses.

Our OLED is a standard 1602-type OLED display, available pretty well everywhere. You can use one that is a different size to mine if you wish and just change the size in the code.

Here is how we hook all of this up, it’s only four wires:

Pico OLED Hookup

Our display will require a library, which we can install using the Thonny ID. You might find it easier to be in full menu mode instead of basic, but it will work both ways:

  • Click on the Tools menu
  • Click on Manage Packages
  • The Package manager will come up
  • Search for “ssd1306”
  • In the responses look for “ssd1306.py” and install it.

Now that we have the library installed we can look at the script that will demonstrate our OLED display.

Our OLED display is an I2C device, so you’ll note that in the beginning of the script we define two of our GPIO pins as SDA (GPIO 20) and SCL (GPIO 21).

The Pico has two I2C busses, and you can use several different GPIO pins to connect them. But they are not just any pins, certain pins are designated as SDA for bus 0 for example, and only they can be used for SDA Bus 0. 

We then define an I2C connection using the I2C function of the machine library. We need to give it the following parameters:

  • The I2C Bus Number – in our case 0
  • The SDA Pin
  • The SCL Pin
  • The I2C Bus Frequency – in our case 400 KHz

We then add the OLED library and create an I2C OLED object. We pass it the size parameters and the I2C connection information.

Note that we don’t pass an I2C address. The SD1306 OLED display has a fixed I2C address, so we don’t need to specify it.

However, I did add a line here that has nothing to do with the display but will allow you to scan the I2C bus and print out the addresses it finds occupied. Note that the printout in the Shell is in decimal, not hex as you’re more likely accustomed to seeing.

Back to the OLED script!

We start printing on our display, a couple of lines of text. Note how we specify the pixel location of the start of each line.

We actually don’t print directly to the display, instead, we are sending data to a buffer. The line oled.show() will transfer that buffer data to the display.

After printing our welcome message and holding it for four seconds we then do an oled.fill(1). This turns on every pixel in the display, or more accurately, in the buffer. We then do an oled.show() to display the fill.

We hold the fill on the display for two seconds and then do oled.fill(0), which turns off every pixel in the display when we show it.

Now onto the True loop.

Once again we enter some text, the classic “Hello World”. But instead of doing a “show” to display it we start a for-loop. Inside this loop, we use an oled.scroll(1,0) to move the display over by 1 pixel horizontally (and zero vertically, which is what the other parameter is). 

We then show the display and then sleep for a very brief period. Then we do it again.

The result will be the display scrolling across the screen. You can play with the parameters in the for-loop to change its behavior.

Pico OLED Display demo

Load the script up to your Pico and watch the display. You should see the welcome text, followed by a display fill and then empty. After that a scrolling “Hello World” should continue as long as you let the experiment run.

Driving a Motor

One of the more popular applications for a microcontroller is driving a DC motor, or several DC motors. 

This is accomplished by using an H-Bridge, an arrangement of power transistors or MOSFETs that can handle the motor current while allowing you to control the direction and speed of the motor.

We are going to do exactly that, with just one motor. We will control a small DC motor using a TB6612FNG H-Bridge and a Raspberry Pi Pico. 

TB6612FNG H-Bridge

The TB6612FNG H-Bridge is something we have used here in the DroneBot Workshop before, it’s a MOSFET-based H-Bridge that has many performance advantages over the old standby L-298N.

The Device actually has two channels, we will only be using channel A for our demonstration.

Channel A has three inputs:

  • AI1 – Direction & Mode
  • AI2 – Direction & Mode
  • PWMA – A PWM input, to control motor speed

Between them, the AI1 and AI2 pins control both the motor direction and mode. Modes include a short brake and a stop mode. We will only be dealing with the direction modes in our simple demonstration.

The TB6612FNG H-Bridge also has connections on the other side of the board for power and motor connections.

Here is the hookup we will be using with the TB6612FNG H-Bridge and the Raspberry Pi Pico.

Pico H-Bridge and Motor Hookup

Note that as I’m using a 6-volt motor I have supplied a 6-volt power supply for it. Do not attempt to use the Pico output voltage to power your motor, a separate power source is a necessity. I used four type AA batteries, a simple and safe arrangement.

The motor polarity doesn’t really matter, it will just determine which way is forward and which is backward!

Once it is all hooked up we’ll need some code to run it. I suggest this:

We are going to make use of the potentiometer, the two switches, and the RGB LED, as well as the motor controller.  The potentiometer will control the motor speed. The switches will control the motor direction, while the LED will display the current direction with a color indicator.

We use our two faithful libraries, and we set up the potentiometer as we have before.

Next, we define the connections from the Pico to the TB6612FNG H-Bridge as outputs. The PWMA output on GPIO 6 is defined as PWM, it will be controlling the motor speed.

The LEDs and pushbuttons are set up as before.  

The PWM frequency is set to 50 Hz, which was an arbitrary choice. Feel free to experiment with it and see if you can improve your motor performance.

The motors AI1 and AI2 inputs are set up to spin forward, so the motor will start off in the forward direction. Note that the Blue segment of the RGB LED is also turned on when we start.

In the True loop, we read the potentiometer value and pass it to the motor PWM signal to control the motor speed. We also look at the button status.

If the Red pushbutton is pressed we set the AI1 and AI2 signals to reverse the motor. We also illuminate the Red LED segment.

If Black is pressed we set the motor direction forward, and turn on the Green LED segment.

Pico H-Bridge and Motor Demo

Load everything up and check it out. It should startup with a Blue LED and you should be able to control the motor speed. Pressing the pushbuttons should allow you to control direction, and will also change the LED color.

Making it all Run – Pico Everything Demo

All that remains is to put everything together for one big final demo. Which we are not too far from doing, as in the last script we used everything except the OLED display.

So let’s add the OLED display to the mix.  Here is the code we will need to do that:

By now you should recognize most of this code, essentially it’s the previous script with the library and set up for the I2C OLED display added. Aside from that, the first several lines are the same, with a sequence of LED colors and a Display print when everything is set up.

In the True loop, we see that an integer is being calculated called “speedvalue”. This variable will be used to set the size of a bar-graph display on the OLED.

The red and black pushbuttons operate exactly as they did before.

The OLED then draws a rectangle under its title line and fills in the rectangle. The length of the rectangle is determined by “speedvalue”, so the rectangle gets longer as the speed is increased.

Pico Everything Demo 2

In a sense, we are using our OLED as a crude speed indicator, or a speedometer!

Load everything up and watch the show. It should operate just like the previous experiment, only this time we have a display for the motor speed in addition to the RGB LED that indicates its direction.

Running without a Host Computer

So far everything we have done has been run by loading the program onto the Pico from the Thonny IDE.

But once you have your program developed you are going to want it to run on it’s own, powered either by the microUSB port or through the Pico VSYS power input.

Pico Everything Demo

Your program, or programs, are already stored on the Pico. So how do we tell it to run the program on boot-up?

The answer is, we change the program name!

main.py

When the Pico boots up it looks for a program titled main.py. If it finds it, then it will load it and run it on startup.

So if you want your program to run unattended you’ll need to save it as main.py. Later on, you can change main.py to another program if you wish, or delete it altogether.

Start by loading the program you wish to run on boot into your Thonny IDE.  Now click on File and then choose Save As.

You’ll be asked if you want to save on your local computer or on the Pico, you definitely want to save on the Pico.

Now save your program as “main.py”.  Exactly like that, all in lowercase.

Pico running on startup, without computer connection

Now unplug your Pico from your computer and plug it into a suitable power source, like a USB adapter. You should see it spring to life and run the program you saved as “main.py”.

Conclusion

This is obviously just the beginning for the Raspberry Pi Pico. Since starting this video and article I’ve already received some new Pico accessories and several more Picos, so I’ll certainly have more content to get out to you about this new microcontroller very soon.

I’m anxious to see PlatformIO and the Arduino IDE offer support for the Raspberry Pi Pico, as the current methods of creating C++ programs for the Pico are a bit cumbersome, especially for beginners.

But it is a great and inexpensive entry into MicroPython, which is something you’ll be seeing more of around here.

So grab a Pico as soon as you can and start experimenting!

 

Parts List

Here are some components that you might need to complete the experiments in this article. Please note that some of these links may be affiliate links, and the DroneBot Workshop may receive a commission on your purchases. This does not increase the cost to you and is a method of supporting this ad-free website.

COMING SOON!

&nbsp

Resources

Pico Code – All of the MicroPython code used in this article, in one handy ZIP file.

Raspberry Pi Pico – The official Raspberry Pi Pico announcement page.

Pico Pinout – Raspberry Pi PDF of Pico Pinout.

Pico Datasheet – PDF document with Pico technical specs and ratings.

RP2040 Datasheet – PDF with ALL the specs for the RP2040 microcontroller. Warning, this is a very large document!

Pico MicroPython SDK – A good PDF guide to MicroPython on the Raspberry Pi Pico.

Pico C++ SDK – A PDF guide to C++ on the Pico. This is also a very large document.

 

Raspberry Pi Pico – How to Interface (almost) Everything!
Summary
Raspberry Pi Pico - How to Interface (almost) Everything
Article Name
Raspberry Pi Pico - How to Interface (almost) Everything
Description
The Raspberry Pi Pico is a 4-dollar microcontroller based around the new RP2040 MCU chip. Today we will examine this new device and we'll hook up a bunch of components to it to see how to use it with MicroPython.
Author
Publisher Name
DroneBot Workshop
Publisher Logo
Subscribe
Notify of

33 Comments
Oldest
Newest
Inline Feedbacks
View all comments
Christine86
3 years ago

Bill, this looks FABULOUS! The size, the speed, the prospect of an entire sweet of sensors & what seems like an explosion of NEW users & uses.

I saw a video from Raspberry Pico, & I want one.

Just seems cruel, that I’m on backorder ATM.

I would love to continue using C++. Is/are there any restrictions or absences if i want to continue using the language I learned at University?

In any event Bill, great video, I look forward to each new release, it gives me something else to learn!

Tony
3 years ago

I must say that I have not seen a more complete or completely knowledgeable informative discussion of a new product. The author is obviously a man far above his peers in explaining as I am usually the guy in the back of the room with no clue but not this time. Thank you and keep up the good work

Joe_99
3 years ago

You are so good at explaining. I appreciate you making this Raspberry Pi Pico post.

Pspeth
3 years ago

I loved this tutorial. Great development and interconnect of the concepts.
Just curious, did you not use an interrupt handler in the final code?

Thx again. Good Stuff.

Lee G.
3 years ago

Great video! Although I did not have all of the components you used in the video; I was able to complete 90% of it. You are a master at explaining things so dummies like me can clearly understand the concepts. Thanks, Bill.
Lee
Dallas

Richard Marsh
3 years ago

Brilliant introduction to the Pico. Better in some respects than ‘Get started with MicroPython…’

Psion739
3 years ago

This is an outstanding presentation of the Pico. Well done!

3 years ago

Try and PlatformIO / Arduino
https://github.com/Wiz-IO/wizio-pico

Nitro
3 years ago

Very good article. MicroPython is the future, so much easier than the other languages.
Every day you see new Pico projects, all in MicroPython, perfect.
Please continue.

John Pugh
3 years ago

A brilliant video presentation and associated online article Bill. I am an old hand (aged 72 yrs) at programming chips, I go right back to the Scamp and Z80 days and have a lot of experience with PICs using assembler. I have moved on since of course and have been using the various types of Arduino for a while, much of my knowledge gleaned from dedicated enthusiasts like your good self. I have some experience of the Raspberry Pi too, as you might expect, but I’m more interested in the control of external devices which everything else I use are… Read more »

Karl Seil
2 years ago

This video is exactly what I needed to get started with the Pico. Thanks for the level of detail you provided and the programming examples.

Tnysat
2 years ago

I think this could be better design using altium best professional pcb design software as multilayer layout

noCoderActually
2 years ago

I like the section about interrupts. However it is a little bit weird since I experience “bouncing” and indefinite system states. However: Keep going. Good content!

Garth
1 year ago

Got the same bouncing problem like you did. There is one little mistake in the code which keeps the debouncing from working: The int_handler beginning at line 23 does not know about “button_red”. It just needs to be made accessible for the defnition by adding “global button_red” above line 24

Ray
2 years ago

Very helpful, thank you!

miles.wu
2 years ago

Thanks a lot for the great tutorial. in the ssd1306 part, i met an issue that install failed. have you met this before? Thanks.. What i did in Thonny: 1: click the “Tools” –> Manage Packages 2: type in the “ssd1306” and click “Search on PypI” 3: click the “micropython-ssd1306” in the Search results 4: click “Install” Below is what i met: Installing to temp directory Error installing ‘micropython-ssd1306′: b’403’ Installing to: /tmp/tmpig7a4dsc/ micropip returned with error code 1 Tried google but no clue for how to due with this error, do you have any idea about the error? Thanks… Read more »

Last edited 2 years ago by miles.wu
MIchael
2 years ago

Runs at 48MHz, but can be overclocked to 133MHz That’s incorrect. It comes stock at 125MHz and depending on how much modifying you are willing to do to the firmware it can be clocked over a 1.5Ghz, however there are a lot of issues to deal with if you bring it that far. It can be overclocked up to 250Mhz just by calling machine.freq(250_000_000), however even this change needs SPI to be re-addressed in the firmware. Using freq() to change the clock will force the peri clock to use usb_pll which is only 48Mhz. I wrote how to make the… Read more »

Dave
2 years ago

Nice site, but you, nor anywhere else i can find explains the difference in setup to use micro python, or C and drag and drop. It seem the modes are incompatible and no clear info on changing between the 2. Any thoughts?

Ralph W. Lundvall
2 years ago

I shared with some Facebook groups. Many likes!
I’m hoping for some tutorials on deepsleep with the Pico using Micropython. Thanks!

Stephen Somogyi
2 years ago

Hello Bill,
I am an Old Plumber and my lifelong hobby is electronics and making things that work. To that end I have been trying to learn some kind of coding. There are so many platforms and languages to chose from, most of which I just cannot grasp even though I speak fluent Hungarian.
I have found your tutorials and videos very understandable and therefore I have decided to concentrate on learning MicroPython, starting on the RPi Pico.
I love your work, please keep it up and I hope you do well out of it.

Gita
2 years ago

Greetings everyone

Joseph Drasal
2 years ago

As always well well done !!

I really enjoy your classes.
You explain very well the subject.
I’ve walked around many explanations about pico pi interrupts and yours is the best.

Many thanks!

Fabio

Robert Liverman
2 years ago

Really enjoyed this and hope to see some more.

k.sarvana
2 years ago

can we interface pm2.5 sensor with the raspberry pi pico.
i am having quite a problems for interfacing it. can you help me about it?.

Dom C
2 years ago

This video is very thorough and I found it to be very instructive, for which I am very grateful. I am currently in the process of trying a to make a plant watering system with a pico, using MicroPython, since using a normal RPI seems overkill for this task., However, there seems to be very little information available with respect to the pico controller being used in this manner. Consequently, I have been struggling to get my resistive soil sensor to work properly. But I just realised (while watching your video) that a resistive sensor would probably work more like… Read more »

Gary Hutton
1 year ago

Ok, Lets have some fun. Just obtained 3 pico. Am determined to make this work. I tried the three color blink sketch. I put everything on a bread board.hooked up all the wires and nothing. Oh, what happened? Now it is trouble shooting time. Look at the sketch, again and again. I put print statements before every sleep commands.

Oh, I should run a ground wire to ground section of the bread board. Now is works.

I feel great accomplishment just doing the blinks.

Jim Craig
1 year ago

Another great video. Not only a first time look at the Pico but also the start of my journey into Micropython. I’ve had some Picos for several months but just now got around to having a serious look at them. This video was just the ticket. I had some frustration with Thonny Package Manager and getting the SSD1306 library but this was solved when I discovered that I was not using the most current Thonny version.

Frank
1 year ago

Outstanding. I never watched such a long introduction completely, that means: every second. Great job. Thank you for your effort.

Gazzanz
1 year ago

thanks for a newbie its been helpful. now have to learn Python syntax…and to think I started on the 8085 machine code all those years ago

Don Wahlquist
1 year ago

Can not load ssd1306 library from Thonny. I aways enjoy tour videos and information however I could not load the ssd1306 for micropython using the utiliies in Thonny. To make it work I had to do to GitHub to get the library and the supporting libraries to use ssd1306. I created the ssd1306.py on the raspberry pi pico. Also had to create framebuf.py, usdI2py so all of the dependencies for ssd1306 could be fulfilled. Is the reason you example worked is that you were using Thonny on a RPI4 to do all of the programming were I was using a… Read more »

John
1 year ago

When trying to run the OLED script, keep getting the following error:
>>> %Run -c $EDITOR_CONTENT
Traceback (most recent call last):
File “”, line 16, in
ImportError: no module named ‘SSD1306’

Have tried with two different OLEDs and my libraries seem to be installed in the Pico correctly. OLED screen stays black – nothing happening.

Any ideas appreciated.

Thanks,
John

Ricardo Carvacho
1 year ago

It was a VERY good help in continue to learn abaout Raspberry Pi pico and micro Python. I am starting to re learn about microcontrollers and work with music projects. Thank you.