Posted on Leave a comment

While Loops in MicroPython on Raspberry Pi Pico

While loops in MicroPython on Raspberry Pi Pico are used to execute a block of code repeatedly as long as a certain condition is true. The general syntax for a while loop is:

while condition:
    # code to execute

The condition is checked at the beginning of each iteration. If the condition is true, the code inside the loop is executed. Once the code has been executed, the condition is checked again, and the loop continues until the condition is false.

Here’s an example of a simple while loop:

i = 0
while i < 5:
    print(i)
    i += 1

In this example, the loop will continue to execute as long as i is less than 5. Inside the loop, the current value of i is printed, and i is incremented by 1. The loop will continue until i reaches 5, at which point the condition i < 5 will be false, and the loop will terminate.

It’s important to make sure that the condition in a while loop will eventually become false, otherwise the loop will continue to execute indefinitely, which is known as an infinite loop. An infinite loop can cause your program to hang or crash, so it’s important to make sure that your loop will eventually terminate.

Here’s an example of an infinite loop:

i = 0
while i < 5:
    print(i)

In this example, the condition i < 5 will always be true, since i is never incremented. This will cause the loop to continue to execute indefinitely, which is not what we want.

While loops can be useful for a variety of tasks, such as reading data from sensors, controlling motors, or performing other repetitive tasks. By using while loops, you can make your code more efficient and easier to read.

Posted on Leave a comment

Introduction to Loops in MicroPython on Raspberry Pi Pico

Loops are an essential part of any programming language, including MicroPython on Raspberry Pi Pico. They allow you to execute a block of code repeatedly, saving you time and effort. In this article, we’ve introduced you to the two types of loops in MicroPython: for loops and while loops.

What are Loops?
A loop is a programming construct that allows you to execute a block of code repeatedly until a specific condition is met. There are two types of loops in MicroPython: for loops and while loops.

For Loops
A for loop is used when you want to execute a block of code a fixed number of times. The syntax of a for loop in MicroPython is as follows:

for variable in sequence:
    # Code to execute

The variable is assigned to each element of the sequence in turn, and the code inside the loop is executed. Here’s an example:

for i in range(5):
    print(i)

This code will print the numbers from 0 to 4.

While Loops
A while loop is used when you want to execute a block of code repeatedly until a specific condition is met. The syntax of a while loop in MicroPython is as follows:

while condition:
    # Code to execute

The code inside the loop will be executed repeatedly until the condition becomes false. Here’s an example:

i = 0
while i < 5:
    print(i)
    i += 1

The code first initializes the value of i to 0. Then it enters the while loop and checks if the value of i is less than 5. Since i is initially 0, the condition is true and the loop begins.

The loop prints the current value of i using the print() function and then increments the value of i by 1 using the += operator. This process repeats until the value of i is no longer less than 5, at which point the loop ends and the program continues with the rest of the code.

When you run this code in a MicroPython environment such as Thonny IDE, the output will be:

0 1 2 3 4

This is because the loop executes five times, with the value of i being printed on each iteration.

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'>