Getting Started with Pico on Windows with Thonny PythonSharath
The Raspberry Pi Pico is the latest flexible microcontroller from Raspberry Pi. In this tutorial, we will see how to use the Pico on Windows Operating System with the Thonny Python IDE.
Step 1: Download and install Python
Visit the python website and download the latest version of python for Windows. Install both and we can proceed to the next step.
Then Download and Install Thonny IDE.
Step 2: Now Press the BOOTSEL pin on your Pico and Connect the Raspberry Pi Pico and you can able to see the Pico Mounted as a Flash Drive. Open the Index.html file and it will head over to raspberry pi pico page. Here we need to download the UF2 File to your computer (for now).
Step 3: Drag and Drop or Copy the UF2 File that we downloaded on your computer to the Pico. Now the pico will eject itself. Now the micropython is running in our Raspberry Pi Pico. We can able to see that pico device on our Thonny IDE.
Step 4: Open Thonny IDE, and at the right bottom of the window click the button and you can able to see ‘MicroPython (Raspberry Pi Pico)’ select it. Now we are ready to code.
Step 5: We can use shell or the python file to code our raspberry pi. Using shell you can test line by line code. Using python file, you can write complete code, then run it using the Pico.
Printing ‘Hi Pico Factory’ on Shell
Let’s try to run this on our Pico. Write ‘Hello from Pico” on the code area and click the Play Icon in the IDE. Now the result will be printed on the output.
If you click save, it will ask where to save the file. Choosing ‘Raspberry Pi Pico’saves the python file directly to the Pico.
Testing with On-board LED.
In Pico we have on-board LED as well as Temperature Sensor. So first we will see how to turn ON and OFF LED with basics, in later blog we will see how to use the temperature sensor. Note that, the latest python uses indentation in Python 3 (Python 2 runs without indentaion, but it is recommended for readability). When you use Thonny IDE, typing the code line by line automatically sets the indentation. So if your code didn’t work, check the indentation also.
from machine import Pin ledpin = Pin(25, Pin.OUT) ledpin.value(1) print("LED is ON")
The first line we are importing Pin from the machine modules. It is for using the GPIO Pins in the code. There are many modules available for delay, timer, pwm, etc. After importing the pins, we can use the Pin(PIN_NUMBER, MODE) to set the pin as Input/Output or any other type required.
After run this code by pressing the play button the On-board LED will be ON. The Python code runs only once. So that, we can see the ‘LED is ON’ is printed only once and not printing continuously like in Arduino c language. To achieve the loop, we can use while loop to run our code continuously if needed.
Similarly, the ledpin.value(0) turns OFF the LED.
LED Blinking with utimer:
In this example, we will blink the led using utimer library. The ‘while True:’ syntax runs the code in a loop. For debugging purpose, we printed the LED Status on output.
from machine import Pin import utime ledpin = Pin(25, Pin.OUT) while True: ledpin.value(1) print("LED ON") utime.sleep(1) ledpin.value(0) print("LED OFF") utime.sleep(1)