Posted on Leave a comment

Conditional Statements in MicroPython on Raspberry Pi Pico

Conditional statements in MicroPython on Raspberry Pi Pico are used to make decisions based on certain conditions. They allow us to execute a block of code only if a certain condition is true, or to execute a different block of code if the condition is false.

In MicroPython, we use the if, elif, and else keywords to create conditional statements. The general syntax for an if statement is:

if condition:
    # Code to execute if condition is True

If the condition is true, then the code inside the block is executed. If the condition is false, then the code inside the block is skipped.

Here’s an example of an if statement:

temperature = 25

if temperature > 30:
    print("It's hot outside!")

In this example, the condition temperature > 30 is false, so the code inside the block is not executed.

We can also use the else keyword to specify what code should be executed if the condition is false:

temperature = 25

if temperature > 30:
    print("It's hot outside!")
else:
    print("It's not that hot outside.")

In this case, since the condition is false, the code inside the else block is executed, and the output would be “It’s not that hot outside.”

We can also use the elif keyword to check multiple conditions:

temperature = 25

if temperature > 30:
    print("It's hot outside!")
elif temperature > 20:
    print("It's warm outside.")
else:
    print("It's not that hot outside.")

In this example, the first condition is false, so the elif condition is checked. Since temperature > 20 is true, the code inside the elif block is executed, and the output would be “It’s warm outside.”

Conditional statements can also be nested inside each other, to create more complex logic. For example:

x = 5
y = 10

if x > 0:
    if y > 0:
        print("Both x and y are positive.")
    else:
        print("x is positive but y is not.")
else:
    if y > 0:
        print("y is positive but x is not.")
    else:
        print("Both x and y are negative.")

In this example, the output would be “Both x and y are positive.” since both x and y are greater than 0.

elif statements can be nested in Python and MicroPython on Raspberry Pi Pico. You can use them to create more complex conditions that depend on multiple variables or factors.

Here’s an example of nested elif statements in MicroPython on Raspberry Pi Pico:

x = 5
y = 10

if x > y:
    print("x is greater than y")
elif x < y:
    print("x is less than y")
    if x < 0:
        print("x is negative")
    elif x == 0:
        print("x is zero")
    else:
        print("x is positive")
else:
    print("x is equal to y")

In this example, if x is less than y, the program checks whether x is negative, zero, or positive using nested elif statements. If x is greater than y, the program prints a message saying so. And if x is equal to y, the program prints a message saying that as well.

Note that when you nest elif statements, you must make sure that the indentation is correct to avoid syntax errors.

Posted on 1 Comment

Variables and Data Types in MicroPython on Raspberry Pi Pico

Variables and data types are fundamental concepts in programming. In MicroPython, just like in any programming language, variables are used to store data values and data types define the kind of data that can be stored.

In this tutorial, we will explore variables and data types in MicroPython on Raspberry Pi Pico.

Variables in MicroPython

Variables in MicroPython are used to store data values such as numbers, strings, and boolean values. A variable is simply a named reference to a value.

To create a variable, you simply assign a value to a name using the equals sign (=). For example:

x = 5

In this case, the variable x is assigned the value of 5.

You can also assign values to multiple variables at once, like this:

x, y, z = 5, "Hello", True

This assigns the value of 5 to the variable x, the string “Hello” to the variable y, and the boolean value True to the variable z.

Further Explanation

x, y, z = 5, “Hello”, True is a way of assigning values to multiple variables at once in Python. In this case, the values 5, “Hello”, and True are assigned to the variables x, y, and z respectively.

This syntax is known as “tuple unpacking”. The values on the right-hand side of the equals sign are packed into a tuple, which is then unpacked and assigned to the variables on the left-hand side.

Essentially, it’s the same as writing:

my_tuple = (5, "Hello", True)
x = my_tuple[0]
y = my_tuple[1]
z = my_tuple[2]

But using tuple unpacking is a more concise and readable way to assign values to multiple variables at once.

Data Types in MicroPython

MicroPython has several built-in data types that define the kind of data that can be stored in a variable. These include:

  • Integer: whole numbers, such as 5 or -3.
  • Float: decimal numbers, such as 3.14 or -2.5.
  • String: a sequence of characters, such as “Hello” or “123”.
  • Boolean: a value that is either True or False.
  • List: a collection of values, such as [1, 2, 3] or [“apple”, “banana”, “orange”].
  • Tuple: a collection of values, like a list, but cannot be modified once created, such as (1, 2, 3) or (“apple”, “banana”, “orange”).
  • Dictionary: a collection of key-value pairs, such as {“name”: “John”, “age”: 30}.

To determine the data type of a variable, you can use the type() function, like this:

This will output <class ‘int’>, indicating that x is an integer.

x = 5
print(type(x))  # Output: <class 'int'>
Posted on Leave a comment

How to Read internal temperature sensor of Raspberry Pi Pico using Thonny IDE

The internal temperature sensor of RP2040 is read using micropython. The Thonny IDE is used in Windows environment.

The temperature sensor is connected to Channel number 4 of the ADC.

But since this is a rather small implementation using Thonny IDE, it does most of the interfacing related code in the background.

Code

import machine
import time

"""
Function Name: Read internal temperature sensor
Description: This function reads the internal temperature sensor of RP2040 chip.
             The temperature sensor measures the Vbe voltage of a biased bipolar diode,
             connected to the fifth ADC channel (AINSEL=4).
"""
def read_internal_temperature_sensor():
    tsi = machine.ADC(machine.ADC.CORE_TEMP)
    temp = tsi.read_u16() * (3.3 / (65535))
    temp = 27 - (temp - 0.706)/0.001721
    return temp

while True:
    #reads the temprature and prints it
    print("Temperature: ", read_internal_temperature_sensor())
    #Create a dealy of 1 second
    time.sleep(1)
Posted on Leave a comment

How to setup Raspberry Pi Pico using Thonny IDE on windows and Blink onboard LED

First you have to install micropython on your raspberry pi pico board.
Follow the steps shown in the video.

To blink the onboard LED you can follow these steps:

  1. Connect your Raspberry Pi Pico to your Windows computer using a micro USB cable.
  2. Open Thonny IDE on your Windows computer.
  3. Click on the “File” menu and select “New”.
  4. In the new window, enter the following code:
import machine
import time

led_onboard = machine.Pin(25, machine.Pin.OUT)

while True:
    led_onboard.toggle()
    time.sleep(1)
  1. Save the file with a meaningful name and the “.py” extension.
  2. Click on the “Run” menu and select “Run current script” or press the “F5” key.
  3. Thonny IDE will run the code and the onboard LED of Raspberry Pi Pico will start blinking at a frequency of 1 second.

That’s it! You have successfully blinked the onboard LED of Raspberry Pi Pico using Thonny IDE on Windows.

Posted on Leave a comment

Raspberry Pi Pico

This board is using an RP2040 microcontroller. Which is using a dual-core arm cortex m0+ processor. The speed of the clock signal can be changed on the fly up to 133MHz.

The Pico board is castellated so that you can mount this board directly on your PCB. This board also has options for mounting male/female headers. which is handy for prototyping on a breadboard.

It can be programmed using micropython and ‘C’ programming language.

Raspberry Pi Pico datasheet
Getting started with Raspberry Pi Pico: C/C++ development
Raspberry Pi Pico C/C++ SDK
Raspberry Pi RP2040 datasheet
Hardware design with RP2040

Technical Specifications

21 mm × 51 mm form factor
RP2040 microcontroller chip designed by Raspberry Pi in the UK
Dual-core Arm Cortex-M0+ processor, flexible clock running up to 133 MHz
264KB on-chip SRAM
2MB on-board QSPI Flash
26 multifunction GPIO pins, including 3 analogue inputs
2 × UART, 2 × SPI controllers, 2 × I2C controllers, 16 × PWM channels
1 × USB 1.1 controller and PHY, with host and device support
8 × Programmable I/O (PIO) state machines for custom peripheral support
Supported input power 1.8–5.5V DC
Operating temperature -20°C to +85°C
Castellated module allows soldering direct to carrier boards
Drag-and-drop programming using mass storage over USB
Low-power sleep and dormant modes
Accurate on-chip clock
Temperature sensor
Accelerated integer and floating-point libraries on-chip

Getting Started with MicroPython on Raspberry Pi Pico using Thonny IDE