Raspberry Pi Pico und Thonny mit MicroPython - Teil 1

Why easy, if it is difficult? So I thought when I tried to deal with the official documentation "Getting Started With Raspberry Pi Pico" The Raspberry Pi Foundation to put the Pico into operation on my Raspberry Pi 400. I would resort to further comments, but rather describe how easy it is to program the Raspberry Pi Pico under Thonny with Micropython. For this I use either my PC or my Raspberry Pi 400. Also note our Pico Quickstart Guide.


Of the Python Editor Thonny Is quickly installed on the PC, the Raspberry Pi belongs to the basic equipment of the Raspberry Pi OS operating system (formerly Raspbian). The settings for the respective microcontroller and associated micropythone are made after the start.

at thonny.org Selecting the operating system, download and installation takes less than five minutes.

Under Extras \ Options (English Tools \ Options) you can set the language on the first tab (General, English General).

After changing the language setting, Thonny must be closed and restarted.

Then we go to the settings for the Raspberry Pi Pico. Here we experience the only real stumbling block in programming the Pico with Micropython.

Use the Run menu \ Select Interpreter ... Come to the second tab of the options. After selecting the Raspberry Pi Pico, press and hold the only button boatel on the PICO and then insert the PICO into the USB socket. In boot mode, the Pico temporarily logs on as a new drive on the Windows 10 PC. (Windows 7/8 users please Blog Post by Andreas Wolter read).

After installing the Micropython firmware, the drive disappears and we get in the command line (lower part of Thonny, often called Shell or Repl = Read Eval Print Loop) the Python-typical prompt >>>.

Now we can either write in the upper part, the editor, a first program or behind the promptly the usual Print ("Hello World") Enter and press ENTER.

Trouble Shooting: If you receive an error message instead of the above image, it may be because the COM port of the PICO was not automatically detected. Then please select the correct port in the pull-down menu on the Options \ Interpreter page.

If that does not work, please repeat the installation: Pull the USB connector, press the boatel, insert the USB connector, release the boat clasp, etc.

Application programs

If you have experience with Python, e.g. The Raspberry Pi, only a few special features will find Micropython on the Pico. No difference e.g. in the five-time writing of "Hello World" with display of counting variables:

When saving is asked if the program is to be stored on the computer or the PICO. Recommendation: Save everything on the computer. Later we will see what is also stored on the Pico.

Only after saving the program is executable, whereby Thonny automatically saves further changes after pressing the start symbol.

Already at our second program, we want to go to the "Physical Computing", ie use the many interfaces that our microcontroller provides. For this we import two program modules at the beginning of the program (Arduino users would say library, library or libe). The module time is an old acquaintance for Raspians, the module machine Just know those who have tried Micropython on the ESP32. Here now the "flashing variant" in Micropython:

From the module machine the class becomes Pin code imported, out time just sleep. Who imports the classes so, can call them later without the module name. Who imports the entire module (e.g. Import Time), must preface the module name for all instructions (ie Time.Sleep (1)).

To the module time calls the documentation Next sleep() also Sleep_ms (Milliseconds) and Sleep_us (Microseconds).

 import time
 time.sleep(1)           # Sleep for 1 second
 time.Sleep_ms(500)      # Sleep for 500 milliseconds
 time.Sleep_us(10)       # Sleep for 10 microseconds
 begin = time.Ticks_ms() # Get millisecond counter
 delta = time.Ticks_diff(time.Ticks_ms(), begin) # Compute Time Difference

In the program, the object LED_BUILTIN is instantiated to pin (25) as output with value 0 (switched off). In the endless loop, the built-in LED is then turned on and off in seconds.

In the third program, we also want to apply the class Pin from the Machine module. At the touch of a button, a traffic light cycle is to run. New is actually only the instantiation of the button as input with pull-up resistance. The button on pin 5 is thus switched against ground.

 button = Pin code(5, Pin code.IN, Pin code.Pull_up)

To find the inputs and outputs used by me, here first the official Pinout diagram:

 from machine import Pin code
 from time import sleep
 greenled = Pin code(4,Pin code.OUT,value=0)     # green
 yellowled = Pin code(3,Pin code.OUT,value=0)    # yellow
 redled = Pin code(2,Pin code.OUT,value=0)       # red
 LED_BUILTIN = Pin code(25,Pin code.OUT,value=0)
 button = Pin code(5,Pin code.IN,Pin code.Pull_up)
 def trafficlight():
     return None
 whiler True:
     IF button.value() == 0:
         print("Button is pressed")

As far as a first look at the digital inputs and outputs. Now we turn to the analog inputs (ADC = Analog Digital Converter). Be useful to be useful

  • ADC0 to GP26
  • ADC1 to GP27
  • ADC2 to GP28

If no reference voltage is connected to the physical pins 35 and 33, the read values ​​relate to 3.3V. In fact, it is only an ADC to which a total of five input is connected by a "MUX" (multiplexer). The fourth ADC (ADC (3) to GP29) I have not found, on the ADC (4) is the internal temperature sensor. More about this Blog of Jörn Way from 13.Nov.2021.

I conclude my homemade game controller with the 10kohm rotary potentiometer to Phys. Pin 36 (3.3V), GND and the ADC inputs. The following small program I have slightly modified comes from the RP2 Chapter The Micropython Documentation.

At the Pico Micropython knows only the Read_U16 method, a 16-bit value between 0 and 65535. After I tried the simple Read () command unsuccessful (i.e., with error message), I divided the respective value by 64 and get so The well-known 10-bit resolution of 0 to 1023. Everything else is mistaken for an accuracy that is not given on how to detect the number jumps in the event of unchanged setting.

Instead of the instantiation made here in line 13 with the GPIO pinsummer ADC = ADC (PIN26)) You can also use the index of the ADC list [0: 4]:

  • ADC (0) = Pin (26)
  • ADC (1) = Pin (27)
  • ADC (3) = Pin (28)
  • ADC (3) = GP29 not found
  • ADC (4) = temperature sensor

At the end of the first part, the well-known alternative to analog outputs: the pulse width modulation (PWM), with which LEDs can be dimmed or engines can be throttled.

From the module machine If the classes are imported PIN and PWM, the object PWM0 instantiates (on pin 25, the built-in LED is connected) and the PWM frequency is set.

In an endless loop, the Duty Cycle the PWM is controlled down to 0 (zero). After one second, the process begins again.

The TRY: - Except: - Construction causes the endless loop with Ctrl-C to be terminated without error message.


In the second part we will get to know the well-known interfaces UART, ONWEWE, I2C and SPI as well as the evaluation of sensors and output on the LCD.

Projects for beginnersRaspberry pi

Leave a comment

All comments are moderated before being published

Recommended blog posts

  1. Install ESP32 now from the board manager
  2. Lüftersteuerung Raspberry Pi
  3. Arduino IDE - Programmieren für Einsteiger - Teil 1
  4. ESP32 - das Multitalent
  5. OTA - Over the Air - ESP programming via WLAN