Raspberry Pi Pico und Thonny mit MicroPython - Teil 2

In the first part We had the installation of Thonny, the establishment of the Raspberry Pi Pico, as well as first application programs to know the use of the inputs and outputs. This time I would like to show you the programming of the well-known interfaces onWire, UART and I2C, all second assignments of pins.

First of all, the reference to the official Pinout diagram.

I would also like to cite the MicroPython documentation, and in particular the RP2 chapter, again as a source.

Components used

 1 Raspberry Pi Pico RP2040 Microcontroller Board
several DS18B20 Digital Stainless Steel Temperature Sensor Temperature Sensor
1 MB 102 Breadboard Kit
1 HD44780 1602 LCD module display 2x16 characters
1 I2C IIC Adapter Serial interface for LCD display
1 Turning potentiometer 10kohm
1 Pull-up resistance 4,7kohm
1 Raspberry Pi 400

Wire bus (One Wire) using the example of the temperature sensor DS18B20

For the popular temperature sensor DS18B20, I prefer the waterproof variant, there are two modules that make programming very easy. First of all, one needs the OneWire library and then the library building for the temperature sensor. The circuit is very simple: the name giving "a" wire goes with my project to pin 12. Please do not forget the pull-up resistor (4.7 kOhm) between signal line and 3.3V. Like each electronic device, the sensor requires a power supply, ie the red wire at 3.3V and the black to GND.

I am trying to connect several DS18B20 temperature sensors in parallel to one input.

Source code download

With the above-mentioned program, the three approximately equal temperatures are measured and output in the shell. Again, I have copied the program fragments from the documentation and supplemented for a running program. Be in the upper part onewire imported and the object OW Instantiated. In the lower program part, the module DS18x20 is first imported. After instantiating the object DS, the single wire bus is scanned and the unique identifiers of the sensors in the list are Rome saved. In the for loop, the temperature values ​​are then determined and output for all elements of the list. That was light, because the modules used are part of the program package. All these modules can be viewed. Do you already have the reference Help () seen in the line before the prompt?

Here you will find the reference to HELP ('Modules') in the last line. Then all Python modules available immediately will be displayed:

For the RP Pico there is another "built-in" module called rp2. After importing, you can also look at a little help for this:

More in the third part of this blog series. Other microcontrollers also have specific modules with functions / methods, mostly extensions for the module machine.

For more information I refer to the Official documentation.


Next, I would like to use a combined temperature / humidity sensor, which also requires only one PIN except the power supply: the DHT22.

Pin assignment of the DHT22:

  • Pin 1 = Vcc
  • Pin 2 = signal (to GP15 and over 10kohm to VCC)
  • Pin 4 = GND

There is no built-in module for this sensor, but there are references to a module on the Internet. Now we learn that the internal memory of the Pico is quite useful after all. From now on the "other" modules will be stored there. But one step at a time.

On Github you will find a page to DHT22 by Daniel Perron. When analyzing the program dht22test.py I notice that a program module called DHT22 is used, which can also be downloaded from the site. Without further ado, I download the entire package as a zip file.

In addition, the test program contains code for the display on an LCD.I want to show that later, so here is just my lean program for now DHT22EasyTest.py With display of temperature and humidity in Thonny. After looking at the line with the instantiation of dht again and discovering the parameter Pin.PULL_UP, I wanted to know whether one can do without the external pull-up resistor. One can.

Source code download

The image of Thonny has changed a little. Under the menu item "View" I set a check mark by clicking on "files" and thus bring the directories on the PC (top left) and the RP Pico (bottom left) to display. There you will find DHT22.PY and other modules we will use later. And by the way: If you save your program here under the name main.py (please do not use boot.py), you get an autostart function for the autonomous pico.


Next, I looked at the modules recommended by Daniel Perron for the LCDs. Their author is called Dave Hylands, his Github name is Dhyland. Again, I invite the whole Repository python_lcd as a zip file down.

From the variety of files for different microcontrollers and the two connection options of the LCDS GPIOS or I2C - I save the files lcd_api.py and machine_i2c_lcd.py in the internal memory of the PICO. For a simple Real Time Clock (RTC) I would like to connect an LCD1602 with the GPIOS of the PICO.

The power supply of the LCD should preferable with 5V, at 3.3V, the image is very weak.

Please open my example program with the name Pico_gpio-lcd1602_rtc.py

With the help of the module GPIO_LCD the object becomes LCD Instantiates with the pin assignment and the display size in the program in the following line:

 LCD = Gpiolcd(RS_PIN=Pin code(0),
               enable_pin=Pin code(1),
               D4_PIN=Pin code(2),
               D5_PIN=Pin code(3),
               D6_PIN=Pin code(4),
               D7_PIN=Pin code(5),
               Num_lines=2, NUM_COLUMNS=16)

Of course you can deviate from this occupancy. Please note, if necessary, the deviating pin assignment in the original DHT22 test program of Daniel Perron, if you want to replicate.

After the values ​​to be output were assigned per line to a string, the output is done with

 LCD.Putstr(Line_oben + "\ n" + Line_unten)

To use the Real Time Clock, the class RTC must be imported from the module machine. By uncommenting line 34, the date and time can be set as a tuple with the values (year,month,day,weekday,hour,minute,second,microsecond). With rtc.datetime() without argument date and time are queried and can then be evaluated via the index - starting at 0 as usual.

To my great surprise, the RTC in the powerless Pico continued to work overnight, so that after restarting the program the next morning the exact time was displayed. However, I cannot say how long the internal RTC continues to work without power supply.

Now the whole thing again from the beginning with my LCD2004 with I2C adapter. The suitable library, the module machin_i2c_lcd.py, I have already stored on the pico. Please load the example program Pico_i2c-lcd2004_rtc.py

LCD with i2c adapter

Raspberry pi pico gp

Raspberry pi pico pin



3, 8, 13,18,23, 28, 33, 38


VBus = 5V



I2C0 SDA = GP8



I2C0 SCL = GP9


With the lines

 from time import sleep
 from machine import I2c, Pin code
 from machine_i2c_lcd import I2CLCD
 from machine import RTC

the necessary classes are imported from the modules.

The initialization of I2C and the LCD via I2C takes place with

 i2c = I2c(0)                              # SDA = PIN (8), SCL = Pin (9)
 LCD = I2CLCD(i2c, 0x27, 4, 20)   # I2c address 0x27, 4 lines, 20 characters per line

In the example program, further functions / methods of the module are shown:

  • lcd.move_to (column, line)
  • lcd.backlight_off ()
  • lcd.backlight_on ()
  • lcd.display_off ()
  • lcd.display_on ()

These are partly in the commented part.

This also uses one of the I2C interfaces, namely I2C (0) to the PINS GP8 and GP9, for the first time.

Instead of another example of I2C, I refer again to the Blog post by Jörn Way from 13.Nov.2021. It shows the temperature display on a 1.3 "OLED with the I2C interface.

UART or the serial interface, AKA RS232

To test the serial interface, we need a remote point for communication. In earlier blog posts, I had already presented a Python program with the SERIAL module on the Raspberry Pi. Here, however, the focus is on the Pico and Micropython, so I use the Linux program Minicom on the Raspi. Alternatively, I would have my PC with one USB serial adapter and the program Putty or the serial monitor in the Arduino IDE can use.

The connection between Raspberry Pi and RP Pico is made quickly. I first connect a ground-pin, then

TX = pin (4) on the pico with RX (Phys. Pin 10) on the Raspi,

RX = PIN (5) on the PICO with TX (Phys. Pin 08) on the Raspi.

Preparation on the Raspi:

 sudo apt update
 sudo Apt Install Minicom
 sudo minicom -D / dev / ttys0 -d 9600

To get a local echo in Minicom, I press Ctrl + A and E.

I did not find a suitable terminal program in Micropython. Either there are examples for sending or receiving data. I would like to combine both. Maybe I did not find the most elegant solution, but it works. Please download from the example programs Pico_uart.py. There are some explanations follow:

The module Serial Do not know Micropython. Instead, there are in the module machine the class Uart With few functions / methods. After importing the class, we instantiate an object UART1 With the following command:

 UART1 = Uart(1, baud rate = 9600, TX = Pin code(4), Rx = Pin code(5))

The commands for sending or receiving data I have each declared in the self-defined functions Send () and RECEIVE (), the change between sending and receiving in the Main () function. After many attempts in which I could only end the program by dragging the USB plug, I have inserted the sys.exit () command after the help of the blogger colleague Jürgen Grzesina. This completes the program with Ctrl + C. Previously, it jumped back to the endless loop or was waiting for receive data.

Another small thing has driven me to the edge of madness: After the program starts, the query takes place in the command line. Whether should be sent or received. To send the "1" is to be entered, for receiving the "0", but it also works with any other character as "1". But the cursor does not automatically jump into the shell, but is still in the editor. So: first mouse click in the lower part of Thonny, the command line.

Here are the pictures of the exciting correspondence between Raspberry Pi 400 and RP Pico:

It is said: The appetite comes while eating. I hope I could infect you with my enthusiasm for the RP Pico and MicroPython and that you will give it a try.

DisplaysProjects for beginnersRaspberry piSensors


Bernd Albrecht

Bernd Albrecht

Auf die github-Seite https://github.com/dhylands/python_lcd wurde bereits im Blog hingewiesen. Im Verzeichnis lcd findet man die benötigten Module. Die Datei lcd_api.py wird für beide Versionen gebraucht. Für die I2C-Version dann zusätzlich machine_i2c_lcd.py benutzen; und für die GPIO-Version bitte die Datei nodemcu_gpio_lcd.py unter folgendem Link
laden und unter dem Namen gpio_lcd.py auf dem Pico abspeichern.



ich habe für die Realtime Clock das ganze GitHub Verzeichnis in Thonny eingebunden, die beiden genannten Dateien auf dem Pico gespeichert und wenn ich das Script starte, heißt es immer:
ImportError: no module named ‘gpio_lcd’
In dem Verzeichnis gibt es nur eine Datei pyb_gpio_lcd.py, aber selbst wenn ich den import Befehl von:
from gpio_lcd import GpioLcd in:
from pyb_gpio_lcd import GpioLcd ändere, wird diese nicht gefunden.
Wo liegt mein Fehler?

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