Skip to content

Typical Errors Using MicroPython on Pico2

When programming the Raspberry Pi Pico2 with MicroPython, you may encounter various errors. Below are some common errors and how to resolve them.

1. Syntax Errors

Example:

print "Hello, world!"

Error Message:

SyntaxError: invalid syntax

Cause: Missing parentheses in print() (MicroPython uses Python 3 syntax).

Fix:

print("Hello, world!")

2. Indentation Errors

Example:

def my_function():
print("Hello")

Error Message:

IndentationError: expected an indented block

Cause: Python requires proper indentation for blocks of code.

Fix:

def my_function():
    print("Hello")

3. Name Errors

Example:

print(msg)

Error Message:

NameError: name 'msg' is not defined

Cause: The variable msg has not been defined before being used.

Fix:

msg = "Hello"
print(msg)

4. Type Errors

Example:

num = 5
print("Number: " + num)

Error Message:

TypeError: can only concatenate str (not "int") to str

Cause: Concatenating a string and an integer directly.

Fix: Convert the integer to a string:

print("Number: " + str(num))

5. Import Errors

Example:

import machineXYZ

Error Message:

ImportError: no module named 'machineXYZ'

Cause: The module does not exist or is incorrectly named.

Fix: Check for the correct module name:

import machine

6. Memory Errors

Example:

big_list = [i for i in range(1000000)]

Error Message:

MemoryError: memory allocation failed

Cause: Trying to allocate too much memory on the Pico2.

Fix: Use smaller data structures or optimize memory usage:

big_list = (i for i in range(1000))  # Generator instead of list

7. Device Not Found (USB Connection Issues)

If the the device is not responding or just looks the new code not uploading to device or get an error message that

Error Message:

Failed to connect to device

or

Device disconnected

Cause: The Pico2 is not properly connected, or the serial port is not detected.

Fix: - Check that the USB cable supports data transfer. - Ensure the Pico2 is in bootloader mode (hold BOOTSEL while plugging in). - Restart your development environment (Thonny, MPRemote, etc.).

8. Pin doesn't have ADC capabilities

Example:

import machine

ADC_CHANNEL = 5 #ADC channel 5 does not exist.
temp_sensor = machine.ADC(ADC_CHANNEL)
Error Message:
ValueError: Pin doesn't have ADC capabilities

Cause: A non-existing ADC channel was selected, or a port without ADC capabilities.

Fix: Refer the datasheet for ADC channels and ports.

Pinout

If the correct ADC channel or Pin object is selected and it is not working, perform a firmware update to the latest version.

9. Bound Method

Example:

import machine
import time

Battery = machine.ADC(28) 

while True:
    raw_battery_voltage = Battery.read_u16
    print(raw_battery_voltage)
    time.sleep(1)
Error Message:
<bound_method>

Cause: This error is often caused by a missing pair of parentheses () at the end of a method.

Fix:

raw_battery_voltage = Battery.read_u16()


➡ Next Steps